2003-09-02 10:29:05 +00:00
|
|
|
/**
|
|
|
|
* \file xfont_metrics.C
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Huang Ying
|
|
|
|
*
|
2003-09-02 17:02:32 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2003-09-02 10:29:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2004-11-16 23:18:46 +00:00
|
|
|
// Too hard to make concept checks work with this file
|
2005-01-29 15:09:14 +00:00
|
|
|
#ifdef _GLIBCXX_CONCEPT_CHECKS
|
|
|
|
#undef _GLIBCXX_CONCEPT_CHECKS
|
|
|
|
#endif
|
2004-11-16 23:18:46 +00:00
|
|
|
#ifdef _GLIBCPP_CONCEPT_CHECKS
|
|
|
|
#undef _GLIBCPP_CONCEPT_CHECKS
|
|
|
|
#endif
|
|
|
|
|
2003-09-02 10:29:05 +00:00
|
|
|
#include "GtkmmX.h"
|
|
|
|
#include "xftFontLoader.h"
|
|
|
|
#include "font_metrics.h"
|
|
|
|
#include "lyxrc.h"
|
|
|
|
#include "encoding.h"
|
|
|
|
#include "language.h"
|
|
|
|
|
2004-09-26 18:36:07 +00:00
|
|
|
#include "support/lstrings.h"
|
2005-08-03 07:47:54 +00:00
|
|
|
#include "debug.h"
|
2004-09-26 18:36:07 +00:00
|
|
|
|
2004-09-26 13:18:29 +00:00
|
|
|
#include <gtkmm.h>
|
|
|
|
|
2003-09-02 10:29:05 +00:00
|
|
|
#include <boost/scoped_array.hpp>
|
|
|
|
|
2004-09-26 13:18:29 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2003-10-06 15:43:21 +00:00
|
|
|
using std::string;
|
|
|
|
|
2003-09-02 10:29:05 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
|
|
inline XftFont * getXftFont(LyXFont const & f)
|
|
|
|
{
|
|
|
|
return fontLoader.load(f.family(), f.series(),
|
|
|
|
f.realShape(), f.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline int XGlyphAscent(XGlyphInfo const & info)
|
|
|
|
{
|
|
|
|
return info.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline int XGlyphDescent(XGlyphInfo const & info)
|
|
|
|
{
|
|
|
|
return info.height - info.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline int XGlyphLbearing(XGlyphInfo const & info)
|
|
|
|
{
|
|
|
|
return -info.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline int XGlyphRbearing(XGlyphInfo const & info)
|
|
|
|
{
|
|
|
|
return -info.x + info.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline int XGlyphLogWidth(XGlyphInfo const & info)
|
|
|
|
{
|
|
|
|
return info.xOff;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace anon
|
|
|
|
|
|
|
|
|
|
|
|
namespace font_metrics {
|
|
|
|
|
|
|
|
|
|
|
|
int maxAscent(LyXFont const & f)
|
|
|
|
{
|
|
|
|
XftFont * font = getXftFont(f);
|
|
|
|
return font->ascent;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int maxDescent(LyXFont const & f)
|
|
|
|
{
|
|
|
|
XftFont * font = getXftFont(f);
|
|
|
|
return font->descent;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-14 14:46:22 +00:00
|
|
|
int ascent(char c,LyXFont const & f)
|
2003-09-02 10:29:05 +00:00
|
|
|
{
|
|
|
|
XftFont * font = getXftFont(f);
|
|
|
|
XGlyphInfo glyph;
|
2006-02-14 14:46:22 +00:00
|
|
|
XftTextExtents8(getDisplay(), font,
|
|
|
|
reinterpret_cast<XftChar8 *>(&c),
|
2003-09-02 10:29:05 +00:00
|
|
|
1,
|
|
|
|
&glyph);
|
|
|
|
return XGlyphAscent(glyph);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-14 14:46:22 +00:00
|
|
|
int descent(char c,LyXFont const & f)
|
2003-09-02 10:29:05 +00:00
|
|
|
{
|
|
|
|
XftFont * font = getXftFont(f);
|
|
|
|
XGlyphInfo glyph;
|
2006-02-14 14:46:22 +00:00
|
|
|
XftTextExtents8(getDisplay(), font,
|
|
|
|
reinterpret_cast<XftChar8 *>(&c),
|
2003-09-02 10:29:05 +00:00
|
|
|
1,
|
|
|
|
&glyph);
|
|
|
|
return XGlyphDescent(glyph);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-14 14:46:22 +00:00
|
|
|
int lbearing(char c,LyXFont const & f)
|
2004-09-26 13:18:29 +00:00
|
|
|
{
|
2003-09-02 10:29:05 +00:00
|
|
|
XftFont * font = getXftFont(f);
|
|
|
|
XGlyphInfo glyph;
|
2006-02-14 14:46:22 +00:00
|
|
|
XftTextExtents8(getDisplay(), font,
|
|
|
|
reinterpret_cast<XftChar8 *>(&c),
|
2003-09-02 10:29:05 +00:00
|
|
|
1,
|
|
|
|
&glyph);
|
|
|
|
return XGlyphLbearing(glyph);
|
2004-09-26 13:18:29 +00:00
|
|
|
}
|
2003-09-02 10:29:05 +00:00
|
|
|
|
|
|
|
|
2006-02-14 14:46:22 +00:00
|
|
|
int rbearing(char c,LyXFont const & f)
|
2003-09-02 10:29:05 +00:00
|
|
|
{
|
|
|
|
XftFont * font = getXftFont(f);
|
|
|
|
XGlyphInfo glyph;
|
2006-02-14 14:46:22 +00:00
|
|
|
XftTextExtents8(getDisplay(), font,
|
|
|
|
reinterpret_cast<XftChar8 *>(&c),
|
2003-09-02 10:29:05 +00:00
|
|
|
1,
|
|
|
|
&glyph);
|
|
|
|
return XGlyphRbearing(glyph);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-14 14:46:22 +00:00
|
|
|
int width(char const * s, size_t n, LyXFont const & f)
|
2003-09-02 10:29:05 +00:00
|
|
|
{
|
|
|
|
XftFont * font = getXftFont(f);
|
|
|
|
XGlyphInfo glyph;
|
|
|
|
if (f.realShape() != LyXFont::SMALLCAPS_SHAPE){
|
2006-02-14 14:46:22 +00:00
|
|
|
XftTextExtents8(getDisplay(), font,
|
|
|
|
reinterpret_cast<XftChar8 *>(const_cast<char *>(s)), n, &glyph);
|
2003-09-02 10:29:05 +00:00
|
|
|
return XGlyphLogWidth(glyph);
|
|
|
|
} else {
|
|
|
|
int result = 0;
|
|
|
|
LyXFont smallfont(f);
|
|
|
|
smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
|
|
|
|
XftFont * fontS = getXftFont(smallfont);
|
|
|
|
for (size_t i = 0; i < n; ++i) {
|
2006-02-14 14:46:22 +00:00
|
|
|
char c = lyx::support::uppercase(s[i]);
|
|
|
|
if (c != s[i]) {
|
|
|
|
XftTextExtents8(getDisplay(), fontS,
|
|
|
|
reinterpret_cast<XftChar8 *>(&c),
|
2003-09-02 10:29:05 +00:00
|
|
|
1,
|
|
|
|
&glyph);
|
|
|
|
result += XGlyphLogWidth(glyph);
|
|
|
|
} else {
|
2006-02-14 14:46:22 +00:00
|
|
|
XftTextExtents8(getDisplay(), font,
|
|
|
|
reinterpret_cast<XftChar8 *>(&c),
|
2003-09-02 10:29:05 +00:00
|
|
|
1,
|
|
|
|
&glyph);
|
|
|
|
result += XGlyphLogWidth(glyph);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int signedWidth(string const & s, LyXFont const & f)
|
|
|
|
{
|
|
|
|
if (s.empty())
|
|
|
|
return 0;
|
2006-02-14 14:46:22 +00:00
|
|
|
if (s[0] == '-')
|
|
|
|
return width(s.c_str() + 1, s.size() - 1, f);
|
2003-09-21 16:02:54 +00:00
|
|
|
else
|
2006-02-14 14:46:22 +00:00
|
|
|
return width(s.c_str(), s.size(), f);
|
2003-09-02 10:29:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void rectText(string const & str, LyXFont const & font,
|
|
|
|
int & width,
|
|
|
|
int & ascent,
|
|
|
|
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,
|
|
|
|
int & width,
|
|
|
|
int & ascent,
|
|
|
|
int & descent)
|
|
|
|
{
|
|
|
|
static int const d = 3;
|
|
|
|
|
|
|
|
width = font_metrics::width(str, font) + d * 2 + 2;
|
|
|
|
ascent = font_metrics::maxAscent(font) + d;
|
|
|
|
descent = font_metrics::maxDescent(font) + d;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace font_metrics
|