From e343febe0b5ac64eeeff9db727da4645f1c93cd3 Mon Sep 17 00:00:00 2001 From: Georg Baum Date: Wed, 18 Oct 2006 20:19:55 +0000 Subject: [PATCH] fontloader for gtk, probably not efficient git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@15363 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/frontends/gtk/GPainter.C | 10 +++-- src/frontends/gtk/Makefile.am | 3 +- src/frontends/gtk/xftFontLoader.h | 11 +++++ src/frontends/gtk/xftFontMetrics.C | 68 +++++++++++++----------------- src/frontends/gtk/xftFontMetrics.h | 48 +++++++++++++++++++++ 5 files changed, 96 insertions(+), 44 deletions(-) create mode 100644 src/frontends/gtk/xftFontMetrics.h diff --git a/src/frontends/gtk/GPainter.C b/src/frontends/gtk/GPainter.C index 5da8aff2a9..96b81d9dea 100644 --- a/src/frontends/gtk/GPainter.C +++ b/src/frontends/gtk/GPainter.C @@ -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(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(&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(&c), 1); - textwidth += font_metrics::width(c, f); + textwidth += theApp->fontLoader().metrics(f).width(c); } } } diff --git a/src/frontends/gtk/Makefile.am b/src/frontends/gtk/Makefile.am index 13ea2570a6..4b7ca7fcfe 100644 --- a/src/frontends/gtk/Makefile.am +++ b/src/frontends/gtk/Makefile.am @@ -146,4 +146,5 @@ libgtk_la_SOURCES = \ io_callback.h \ xftFontLoader.C \ xftFontLoader.h \ - xftFontMetrics.C + xftFontMetrics.C \ + xftFontMetrics.h diff --git a/src/frontends/gtk/xftFontLoader.h b/src/frontends/gtk/xftFontLoader.h index 4c2cb086c6..0599a998a1 100644 --- a/src/frontends/gtk/xftFontLoader.h +++ b/src/frontends/gtk/xftFontLoader.h @@ -14,6 +14,8 @@ #include "frontends/FontLoader.h" +#include "xftFontMetrics.h" + #include "lyxfont.h" #include @@ -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, diff --git a/src/frontends/gtk/xftFontMetrics.C b/src/frontends/gtk/xftFontMetrics.C index 5445660903..ad2ebbcbc0 100644 --- a/src/frontends/gtk/xftFontMetrics.C +++ b/src/frontends/gtk/xftFontMetrics.C @@ -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(&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(&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(&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(&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(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; } diff --git a/src/frontends/gtk/xftFontMetrics.h b/src/frontends/gtk/xftFontMetrics.h new file mode 100644 index 0000000000..c4450d9882 --- /dev/null +++ b/src/frontends/gtk/xftFontMetrics.h @@ -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 +#include + +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 -- 2.39.2