#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)
OtkFocusWidget::update();
- ft.drawString(getWindow(), x, bevel, *text_color, t);
+ ft.drawString(_xftdraw, x, bevel, *text_color, t);
} else
OtkFocusWidget::update();
}
#define __label_hh
#include "focuswidget.hh"
+#include "font.hh"
namespace otk {
void update(void);
private:
-
+ //! Object used by Xft to render to the drawable
+ XftDraw *_xftdraw;
+ //! Text displayed in the label
std::string _text;
};
#include "color.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 {
-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;
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;
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;
}
extern "C" {
#include <X11/Xlib.h>
+#define _XFT_NO_COMPAT_ // no Xft 1 API
#include <X11/Xft/Xft.h>
}
*/
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
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;
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;
};
}
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)
OtkWidget::update();
- ft.drawString(getWindow(), x, bevel, *getStyle()->getTextUnfocus(), t);
+ ft.drawString(_xftdraw, x, bevel, *getStyle()->getTextUnfocus(), t);
} else
OtkWidget::update();
}
#define __label_hh
#include "widget.hh"
+#include "font.hh"
namespace otk {
void update(void);
private:
-
+ //! Object used by Xft to render to the drawable
+ XftDraw *_xftdraw;
+ //! Text displayed in the label
std::string _text;
};
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);
}
}
# List of source files containing translatable strings.
-
+otk/display.cc
+otk/font.cc
src/openbox.cc
src/display.cc
src/client.cc