]> icculus.org git repositories - dana/openbox.git/blob - otk/font.cc
use "fixed" as the fallback font always
[dana/openbox.git] / otk / font.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #include "config.h"
4
5 #include "font.hh"
6 #include "surface.hh"
7 #include "util.hh"
8 #include "display.hh"
9 #include "screeninfo.hh"
10
11 extern "C" {
12 #include "../src/gettext.h"
13 #define _(str) gettext(str)
14 }
15
16 #include <cstdio>
17 #include <cstdlib>
18 #include <iostream>
19 #include <algorithm>
20
21 namespace otk {
22
23 bool        Font::_xft_init      = false;
24
25 Font::Font(int screen_num, const std::string &fontstring,
26              bool shadow, unsigned char offset, unsigned char tint)
27   : _screen_num(screen_num),
28     _fontstring(fontstring),
29     _shadow(shadow),
30     _offset(offset),
31     _tint(tint),
32     _xftfont(0)
33 {
34   assert(screen_num >= 0);
35   assert(tint <= CHAR_MAX);
36   
37   if (!_xft_init) {
38     if (!XftInit(0)) {
39       printf(_("Couldn't initialize Xft.\n\n"));
40       ::exit(3);
41     }
42     int version = XftGetVersion();
43     printf(_("Using Xft %d.%d.%d (Built against %d.%d.%d).\n"),
44            version / 10000 % 100, version / 100 % 100, version % 100,
45            XFT_MAJOR, XFT_MINOR, XFT_REVISION);
46     _xft_init = true;
47   }
48
49   if ((_xftfont = XftFontOpenName(**display, _screen_num,
50                                   _fontstring.c_str())))
51     return;
52
53   printf(_("Unable to load font: %s\n"), _fontstring.c_str());
54   printf(_("Trying fallback font: %s\n"), "fixed");
55
56   if ((_xftfont = XftFontOpenName(**display, _screen_num,
57                                   "fixed")))
58     return;
59
60   printf(_("Unable to load font: %s\n"), "fixed");
61   printf(_("Aborting!.\n"));
62
63   ::exit(3); // can't continue without a font
64 }
65
66
67 Font::~Font(void)
68 {
69   if (_xftfont)
70     XftFontClose(**display, _xftfont);
71 }
72
73
74 int Font::measureString(const ustring &string) const
75 {
76   XGlyphInfo info;
77
78   if (string.utf8())
79     XftTextExtentsUtf8(**display, _xftfont,
80                        (FcChar8*)string.c_str(), string.bytes(), &info);
81   else
82     XftTextExtents8(**display, _xftfont,
83                     (FcChar8*)string.c_str(), string.bytes(), &info);
84
85   return (signed) info.xOff + (_shadow ? _offset : 0);
86 }
87
88
89 int Font::height(void) const
90 {
91   return (signed) _xftfont->height + (_shadow ? _offset : 0);
92 }
93
94
95 int Font::maxCharWidth(void) const
96 {
97   return (signed) _xftfont->max_advance_width;
98 }
99
100 }