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 static gboolean started = FALSE;
17 static void font_startup(void)
23 g_warning(_("Couldn't initialize Xft.\n"));
27 version = XftGetVersion();
28 g_message("Using Xft %d.%d.%d (Built against %d.%d.%d).",
29 version / 10000 % 100, version / 100 % 100, version % 100,
30 XFT_MAJOR, XFT_MINOR, XFT_REVISION);
34 static void measure_font(RrFont *f)
38 /* measure an elipses */
39 XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
40 (FcChar8*)ELIPSES, strlen(ELIPSES), &info);
41 f->elipses_length = (signed) info.xOff;
44 RrFont *RrFontOpen(const RrInstance *inst, char *fontstring)
54 if ((xf = XftFontOpenName(RrDisplay(inst), RrScreen(inst), fontstring))) {
55 out = g_new(RrFont, 1);
61 g_warning(_("Unable to load font: %s\n"), fontstring);
62 g_warning(_("Trying fallback font: %s\n"), "sans");
64 if ((xf = XftFontOpenName(RrDisplay(inst), RrScreen(inst), "sans"))) {
65 out = g_new(RrFont, 1);
71 g_warning(_("Unable to load font: %s\n"), "sans");
72 g_warning(_("Aborting!.\n"));
74 exit(3); /* can't continue without a font */
77 void RrFontClose(RrFont *f)
80 XftFontClose(RrDisplay(f->inst), f->xftfont);
85 static void font_measure_full(const RrFont *f, const gchar *str,
86 gint shadow, gint offset, gint *x, gint *y)
90 XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
91 (const FcChar8*)str, strlen(str), &info);
93 *x = (signed) info.xOff + (shadow ? ABS(offset) : 0);
94 *y = info.height + (shadow ? ABS(offset) : 0);
97 int RrFontMeasureString(const RrFont *f, const gchar *str,
98 gint shadow, gint offset)
101 font_measure_full (f, str, shadow, offset, &x, &y);
105 int RrFontHeight(const RrFont *f, gint shadow, gint offset)
107 return f->xftfont->ascent + f->xftfont->descent + (shadow ? offset : 0);
110 int RrFontMaxCharWidth(const RrFont *f)
112 return (signed) f->xftfont->max_advance_width;
115 void RrFontDraw(XftDraw *d, RrTextureText *t, Rect *area)
122 gboolean shortened = FALSE;
124 /* center vertically */
126 (area->height - RrFontHeight(t->font, t->shadow, t->offset)) / 2;
130 text = g_string_new(t->string);
131 l = g_utf8_strlen(text->str, -1);
132 font_measure_full(t->font, text->str, t->shadow, t->offset, &mw, &mh);
133 while (l && mw > area->width) {
135 /* remove a character from the middle */
136 text = g_string_erase(text, l-- / 2, 1);
137 em = ELIPSES_LENGTH(t->font, t->shadow, t->offset);
138 /* if the elipses are too large, don't show them at all */
139 if (em > area->width)
141 font_measure_full(t->font, text->str, t->shadow, t->offset, &mw, &mh);
145 text = g_string_insert(text, (l + 1) / 2, ELIPSES);
150 switch (t->justify) {
151 case RR_JUSTIFY_LEFT:
154 case RR_JUSTIFY_RIGHT:
155 x = area->x + (w - mw);
157 case RR_JUSTIFY_CENTER:
158 x = area->x + (w - mw) / 2;
162 l = strlen(text->str); /* number of bytes */
169 c.color.alpha = 0xffff * t->tint / 100; /* transparent shadow */
170 c.pixel = BlackPixel(RrDisplay(t->font->inst),
171 RrScreen(t->font->inst));
173 c.color.red = 0xffff * -t->tint / 100;
174 c.color.green = 0xffff * -t->tint / 100;
175 c.color.blue = 0xffff * -t->tint / 100;
176 c.color.alpha = 0xffff * -t->tint / 100; /* transparent shadow */
177 c.pixel = WhitePixel(RrDisplay(t->font->inst),
178 RrScreen(t->font->inst));
180 XftDrawStringUtf8(d, &c, t->font->xftfont, x + t->offset,
181 t->font->xftfont->ascent + y + t->offset,
182 (FcChar8*)text->str, l);
184 c.color.red = t->color->r | t->color->r << 8;
185 c.color.green = t->color->g | t->color->g << 8;
186 c.color.blue = t->color->b | t->color->b << 8;
187 c.color.alpha = 0xff | 0xff << 8; /* fully opaque text */
188 c.pixel = t->color->pixel;
190 XftDrawStringUtf8(d, &c, t->font->xftfont, x,
191 t->font->xftfont->ascent + y,
192 (FcChar8*)text->str, l);