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