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