xft2 works. and works good.

This commit is contained in:
Dana Jansens 2002-12-03 08:54:51 +00:00
parent f77502100a
commit c6f228f3ff
8 changed files with 136 additions and 127 deletions

View file

@ -5,18 +5,25 @@
#endif #endif
#include "focuslabel.hh" #include "focuslabel.hh"
#include "display.hh"
#include "screeninfo.hh"
namespace otk { namespace otk {
OtkFocusLabel::OtkFocusLabel(OtkWidget *parent) OtkFocusLabel::OtkFocusLabel(OtkWidget *parent)
: OtkFocusWidget(parent), _text("") : OtkFocusWidget(parent), _text("")
{ {
const ScreenInfo *info = OBDisplay::screenInfo(getScreen());
_xftdraw = XftDrawCreate(OBDisplay::display, getWindow(), info->getVisual(),
info->getColormap());
setTexture(getStyle()->getLabelFocus()); setTexture(getStyle()->getLabelFocus());
setUnfocusTexture(getStyle()->getLabelUnfocus()); setUnfocusTexture(getStyle()->getLabelUnfocus());
} }
OtkFocusLabel::~OtkFocusLabel() OtkFocusLabel::~OtkFocusLabel()
{ {
XftDrawDestroy(_xftdraw);
} }
void OtkFocusLabel::update(void) void OtkFocusLabel::update(void)
@ -58,7 +65,7 @@ void OtkFocusLabel::update(void)
OtkFocusWidget::update(); OtkFocusWidget::update();
ft.drawString(getWindow(), x, bevel, *text_color, t); ft.drawString(_xftdraw, x, bevel, *text_color, t);
} else } else
OtkFocusWidget::update(); OtkFocusWidget::update();
} }

View file

@ -2,6 +2,7 @@
#define __label_hh #define __label_hh
#include "focuswidget.hh" #include "focuswidget.hh"
#include "font.hh"
namespace otk { namespace otk {
@ -18,7 +19,9 @@ public:
void update(void); void update(void);
private: private:
//! Object used by Xft to render to the drawable
XftDraw *_xftdraw;
//! Text displayed in the label
std::string _text; std::string _text;
}; };

View file

