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 string BFont::_fallback_font = "fixed";
50 BFont::BFont(Display *d, BScreen *screen, const string &family, int size,
51 bool bold, bool italic, bool antialias) :
59 _antialias(antialias),
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) :
101 int_xlfd = _fallback_font;
105 if ((_valid = createXFont(int_xlfd)))
108 if (int_xlfd != _fallback_font) {
110 cerr << "BFont::BFont(): couldn't load font '" << _family << "'" << endl <<
111 "Falling back to default '" << _fallback_font << "'" << endl;
113 if ((_valid = createXFont(_fallback_font)))
117 cerr << "BFont::BFont(): couldn't load font '" << _family << "'" << endl <<
118 "Giving up!" << endl;
123 bool BFont::createXFont(const std::string &xlfd) {
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.
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.
133 if (! parseXlfd(xlfd))
136 if (i18n.multibyte()) {
137 char **missing, *def = "-";
140 _fontset = XCreateFontSet(_display, buildMultibyteXlfd().c_str(),
141 &missing, &nmissing, &def);
142 if (nmissing) XFreeStringList(missing);
144 _fontset_extents = XExtentsOfFontSet(_fontset);
148 assert(_fontset_extents);
151 _font = XLoadQueryFont(_display, xlfd.c_str());
158 BFont::~BFont(void) {
161 XftFontClose(_display, _xftfont);
164 if (i18n.multibyte() && _fontset)
165 XFreeFontSet(_display, _fontset);
167 XFreeFont(_display, _font);
172 * Takes _family, _size, _bold, _italic, etc and builds them into a full XLFD.
174 string BFont::buildXlfd(void) const {
178 string weight = _bold ? "bold" : "medium";
179 string slant = _italic ? "i" : "r";
180 string sizestr= _size ? itostring(_size * 10) : "*";
182 return "-*-" + _family + "-" + weight + "-" + slant + "-*-*-*-" + sizestr +
188 * Takes _family, _size, _bold, _italic, etc and builds them into a full XLFD.
190 string BFont::buildMultibyteXlfd(void) const {
191 string weight = _bold ? "bold" : "medium";
192 string slant = _italic ? "i" : "r";
193 string sizestr= _size ? itostring(_size) : "*";
196 + "-*-*-" + weight + "-" + slant + "-*-*-*-" + sizestr +
198 + "-*-*-*-*-*-*-*-" + sizestr + "-*-*-*-*-*-*" + ',' +
204 * Takes a full X font name and parses it out so we know if we're bold, our
207 bool BFont::parseXlfd(const string &xlfd) {
208 if (xlfd.empty() || xlfd[0] != '-') {
221 string::const_iterator it = xlfd.begin(), end = xlfd.end();
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)
236 if (i < 3) // no name even! can't parse that
238 _bold = weight == "bold" || weight == "demibold";
239 _italic = slant == "i" || slant == "o";
240 _size = atoi(sizestr.c_str()) / 10;
243 // min/max size restrictions for sanity, but 0 is the font's "default size"
244 if (_size && _size < 3)
253 void BFont::drawString(Drawable d, int x, int y, const BColor &color,
254 const string &string) const {
259 XftDraw *draw = XftDrawCreate(_display, d, _screen->getVisual(),
260 _screen->getColormap());
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();
270 XftDrawStringUtf8(draw, &c, _xftfont, x, _xftfont->ascent + y,
271 (XftChar8 *) string.c_str(), string.size());
273 XftDrawDestroy(draw);
278 BPen pen(color, _font);
280 if (i18n.multibyte())
281 XmbDrawString(_display, d, _fontset, pen.gc(),
282 x, y - _fontset_extents->max_ink_extent.y,
283 string.c_str(), string.size());
285 XDrawString(_display, d, pen.gc(),
286 x, _font->ascent + y,
287 string.c_str(), string.size());
291 unsigned int BFont::measureString(const string &string) const {
297 XftTextExtentsUtf8(_display, _xftfont, (XftChar8 *) string.c_str(),
298 string.size(), &info);
303 if (i18n.multibyte()) {
304 XRectangle ink, logical;
305 XmbTextExtents(_fontset, string.c_str(), string.size(), &ink, &logical);
306 return logical.width;
308 return XTextWidth(_font, string.c_str(), string.size());
313 unsigned int BFont::height(void) const {
318 return _xftfont->height;
321 if (i18n.multibyte())
322 return _fontset_extents->max_ink_extent.height;
324 return _font->ascent + _font->descent;
328 unsigned int BFont::maxCharWidth(void) const {
333 return _xftfont->max_advance_width;
336 if (i18n.multibyte())
337 return _fontset_extents->max_logical_extent.width;
339 return _font->max_bounds.rbearing - _font->min_bounds.lbearing;