From c6f228f3ff9ab7087669366a23d2ae61d8e5b9b9 Mon Sep 17 00:00:00 2001 From: Dana Jansens Date: Tue, 3 Dec 2002 08:54:51 +0000 Subject: [PATCH] xft2 works. and works good. --- otk/focuslabel.cc | 9 ++- otk/focuslabel.hh | 5 +- otk/font.cc | 140 ++++++++++++++++++++++++++-------------------- otk/font.hh | 41 ++++++-------- otk/label.cc | 7 ++- otk/label.hh | 5 +- otk/style.cc | 57 ++++++------------- po/POTFILES.in | 3 +- 8 files changed, 138 insertions(+), 129 deletions(-) diff --git a/otk/focuslabel.cc b/otk/focuslabel.cc index e75a1d33..9eeaed17 100644 --- a/otk/focuslabel.cc +++ b/otk/focuslabel.cc @@ -5,18 +5,25 @@ #endif #include "focuslabel.hh" +#include "display.hh" +#include "screeninfo.hh" namespace otk { OtkFocusLabel::OtkFocusLabel(OtkWidget *parent) : OtkFocusWidget(parent), _text("") { + const ScreenInfo *info = OBDisplay::screenInfo(getScreen()); + _xftdraw = XftDrawCreate(OBDisplay::display, getWindow(), info->getVisual(), + info->getColormap()); + setTexture(getStyle()->getLabelFocus()); setUnfocusTexture(getStyle()->getLabelUnfocus()); } OtkFocusLabel::~OtkFocusLabel() { + XftDrawDestroy(_xftdraw); } void OtkFocusLabel::update(void) @@ -58,7 +65,7 @@ void OtkFocusLabel::update(void) OtkFocusWidget::update(); - ft.drawString(getWindow(), x, bevel, *text_color, t); + ft.drawString(_xftdraw, x, bevel, *text_color, t); } else OtkFocusWidget::update(); } diff --git a/otk/focuslabel.hh b/otk/focuslabel.hh index c15876fe..cc94f05a 100644 --- a/otk/focuslabel.hh +++ b/otk/focuslabel.hh @@ -2,6 +2,7 @@ #define __label_hh #include "focuswidget.hh" +#include "font.hh" namespace otk { @@ -18,7 +19,9 @@ public: void update(void); private: - + //! Object used by Xft to render to the drawable + XftDraw *_xftdraw; + //! Text displayed in the label std::string _text; }; diff --git a/otk/font.cc b/otk/font.cc index a68aeba2..e7b8bab1 100644 --- a/otk/font.cc +++ b/otk/font.cc @@ -23,58 +23,70 @@ using std::endl; #include "color.hh" #include "screeninfo.hh" +extern "C" { +#ifdef HAVE_STDIO_H +# include +#endif // HAVE_STDIO_H + +#include "gettext.h" +#define _(str) gettext(str) +} + namespace otk { -string BFont::_fallback_font = "fixed"; - -BFont::BFont(int screen_num, const string &family, int size, - bool bold, bool italic, bool shadow, unsigned char offset, - unsigned char tint, bool antialias) : - _screen_num(screen_num), - _family(family), - _simplename(False), - _size(size), - _bold(bold), - _italic(italic), - _antialias(antialias), - _shadow(shadow), - _offset(offset), - _tint(tint), - _xftfont(0) { - _valid = False; - - _xftfont = XftFontOpen(OBDisplay::display, _screen_num, - XFT_FAMILY, XftTypeString, _family.c_str(), - XFT_SIZE, XftTypeInteger, _size, - XFT_WEIGHT, XftTypeInteger, (_bold ? - XFT_WEIGHT_BOLD : - XFT_WEIGHT_MEDIUM), - XFT_SLANT, XftTypeInteger, (_italic ? - XFT_SLANT_ITALIC : - XFT_SLANT_ROMAN), - XFT_ANTIALIAS, XftTypeBool, _antialias, - 0); - if (! _xftfont) - return; // failure - - _valid = True; +string BFont::_fallback_font = "fixed"; +bool BFont::_xft_init = false; + +BFont::BFont(int screen_num, const string &fontstring, + bool shadow, unsigned char offset, unsigned char tint) + : _screen_num(screen_num), + _fontstring(fontstring), + _shadow(shadow), + _offset(offset), + _tint(tint), + _xftfont(0) +{ + assert(screen_num >= 0); + assert(tint <= CHAR_MAX); + + if (!_xft_init) { + if (!XftInit(0)) { + printf(_("Couldn't initialize Xft version %d.\n\n"), XftVersion); + ::exit(3); + } + printf(_("Using Xft %d.\n"), XftVersion); + _xft_init = true; + } + + if ((_xftfont = XftFontOpenName(OBDisplay::display, _screen_num, + _fontstring.c_str()))) + return; + + printf(_("Unable to load font: %s"), _fontstring.c_str()); + printf(_("Trying fallback font: %s\n"), _fallback_font.c_str()); + + if ((_xftfont = XftFontOpenName(OBDisplay::display, _screen_num, + _fallback_font.c_str()))) + return; + + printf(_("Unable to load font: %s"), _fallback_font.c_str()); + printf(_("Aborting!.\n")); + + ::exit(3); // can't continue without a font } -BFont::~BFont(void) { +BFont::~BFont(void) +{ if (_xftfont) XftFontClose(OBDisplay::display, _xftfont); } -void BFont::drawString(Drawable d, int x, int y, const BColor &color, - const string &string) const { - assert(_valid); - - const ScreenInfo *info = OBDisplay::screenInfo(_screen_num); - XftDraw *draw = XftDrawCreate(OBDisplay::display, d, - info->getVisual(), info->getColormap()); - assert(draw); +void BFont::drawString(XftDraw *d, int x, int y, const BColor &color, + const string &string, bool utf8) const +{ + assert(d); if (_shadow) { XftColor c; @@ -84,10 +96,14 @@ void BFont::drawString(Drawable d, int x, int y, const BColor &color, c.color.alpha = _tint | _tint << 8; // transparent shadow c.pixel = BlackPixel(OBDisplay::display, _screen_num); - XftDrawString8(draw, &c, _xftfont, x + _offset, - _xftfont->ascent + y + _offset, - (XftChar8 *) string.c_str(), - string.size()); + if (utf8) + XftDrawStringUtf8(d, &c, _xftfont, x + _offset, + _xftfont->ascent + y + _offset, + (const FcChar8*)string.c_str(), string.size()); + else + XftDrawString8(d, &c, _xftfont, x + _offset, + _xftfont->ascent + y + _offset, + (const FcChar8*)string.c_str(), string.size()); } XftColor c; @@ -97,36 +113,40 @@ void BFont::drawString(Drawable d, int x, int y, const BColor &color, c.pixel = color.pixel(); c.color.alpha = 0xff | 0xff << 8; // no transparency in BColor yet - XftDrawString8(draw, &c, _xftfont, x, _xftfont->ascent + y, - (XftChar8 *) string.c_str(), string.size()); + if (utf8) + XftDrawStringUtf8(d, &c, _xftfont, x, _xftfont->ascent + y, + (const FcChar8*)string.c_str(), string.size()); + else + XftDrawString8(d, &c, _xftfont, x, _xftfont->ascent + y, + (const FcChar8*)string.c_str(), string.size()); - XftDrawDestroy(draw); return; } -unsigned int BFont::measureString(const string &string) const { - assert(_valid); - +unsigned int BFont::measureString(const string &string, bool utf8) const +{ XGlyphInfo info; - XftTextExtents8(OBDisplay::display, _xftfont, - (XftChar8 *) string.c_str(), string.size(), &info); + if (utf8) + XftTextExtentsUtf8(OBDisplay::display, _xftfont, + (const FcChar8*)string.c_str(), string.size(), &info); + else + XftTextExtents8(OBDisplay::display, _xftfont, + (const FcChar8*)string.c_str(), string.size(), &info); return info.xOff + (_shadow ? _offset : 0); } -unsigned int BFont::height(void) const { - assert(_valid); - +unsigned int BFont::height(void) const +{ return _xftfont->height + (_shadow ? _offset : 0); } -unsigned int BFont::maxCharWidth(void) const { - assert(_valid); - +unsigned int BFont::maxCharWidth(void) const +{ return _xftfont->max_advance_width; } diff --git a/otk/font.hh b/otk/font.hh index 8afd2bd1..8ff1f67d 100644 --- a/otk/font.hh +++ b/otk/font.hh @@ -4,6 +4,7 @@ extern "C" { #include +#define _XFT_NO_COMPAT_ // no Xft 1 API #include } @@ -23,6 +24,7 @@ class BFont { */ private: static std::string _fallback_font; + static bool _xft_init; public: // the fallback is only used for X fonts, not for Xft fonts, since it is @@ -37,13 +39,8 @@ public: private: int _screen_num; - std::string _family; - bool _simplename; // true if not spec'd as a -*-* string - int _size; - bool _bold; - bool _italic; + std::string _fontstring; - bool _antialias; bool _shadow; unsigned char _offset; unsigned char _tint; @@ -52,29 +49,27 @@ private: bool createXftFont(void); - bool _valid; - public: // loads an Xft font - BFont(int screen_num, const std::string &family, int size, - bool bold, bool italic, bool shadow, unsigned char offset, - unsigned char tint, bool antialias = True); - virtual ~BFont(void); - - inline bool valid(void) const { return _valid; } + BFont(int screen_num, const std::string &fontstring, bool shadow, + unsigned char offset, unsigned char tint); + virtual ~BFont(); - inline std::string family(void) const { assert(_valid); return _family; } - inline int size(void) const { assert(_valid); return _size; } - inline bool bold(void) const { assert(_valid); return _bold; } - inline bool italic(void) const { assert(_valid); return _italic; } + inline const std::string &fontstring() const { return _fontstring; } - unsigned int height(void) const; - unsigned int maxCharWidth(void) const; + unsigned int height() const; + unsigned int maxCharWidth() const; - unsigned int measureString(const std::string &string) const; + unsigned int measureString(const std::string &string, + bool utf8 = false) const; - void drawString(Drawable d, int x, int y, const BColor &color, - const std::string &string) const; + //! Draws a string into an XftDraw object + /*! + Be Warned: If you use an XftDraw object and a color, or a font from + different screens, you WILL have unpredictable results! :) + */ + void drawString(XftDraw *d, int x, int y, const BColor &color, + const std::string &string, bool utf8 = false) const; }; } diff --git a/otk/label.cc b/otk/label.cc index 1537b927..21811a2c 100644 --- a/otk/label.cc +++ b/otk/label.cc @@ -11,11 +11,16 @@ namespace otk { OtkLabel::OtkLabel(OtkWidget *parent) : OtkWidget(parent), _text("") { + const ScreenInfo *info = OBDisplay::screenInfo(getScreen()); + _xftdraw = XftDrawCreate(OBDisplay::display, getWindow(), info->getVisual(), + info->getColormap()); + setTexture(getStyle()->getLabelUnfocus()); } OtkLabel::~OtkLabel() { + XftDrawDestroy(_xftdraw); } void OtkLabel::update(void) @@ -55,7 +60,7 @@ void OtkLabel::update(void) OtkWidget::update(); - ft.drawString(getWindow(), x, bevel, *getStyle()->getTextUnfocus(), t); + ft.drawString(_xftdraw, x, bevel, *getStyle()->getTextUnfocus(), t); } else OtkWidget::update(); } diff --git a/otk/label.hh b/otk/label.hh index 7cfa23bf..c959a2fa 100644 --- a/otk/label.hh +++ b/otk/label.hh @@ -2,6 +2,7 @@ #define __label_hh #include "widget.hh" +#include "font.hh" namespace otk { @@ -18,7 +19,9 @@ public: void update(void); private: - + //! Object used by Xft to render to the drawable + XftDraw *_xftdraw; + //! Text displayed in the label std::string _text; }; diff --git a/otk/style.cc b/otk/style.cc index 89c7b05d..a3d21788 100644 --- a/otk/style.cc +++ b/otk/style.cc @@ -234,53 +234,28 @@ BColor Style::readDatabaseColor(const std::string &rname, BFont *Style::readDatabaseFont(const std::string &rbasename, const Configuration &style) { - std::string fontname; + std::string fontstring, s; - std::string s; + // XXX: load all this font stuff from the style... - int i; - if (style.getValue(rbasename + "xft.font", s) && - style.getValue(rbasename + "xft.size", i)) { - std::string family = s; - bool bold = False; - bool italic = False; - bool dropShadow = False; - - if (style.getValue(rbasename + "xft.flags", s)) { - if (s.find("bold") != std::string::npos) - bold = True; - if (s.find("italic") != std::string::npos) - italic = True; - if (s.find("shadow") != std::string::npos) - dropShadow = True; - } + bool dropShadow = True; - unsigned char offset = 1; - if (style.getValue(rbasename + "xft.shadow.offset", s)) { - offset = atoi(s.c_str()); //doesn't detect errors - if (offset > CHAR_MAX) - offset = 1; - } - - unsigned char tint = 0x40; - if (style.getValue(rbasename + "xft.shadow.tint", s)) { - tint = atoi(s.c_str()); - } + unsigned char offset = 1; + if (style.getValue(rbasename + "xft.shadow.offset", s)) { + offset = atoi(s.c_str()); //doesn't detect errors + if (offset > CHAR_MAX) + offset = 1; + } - - BFont *b = new BFont(screen_number, family, i, bold, italic, - dropShadow && shadow_fonts, - offset, tint, aa_fonts); - if (b->valid()) - return b; - delete b; + unsigned char tint = 0x40; + if (style.getValue(rbasename + "xft.shadow.tint", s)) { + tint = atoi(s.c_str()); } - if (style.getValue(rbasename + "xft.font", s)) - printf("Unable to load font \"%s\". Exiting\n", s.c_str()); - else - printf("Font not defined by style. Exiting\n"); - exit(2); // can't continue without a font + fontstring = "Arial,Sans-8:bold"; + + // if this fails, it ::exit()'s + return new BFont(screen_number, fontstring, dropShadow, offset, tint); } } diff --git a/po/POTFILES.in b/po/POTFILES.in index 2d5b090d..f783532e 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -1,5 +1,6 @@ # List of source files containing translatable strings. - +otk/display.cc +otk/font.cc src/openbox.cc src/display.cc src/client.cc -- 2.39.2