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