]> Dogcows Code - chaz/openbox/blob - src/Font.cc
add option to the rc file to use/not use AA for Xft fonts
[chaz/openbox] / src / Font.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // Font.cc for Blackbox - an X11 Window manager
3 // Copyright (c) 2001 - 2002 Sean 'Shaleh' Perry <shaleh@debian.org>
4 // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a
7 // copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 // DEALINGS IN THE SOFTWARE.
23
24 #ifdef HAVE_CONFIG_H
25 # include "../config.h"
26 #endif // HAVE_CONFIG_H
27
28 extern "C" {
29 #ifdef HAVE_STDLIB_H
30 # include <stdlib.h>
31 #endif // HAVE_STDLIB_H
32 }
33
34 #include <iostream>
35 #include <algorithm>
36
37 using std::string;
38 using std::cerr;
39 using std::endl;
40
41 #include "i18n.hh"
42 #include "Font.hh"
43 #include "Util.hh"
44 #include "GCCache.hh"
45 #include "Color.hh"
46
47 string BFont::_fallback_font = "fixed";
48
49 #ifdef XFT
50 BFont::BFont(Display *d, BScreen *screen, const string &family, int size,
51 bool bold, bool italic, bool antialias) :
52 _display(d),
53 _screen(screen),
54 _family(family),
55 _simplename(False),
56 _size(size),
57 _bold(bold),
58 _italic(italic),
59 _antialias(antialias),
60 _xftfont(0),
61 _font(0),
62 _fontset(0),
63 _fontset_extents(0) {
64 _valid = False;
65
66 _xftfont = XftFontOpen(_display, _screen->getScreenNumber(),
67 XFT_FAMILY, XftTypeString, _family.c_str(),
68 XFT_SIZE, XftTypeInteger, _size,
69 XFT_WEIGHT, XftTypeInteger, (_bold ?
70 XFT_WEIGHT_BOLD :
71 XFT_WEIGHT_MEDIUM),
72 XFT_SLANT, XftTypeInteger, (_italic ?
73 XFT_SLANT_ITALIC :
74 XFT_SLANT_ROMAN),
75 XFT_ANTIALIAS, XftTypeBool, _antialias,
76 0);
77 if (! _xftfont)
78 return; // failure
79
80 _font = XLoadQueryFont(_display, buildXlfd().c_str());
81 if (! _font)
82 return; // failure
83
84 _valid = True;
85 }
86 #endif
87
88
89 BFont::BFont(Display *d, BScreen *screen, const string &xlfd) :
90 _display(d),
91 _screen(screen),
92 #ifdef XFT
93 _antialias(False),
94 _xftfont(0),
95 #endif // XFT
96 _font(0),
97 _fontset(0),
98 _fontset_extents(0) {
99 string int_xlfd;
100 if (xlfd.empty())
101 int_xlfd = _fallback_font;
102 else
103 int_xlfd = xlfd;
104
105 if ((_valid = createXFont(int_xlfd)))
106 return; // success
107
108 if (int_xlfd != _fallback_font) {
109 // try the fallback
110 cerr << "BFont::BFont(): couldn't load font '" << _family << "'" << endl <<
111 "Falling back to default '" << _fallback_font << "'" << endl;
112
113 if ((_valid = createXFont(_fallback_font)))
114 return; // success
115 }
116
117 cerr << "BFont::BFont(): couldn't load font '" << _family << "'" << endl <<
118 "Giving up!" << endl;
119 return; // failure
120 }
121
122
123 bool BFont::createXFont(const std::string &xlfd) {
124 /*
125 Even though this is only used for font sets (multibyte), it is still parsed
126 out so that the bold/italic/etc information is still available from the
127 class when using non-multibyte.
128
129 This is where _simplename, _bold, _italic, and _size are initialized, since
130 they are not initialized in the constructor. This needs to occur before
131 calling any Xlfd-building functions.
132 */
133 if (! parseXlfd(xlfd))
134 return False;
135
136 if (i18n.multibyte()) {
137 char **missing, *def = "-";
138 int nmissing;
139
140 _fontset = XCreateFontSet(_display, buildMultibyteXlfd().c_str(),
141 &missing, &nmissing, &def);
142 if (nmissing) XFreeStringList(missing);
143 if (_fontset)
144 _fontset_extents = XExtentsOfFontSet(_fontset);
145 else
146 return False;
147
148 assert(_fontset_extents);
149 }
150
151 _font = XLoadQueryFont(_display, xlfd.c_str());
152 if (! _font)
153 return False;
154 return True;
155 }
156
157
158 BFont::~BFont(void) {
159 #ifdef XFT
160 if (_xftfont)
161 XftFontClose(_display, _xftfont);
162 #endif // XFT
163
164 if (i18n.multibyte() && _fontset)
165 XFreeFontSet(_display, _fontset);
166 if (_font)
167 XFreeFont(_display, _font);
168 }
169
170
171 /*
172 * Takes _family, _size, _bold, _italic, etc and builds them into a full XLFD.
173 */
174 string BFont::buildXlfd(void) const {
175 if (_simplename)
176 return _family;
177
178 string weight = _bold ? "bold" : "medium";
179 string slant = _italic ? "i" : "r";
180 string sizestr= _size ? itostring(_size * 10) : "*";
181
182 return "-*-" + _family + "-" + weight + "-" + slant + "-*-*-*-" + sizestr +
183 "-*-*-*-*-*-*";
184 }
185
186
187 /*
188 * Takes _family, _size, _bold, _italic, etc and builds them into a full XLFD.
189 */
190 string BFont::buildMultibyteXlfd(void) const {
191 string weight = _bold ? "bold" : "medium";
192 string slant = _italic ? "i" : "r";
193 string sizestr= _size ? itostring(_size) : "*";
194
195 return _family + ','
196 + "-*-*-" + weight + "-" + slant + "-*-*-*-" + sizestr +
197 "-*-*-*-*-*-*" + ','
198 + "-*-*-*-*-*-*-*-" + sizestr + "-*-*-*-*-*-*" + ',' +
199 + "*";
200 }
201
202
203 /*
204 * Takes a full X font name and parses it out so we know if we're bold, our
205 * size, etc.
206 */
207 bool BFont::parseXlfd(const string &xlfd) {
208 if (xlfd.empty() || xlfd[0] != '-') {
209 _family = xlfd;
210 _simplename = True;
211 _bold = False;
212 _italic = False;
213 _size = 0;
214 } else {
215 _simplename = False;
216 string weight,
217 slant,
218 sizestr;
219 int i = 0;
220
221 string::const_iterator it = xlfd.begin(), end = xlfd.end();
222 while(1) {
223 string::const_iterator tmp = it; // current string.begin()
224 it = std::find(tmp, end, '-'); // look for comma between tmp and end
225 if (i == 2) _family = string(tmp, it); // s[tmp:it]
226 if (i == 3) weight = string(tmp, it);
227 if (i == 4) slant = string(tmp, it);
228 if (i == 7 && string(tmp, it) != "*") sizestr = string(tmp, it);
229 if (sizestr.empty() &&
230 i == 8 && string(tmp, it) != "*") sizestr = string(tmp, it);
231 if (it == end || i >= 8)
232 break;
233 ++it;
234 ++i;
235 }
236 if (i < 3) // no name even! can't parse that
237 return False;
238 _bold = weight == "bold" || weight == "demibold";
239 _italic = slant == "i" || slant == "o";
240 _size = atoi(sizestr.c_str()) / 10;
241 }
242
243 // min/max size restrictions for sanity, but 0 is the font's "default size"
244 if (_size && _size < 3)
245 _size = 3;
246 else if (_size > 97)
247 _size = 97;
248
249 return True;
250 }
251
252
253 void BFont::drawString(Drawable d, int x, int y, const BColor &color,
254 const string &string) const {
255 assert(_valid);
256
257 #ifdef XFT
258 if (_xftfont) {
259 XftDraw *draw = XftDrawCreate(_display, d, _screen->getVisual(),
260 _screen->getColormap());
261 assert(draw);
262
263 XftColor c;
264 c.color.red = color.red() | color.red() << 8;
265 c.color.green = color.green() | color.green() << 8;
266 c.color.blue = color.blue() | color.blue() << 8;
267 c.color.alpha = 0xff | 0xff << 8; // no transparency in BColor yet
268 c.pixel = color.pixel();
269
270 XftDrawStringUtf8(draw, &c, _xftfont, x, _xftfont->ascent + y,
271 (XftChar8 *) string.c_str(), string.size());
272
273 XftDrawDestroy(draw);
274 return;
275 }
276 #endif // XFT
277
278 BGCCache *_cache = color.display()->gcCache();
279 BGCCacheItem *_item = _cache->find(color, _font, GXcopy, ClipByChildren);
280
281 assert(_cache);
282 assert(_item);
283
284 if (i18n.multibyte())
285 XmbDrawString(_display, d, _fontset, _item->gc(),
286 x, y - _fontset_extents->max_ink_extent.y,
287 string.c_str(), string.size());
288 else
289 XDrawString(_display, d, _item->gc(),
290 x, _font->ascent + y,
291 string.c_str(), string.size());
292
293 _cache->release(_item);
294 }
295
296
297 unsigned int BFont::measureString(const string &string) const {
298 assert(_valid);
299
300 #ifdef XFT
301 if (_xftfont) {
302 XGlyphInfo info;
303 XftTextExtentsUtf8(_display, _xftfont, (XftChar8 *) string.c_str(),
304 string.size(), &info);
305 return info.xOff;
306 }
307 #endif // XFT
308
309 if (i18n.multibyte()) {
310 XRectangle ink, logical;
311 XmbTextExtents(_fontset, string.c_str(), string.size(), &ink, &logical);
312 return logical.width;
313 } else {
314 return XTextWidth(_font, string.c_str(), string.size());
315 }
316 }
317
318
319 unsigned int BFont::height(void) const {
320 assert(_valid);
321
322 #ifdef XFT
323 if (_xftfont)
324 return _xftfont->height;
325 #endif // XFT
326
327 if (i18n.multibyte())
328 return _fontset_extents->max_ink_extent.height;
329 else
330 return _font->ascent + _font->descent;
331 }
332
333
334 unsigned int BFont::maxCharWidth(void) const {
335 assert(_valid);
336
337 #ifdef XFT
338 if (_xftfont)
339 return _xftfont->max_advance_width;
340 #endif // XFT
341
342 if (i18n.multibyte())
343 return _fontset_extents->max_logical_extent.width;
344 else
345 return _font->max_bounds.rbearing - _font->min_bounds.lbearing;
346 }
This page took 0.054411 seconds and 5 git commands to generate.