fontloader for gtk, probably not efficient

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@15363 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Georg Baum 2006-10-18 20:19:55 +00:00
parent 39acc2260d
commit e343febe0b
5 changed files with 96 additions and 44 deletions

View File

@ -28,7 +28,9 @@
#include "language.h"
#include "LColor.h"
#include "xftFontLoader.h"
#include "frontends/font_metrics.h"
#include "frontends/Application.h"
#include "frontends/FontMetrics.h"
#include "support/lstrings.h"
@ -213,7 +215,7 @@ int GPainter::text(int x, int y,
x, y,
reinterpret_cast<FcChar32 const *>(s),
ls);
textwidth = font_metrics::width(s, ls, f);
textwidth = theApp->fontLoader().metrics(f).width(s, ls);
} else {
LyXFont smallfont(f);
smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
@ -228,7 +230,7 @@ int GPainter::text(int x, int y,
x + textwidth, y,
reinterpret_cast<FcChar32 *>(&c),
1);
textwidth += font_metrics::width(c, smallfont);
textwidth += theApp->fontLoader().metrics(smallfont).width(c);
} else {
XftDrawString32(draw,
xftClr,
@ -236,7 +238,7 @@ int GPainter::text(int x, int y,
x + textwidth, y,
reinterpret_cast<FcChar32 *>(&c),
1);
textwidth += font_metrics::width(c, f);
textwidth += theApp->fontLoader().metrics(f).width(c);
}
}
}

View File

@ -146,4 +146,5 @@ libgtk_la_SOURCES = \
io_callback.h \
xftFontLoader.C \
xftFontLoader.h \
xftFontMetrics.C
xftFontMetrics.C \
xftFontMetrics.h

View File

