2 #include "kernel/openbox.h"
3 #include "kernel/geom.h"
4 #include "kernel/gettext.h"
5 #define _(str) gettext(str)
7 #include <X11/Xft/Xft.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)
36 /* measure an elipses */
37 XftTextExtentsUtf8(ob_display, f->xftfont,
38 (FcChar8*)ELIPSES, strlen(ELIPSES), &info);
39 f->elipses_length = (signed) info.xOff;
42 ObFont *font_open(char *fontstring)
47 if ((xf = XftFontOpenName(ob_display, ob_screen, fontstring))) {
48 out = g_new(ObFont, 1);
53 g_warning(_("Unable to load font: %s\n"), fontstring);
54 g_warning(_("Trying fallback font: %s\n"), "sans");
56 if ((xf = XftFontOpenName(ob_display, ob_screen, "sans"))) {
57 out = g_new(ObFont, 1);
62 g_warning(_("Unable to load font: %s\n"), "sans");
63 g_warning(_("Aborting!.\n"));
65 exit(3); /* can't continue without a font */
68 void font_close(ObFont *f)
71 XftFontClose(ob_display, f->xftfont);
76 void font_measure_full(ObFont *f, char *str, int shadow, int offset,
81 XftTextExtentsUtf8(ob_display, f->xftfont,
82 (FcChar8*)str, strlen(str), &info);
84 *x = (signed) info.xOff + (shadow ? ABS(offset) : 0);
85 *y = info.height + (shadow ? ABS(offset) : 0);
88 int font_measure_string(ObFont *f, char *str, int shadow, int offset)
91 font_measure_full (f, str, shadow, offset, &x, &y);
95 int font_height(ObFont *f, int shadow, int offset)
97 return f->xftfont->ascent + f->xftfont->descent + (shadow ? offset : 0);
100 int font_max_char_width(ObFont *f)
102 return (signed) f->xftfont->max_advance_width;
105 void font_draw(XftDraw *d, TextureText *t, Rect *position)
112 gboolean shortened = FALSE;
116 h = position->height;
118 text = g_string_new(t->string);
119 l = g_utf8_strlen(text->str, -1);
120 font_measure_full(t->font, text->str, t->shadow, t->offset, &mw, &mh);
121 while (l && mw > position->width) {
123 /* remove a character from the middle */
124 text = g_string_erase(text, l-- / 2, 1);
125 em = ELIPSES_LENGTH(t->font, t->shadow, t->offset);
126 /* if the elipses are too large, don't show them at all */
127 if (em > position->width)
129 font_measure_full(t->font, text->str, t->shadow, t->offset, &mw, &mh);
133 text = g_string_insert(text, (l + 1) / 2, ELIPSES);
138 /* center vertically */
139 y -= ((t->font->xftfont->ascent + t->font->xftfont->descent) - mh) / 2;
141 switch (t->justify) {
146 x = position->x + (w - mw);
149 x = position->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);