]> git.lyx.org Git - lyx.git/commitdiff
fontloader for gtk, probably not efficient
authorGeorg Baum <Georg.Baum@post.rwth-aachen.de>
Wed, 18 Oct 2006 20:19:55 +0000 (20:19 +0000)
committerGeorg Baum <Georg.Baum@post.rwth-aachen.de>
Wed, 18 Oct 2006 20:19:55 +0000 (20:19 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@15363 a592a061-630c-0410-9148-cb99ea01b6c8

src/frontends/gtk/GPainter.C
src/frontends/gtk/Makefile.am
src/frontends/gtk/xftFontLoader.h
src/frontends/gtk/xftFontMetrics.C
src/frontends/gtk/xftFontMetrics.h [new file with mode: 0644]

index 5da8aff2a95f10605d91e40512ad0bd8bad44e24..96b81d9dea367a1635d20ce6f18eae75c5a44821 100644 (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);
                        }
                }
        }
index 13ea2570a67f8061031ede67b5fa952b9db5c61b..4b7ca7fcfe097df496192341b344a3ee1b56bedf 100644 (file)
@@ -146,4 +146,5 @@ libgtk_la_SOURCES = \
        io_callback.h \
        xftFontLoader.C \
        xftFontLoader.h \
-       xftFontMetrics.C
+       xftFontMetrics.C \
+       xftFontMetrics.h
index 4c2cb086c67bd18b1d3891ae4ae7932a8ba5556b..0599a998a1110d771f8bbd932f5cc6fb7ecf8960 100644 (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,
index 5445660903903382d10e2371b0837c564849b600..ad2ebbcbc0167009e94ed0f1272102af3e13341d 100644 (file)
 #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;
 }
diff --git a/src/frontends/gtk/xftFontMetrics.h b/src/frontends/gtk/xftFontMetrics.h
new file mode 100644 (file)
index 0000000..c4450d9
--- /dev/null
@@ -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