]> icculus.org git repositories - mikachu/openbox.git/blob - otk/font.cc
dont use append, dont need to extend ustring!
[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 #include "font.hh"
17 #include "util.hh"
18 #include "display.hh"
19 #include "color.hh"
20 #include "screeninfo.hh"
21
22 extern "C" {
23 #ifdef    HAVE_STDIO_H
24 #  include <stdio.h>
25 #endif // HAVE_STDIO_H
26
27 #include "gettext.h"
28 #define _(str) gettext(str)
29 }
30
31 namespace otk {
32
33 std::string Font::_fallback_font = "fixed";
34 bool        Font::_xft_init      = false;
35
36 Font::Font(int screen_num, const std::string &fontstring,
37              bool shadow, unsigned char offset, unsigned char tint)
38   : _screen_num(screen_num),
39     _fontstring(fontstring),
40     _shadow(shadow),
41     _offset(offset),
42     _tint(tint),
43     _xftfont(0)
44 {
45   assert(screen_num >= 0);
46   assert(tint <= CHAR_MAX);
47   
48   if (!_xft_init) {
49     if (!XftInit(0)) {
50       printf(_("Couldn't initialize Xft.\n\n"));
51       ::exit(3);
52     }
53     int version = XftGetVersion();
54     printf(_("Using Xft %d.%d.%d.\n"),
55            version / 10000 % 100, version / 100 % 100, version % 100);
56     _xft_init = true;
57   }
58
59   if ((_xftfont = XftFontOpenName(Display::display, _screen_num,
60                                   _fontstring.c_str())))
61     return;
62
63   printf(_("Unable to load font: %s\n"), _fontstring.c_str());
64   printf(_("Trying fallback font: %s\n"), _fallback_font.c_str());
65
66   if ((_xftfont = XftFontOpenName(Display::display, _screen_num,
67                                   _fallback_font.c_str())))
68     return;
69
70   printf(_("Unable to load font: %s\n"), _fallback_font.c_str());
71   printf(_("Aborting!.\n"));
72
73   ::exit(3); // can't continue without a font
74 }
75
76
77 Font::~Font(void)
78 {
79   if (_xftfont)
80     XftFontClose(Display::display, _xftfont);
81 }
82
83
84 void Font::drawString(XftDraw *d, int x, int y, const Color &color,
85                        const ustring &string) const
86 {
87   assert(d);
88
89   if (_shadow) {
90     XftColor c;
91     c.color.red = 0;
92     c.color.green = 0;
93     c.color.blue = 0;
94     c.color.alpha = _tint | _tint << 8; // transparent shadow
95     c.pixel = BlackPixel(Display::display, _screen_num);
96
97     if (string.utf8())
98       XftDrawStringUtf8(d, &c, _xftfont, x + _offset,
99                         _xftfont->ascent + y + _offset,
100                         (FcChar8*)string.c_str(), string.size());
101     else
102       XftDrawString8(d, &c, _xftfont, x + _offset,
103                      _xftfont->ascent + y + _offset,
104                      (FcChar8*)string.c_str(), string.size());
105   }
106     
107   XftColor c;
108   c.color.red = color.red() | color.red() << 8;
109   c.color.green = color.green() | color.green() << 8;
110   c.color.blue = color.blue() | color.blue() << 8;
111   c.pixel = color.pixel();
112   c.color.alpha = 0xff | 0xff << 8; // no transparency in Color yet
113
114   if (string.utf8())
115     XftDrawStringUtf8(d, &c, _xftfont, x, _xftfont->ascent + y,
116                       (FcChar8*)string.c_str(), string.size());
117   else
118     XftDrawString8(d, &c, _xftfont, x, _xftfont->ascent + y,
119                    (FcChar8*)string.c_str(), string.size());
120
121   return;
122 }
123
124
125 unsigned int Font::measureString(const ustring &string) const
126 {
127   XGlyphInfo info;
128
129   if (string.utf8())
130     XftTextExtentsUtf8(Display::display, _xftfont,
131                        (FcChar8*)string.c_str(), string.size(), &info);
132   else
133     XftTextExtents8(Display::display, _xftfont,
134                     (FcChar8*)string.c_str(), string.size(), &info);
135
136   return info.xOff + (_shadow ? _offset : 0);
137 }
138
139
140 unsigned int Font::height(void) const
141 {
142   return _xftfont->height + (_shadow ? _offset : 0);
143 }
144
145
146 unsigned int Font::maxCharWidth(void) const
147 {
148   return _xftfont->max_advance_width;
149 }
150
151 }