]> icculus.org git repositories - mikachu/openbox.git/blob - render/font.c
added some font stuff
[mikachu/openbox.git] / render / font.c
1 #include <stdlib.h>
2 #include <X11/Xft/Xft.h>
3 #include "../kernel/openbox.h"
4 #include "font.h"
5
6 #include "../src/gettext.h"
7 #define _(str) gettext(str)
8
9 void font_startup(void)
10 {
11 #ifdef DEBUG
12     int version;
13 #endif /* DEBUG */
14     if (!XftInit(0)) {
15         g_warning(_("Couldn't initialize Xft.\n\n"));
16         exit(3);
17     }
18 #ifdef DEBUG
19     version = XftGetVersion();
20     g_message("Using Xft %d.%d.%d (Built against %d.%d.%d).\n",
21               version / 10000 % 100, version / 100 % 100, version % 100,
22               XFT_MAJOR, XFT_MINOR, XFT_REVISION);
23 #endif
24 }
25
26 ObFont *font_open(char *fontstring)
27 {
28     ObFont *out;
29     XftFont *xf;
30     
31     if ((xf = XftFontOpenName(ob_display, ob_screen, fontstring))) {
32         out = malloc(sizeof(ObFont));
33         out->xftfont = xf;
34         return out;
35     }
36     g_warning(_("Unable to load font: %s\n"), fontstring);
37     g_warning(_("Trying fallback font: %s\n"), "fixed");
38
39     if ((xf = XftFontOpenName(ob_display, ob_screen, "fixed"))) {
40         out = malloc(sizeof(ObFont));
41         out->xftfont = xf;
42         return out;
43     }
44     g_warning(_("Unable to load font: %s\n"), "fixed");
45     g_warning(_("Aborting!.\n"));
46
47     exit(3); // can't continue without a font
48 }
49
50
51 void font_close(ObFont *f)
52 {
53     XftFontClose(ob_display, f->xftfont);
54 }
55
56
57 int font_measure_string(ObFont *f, const char *str, int shadow, int offset)
58 {
59     XGlyphInfo info;
60
61     XftTextExtentsUtf8(ob_display, f->xftfont,
62                        (FcChar8*)str, strlen(str), &info);
63
64     return (signed) info.xOff + (shadow ? offset : 0);
65 }
66
67
68 int font_height(ObFont *f, int shadow, int offset)
69 {
70   return (signed) f->xftfont->height + (shadow ? offset : 0);
71 }
72
73
74 int font_max_char_width(ObFont *f)
75 {
76   return (signed) f->xftfont->max_advance_width;
77 }