2002-06-11 21:44:00 +00:00
|
|
|
/**
|
|
|
|
* \file xfont_metrics.C
|
2002-09-05 15:14:23 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-03-21 17:27:08 +00:00
|
|
|
*
|
2002-06-11 21:44:00 +00:00
|
|
|
* \author unknown
|
2002-12-01 22:59:25 +00:00
|
|
|
* \author John Levon
|
2002-09-05 14:10:50 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-06-11 21:44:00 +00:00
|
|
|
*/
|
2000-07-24 13:53:19 +00:00
|
|
|
|
2000-04-04 00:19:15 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2001-12-20 15:11:51 +00:00
|
|
|
#include "support/lstrings.h"
|
2003-03-31 03:28:31 +00:00
|
|
|
#include "frontends/lyx_gui.h"
|
2002-05-24 14:34:32 +00:00
|
|
|
#include "xfont_metrics.h"
|
2002-05-24 18:24:14 +00:00
|
|
|
#include "xfont_loader.h"
|
2000-04-04 00:19:15 +00:00
|
|
|
#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
|
|
|
|
2002-05-22 01:16:37 +00:00
|
|
|
#include <boost/scoped_array.hpp>
|
2001-12-18 03:30:35 +00:00
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
using namespace lyx::support;
|
|
|
|
|
2001-03-20 01:22:46 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
inline
|
2000-04-04 00:19:15 +00:00
|
|
|
XFontStruct * getXFontstruct(LyXFont const & f)
|
|
|
|
{
|
2002-06-11 21:44:00 +00:00
|
|
|
return fontloader.load
|
|
|
|
(f.family(), f.series(),
|
|
|
|
f.realShape(), f.size());
|
2000-04-04 00:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2002-12-01 22:59:25 +00:00
|
|
|
|
2002-06-11 21:44:00 +00:00
|
|
|
namespace font_metrics {
|
2002-12-01 22:59:25 +00:00
|
|
|
|
2002-06-11 21:44:00 +00:00
|
|
|
int maxAscent(LyXFont const & f)
|
2000-04-04 00:19:15 +00:00
|
|
|
{
|
|
|
|
return getXFontstruct(f)->ascent;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-11 21:44:00 +00:00
|
|
|
int maxDescent(LyXFont const & f)
|
2000-04-04 00:19:15 +00:00
|
|
|
{
|
|
|
|
return getXFontstruct(f)->descent;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-11 21:44:00 +00:00
|
|
|
int ascent(char c, LyXFont const & f)
|
2000-04-04 00:19:15 +00:00
|
|
|
{
|
|
|
|
XFontStruct * finfo = getXFontstruct(f);
|
|
|
|
unsigned int uc = static_cast<unsigned char>(c);
|
|
|
|
if (finfo->per_char
|
|
|
|
&& uc >= finfo->min_char_or_byte2
|
2002-12-01 22:59:25 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-11 21:44:00 +00:00
|
|
|
int descent(char c, LyXFont const & f)
|
2000-04-04 00:19:15 +00:00
|
|
|
{
|
|
|
|
XFontStruct * finfo = getXFontstruct(f);
|
|
|
|
unsigned int uc = static_cast<unsigned char>(c);
|
|
|
|
if (finfo->per_char
|
|
|
|
&& uc >= finfo->min_char_or_byte2
|
2002-12-01 22:59:25 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-11 21:44:00 +00:00
|
|
|
int lbearing(char c, LyXFont const & f)
|
2000-04-04 00:19:15 +00:00
|
|
|
{
|
|
|
|
XFontStruct * finfo = getXFontstruct(f);
|
|
|
|
unsigned int uc = static_cast<unsigned char>(c);
|
|
|
|
if (finfo->per_char
|
|
|
|
&& uc >= finfo->min_char_or_byte2
|
2002-12-01 22:59:25 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-11 21:44:00 +00:00
|
|
|
int rbearing(char c, LyXFont const & f)
|
2000-04-04 00:19:15 +00:00
|
|
|
{
|
|
|
|
XFontStruct * finfo = getXFontstruct(f);
|
|
|
|
unsigned int uc = static_cast<unsigned char>(c);
|
|
|
|
if (finfo->per_char
|
|
|
|
&& uc >= finfo->min_char_or_byte2
|
2002-12-01 22:59:25 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-11 21:44:00 +00:00
|
|
|
int width(char const * s, size_t n, LyXFont const & f)
|
2000-04-04 00:19:15 +00:00
|
|
|
{
|
2003-03-31 02:59:34 +00:00
|
|
|
if (!lyx_gui::use_gui)
|
2000-04-04 00:19:15 +00:00
|
|
|
return n;
|
2000-07-04 20:32:37 +00:00
|
|
|
|
|
|
|
if (lyxrc.font_norm_type == LyXRC::ISO_10646_1) {
|
2001-12-18 03:30:35 +00:00
|
|
|
boost::scoped_array<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 font(f);
|
2001-08-30 22:42:26 +00:00
|
|
|
if (f.isSymbolFont()) {
|
2000-07-20 10:04:27 +00:00
|
|
|
#ifdef USE_UNICODE_FOR_SYMBOLS
|
2000-10-11 21:06:43 +00:00
|
|
|
font.setFamily(LyXFont::ROMAN_FAMILY);
|
|
|
|
font.setShape(LyXFont::UP_SHAPE);
|
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;
|
2002-12-01 22:59:25 +00:00
|
|
|
}
|
2002-06-11 21:44:00 +00:00
|
|
|
int result = xfont_metrics::width(xs.get(), n, font);
|
2000-07-04 20:32:37 +00:00
|
|
|
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
|
2002-06-11 21:44:00 +00:00
|
|
|
int result = 0;
|
2000-04-04 00:19:15 +00:00
|
|
|
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) {
|
2001-12-20 15:11:51 +00:00
|
|
|
char const c = uppercase(s[i]);
|
|
|
|
if (c != s[i]) {
|
2000-04-04 00:19:15 +00:00
|
|
|
result += ::XTextWidth(getXFontstruct(smallfont), &c, 1);
|
|
|
|
} else {
|
|
|
|
result += ::XTextWidth(getXFontstruct(f), &c, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-11 21:44:00 +00:00
|
|
|
int signedWidth(string const & s, LyXFont const & f)
|
2000-04-04 00:19:15 +00:00
|
|
|
{
|
2001-12-28 13:26:54 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-11 21:44:00 +00:00
|
|
|
void rectText(string const & str, LyXFont const & font,
|
2002-12-01 22:59:25 +00:00
|
|
|
int & width,
|
|
|
|
int & ascent,
|
2002-06-11 21:44:00 +00:00
|
|
|
int & descent)
|
|
|
|
{
|
|
|
|
static int const d = 2;
|
|
|
|
width = font_metrics::width(str, font) + d * 2 + 2;
|
|
|
|
ascent = font_metrics::maxAscent(font) + d;
|
|
|
|
descent = font_metrics::maxDescent(font) + d;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void buttonText(string const & str, LyXFont const & font,
|
2002-12-01 22:59:25 +00:00
|
|
|
int & width,
|
|
|
|
int & ascent,
|
2002-06-11 21:44:00 +00:00
|
|
|
int & descent)
|
|
|
|
{
|
|
|
|
static int const d = 3;
|
2002-12-01 22:59:25 +00:00
|
|
|
|
2002-06-11 21:44:00 +00:00
|
|
|
width = font_metrics::width(str, font) + d * 2 + 2;
|
|
|
|
ascent = font_metrics::maxAscent(font) + d;
|
|
|
|
descent = font_metrics::maxDescent(font) + d;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace font_metrics
|
2002-12-01 22:59:25 +00:00
|
|
|
|
2002-06-11 21:44:00 +00:00
|
|
|
namespace xfont_metrics {
|
2002-12-01 22:59:25 +00:00
|
|
|
|
2002-06-11 21:44:00 +00:00
|
|
|
int width(XChar2b const * s, int n, LyXFont const & f)
|
2000-07-04 20:32:37 +00:00
|
|
|
{
|
2003-03-31 02:59:34 +00:00
|
|
|
if (!lyx_gui::use_gui)
|
2000-07-04 20:32:37 +00:00
|
|
|
return n;
|
2002-12-01 22:59:25 +00:00
|
|
|
|
2000-07-04 20:32:37 +00:00
|
|
|
if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
|
|
|
|
return ::XTextWidth16(getXFontstruct(f), s, n);
|
|
|
|
} else {
|
|
|
|
// emulate smallcaps since X doesn't support this
|
2002-06-11 21:44:00 +00:00
|
|
|
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) {
|
2001-12-20 15:11:51 +00:00
|
|
|
if (s[i].byte1)
|
|
|
|
c = s[i];
|
|
|
|
else {
|
|
|
|
c.byte1 = s[i].byte1;
|
|
|
|
c.byte2 = uppercase(s[i].byte2);
|
|
|
|
}
|
|
|
|
if (c.byte2 != s[i].byte2) {
|
2000-07-04 20:32:37 +00:00
|
|
|
result += ::XTextWidth16(getXFontstruct(smallfont), &c, 1);
|
|
|
|
} else {
|
|
|
|
result += ::XTextWidth16(getXFontstruct(f), &s[i], 1);
|
2001-07-24 09:12:20 +00:00
|
|
|
}
|
2000-07-04 20:32:37 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2002-12-01 22:59:25 +00:00
|
|
|
|
2000-07-04 20:32:37 +00:00
|
|
|
|
2002-06-11 21:44:00 +00:00
|
|
|
int XTextWidth(LyXFont const & f, char const * str, int count)
|
2000-04-04 00:19:15 +00:00
|
|
|
{
|
|
|
|
return ::XTextWidth(getXFontstruct(f), str, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-11 21:44:00 +00:00
|
|
|
int XTextWidth16(LyXFont const & f, XChar2b const * str, int count)
|
2000-07-04 20:32:37 +00:00
|
|
|
{
|
|
|
|
return ::XTextWidth16(getXFontstruct(f), str, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-11 21:44:00 +00:00
|
|
|
/// hmm, not a metric !
|
2002-12-01 22:59:25 +00:00
|
|
|
void XSetFont(Display * display, GC gc, LyXFont const & f)
|
2000-04-04 00:19:15 +00:00
|
|
|
{
|
|
|
|
::XSetFont(display, gc, getFontID(f));
|
|
|
|
}
|
2002-12-01 22:59:25 +00:00
|
|
|
|
2002-06-11 21:44:00 +00:00
|
|
|
} // namespace xfont_metrics
|