@ -23,58 +23,70 @@ using std::endl;
#include "color.hh" #include "color.hh"
#include "screeninfo.hh" #include "screeninfo.hh"
extern "C" {
#ifdef HAVE_STDIO_H
# include <stdio.h>
#endif // HAVE_STDIO_H
#include "gettext.h"
#define _(str) gettext(str)
}
namespace otk { namespace otk {
string BFont::_fallback_font = "fixed"; string BFont::_fallback_font = "fixed";
bool BFont::_xft_init = false;
BFont::BFont(int screen_num, const string &family, int size, BFont::BFont(int screen_num, const string &fontstring,
bool bold, bool italic, bool shadow, unsigned char offset, bool shadow, unsigned char offset, unsigned char tint)
unsigned char tint, bool antialias) : : _screen_num(screen_num),
_screen_num(screen_num), _fontstring(fontstring),
_family(family), _shadow(shadow),
_simplename(False), _offset(offset),
_size(size), _tint(tint),
_bold(bold), _xftfont(0)
_italic(italic), {
_antialias(antialias), assert(screen_num >= 0);
_shadow(shadow), assert(tint <= CHAR_MAX);
_offset(offset),
_tint(tint),
_xftfont(0) {
_valid = False;
_xftfont = XftFontOpen(OBDisplay::display, _screen_num, if (!_xft_init) {
XFT_FAMILY, XftTypeString, _family.c_str(), if (!XftInit(0)) {
XFT_SIZE, XftTypeInteger, _size, printf(_("Couldn't initialize Xft version %d.\n\n"), XftVersion);
XFT_WEIGHT, XftTypeInteger, (_bold ? ::exit(3);
XFT_WEIGHT_BOLD : }
XFT_WEIGHT_MEDIUM), printf(_("Using Xft %d.\n"), XftVersion);
XFT_SLANT, XftTypeInteger, (_italic ? _xft_init = true;
XFT_SLANT_ITALIC : }
XFT_SLANT_ROMAN),
XFT_ANTIALIAS, XftTypeBool, _antialias,
0);
if (! _xftfont)
return; // failure
_valid = 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) if (_xftfont)
XftFontClose(OBDisplay::display, _xftfont); XftFontClose(OBDisplay::display, _xftfont);
} }
void BFont::drawString(Drawable d, int x, int y, const BColor &color, void BFont::drawString(XftDraw *d, int x, int y, const BColor &color,
const string &string) const { const string &string, bool utf8) const
assert(_valid); {
assert(d);
const ScreenInfo *info = OBDisplay::screenInfo(_screen_num);
XftDraw *draw = XftDrawCreate(OBDisplay::display, d,
info->getVisual(), info->getColormap());
assert(draw);
if (_shadow) { if (_shadow) {
XftColor c; 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.color.alpha = _tint | _tint << 8; // transparent shadow
c.pixel = BlackPixel(OBDisplay::display, _screen_num); c.pixel = BlackPixel(OBDisplay::display, _screen_num);
XftDrawString8(draw, &c, _xftfont, x + _offset, if (utf8)
_xftfont->ascent + y + _offset, XftDrawStringUtf8(d, &c, _xftfont, x + _offset,
(XftChar8 *) string.c_str(), _xftfont->ascent + y + _offset,
string.size()); (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; XftColor c;
@ -97,36 +113,40 @@ void BFont::drawString(Drawable d, int x, int y, const BColor &color,
c.pixel = color.pixel(); c.pixel = color.pixel();
c.color.alpha = 0xff | 0xff << 8; // no transparency in BColor yet c.color.alpha = 0xff | 0xff << 8; // no transparency in BColor yet
XftDrawString8(draw, &c, _xftfont, x, _xftfont->ascent + y, if (utf8)
(XftChar8 *) string.c_str(), string.size()); 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; return;
} }
unsigned int BFont::measureString(const string &string) const { unsigned int BFont::measureString(const string &string, bool utf8) const
assert(_valid); {
XGlyphInfo info; XGlyphInfo info;
XftTextExtents8(OBDisplay::display, _xftfont, if (utf8)
(XftChar8 *) string.c_str(), string.size(), &info); 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); return info.xOff + (_shadow ? _offset : 0);
} }
unsigned int BFont::height(void) const { unsigned int BFont::height(void) const
assert(_valid); {
return _xftfont->height + (_shadow ? _offset : 0); return _xftfont->height + (_shadow ? _offset : 0);
} }
unsigned int BFont::maxCharWidth(void) const { unsigned int BFont::maxCharWidth(void) const
assert(_valid); {
return _xftfont->max_advance_width; return _xftfont->max_advance_width;
} }

View file

@ -4,6 +4,7 @@
extern "C" { extern "C" {
#include <X11/Xlib.h> #include <X11/Xlib.h>
#define _XFT_NO_COMPAT_ // no Xft 1 API
#include <X11/Xft/Xft.h> #include <X11/Xft/Xft.h>
} }
@ -23,6 +24,7 @@ class BFont {
*/ */
private: private:
static std::string _fallback_font; static std::string _fallback_font;
static bool _xft_init;
public: public:
// the fallback is only used for X fonts, not for Xft fonts, since it is // the fallback is only used for X fonts, not for Xft fonts, since it is
@ -37,13 +39,8 @@ public:
private: private:
int _screen_num; int _screen_num;
std::string _family; std::string _fontstring;
bool _simplename; // true if not spec'd as a -*-* string
int _size;
bool _bold;
bool _italic;
bool _antialias;
bool _shadow; bool _shadow;
unsigned char _offset; unsigned char _offset;
unsigned char _tint; unsigned char _tint;
@ -52,29 +49,27 @@ private:
bool createXftFont(void); bool createXftFont(void);
bool _valid;
public: public:
// loads an Xft font // loads an Xft font
BFont(int screen_num, const std::string &family, int size, BFont(int screen_num, const std::string &fontstring, bool shadow,
bool bold, bool italic, bool shadow, unsigned char offset, unsigned char offset, unsigned char tint);
unsigned char tint, bool antialias = True); virtual ~BFont();
virtual ~BFont(void);
inline bool valid(void) const { return _valid; } inline const std::string &fontstring() const { return _fontstring; }
inline std::string family(void) const { assert(_valid); return _family; } unsigned int height() const;
inline int size(void) const { assert(_valid); return _size; } unsigned int maxCharWidth() const;
inline bool bold(void) const { assert(_valid); return _bold; }
inline bool italic(void) const { assert(_valid); return _italic; }
unsigned int height(void) const; unsigned int measureString(const std::string &string,
unsigned int maxCharWidth(void) const; bool utf8 = false) const;
unsigned int measureString(const std::string &string) const; //! Draws a string into an XftDraw object
/*!
void drawString(Drawable d, int x, int y, const BColor &color, Be Warned: If you use an XftDraw object and a color, or a font from
const std::string &string) const; 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;
}; };
} }

View file

@ -11,11 +11,16 @@ namespace otk {
OtkLabel::OtkLabel(OtkWidget *parent) OtkLabel::OtkLabel(OtkWidget *parent)
: OtkWidget(parent), _text("") : OtkWidget(parent), _text("")
{ {
const ScreenInfo *info = OBDisplay::screenInfo(getScreen());
_xftdraw = XftDrawCreate(OBDisplay::display, getWindow(), info->getVisual(),
info->getColormap());
setTexture(getStyle()->getLabelUnfocus()); setTexture(getStyle()->getLabelUnfocus());
} }
OtkLabel::~OtkLabel() OtkLabel::~OtkLabel()
{ {
XftDrawDestroy(_xftdraw);
} }
void OtkLabel::update(void) void OtkLabel::update(void)
@ -55,7 +60,7 @@ void OtkLabel::update(void)
OtkWidget::update(); OtkWidget::update();
ft.drawString(getWindow(), x, bevel, *getStyle()->getTextUnfocus(), t); ft.drawString(_xftdraw, x, bevel, *getStyle()->getTextUnfocus(), t);
} else } else
OtkWidget::update(); OtkWidget::update();
} }

View file

@ -2,6 +2,7 @@
#define __label_hh #define __label_hh
#include "widget.hh" #include "widget.hh"
#include "font.hh"
namespace otk { namespace otk {
@ -18,7 +19,9 @@ public:
void update(void); void update(void);
private: private:
//! Object used by Xft to render to the drawable
XftDraw *_xftdraw;
//! Text displayed in the label
std::string _text; std::string _text;
}; };

View file

@ -234,53 +234,28 @@ BColor Style::readDatabaseColor(const std::string &rname,
BFont *Style::readDatabaseFont(const std::string &rbasename, BFont *Style::readDatabaseFont(const std::string &rbasename,
const Configuration &style) { const Configuration &style) {
std::string fontname; std::string fontstring, s;
std::string s; // XXX: load all this font stuff from the style...
int i; bool dropShadow = True;
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)) { unsigned char offset = 1;
if (s.find("bold") != std::string::npos) if (style.getValue(rbasename + "xft.shadow.offset", s)) {
bold = True; offset = atoi(s.c_str()); //doesn't detect errors
if (s.find("italic") != std::string::npos) if (offset > CHAR_MAX)
italic = True; offset = 1;
if (s.find("shadow") != std::string::npos)
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());
}
BFont *b = new BFont(screen_number, family, i, bold, italic,
dropShadow && shadow_fonts,
offset, tint, aa_fonts);
if (b->valid())
return b;
delete b;
} }
if (style.getValue(rbasename + "xft.font", s)) unsigned char tint = 0x40;
printf("Unable to load font \"%s\". Exiting\n", s.c_str()); if (style.getValue(rbasename + "xft.shadow.tint", s)) {
else tint = atoi(s.c_str());
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);
} }
} }

View file

@ -1,5 +1,6 @@
# List of source files containing translatable strings. # List of source files containing translatable strings.
otk/display.cc
otk/font.cc
src/openbox.cc src/openbox.cc
src/display.cc src/display.cc
src/client.cc src/client.cc