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