]> icculus.org git repositories - mikachu/openbox.git/blob - src/Font.cc
add some new styles for 2.0 from miklos
[mikachu/openbox.git] / 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)
5 //
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:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
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.
23
24 #ifdef    HAVE_CONFIG_H
25 #  include "../config.h"
26 #endif // HAVE_CONFIG_H
27
28 extern "C" {
29 #ifdef HAVE_STDLIB_H
30 #  include <stdlib.h>
31 #endif // HAVE_STDLIB_H
32 }
33
34 #include <iostream>
35 #include <algorithm>
36
37 using std::string;
38 using std::cerr;
39 using std::endl;
40
41 #include "i18n.hh"
42 #include "Font.hh"
43 #include "Util.hh"
44 #include "GCCache.hh"
45 #include "Color.hh"
46
47 string      BFont::_fallback_font   = "fixed";
48
49 #ifdef XFT
50 BFont::BFont(Display *d, BScreen *screen, const string &family, int size,
51              bool bold, bool italic, bool antialias) :
52                                           _display(d),
53                                           _screen(screen),
54                                           _family(family),
55                                           _simplename(False),
56                                           _size(size),
57                                           _bold(bold),
58                                           _italic(italic),
59                                           _antialias(antialias),
60                                           _xftfont(0),
61                                           _font(0),
62                                           _fontset(0),
63                                           _fontset_extents(0) {
64   _valid = False;
65
66   _xftfont = XftFontOpen(_display, _screen->getScreenNumber(),
67                          XFT_FAMILY, XftTypeString,  _family.c_str(),
68                          XFT_SIZE,   XftTypeInteger, _size,
69                          XFT_WEIGHT, XftTypeInteger, (_bold ?
70                                                       XFT_WEIGHT_BOLD :
71                                                       XFT_WEIGHT_MEDIUM),
72                          XFT_SLANT,  XftTypeInteger, (_italic ?
73                                                       XFT_SLANT_ITALIC :
74                                                       XFT_SLANT_ROMAN),
75                          XFT_ANTIALIAS, XftTypeBool, _antialias,
76                          0);
77   if (! _xftfont)
78     return; // failure
79
80   _font = XLoadQueryFont(_display, buildXlfd().c_str()); 
81   if (! _font)
82     return; // failure
83
84   _valid = True;
85 }
86 #endif
87
88
89 BFont::BFont(Display *d, BScreen *screen, const string &xlfd) :
90                                        _display(d),
91                                        _screen(screen),
92 #ifdef    XFT
93                                        _antialias(False),
94                                        _xftfont(0),
95 #endif // XFT
96                                        _font(0),
97                                        _fontset(0),
98                                        _fontset_extents(0) {
99   string int_xlfd;
100   if (xlfd.empty())
101     int_xlfd = _fallback_font;
102   else
103     int_xlfd = xlfd;
104   
105   if ((_valid = createXFont(int_xlfd)))
106     return; // success
107
108   if (int_xlfd != _fallback_font) {
109     // try the fallback
110     cerr << "BFont::BFont(): couldn't load font '" << _family << "'" << endl <<
111       "Falling back to default '" << _fallback_font << "'" << endl;
112
113     if ((_valid = createXFont(_fallback_font)))
114       return; // success
115   }
116
117   cerr << "BFont::BFont(): couldn't load font '" << _family << "'" << endl <<
118     "Giving up!" << endl;
119   return; // failure
120 }
121
122
123 bool BFont::createXFont(const std::string &xlfd) {
124   /*
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.
128
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.
132   */
133   if (! parseXlfd(xlfd))
134     return False;
135
136   if (i18n.multibyte()) {
137     char **missing, *def = "-";
138     int nmissing;
139  
140     _fontset = XCreateFontSet(_display, buildMultibyteXlfd().c_str(),
141                               &missing, &nmissing, &def);
142     if (nmissing) XFreeStringList(missing);
143     if (_fontset)
144       _fontset_extents = XExtentsOfFontSet(_fontset);
145     else
146       return False;
147
148     assert(_fontset_extents);
149   }
150     
151   _font = XLoadQueryFont(_display, xlfd.c_str());
152   if (! _font)
153     return False;
154   return True;
155 }
156
157
158 BFont::~BFont(void) {
159 #ifdef    XFT
160   if (_xftfont)
161     XftFontClose(_display, _xftfont);
162 #endif // XFT
163
164   if (i18n.multibyte() && _fontset)
165     XFreeFontSet(_display, _fontset);
166   if (_font)
167     XFreeFont(_display, _font);
168 }
169
170
171 /*
172  * Takes _family, _size, _bold, _italic, etc and builds them into a full XLFD.
173  */
174 string BFont::buildXlfd(void) const {
175   if (_simplename) 
176     return _family;
177
178   string weight = _bold ? "bold" : "medium";
179   string slant = _italic ? "i" : "r";
180   string sizestr= _size ? itostring(_size * 10) : "*";
181
182   return "-*-" + _family + "-" + weight + "-" + slant + "-*-*-*-" + sizestr +
183       "-*-*-*-*-*-*";
184 }
185
186
187 /*
188  * Takes _family, _size, _bold, _italic, etc and builds them into a full XLFD.
189  */
190 string BFont::buildMultibyteXlfd(void) const {
191   string weight = _bold ? "bold" : "medium";
192   string slant = _italic ? "i" : "r";
193   string sizestr= _size ? itostring(_size) : "*";
194
195   return _family + ','
196     + "-*-*-" + weight + "-" + slant + "-*-*-*-" + sizestr +
197       "-*-*-*-*-*-*" + ','
198     + "-*-*-*-*-*-*-*-" + sizestr + "-*-*-*-*-*-*" + ',' +
199     + "*";
200 }
201
202
203 /*
204  * Takes a full X font name and parses it out so we know if we're bold, our
205  * size, etc.
206  */
207 bool BFont::parseXlfd(const string &xlfd) {
208   if (xlfd.empty() || xlfd[0] != '-') {
209     _family = xlfd;
210     _simplename = True;
211     _bold = False;
212     _italic = False;
213     _size = 0;
214   } else {
215     _simplename = False;
216     string weight,
217            slant,
218            sizestr;
219     int i = 0;
220
221     string::const_iterator it = xlfd.begin(), end = xlfd.end();
222     while(1) {
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)
232         break;
233       ++it;
234       ++i;
235     }
236     if (i < 3)  // no name even! can't parse that
237       return False;
238     _bold = weight == "bold" || weight == "demibold";
239     _italic = slant == "i" || slant == "o";
240     _size = atoi(sizestr.c_str()) / 10;
241   }
242   
243   // min/max size restrictions for sanity, but 0 is the font's "default size"
244   if (_size && _size < 3)
245     _size = 3;
246   else if (_size > 97)
247     _size = 97;
248
249   return True;
250 }
251
252
253 void BFont::drawString(Drawable d, int x, int y, const BColor &color,
254                        const string &string) const {
255   assert(_valid);
256
257 #ifdef    XFT
258   if (_xftfont) {
259     XftDraw *draw = XftDrawCreate(_display, d, _screen->getVisual(),
260                                   _screen->getColormap());
261     assert(draw);
262
263     XftColor c;
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();
269     
270     XftDrawStringUtf8(draw, &c, _xftfont, x, _xftfont->ascent + y,
271                       (XftChar8 *) string.c_str(), string.size());
272
273     XftDrawDestroy(draw);
274     return;
275   }
276 #endif // XFT
277
278   BPen pen(color, _font);
279
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());
284   else
285     XDrawString(_display, d, pen.gc(),
286                 x, _font->ascent + y,
287                 string.c_str(), string.size());
288 }
289
290
291 unsigned int BFont::measureString(const string &string) const {
292   assert(_valid);
293
294 #ifdef    XFT
295   if (_xftfont) {
296     XGlyphInfo info;
297     XftTextExtentsUtf8(_display, _xftfont, (XftChar8 *) string.c_str(),
298                        string.size(), &info);
299     return info.xOff;
300   }
301 #endif // XFT
302
303   if (i18n.multibyte()) {
304     XRectangle ink, logical;
305     XmbTextExtents(_fontset, string.c_str(), string.size(), &ink, &logical);
306     return logical.width;
307   } else {
308     return XTextWidth(_font, string.c_str(), string.size());
309   }
310 }
311
312
313 unsigned int BFont::height(void) const {
314   assert(_valid);
315
316 #ifdef    XFT
317   if (_xftfont)
318     return _xftfont->height;
319 #endif // XFT
320
321   if (i18n.multibyte())
322     return _fontset_extents->max_ink_extent.height;
323   else
324     return _font->ascent + _font->descent;
325 }
326
327
328 unsigned int BFont::maxCharWidth(void) const {
329   assert(_valid);
330
331 #ifdef    XFT
332   if (_xftfont)
333     return _xftfont->max_advance_width;
334 #endif // XFT
335
336   if (i18n.multibyte())
337     return _fontset_extents->max_logical_extent.width;
338   else
339     return _font->max_bounds.rbearing - _font->min_bounds.lbearing;
340 }