]> icculus.org git repositories - mikachu/openbox.git/blob - otk/font.cc
set the style
[mikachu/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 std::string Font::_fallback_font = "fixed";
24 bool        Font::_xft_init      = false;
25
26 Font::Font(int screen_num, const std::string &fontstring,
27              bool shadow, unsigned char offset, unsigned char tint)
28   : _screen_num(screen_num),
29     _fontstring(fontstring),
30     _shadow(shadow),
31     _offset(offset),
32     _tint(tint),
33     _xftfont(0)
34 {
35   assert(screen_num >= 0);
36   assert(tint <= CHAR_MAX);
37   
38   if (!_xft_init) {
39     if (!XftInit(0)) {
40       printf(_("Couldn't initialize Xft.\n\n"));
41       ::exit(3);
42     }
43     int version = XftGetVersion();
44     printf(_("Using Xft %d.%d.%d (Built against %d.%d.%d).\n"),
45            version / 10000 % 100, version / 100 % 100, version % 100,
46            XFT_MAJOR, XFT_MINOR, XFT_REVISION);
47     _xft_init = true;
48   }
49
50   if ((_xftfont = XftFontOpenName(**display, _screen_num,
51                                   _fontstring.c_str())))
52     return;
53
54   printf(_("Unable to load font: %s\n"), _fontstring.c_str());
55   printf(_("Trying fallback font: %s\n"), _fallback_font.c_str());
56
57   if ((_xftfont = XftFontOpenName(**display, _screen_num,
58                                   _fallback_font.c_str())))
59     return;
60
61   printf(_("Unable to load font: %s\n"), _fallback_font.c_str());
62   printf(_("Aborting!.\n"));
63
64   ::exit(3); // can't continue without a font
65 }
66
67
68 Font::~Font(void)
69 {
70   if (_xftfont)
71     XftFontClose(**display, _xftfont);
72 }
73
74
75 int Font::measureString(const ustring &string) const
76 {
77   XGlyphInfo info;
78
79   if (string.utf8())
80     XftTextExtentsUtf8(**display, _xftfont,
81                        (FcChar8*)string.c_str(), string.bytes(), &info);
82   else
83     XftTextExtents8(**display, _xftfont,
84                     (FcChar8*)string.c_str(), string.bytes(), &info);
85
86   return (signed) info.xOff + (_shadow ? _offset : 0);
87 }
88
89
90 int Font::height(void) const
91 {
92   return (signed) _xftfont->height + (_shadow ? _offset : 0);
93 }
94
95
96 int Font::maxCharWidth(void) const
97 {
98   return (signed) _xftfont->max_advance_width;
99 }
100
101 }