]> icculus.org git repositories - mikachu/openbox.git/blob - render/font.c
measure the font's height instead of listening to Xft
[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 static void measure_height(ObFont *f)
27 {
28     XGlyphInfo info;
29     char *str;
30
31     /* XXX add some extended UTF8 characters in here? */
32     str = "12345678900-qwertyuiopasdfghjklzxcvbnm"
33         "!@#$%^&*()_+QWERTYUIOPASDFGHJKLZXCVBNM"
34         "`~[]\\;',./{}|:\"<>?";
35
36     XftTextExtentsUtf8(ob_display, f->xftfont,
37                        (FcChar8*)str, strlen(str), &info);
38     g_message("measured: %d", info.height);
39     f->height = (signed) info.height;
40 }
41
42 ObFont *font_open(char *fontstring)
43 {
44     ObFont *out;
45     XftFont *xf;
46     
47     if ((xf = XftFontOpenName(ob_display, ob_screen, fontstring))) {
48         out = malloc(sizeof(ObFont));
49         out->xftfont = xf;
50         measure_height(out);
51         return out;
52     }
53     g_warning(_("Unable to load font: %s\n"), fontstring);
54     g_warning(_("Trying fallback font: %s\n"), "fixed");
55
56     if ((xf = XftFontOpenName(ob_display, ob_screen, "fixed"))) {
57         out = malloc(sizeof(ObFont));
58         out->xftfont = xf;
59         measure_height(out);
60         return out;
61     }
62     g_warning(_("Unable to load font: %s\n"), "fixed");
63     g_warning(_("Aborting!.\n"));
64
65     exit(3); // can't continue without a font
66 }
67
68 void font_close(ObFont *f)
69 {
70     XftFontClose(ob_display, f->xftfont);
71 }
72
73 int font_measure_string(ObFont *f, const char *str, int shadow, int offset)
74 {
75     XGlyphInfo info;
76
77     XftTextExtentsUtf8(ob_display, f->xftfont,
78                        (FcChar8*)str, strlen(str), &info);
79
80     return (signed) info.xOff + (shadow ? offset : 0);
81 }
82
83 int font_height(ObFont *f, int shadow, int offset)
84 {
85     return f->height + (shadow ? offset : 0);
86 }
87
88 int font_max_char_width(ObFont *f)
89 {
90     return (signed) f->xftfont->max_advance_width;
91 }
92
93 void font_draw(XftDraw *d, TextureText *t)
94 {
95     int x = 0, y = 0;
96     XftColor c;
97     if (t->shadow) {
98         c.color.red = 0;
99         c.color.green = 0;
100         c.color.blue = 0;
101         c.color.alpha = t->tint | t->tint << 8; // transparent shadow
102         c.pixel = BlackPixel(ob_display, ob_screen);
103   
104         XftDrawStringUtf8(d, &c, t->font->xftfont, x + t->offset,
105                           t->font->xftfont->ascent + y + t->offset,
106                           (FcChar8*)t->string, strlen(t->string));
107     }  
108     c.color.red = t->color->r | t->color->r << 8;
109     c.color.green = t->color->g | t->color->g << 8;
110     c.color.blue = t->color->b | t->color->b << 8;
111     c.pixel = t->color->pixel;
112     c.color.alpha = 0xff | 0xff << 8; // no transparency in Color yet
113                      
114     XftDrawStringUtf8(d, &c, t->font->xftfont, x,
115                       t->font->xftfont->ascent + y -
116                       (t->font->xftfont->height - t->font->height) / 2,
117                      (FcChar8*)t->string, strlen(t->string));
118     return;
119 }