]> icculus.org git repositories - mikachu/openbox.git/blob - otk/font.cc
stop using the Utf8 functions. will use them later for ONLY Utf-8 encoded strings!!
[mikachu/openbox.git] / otk / font.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #ifdef    HAVE_CONFIG_H
4 #  include "../config.h"
5 #endif // HAVE_CONFIG_H
6
7 extern "C" {
8 #ifdef HAVE_STDLIB_H
9 #  include <stdlib.h>
10 #endif // HAVE_STDLIB_H
11 }
12
13 #include <iostream>
14 #include <algorithm>
15
16 using std::string;
17 using std::cerr;
18 using std::endl;
19
20 #include "font.hh"
21 #include "util.hh"
22 #include "gccache.hh"
23 #include "color.hh"
24 #include "screeninfo.hh"
25
26 namespace otk {
27
28 string      BFont::_fallback_font   = "fixed";
29
30 BFont::BFont(int screen_num, const string &family, int size,
31              bool bold, bool italic, bool shadow, unsigned char offset, 
32              unsigned char tint, bool antialias) :
33                                           _screen_num(screen_num),
34                                           _family(family),
35                                           _simplename(False),
36                                           _size(size),
37                                           _bold(bold),
38                                           _italic(italic),
39                                           _antialias(antialias),
40                                           _shadow(shadow),
41                                           _offset(offset),
42                                           _tint(tint),
43                                           _xftfont(0) {
44   _valid = False;
45
46   _xftfont = XftFontOpen(OBDisplay::display, _screen_num,
47                          XFT_FAMILY, XftTypeString,  _family.c_str(),
48                          XFT_SIZE,   XftTypeInteger, _size,
49                          XFT_WEIGHT, XftTypeInteger, (_bold ?
50                                                       XFT_WEIGHT_BOLD :
51                                                       XFT_WEIGHT_MEDIUM),
52                          XFT_SLANT,  XftTypeInteger, (_italic ?
53                                                       XFT_SLANT_ITALIC :
54                                                       XFT_SLANT_ROMAN),
55                          XFT_ANTIALIAS, XftTypeBool, _antialias,
56                          0);
57   if (! _xftfont)
58     return; // failure
59
60   _valid = True;
61 }
62
63
64 BFont::~BFont(void) {
65   if (_xftfont)
66     XftFontClose(OBDisplay::display, _xftfont);
67 }
68
69
70 void BFont::drawString(Drawable d, int x, int y, const BColor &color,
71                        const string &string) const {
72   assert(_valid);
73
74   const ScreenInfo *info = OBDisplay::screenInfo(_screen_num);
75   XftDraw *draw = XftDrawCreate(OBDisplay::display, d,
76                                 info->getVisual(), info->getColormap());
77   assert(draw);
78
79   if (_shadow) {
80     XftColor c;
81     c.color.red = 0;
82     c.color.green = 0;
83     c.color.blue = 0;
84     c.color.alpha = _tint | _tint << 8; // transparent shadow
85     c.pixel = BlackPixel(OBDisplay::display, _screen_num);
86
87     XftDrawString8(draw, &c, _xftfont, x + _offset,
88                    _xftfont->ascent + y + _offset,
89                    (XftChar8 *) string.c_str(),
90                    string.size());
91   }
92     
93   XftColor c;
94   c.color.red = color.red() | color.red() << 8;
95   c.color.green = color.green() | color.green() << 8;
96   c.color.blue = color.blue() | color.blue() << 8;
97   c.pixel = color.pixel();
98   c.color.alpha = 0xff | 0xff << 8; // no transparency in BColor yet
99
100   XftDrawString8(draw, &c, _xftfont, x, _xftfont->ascent + y,
101                  (XftChar8 *) string.c_str(), string.size());
102
103   XftDrawDestroy(draw);
104   return;
105 }
106
107
108 unsigned int BFont::measureString(const string &string) const {
109   assert(_valid);
110
111   XGlyphInfo info;
112
113   XftTextExtents8(OBDisplay::display, _xftfont,
114                   (XftChar8 *) string.c_str(), string.size(), &info);
115
116   return info.xOff + (_shadow ? _offset : 0);
117 }
118
119
120 unsigned int BFont::height(void) const {
121   assert(_valid);
122
123   return _xftfont->height + (_shadow ? _offset : 0);
124 }
125
126
127 unsigned int BFont::maxCharWidth(void) const {
128   assert(_valid);
129
130   return _xftfont->max_advance_width;
131 }
132
133 }