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 shadow, unsigned char offset,
52 int tint, bool antialias) :
60 _antialias(antialias),
70 _xftfont = XftFontOpen(_display, _screen->getScreenNumber(),
71 XFT_FAMILY, XftTypeString, _family.c_str(),
72 XFT_SIZE, XftTypeInteger, _size,
73 XFT_WEIGHT, XftTypeInteger, (_bold ?
76 XFT_SLANT, XftTypeInteger, (_italic ?
79 XFT_ANTIALIAS, XftTypeBool, _antialias,
84 _font = XLoadQueryFont(_display, buildXlfd().c_str());
93 BFont::BFont(Display *d, BScreen *screen, const string &xlfd) :
103 _fontset_extents(0) {
106 int_xlfd = _fallback_font;
110 if ((_valid = createXFont(int_xlfd)))
113 if (int_xlfd != _fallback_font) {
115 cerr << "BFont::BFont(): couldn't load font '" << _family << "'" << endl <<
116 "Falling back to default '" << _fallback_font << "'" << endl;
118 if ((_valid = createXFont(_fallback_font)))
122 cerr << "BFont::BFont(): couldn't load font '" << _family << "'" << endl <<
123 "Giving up!" << endl;
128 bool BFont::createXFont(const std::string &xlfd) {
130 Even though this is only used for font sets (multibyte), it is still parsed
131 out so that the bold/italic/etc information is still available from the
132 class when using non-multibyte.
134 This is where _simplename, _bold, _italic, and _size are initialized, since
135 they are not initialized in the constructor. This needs to occur before
136 calling any Xlfd-building functions.
138 if (! parseXlfd(xlfd))
141 if (i18n.multibyte()) {
142 char **missing, *def = "-";
145 _fontset = XCreateFontSet(_display, buildMultibyteXlfd().c_str(),
146 &missing, &nmissing, &def);
147 if (nmissing) XFreeStringList(missing);
149 _fontset_extents = XExtentsOfFontSet(_fontset);
153 assert(_fontset_extents);
156 _font = XLoadQueryFont(_display, xlfd.c_str());
163 BFont::~BFont(void) {
166 XftFontClose(_display, _xftfont);
169 if (i18n.multibyte() && _fontset)
170 XFreeFontSet(_display, _fontset);
172 XFreeFont(_display, _font);
177 * Takes _family, _size, _bold, _italic, etc and builds them into a full XLFD.
179 string BFont::buildXlfd(void) const {
183 string weight = _bold ? "bold" : "medium";
184 string slant = _italic ? "i" : "r";
185 string sizestr= _size ? itostring(_size * 10) : "*";
187 return "-*-" + _family + "-" + weight + "-" + slant + "-*-*-*-" + sizestr +
193 * Takes _family, _size, _bold, _italic, etc and builds them into a full XLFD.
195 string BFont::buildMultibyteXlfd(void) const {
196 string weight = _bold ? "bold" : "medium";
197 string slant = _italic ? "i" : "r";
198 string sizestr= _size ? itostring(_size) : "*";
201 + "-*-*-" + weight + "-" + slant + "-*-*-*-" + sizestr +
203 + "-*-*-*-*-*-*-*-" + sizestr + "-*-*-*-*-*-*" + ',' +
209 * Takes a full X font name and parses it out so we know if we're bold, our
212 bool BFont::parseXlfd(const string &xlfd) {
213 if (xlfd.empty() || xlfd[0] != '-') {
226 string::const_iterator it = xlfd.begin(), end = xlfd.end();
228 string::const_iterator tmp = it; // current string.begin()
229 it = std::find(tmp, end, '-'); // look for comma between tmp and end
230 if (i == 2) _family = string(tmp, it); // s[tmp:it]
231 if (i == 3) weight = string(tmp, it);
232 if (i == 4) slant = string(tmp, it);
233 if (i == 7 && string(tmp, it) != "*") sizestr = string(tmp, it);
234 if (sizestr.empty() &&
235 i == 8 && string(tmp, it) != "*") sizestr = string(tmp, it);
236 if (it == end || i >= 8)
241 if (i < 3) // no name even! can't parse that
243 _bold = weight == "bold" || weight == "demibold";
244 _italic = slant == "i" || slant == "o";
245 _size = atoi(sizestr.c_str()) / 10;
248 // min/max size restrictions for sanity, but 0 is the font's "default size"
249 if (_size && _size < 3)
258 void BFont::drawString(Drawable d, int x, int y, const BColor &color,
259 const string &string) const {
264 XftDraw *draw = XftDrawCreate(_display, d, _screen->getVisual(),
265 _screen->getColormap());
275 c.color.alpha = 0xffff * _tint/100; // transparent shadow
276 c.pixel = BlackPixel(_display, _screen->getScreenNumber());
278 c.color.red = 0xffff * -_tint/100;
279 c.color.green = 0xffff * -_tint/100;
280 c.color.blue = 0xffff * -_tint/100;
281 c.color.alpha = 0xffff * -_tint/100;
282 c.pixel = WhitePixel(_display, _screen->getScreenNumber());
289 draw, &c, _xftfont, x + _offset,
290 _xftfont->ascent + y + _offset, (XftChar8 *) string.c_str(),
295 c.color.red = color.red() | color.red() << 8;
296 c.color.green = color.green() | color.green() << 8;
297 c.color.blue = color.blue() | color.blue() << 8;
298 c.pixel = color.pixel();
299 c.color.alpha = 0xff | 0xff << 8; // no transparency in BColor yet
306 draw, &c, _xftfont, x, _xftfont->ascent + y,
307 (XftChar8 *) string.c_str(), string.size());
309 XftDrawDestroy(draw);
314 BPen pen(color, _font);
316 if (i18n.multibyte())
317 XmbDrawString(_display, d, _fontset, pen.gc(),
318 x, y - _fontset_extents->max_ink_extent.y,
319 string.c_str(), string.size());
321 XDrawString(_display, d, pen.gc(),
322 x, _font->ascent + y,
323 string.c_str(), string.size());
327 unsigned int BFont::measureString(const string &string) const {
339 _display, _xftfont, (XftChar8 *) string.c_str(),
340 string.size(), &info);
342 return info.xOff + (_shadow ? _offset : 0);
346 if (i18n.multibyte()) {
347 XRectangle ink, logical;
348 XmbTextExtents(_fontset, string.c_str(), string.size(), &ink, &logical);
349 return logical.width;
351 return XTextWidth(_font, string.c_str(), string.size());
356 unsigned int BFont::height(void) const {
361 return _xftfont->height + (_shadow ? _offset : 0);
364 if (i18n.multibyte())
365 return _fontset_extents->max_ink_extent.height;
367 return _font->ascent + _font->descent;
371 unsigned int BFont::maxCharWidth(void) const {
376 return _xftfont->max_advance_width;
379 if (i18n.multibyte())
380 return _fontset_extents->max_logical_extent.width;
382 return _font->max_bounds.rbearing - _font->min_bounds.lbearing;