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