3 #include "kernel/openbox.h"
4 #include "kernel/geom.h"
5 #include "kernel/gettext.h"
6 #define _(str) gettext(str)
8 #include <X11/Xft/Xft.h>
13 #define ELIPSES_LENGTH(font, shadow, offset) \
14 (font->elipses_length + (shadow ? offset : 0))
16 void font_startup(void)
22 g_warning(_("Couldn't initialize Xft.\n"));
26 version = XftGetVersion();
27 g_message("Using Xft %d.%d.%d (Built against %d.%d.%d).",
28 version / 10000 % 100, version / 100 % 100, version % 100,
29 XFT_MAJOR, XFT_MINOR, XFT_REVISION);
33 static void measure_height(ObFont *f)
37 /* measure an elipses */
38 XftTextExtentsUtf8(ob_display, f->xftfont,
39 (FcChar8*)ELIPSES, strlen(ELIPSES), &info);
40 f->elipses_length = (signed) info.xOff;
43 ObFont *font_open(char *fontstring)
48 if ((xf = XftFontOpenName(ob_display, ob_screen, fontstring))) {
49 out = g_new(ObFont, 1);
54 g_warning(_("Unable to load font: %s\n"), fontstring);
55 g_warning(_("Trying fallback font: %s\n"), "sans");
57 if ((xf = XftFontOpenName(ob_display, ob_screen, "sans"))) {
58 out = g_new(ObFont, 1);
63 g_warning(_("Unable to load font: %s\n"), "sans");
64 g_warning(_("Aborting!.\n"));
66 exit(3); /* can't continue without a font */
69 void font_close(ObFont *f)
72 XftFontClose(ob_display, f->xftfont);
77 void font_measure_full(ObFont *f, char *str, int shadow, int offset,
82 XftTextExtentsUtf8(ob_display, f->xftfont,
83 (FcChar8*)str, strlen(str), &info);
85 *x = (signed) info.xOff + (shadow ? ABS(offset) : 0);
86 *y = info.height + (shadow ? ABS(offset) : 0);
89 int font_measure_string(ObFont *f, char *str, int shadow, int offset)
92 font_measure_full (f, str, shadow, offset, &x, &y);
96 int font_height(ObFont *f, int shadow, int offset)
98 return f->xftfont->ascent + f->xftfont->descent + (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 *area)
113 gboolean shortened = FALSE;
115 /* center vertically */
117 (area->height - font_height(t->font, t->shadow, t->offset)) / 2;
118 w = area->width - theme-bevel * 2;
121 text = g_string_new(t->string);
122 l = g_utf8_strlen(text->str, -1);
123 font_measure_full(t->font, text->str, t->shadow, t->offset, &mw, &mh);
124 while (l && mw > area->width) {
126 /* remove a character from the middle */
127 text = g_string_erase(text, l-- / 2, 1);
128 em = ELIPSES_LENGTH(t->font, t->shadow, t->offset);
129 /* if the elipses are too large, don't show them at all */
130 if (em > area->width)
132 font_measure_full(t->font, text->str, t->shadow, t->offset, &mw, &mh);
136 text = g_string_insert(text, (l + 1) / 2, ELIPSES);
141 switch (t->justify) {
143 x = area->x + theme_bevel;
146 x = area->x + (w - mw) - theme_bevel;
149 x = area->x + (w - mw) / 2;
153 l = strlen(text->str); /* number of bytes */
160 c.color.alpha = 0xffff * t->tint / 100; /* transparent shadow */
161 c.pixel = BlackPixel(ob_display, ob_screen);
163 c.color.red = 0xffff * -t->tint / 100;
164 c.color.green = 0xffff * -t->tint / 100;
165 c.color.blue = 0xffff * -t->tint / 100;
166 c.color.alpha = 0xffff * -t->tint / 100; /* transparent shadow */
167 c.pixel = WhitePixel(ob_display, ob_screen);
169 XftDrawStringUtf8(d, &c, t->font->xftfont, x + t->offset,
170 t->font->xftfont->ascent + y + t->offset,
171 (FcChar8*)text->str, l);
173 c.color.red = t->color->r | t->color->r << 8;
174 c.color.green = t->color->g | t->color->g << 8;
175 c.color.blue = t->color->b | t->color->b << 8;
176 c.color.alpha = 0xff | 0xff << 8; /* fully opaque text */
177 c.pixel = t->color->pixel;
179 XftDrawStringUtf8(d, &c, t->font->xftfont, x,
180 t->font->xftfont->ascent + y,
181 (FcChar8*)text->str, l);