@ -14,6 +14,8 @@
#include "frontends/FontLoader.h"
#include "xftFontMetrics.h"
#include "lyxfont.h"
#include <gtkmm.h>
@ -34,6 +36,15 @@ public:
virtual void update();
virtual bool available(LyXFont const & f);
virtual lyx::frontend::FontMetrics const & metrics(LyXFont const & f)
{
if (f.realShape() != LyXFont::SMALLCAPS_SHAPE)
return font_metrics(load(f.family(), f.series(), f.realShape(), f.size()), 0);
LyXFont scf(f);
scf.decSize().decSize().setShape(LyXFont::UP_SHAPE);
return font_metrics(load(f.family(), f.series(), f.realShape(), f.size()),
load(scf.family(), scf.series(), scf.realShape(), scf.size()));
}
/// Load font
XftFont * load(LyXFont::FONT_FAMILY family,

View File

@ -18,9 +18,10 @@
#undef _GLIBCPP_CONCEPT_CHECKS
#endif
#include "xftFontMetrics.h"
#include "GtkmmX.h"
#include "xftFontLoader.h"
#include "font_metrics.h"
#include "lyxrc.h"
#include "encoding.h"
#include "language.h"
@ -43,13 +44,6 @@ using std::string;
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;
@ -82,23 +76,26 @@ inline int XGlyphLogWidth(XGlyphInfo const & info)
} // namespace anon
int font_metrics::maxAscent(LyXFont const & f)
font_metrics::font_metrics(XftFont * f, XftFont * scf)
: font(f), fontS(scf)
{
}
int font_metrics::maxAscent() const
{
XftFont * font = getXftFont(f);
return font->ascent;
}
int font_metrics::maxDescent(LyXFont const & f)
int font_metrics::maxDescent() const
{
XftFont * font = getXftFont(f);
return font->descent;
}
int font_metrics::ascent(char_type c,LyXFont const & f)
int font_metrics::ascent(char_type c) const
{
XftFont * font = getXftFont(f);
XGlyphInfo glyph;
XftTextExtents32(getDisplay(), font,
reinterpret_cast<FcChar32 *>(&c),
@ -108,9 +105,8 @@ int font_metrics::ascent(char_type c,LyXFont const & f)
}
int font_metrics::descent(char_type c,LyXFont const & f)
int font_metrics::descent(char_type c) const
{
XftFont * font = getXftFont(f);
XGlyphInfo glyph;
XftTextExtents32(getDisplay(), font,
reinterpret_cast<FcChar32 *>(&c),
@ -120,9 +116,8 @@ int font_metrics::descent(char_type c,LyXFont const & f)
}
int font_metrics::lbearing(char_type c,LyXFont const & f)
int font_metrics::lbearing(char_type c) const
{
XftFont * font = getXftFont(f);
XGlyphInfo glyph;
XftTextExtents32(getDisplay(), font,
reinterpret_cast<FcChar32 *>(&c),
@ -132,9 +127,8 @@ int font_metrics::lbearing(char_type c,LyXFont const & f)
}
int font_metrics::rbearing(char_type c,LyXFont const & f)
int font_metrics::rbearing(char_type c) const
{
XftFont * font = getXftFont(f);
XGlyphInfo glyph;
XftTextExtents32(getDisplay(), font,
reinterpret_cast<FcChar32 *>(&c),
@ -144,11 +138,10 @@ int font_metrics::rbearing(char_type c,LyXFont const & f)
}
int font_metrics::width(char_type const * s, size_t n, LyXFont const & f)
int font_metrics::width(char_type const * s, size_t n) const
{
XftFont * font = getXftFont(f);
XGlyphInfo glyph;
if (f.realShape() != LyXFont::SMALLCAPS_SHAPE){
if (!fontS){
XftTextExtents32(getDisplay(),
font,
reinterpret_cast<FcChar32 const *>(s),
@ -157,9 +150,6 @@ int font_metrics::width(char_type const * s, size_t n, LyXFont const & f)
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) {
char_type c = lyx::support::uppercase(s[i]);
if (c != s[i]) {
@ -183,37 +173,37 @@ int font_metrics::width(char_type const * s, size_t n, LyXFont const & f)
}
int font_metrics::signedWidth(docstring const & s, LyXFont const & f)
int font_metrics::signedWidth(docstring const & s) const
{
if (s.empty())
return 0;
if (s[0] == '-')
return width(s.substr(1, s.length() - 1), f);
return width(s.substr(1, s.length() - 1).c_str(), s.length() - 1);
else
return width(s, f);
return width(s.c_str(), s.length());
}
void font_metrics::rectText(docstring const & str, LyXFont const & font,
void font_metrics::rectText(docstring const & str,
int & width,
int & ascent,
int & descent)
int & descent) const
{
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;
width = font_metrics::width(str.c_str(), str.length()) + d * 2 + 2;
ascent = font_metrics::maxAscent() + d;
descent = font_metrics::maxDescent() + d;
}
void font_metrics::buttonText(docstring const & str, LyXFont const & font,
void font_metrics::buttonText(docstring const & str,
int & width,
int & ascent,
int & descent)
int & descent) const
{
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;
width = font_metrics::width(str.c_str(), str.length()) + d * 2 + 2;
ascent = font_metrics::maxAscent() + d;
descent = font_metrics::maxDescent() + d;
}

View File

@ -0,0 +1,48 @@
// -*- C++ -*-
/**
* \file xftFontMetrics.h
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Abdelrazak Younes
*
* Full author contact details are available in file CREDITS.
*/
#ifndef XFT_FONT_METRICS_H
#define XFT_FONT_METRICS_H
#include "frontends/FontMetrics.h"
#include <gtkmm.h>
#include <X11/Xft/Xft.h>
class font_metrics: public lyx::frontend::FontMetrics
{
public:
font_metrics(XftFont * f, XftFont * scf);
virtual ~font_metrics() {}
virtual int maxAscent() const;
virtual int maxDescent() const;
virtual int ascent(lyx::char_type c) const;
int descent(lyx::char_type c) const;
virtual int lbearing(lyx::char_type c) const;
virtual int rbearing(lyx::char_type c) const;
virtual int width(lyx::char_type const * s, size_t n) const;
virtual int signedWidth(lyx::docstring const & s) const;
virtual void rectText(lyx::docstring const & str,
int & width,
int & ascent,
int & descent) const;
virtual void buttonText(lyx::docstring const & str,
int & width,
int & ascent,
int & descent) const;
private:
XftFont * font;
XftFont * fontS;
};
#endif // XFT_FONT_METRICS_H