1 #include "../kernel/openbox.h"
4 #include "../kernel/gettext.h"
5 #define _(str) gettext(str)
7 #include <X11/Xft/Xft.h>
9 #include "../kernel/geom.h"
12 #define ELIPSES_LENGTH(font, shadow, offset) \
13 (font->elipses_length + (shadow ? offset : 0))
15 void font_startup(void)
21 g_warning(_("Couldn't initialize Xft.\n"));
25 version = XftGetVersion();
26 g_message("Using Xft %d.%d.%d (Built against %d.%d.%d).",
27 version / 10000 % 100, version / 100 % 100, version % 100,
28 XFT_MAJOR, XFT_MINOR, XFT_REVISION);
32 static void measure_height(ObFont *f)
37 /* XXX add some extended UTF8 characters in here? */
38 str = "12345678900-qwertyuiopasdfghjklzxcvbnm"
39 "!@#$%^&*()_+QWERTYUIOPASDFGHJKLZXCVBNM"
40 "`~[]\\;',./{}|:\"<>?";
42 XftTextExtentsUtf8(ob_display, f->xftfont,
43 (FcChar8*)str, strlen(str), &info);
44 f->height = (signed) info.height;
46 /* measure an elipses */
47 XftTextExtentsUtf8(ob_display, f->xftfont,
48 (FcChar8*)ELIPSES, strlen(ELIPSES), &info);
49 f->elipses_length = (signed) info.xOff;
52 ObFont *font_open(char *fontstring)
57 if ((xf = XftFontOpenName(ob_display, ob_screen, fontstring))) {
58 out = g_new(ObFont, 1);
63 g_warning(_("Unable to load font: %s\n"), fontstring);
64 g_warning(_("Trying fallback font: %s\n"), "sans");
66 if ((xf = XftFontOpenName(ob_display, ob_screen, "sans"))) {
67 out = g_new(ObFont, 1);
72 g_warning(_("Unable to load font: %s\n"), "sans");
73 g_warning(_("Aborting!.\n"));
75 exit(3); /* can't continue without a font */
78 void font_close(ObFont *f)
81 XftFontClose(ob_display, f->xftfont);
86 int font_measure_string(ObFont *f, char *str, int shadow, int offset)
90 XftTextExtentsUtf8(ob_display, f->xftfont,
91 (FcChar8*)str, strlen(str), &info);
93 return (signed) info.xOff + (shadow ? offset : 0);
96 int font_height(ObFont *f, int shadow, int offset)
98 return f->height + (shadow ? offset : 0);
101 int font_max_char_width(ObFont *f)
103 return (signed) f->xftfont->max_advance_width;
106 void font_draw(XftDraw *d, TextureText *t, Rect *position)
113 gboolean shortened = FALSE;
117 h = position->height;
119 /* accomidate for areas bigger/smaller than Xft thinks the font is tall */
120 y -= (2 * (t->font->xftfont->ascent + t->font->xftfont->descent) -
121 (t->font->height + h) - 1) / 2;
123 text = g_string_new(t->string);
124 l = g_utf8_strlen(text->str, -1);
125 m = font_measure_string(t->font, text->str, t->shadow, t->offset);
126 while (l && m > position->width) {
128 /* remove a character from the middle */
129 text = g_string_erase(text, l-- / 2, 1);
130 em = ELIPSES_LENGTH(t->font, t->shadow, t->offset);
131 /* if the elipses are too large, don't show them at all */
132 if (em > position->width)
134 m = font_measure_string(t->font, text->str, t->shadow, t->offset) + em;
137 text = g_string_insert(text, (l + 1) / 2, ELIPSES);
142 switch (t->justify) {
147 x = position->x + (w - m);
150 x = position->x + (w - m) / 2;
159 c.color.alpha = 0xffff * t->tint / 100; /* transparent shadow */
160 c.pixel = BlackPixel(ob_display, ob_screen);
162 c.color.red = 0xffff * -t->tint / 100;
163 c.color.green = 0xffff * -t->tint / 100;
164 c.color.blue = 0xffff * -t->tint / 100;
165 c.color.alpha = 0xffff * -t->tint / 100; /* transparent shadow */
166 c.pixel = WhitePixel(ob_display, ob_screen);
168 XftDrawStringUtf8(d, &c, t->font->xftfont, x + t->offset,
169 t->font->xftfont->ascent + y + t->offset,
170 (FcChar8*)text->str, l);
172 c.color.red = t->color->r | t->color->r << 8;
173 c.color.green = t->color->g | t->color->g << 8;
174 c.color.blue = t->color->b | t->color->b << 8;
175 c.color.alpha = 0xff | 0xff << 8; /* fully opaque text */
176 c.pixel = t->color->pixel;
178 XftDrawStringUtf8(d, &c, t->font->xftfont, x,
179 t->font->xftfont->ascent + y,
180 (FcChar8*)text->str, l);