]> Dogcows Code - chaz/openbox/blob - otk/font.cc
add a keyboard plugin
[chaz/openbox] / 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 #ifdef DEBUG
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 #endif
48 _xft_init = true;
49 }
50
51 if ((_xftfont = XftFontOpenName(**display, _screen_num,
52 _fontstring.c_str())))
53 return;
54
55 printf(_("Unable to load font: %s\n"), _fontstring.c_str());
56 printf(_("Trying fallback font: %s\n"), "fixed");
57
58 if ((_xftfont = XftFontOpenName(**display, _screen_num,
59 "fixed")))
60 return;
61
62 printf(_("Unable to load font: %s\n"), "fixed");
63 printf(_("Aborting!.\n"));
64
65 ::exit(3); // can't continue without a font
66 }
67
68
69 Font::~Font(void)
70 {
71 if (_xftfont)
72 XftFontClose(**display, _xftfont);
73 }
74
75
76 int Font::measureString(const ustring &string) const
77 {
78 XGlyphInfo info;
79
80 if (string.utf8())
81 XftTextExtentsUtf8(**display, _xftfont,
82 (FcChar8*)string.c_str(), string.bytes(), &info);
83 else
84 XftTextExtents8(**display, _xftfont,
85 (FcChar8*)string.c_str(), string.bytes(), &info);
86
87 return (signed) info.xOff + (_shadow ? _offset : 0);
88 }
89
90
91 int Font::height(void) const
92 {
93 return (signed) _xftfont->height + (_shadow ? _offset : 0);
94 }
95
96
97 int Font::maxCharWidth(void) const
98 {
99 return (signed) _xftfont->max_advance_width;
100 }
101
102 }
This page took 0.036796 seconds and 4 git commands to generate.