]> icculus.org git repositories - mikachu/openbox.git/blob - src/font.cc
compiles now. uses xft2
[mikachu/openbox.git] / src / font.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // Font.cc for Blackbox - an X11 Window manager
3 // Copyright (c) 2001 - 2002 Sean 'Shaleh' Perry <shaleh@debian.org>
4 // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a
7 // copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 // DEALINGS IN THE SOFTWARE.
23
24 #ifdef    HAVE_CONFIG_H
25 #  include "../config.h"
26 #endif // HAVE_CONFIG_H
27
28 extern "C" {
29 #ifdef HAVE_STDLIB_H
30 #  include <stdlib.h>
31 #endif // HAVE_STDLIB_H
32 }
33
34 #include <iostream>
35 #include <algorithm>
36
37 using std::string;
38 using std::cerr;
39 using std::endl;
40
41 #include "font.hh"
42 #include "util.hh"
43 #include "gccache.hh"
44 #include "color.hh"
45
46 string      BFont::_fallback_font   = "fixed";
47
48 BFont::BFont(Display *d, BScreen *screen, const string &family, int size,
49              bool bold, bool italic, bool shadow, unsigned char offset, 
50              unsigned char tint, bool antialias) :
51                                           _display(d),
52                                           _screen(screen),
53                                           _family(family),
54                                           _simplename(False),
55                                           _size(size),
56                                           _bold(bold),
57                                           _italic(italic),
58                                           _antialias(antialias),
59                                           _shadow(shadow),
60                                           _offset(offset),
61                                           _tint(tint),
62                                           _xftfont(0) {
63   _valid = False;
64
65   _xftfont = XftFontOpen(_display, _screen->getScreenNumber(),
66                          XFT_FAMILY, XftTypeString,  _family.c_str(),
67                          XFT_SIZE,   XftTypeInteger, _size,
68                          XFT_WEIGHT, XftTypeInteger, (_bold ?
69                                                       XFT_WEIGHT_BOLD :
70                                                       XFT_WEIGHT_MEDIUM),
71                          XFT_SLANT,  XftTypeInteger, (_italic ?
72                                                       XFT_SLANT_ITALIC :
73                                                       XFT_SLANT_ROMAN),
74                          XFT_ANTIALIAS, XftTypeBool, _antialias,
75                          0);
76   if (! _xftfont)
77     return; // failure
78
79   _valid = True;
80 }
81
82
83 BFont::~BFont(void) {
84   if (_xftfont)
85     XftFontClose(_display, _xftfont);
86 }
87
88
89 void BFont::drawString(Drawable d, int x, int y, const BColor &color,
90                        const string &string) const {
91   assert(_valid);
92
93   XftDraw *draw = XftDrawCreate(_display, d, _screen->getVisual(),
94                                 _screen->getColormap());
95   assert(draw);
96
97   if (_shadow) {
98     XftColor c;
99     c.color.red = 0;
100     c.color.green = 0;
101     c.color.blue = 0;
102     c.color.alpha = _tint | _tint << 8; // transparent shadow
103     c.pixel = BlackPixel(_display, _screen->getScreenNumber());
104
105     XftDrawStringUtf8(draw, &c, _xftfont, x + _offset,
106                       _xftfont->ascent + y + _offset,
107                       (XftChar8 *) string.c_str(),
108                       string.size());
109   }
110     
111   XftColor c;
112   c.color.red = color.red() | color.red() << 8;
113   c.color.green = color.green() | color.green() << 8;
114   c.color.blue = color.blue() | color.blue() << 8;
115   c.pixel = color.pixel();
116   c.color.alpha = 0xff | 0xff << 8; // no transparency in BColor yet
117
118   XftDrawStringUtf8(draw, &c, _xftfont, x, _xftfont->ascent + y,
119                     (XftChar8 *) string.c_str(), string.size());
120
121   XftDrawDestroy(draw);
122   return;
123 }
124
125
126 unsigned int BFont::measureString(const string &string) const {
127   assert(_valid);
128
129   XGlyphInfo info;
130
131   XftTextExtentsUtf8(_display, _xftfont, (XftChar8 *) string.c_str(),
132                      string.size(), &info);
133
134   return info.xOff + (_shadow ? _offset : 0);
135 }
136
137
138 unsigned int BFont::height(void) const {
139   assert(_valid);
140
141   return _xftfont->height + (_shadow ? _offset : 0);
142 }
143
144
145 unsigned int BFont::maxCharWidth(void) const {
146   assert(_valid);
147
148   return _xftfont->max_advance_width;
149 }