]> Dogcows Code - chaz/openbox/blob - otk/font.cc
display the build version as well as the runtime version of Xft
[chaz/openbox] / 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 build = XFT_VERSION;
54 int version = XftGetVersion();
55 printf(_("Using Xft %d.%d.%d (Built against %d.%d.%d).\n"),
56 version / 10000 % 100, version / 100 % 100, version % 100,
57 build / 10000 % 100, build / 100 % 100, build % 100);
58 _xft_init = true;
59 }
60
61 if ((_xftfont = XftFontOpenName(**display, _screen_num,
62 _fontstring.c_str())))
63 return;
64
65 printf(_("Unable to load font: %s\n"), _fontstring.c_str());
66 printf(_("Trying fallback font: %s\n"), _fallback_font.c_str());
67
68 if ((_xftfont = XftFontOpenName(**display, _screen_num,
69 _fallback_font.c_str())))
70 return;
71
72 printf(_("Unable to load font: %s\n"), _fallback_font.c_str());
73 printf(_("Aborting!.\n"));
74
75 ::exit(3); // can't continue without a font
76 }
77
78
79 Font::~Font(void)
80 {
81 if (_xftfont)
82 XftFontClose(**display, _xftfont);
83 }
84
85
86 void Font::drawString(XftDraw *d, int x, int y, const Color &color,
87 const ustring &string) const
88 {
89 assert(d);
90
91 if (_shadow) {
92 XftColor c;
93 c.color.red = 0;
94 c.color.green = 0;
95 c.color.blue = 0;
96 c.color.alpha = _tint | _tint << 8; // transparent shadow
97 c.pixel = BlackPixel(**display, _screen_num);
98
99 if (string.utf8())
100 XftDrawStringUtf8(d, &c, _xftfont, x + _offset,
101 _xftfont->ascent + y + _offset,
102 (FcChar8*)string.c_str(), string.bytes());
103 else
104 XftDrawString8(d, &c, _xftfont, x + _offset,
105 _xftfont->ascent + y + _offset,
106 (FcChar8*)string.c_str(), string.bytes());
107 }
108
109 XftColor c;
110 c.color.red = color.red() | color.red() << 8;
111 c.color.green = color.green() | color.green() << 8;
112 c.color.blue = color.blue() | color.blue() << 8;
113 c.pixel = color.pixel();
114 c.color.alpha = 0xff | 0xff << 8; // no transparency in Color yet
115
116 if (string.utf8())
117 XftDrawStringUtf8(d, &c, _xftfont, x, _xftfont->ascent + y,
118 (FcChar8*)string.c_str(), string.bytes());
119 else
120 XftDrawString8(d, &c, _xftfont, x, _xftfont->ascent + y,
121 (FcChar8*)string.c_str(), string.bytes());
122
123 return;
124 }
125
126
127 unsigned int Font::measureString(const ustring &string) const
128 {
129 XGlyphInfo info;
130
131 if (string.utf8())
132 XftTextExtentsUtf8(**display, _xftfont,
133 (FcChar8*)string.c_str(), string.bytes(), &info);
134 else
135 XftTextExtents8(**display, _xftfont,
136 (FcChar8*)string.c_str(), string.bytes(), &info);
137
138 return info.xOff + (_shadow ? _offset : 0);
139 }
140
141
142 unsigned int Font::height(void) const
143 {
144 return _xftfont->height + (_shadow ? _offset : 0);
145 }
146
147
148 unsigned int Font::maxCharWidth(void) const
149 {
150 return _xftfont->max_advance_width;
151 }
152
153 }
This page took 0.046431 seconds and 5 git commands to generate.