]>
Dogcows Code - chaz/openbox/blob - Font.cc
a3a42f49a9ea5b8c8b252b8dcd138479306acf30
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 = False;
48 string
BFont::_fallback_font
= "fixed";
51 BFont::BFont(Display
*d
, BScreen
*screen
, const string
&family
, int size
,
52 bool bold
, bool italic
) : _display(d
),
69 BFont::BFont(Display
*d
, BScreen
*screen
, const string
&xlfd
) :
80 int_xlfd
= _fallback_font
;
88 bool BFont::init(const string
&xlfd
) {
89 // try load the specified font
90 if (xlfd
.empty() || parseFontString(xlfd
))
94 if (xlfd
!= _fallback_font
) {
96 cerr
<< "BFont::BFont(): couldn't load font '" << _name
<< "'" << endl
<<
97 "Falling back to default '" << _fallback_font
<< "'" << endl
;
98 if (parseFontString(_fallback_font
))
103 cerr
<< "BFont::BFont(): couldn't load font '" << _name
<< "'" << endl
<<
104 "Giving up!" << endl
;
110 bool BFont::createFont(void) {
111 std::string fullname
;
114 fullname
= buildXlfdName(False
);
115 _xftfont
= XftFontOpenXlfd(_display
, _screen
->getScreenNumber(),
120 cerr
<< "BFont::BFont(): couldn't load font '" << _name
<< "'" << endl
<<
121 "as an Xft font, trying as a standard X font." << endl
;
124 if (i18n
.multibyte()) {
125 char **missing
, *def
= "-";
128 fullname
= buildXlfdName(True
);
129 _fontset
= XCreateFontSet(_display
, fullname
.c_str(), &missing
, &nmissing
,
131 if (nmissing
) XFreeStringList(missing
);
133 _fontset_extents
= XExtentsOfFontSet(_fontset
);
137 assert(_fontset_extents
);
140 fullname
= buildXlfdName(False
);
141 cerr
<< "loading font '" << fullname
.c_str() << "'\n";
142 _font
= XLoadQueryFont(_display
, fullname
.c_str());
152 XftFontClose(_display
, _xftfont
);
155 if (i18n
.multibyte() && _fontset
)
156 XFreeFontSet(_display
, _fontset
);
158 XFreeFont(_display
, _font
);
163 * Takes _name, _size, _bold, _italic, etc and builds them into a full XLFD.
165 string
BFont::buildXlfdName(bool mb
) const {
166 string weight
= _bold
? "bold" : "medium";
167 string slant
= _italic
? "i" : "r";
168 string sizestr
= _size
? itostring(_size
) : "*";
172 "-*-*-" + weight
+ "-" + slant
+ "-*-*-" + sizestr
+
173 "-*-*-*-*-*-*-*" + ',' +
174 "-*-*-*-*-*-*-" + sizestr
+ "-*-*-*-*-*-*-*" + ',' +
176 else if (_simplename
)
179 return "-*-" + _name
+ "-" + weight
+ "-" + slant
+ "-*-*-*-" +
180 sizestr
+ "-*-*-*-*-*-*";
185 * Takes a full X font name and parses it out so we know if we're bold, our
188 bool BFont::parseFontString(const string
&xlfd
) {
189 if (xlfd
.empty() || xlfd
[0] != '-') {
202 string::const_iterator it
= xlfd
.begin(), end
= xlfd
.end();
204 string::const_iterator tmp
= it
; // current string.begin()
205 it
= std::find(tmp
, end
, '-'); // look for comma between tmp and end
206 if (i
== 2) _name
= string(tmp
, it
); // s[tmp:it]
207 if (i
== 3) weight
= string(tmp
, it
);
208 if (i
== 4) slant
= string(tmp
, it
);
209 if (i
== 8) sizestr
= string(tmp
, it
);
210 if (it
== end
|| i
>= 8)
215 if (i
< 3) // no name even! can't parse that
217 _bold
= weight
== "bold" || weight
== "demibold";
218 _italic
= slant
== "i" || slant
== "o";
219 if (atoi(sizestr
.c_str()))
220 _size
= atoi(sizestr
.c_str());
223 // min/max size restrictions for sanity, but 0 is the font's "default size"
224 if (_size
&& _size
< 30)
226 else if (_size
> 970)
233 void BFont::drawString(Drawable d
, int x
, int y
, const BColor
&color
,
234 const string
&string
) const {
239 XftDraw
*draw
= XftDrawCreate(_display
, d
, _screen
->getVisual(),
240 _screen
->getColormap());
244 c
.color
.red
= color
.red() | color
.red() << 8;
245 c
.color
.green
= color
.green() | color
.green() << 8;
246 c
.color
.blue
= color
.blue() | color
.blue() << 8;
247 c
.color
.alpha
= 0xff | 0xff << 8; // no transparency in BColor yet
248 c
.pixel
= color
.pixel();
250 XftDrawStringUtf8(draw
, &c
, _xftfont
, x
, _xftfont
->ascent
+ y
,
251 (XftChar8
*) string
.c_str(), string
.size());
253 XftDrawDestroy(draw
);
258 BGCCache
*_cache
= color
.display()->gcCache();
259 BGCCacheItem
*_item
= _cache
->find(color
, _font
, GXcopy
, ClipByChildren
);
264 if (i18n
.multibyte())
265 XmbDrawString(_display
, d
, _fontset
, _item
->gc(),
266 x
, y
- _fontset_extents
->max_ink_extent
.y
,
267 string
.c_str(), string
.size());
269 XDrawString(_display
, d
, _item
->gc(),
270 x
, _font
->ascent
+ y
,
271 string
.c_str(), string
.size());
273 _cache
->release(_item
);
277 unsigned int BFont::measureString(const string
&string
) const {
283 XftTextExtentsUtf8(_display
, _xftfont
, (XftChar8
*) string
.c_str(),
284 string
.size(), &info
);
289 if (i18n
.multibyte()) {
290 XRectangle ink
, logical
;
291 XmbTextExtents(_fontset
, string
.c_str(), string
.size(), &ink
, &logical
);
292 return logical
.width
;
294 return XTextWidth(_font
, string
.c_str(), string
.size());
299 unsigned int BFont::height(void) const {
304 return _xftfont
->height
;
307 if (i18n
.multibyte())
308 return _fontset_extents
->max_ink_extent
.height
;
310 return _font
->ascent
+ _font
->descent
;
314 unsigned int BFont::maxCharWidth(void) const {
319 return _xftfont
->max_advance_width
;
322 if (i18n
.multibyte())
323 return _fontset_extents
->max_logical_extent
.width
;
325 return _font
->max_bounds
.rbearing
- _font
->min_bounds
.lbearing
;
This page took 0.051882 seconds and 3 git commands to generate.