]>
Dogcows Code - chaz/openbox/blob - 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)
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
, bool antialias
) :
59 _antialias(antialias
),
67 _xftfont
= XftFontOpen(_display
, _screen
->getScreenNumber(),
68 XFT_FAMILY
, XftTypeString
, _family
.c_str(),
69 XFT_SIZE
, XftTypeInteger
, _size
,
70 XFT_WEIGHT
, XftTypeInteger
, (_bold
?
73 XFT_SLANT
, XftTypeInteger
, (_italic
?
76 XFT_ANTIALIAS
, XftTypeBool
, _antialias
,
81 _font
= XLoadQueryFont(_display
, buildXlfd().c_str());
90 BFont::BFont(Display
*d
, BScreen
*screen
, const string
&xlfd
) :
100 _fontset_extents(0) {
103 int_xlfd
= _fallback_font
;
107 if ((_valid
= createXFont(int_xlfd
)))
110 if (int_xlfd
!= _fallback_font
) {
112 cerr
<< "BFont::BFont(): couldn't load font '" << _family
<< "'" << endl
<<
113 "Falling back to default '" << _fallback_font
<< "'" << endl
;
115 if ((_valid
= createXFont(_fallback_font
)))
119 cerr
<< "BFont::BFont(): couldn't load font '" << _family
<< "'" << endl
<<
120 "Giving up!" << endl
;
125 bool BFont::createXFont(const std::string
&xlfd
) {
127 Even though this is only used for font sets (multibyte), it is still parsed
128 out so that the bold/italic/etc information is still available from the
129 class when using non-multibyte.
131 This is where _simplename, _bold, _italic, and _size are initialized, since
132 they are not initialized in the constructor. This needs to occur before
133 calling any Xlfd-building functions.
135 if (! parseXlfd(xlfd
))
138 if (i18n
.multibyte()) {
139 char **missing
, *def
= "-";
142 _fontset
= XCreateFontSet(_display
, buildMultibyteXlfd().c_str(),
143 &missing
, &nmissing
, &def
);
144 if (nmissing
) XFreeStringList(missing
);
146 _fontset_extents
= XExtentsOfFontSet(_fontset
);
150 assert(_fontset_extents
);
153 _font
= XLoadQueryFont(_display
, xlfd
.c_str());
160 BFont::~BFont(void) {
163 XftFontClose(_display
, _xftfont
);
166 if (i18n
.multibyte() && _fontset
)
167 XFreeFontSet(_display
, _fontset
);
169 XFreeFont(_display
, _font
);
174 * Takes _family, _size, _bold, _italic, etc and builds them into a full XLFD.
176 string
BFont::buildXlfd(void) const {
180 string weight
= _bold
? "bold" : "medium";
181 string slant
= _italic
? "i" : "r";
182 string sizestr
= _size
? itostring(_size
* 10) : "*";
184 return "-*-" + _family
+ "-" + weight
+ "-" + slant
+ "-*-*-*-" + sizestr
+
190 * Takes _family, _size, _bold, _italic, etc and builds them into a full XLFD.
192 string
BFont::buildMultibyteXlfd(void) const {
193 string weight
= _bold
? "bold" : "medium";
194 string slant
= _italic
? "i" : "r";
195 string sizestr
= _size
? itostring(_size
) : "*";
198 + "-*-*-" + weight
+ "-" + slant
+ "-*-*-*-" + sizestr
+
200 + "-*-*-*-*-*-*-*-" + sizestr
+ "-*-*-*-*-*-*" + ',' +
206 * Takes a full X font name and parses it out so we know if we're bold, our
209 bool BFont::parseXlfd(const string
&xlfd
) {
210 if (xlfd
.empty() || xlfd
[0] != '-') {
223 string::const_iterator it
= xlfd
.begin(), end
= xlfd
.end();
225 string::const_iterator tmp
= it
; // current string.begin()
226 it
= std::find(tmp
, end
, '-'); // look for comma between tmp and end
227 if (i
== 2) _family
= string(tmp
, it
); // s[tmp:it]
228 if (i
== 3) weight
= string(tmp
, it
);
229 if (i
== 4) slant
= string(tmp
, it
);
230 if (i
== 7 && string(tmp
, it
) != "*") sizestr
= string(tmp
, it
);
231 if (sizestr
.empty() &&
232 i
== 8 && string(tmp
, it
) != "*") sizestr
= string(tmp
, it
);
233 if (it
== end
|| i
>= 8)
238 if (i
< 3) // no name even! can't parse that
240 _bold
= weight
== "bold" || weight
== "demibold";
241 _italic
= slant
== "i" || slant
== "o";
242 _size
= atoi(sizestr
.c_str()) / 10;
245 // min/max size restrictions for sanity, but 0 is the font's "default size"
246 if (_size
&& _size
< 3)
255 void BFont::drawString(Drawable d
, int x
, int y
, const BColor
&color
,
256 const string
&string
) const {
261 XftDraw
*draw
= XftDrawCreate(_display
, d
, _screen
->getVisual(),
262 _screen
->getColormap());
270 c
.color
.alpha
= 0x40 | 0x40 << 8; // transparent shadow
271 c
.pixel
= BlackPixel(_display
, _screen
->getScreenNumber());
278 draw
, &c
, _xftfont
, x
+ 1, _xftfont
->ascent
+ y
+ 1,
279 (XftChar8
*) string
.c_str(), string
.size());
283 c
.color
.red
= color
.red() | color
.red() << 8;
284 c
.color
.green
= color
.green() | color
.green() << 8;
285 c
.color
.blue
= color
.blue() | color
.blue() << 8;
286 c
.pixel
= color
.pixel();
287 c
.color
.alpha
= 0xff | 0xff << 8; // no transparency in BColor yet
294 draw
, &c
, _xftfont
, x
, _xftfont
->ascent
+ y
,
295 (XftChar8
*) string
.c_str(), string
.size());
297 XftDrawDestroy(draw
);
302 BPen
pen(color
, _font
);
304 if (i18n
.multibyte())
305 XmbDrawString(_display
, d
, _fontset
, pen
.gc(),
306 x
, y
- _fontset_extents
->max_ink_extent
.y
,
307 string
.c_str(), string
.size());
309 XDrawString(_display
, d
, pen
.gc(),
310 x
, _font
->ascent
+ y
,
311 string
.c_str(), string
.size());
315 unsigned int BFont::measureString(const string
&string
) const {
327 _display
, _xftfont
, (XftChar8
*) string
.c_str(),
328 string
.size(), &info
);
330 return info
.xOff
+ (_shadow
? 1 : 0);
334 if (i18n
.multibyte()) {
335 XRectangle ink
, logical
;
336 XmbTextExtents(_fontset
, string
.c_str(), string
.size(), &ink
, &logical
);
337 return logical
.width
;
339 return XTextWidth(_font
, string
.c_str(), string
.size());
344 unsigned int BFont::height(void) const {
349 return _xftfont
->height
+ (_shadow
? 1 : 0);
352 if (i18n
.multibyte())
353 return _fontset_extents
->max_ink_extent
.height
;
355 return _font
->ascent
+ _font
->descent
;
359 unsigned int BFont::maxCharWidth(void) const {
364 return _xftfont
->max_advance_width
;
367 if (i18n
.multibyte())
368 return _fontset_extents
->max_logical_extent
.width
;
370 return _font
->max_bounds
.rbearing
- _font
->min_bounds
.lbearing
;
This page took 0.055102 seconds and 4 git commands to generate.