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)
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:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
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.
25 # include "../config.h"
26 #endif // HAVE_CONFIG_H
31 #endif // HAVE_STDLIB_H
47 bool BFont::_antialias = True;
48 string BFont::_fallback_font = "fixed";
52 BFont::BFont(Display *d, BScreen *screen, const string &family, int size,
53 bool bold, bool italic) : _display(d),
66 _xftfont = XftFontOpen(_display, _screen->getScreenNumber(),
67 XFT_FAMILY, XftTypeString, _family.c_str(),
68 XFT_SIZE, XftTypeInteger, _size,
69 XFT_WEIGHT, XftTypeInteger, (_bold ?
72 XFT_SLANT, XftTypeInteger, (_italic ?
75 XFT_ANTIALIAS, XftTypeBool, _antialias,
80 _font = XLoadQueryFont(_display, buildXlfd().c_str());
89 BFont::BFont(Display *d, BScreen *screen, const string &xlfd) :
100 int_xlfd = _fallback_font;
104 if ((_valid = createXFont(int_xlfd)))
107 if (int_xlfd != _fallback_font) {
109 cerr << "BFont::BFont(): couldn't load font '" << _family << "'" << endl <<
110 "Falling back to default '" << _fallback_font << "'" << endl;
112 if ((_valid = createXFont(_fallback_font)))
116 cerr << "BFont::BFont(): couldn't load font '" << _family << "'" << endl <<
117 "Giving up!" << endl;
122 bool BFont::createXFont(const std::string &xlfd) {
124 Even though this is only used for font sets (multibyte), it is still parsed
125 out so that the bold/italic/etc information is still available from the
126 class when using non-multibyte.
128 This is where _simplename, _bold, _italic, and _size are initialized, since
129 they are not initialized in the constructor. This needs to occur before
130 calling any Xlfd-building functions.
132 if (! parseXlfd(xlfd))
135 if (i18n.multibyte()) {
136 char **missing, *def = "-";
139 _fontset = XCreateFontSet(_display, buildMultibyteXlfd().c_str(),
140 &missing, &nmissing, &def);
141 if (nmissing) XFreeStringList(missing);
143 _fontset_extents = XExtentsOfFontSet(_fontset);
147 assert(_fontset_extents);
150 _font = XLoadQueryFont(_display, xlfd.c_str());
157 BFont::~BFont(void) {
160 XftFontClose(_display, _xftfont);
163 if (i18n.multibyte() && _fontset)
164 XFreeFontSet(_display, _fontset);
166 XFreeFont(_display, _font);
171 * Takes _family, _size, _bold, _italic, etc and builds them into a full XLFD.
173 string BFont::buildXlfd(void) const {
177 string weight = _bold ? "bold" : "medium";
178 string slant = _italic ? "i" : "r";
179 string sizestr= _size ? itostring(_size * 10) : "*";
181 return "-*-" + _family + "-" + weight + "-" + slant + "-*-*-*-" + sizestr +
187 * Takes _family, _size, _bold, _italic, etc and builds them into a full XLFD.
189 string BFont::buildMultibyteXlfd(void) const {
190 string weight = _bold ? "bold" : "medium";
191 string slant = _italic ? "i" : "r";
192 string sizestr= _size ? itostring(_size) : "*";
195 + "-*-*-" + weight + "-" + slant + "-*-*-*-" + sizestr +
197 + "-*-*-*-*-*-*-*-" + sizestr + "-*-*-*-*-*-*" + ',' +
203 * Takes a full X font name and parses it out so we know if we're bold, our
206 bool BFont::parseXlfd(const string &xlfd) {
207 if (xlfd.empty() || xlfd[0] != '-') {
220 string::const_iterator it = xlfd.begin(), end = xlfd.end();
222 string::const_iterator tmp = it; // current string.begin()
223 it = std::find(tmp, end, '-'); // look for comma between tmp and end
224 if (i == 2) _family = string(tmp, it); // s[tmp:it]
225 if (i == 3) weight = string(tmp, it);
226 if (i == 4) slant = string(tmp, it);
227 if (i == 7 && string(tmp, it) != "*") sizestr = string(tmp, it);
228 if (sizestr.empty() &&
229 i == 8 && string(tmp, it) != "*") sizestr = string(tmp, it);
230 if (it == end || i >= 8)
235 if (i < 3) // no name even! can't parse that
237 _bold = weight == "bold" || weight == "demibold";
238 _italic = slant == "i" || slant == "o";
239 _size = atoi(sizestr.c_str()) / 10;
242 // min/max size restrictions for sanity, but 0 is the font's "default size"
243 if (_size && _size < 3)
252 void BFont::drawString(Drawable d, int x, int y, const BColor &color,
253 const string &string) const {
258 XftDraw *draw = XftDrawCreate(_display, d, _screen->getVisual(),
259 _screen->getColormap());
263 c.color.red = color.red() | color.red() << 8;
264 c.color.green = color.green() | color.green() << 8;
265 c.color.blue = color.blue() | color.blue() << 8;
266 c.color.alpha = 0xff | 0xff << 8; // no transparency in BColor yet
267 c.pixel = color.pixel();
269 XftDrawStringUtf8(draw, &c, _xftfont, x, _xftfont->ascent + y,
270 (XftChar8 *) string.c_str(), string.size());
272 XftDrawDestroy(draw);
277 BGCCache *_cache = color.display()->gcCache();
278 BGCCacheItem *_item = _cache->find(color, _font, GXcopy, ClipByChildren);
283 if (i18n.multibyte())
284 XmbDrawString(_display, d, _fontset, _item->gc(),
285 x, y - _fontset_extents->max_ink_extent.y,
286 string.c_str(), string.size());
288 XDrawString(_display, d, _item->gc(),
289 x, _font->ascent + y,
290 string.c_str(), string.size());
292 _cache->release(_item);
296 unsigned int BFont::measureString(const string &string) const {
302 XftTextExtentsUtf8(_display, _xftfont, (XftChar8 *) string.c_str(),
303 string.size(), &info);
308 if (i18n.multibyte()) {
309 XRectangle ink, logical;
310 XmbTextExtents(_fontset, string.c_str(), string.size(), &ink, &logical);
311 return logical.width;
313 return XTextWidth(_font, string.c_str(), string.size());
318 unsigned int BFont::height(void) const {
323 return _xftfont->height;
326 if (i18n.multibyte())
327 return _fontset_extents->max_ink_extent.height;
329 return _font->ascent + _font->descent;
333 unsigned int BFont::maxCharWidth(void) const {
338 return _xftfont->max_advance_width;
341 if (i18n.multibyte())
342 return _fontset_extents->max_logical_extent.width;
344 return _font->max_bounds.rbearing - _font->min_bounds.lbearing;