5 #include "kernel/geom.h"
6 #include "kernel/gettext.h"
7 #define _(str) gettext(str)
9 #include <X11/Xft/Xft.h>
14 #define ELIPSES_LENGTH(font, shadow, offset) \
15 (font->elipses_length + (shadow ? offset : 0))
17 static gboolean started = FALSE;
19 static void font_startup(void)
25 g_warning(_("Couldn't initialize Xft.\n"));
29 version = XftGetVersion();
30 g_message("Using Xft %d.%d.%d (Built against %d.%d.%d).",
31 version / 10000 % 100, version / 100 % 100, version % 100,
32 XFT_MAJOR, XFT_MINOR, XFT_REVISION);
36 static void measure_font(RrFont *f)
40 /* measure an elipses */
41 XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
42 (FcChar8*)ELIPSES, strlen(ELIPSES), &info);
43 f->elipses_length = (signed) info.xOff;
46 RrFont *RrFontOpen(const RrInstance *inst, char *fontstring)
56 if ((xf = XftFontOpenName(RrDisplay(inst), RrScreen(inst), fontstring))) {
57 out = g_new(RrFont, 1);
63 g_warning(_("Unable to load font: %s\n"), fontstring);
64 g_warning(_("Trying fallback font: %s\n"), "sans");
66 if ((xf = XftFontOpenName(RrDisplay(inst), RrScreen(inst), "sans"))) {
67 out = g_new(RrFont, 1);
73 g_warning(_("Unable to load font: %s\n"), "sans");
74 g_warning(_("Aborting!.\n"));
76 exit(3); /* can't continue without a font */
79 void RrFontClose(RrFont *f)
82 XftFontClose(RrDisplay(f->inst), f->xftfont);
87 static void font_measure_full(const RrFont *f, const gchar *str,
88 gint shadow, gint offset, gint *x, gint *y)
92 XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
93 (const FcChar8*)str, strlen(str), &info);
95 *x = (signed) info.xOff + (shadow ? ABS(offset) : 0);
96 *y = info.height + (shadow ? ABS(offset) : 0);
99 int RrFontMeasureString(const RrFont *f, const gchar *str,
100 gint shadow, gint offset)
103 font_measure_full (f, str, shadow, offset, &x, &y);
107 int RrFontHeight(const RrFont *f, gint shadow, gint offset)
109 return f->xftfont->ascent + f->xftfont->descent + (shadow ? offset : 0);
112 int RrFontMaxCharWidth(const RrFont *f)
114 return (signed) f->xftfont->max_advance_width;
117 void RrFontDraw(XftDraw *d, RrTextureText *t, Rect *area)
124 gboolean shortened = FALSE;
126 /* center vertically */
128 (area->height - RrFontHeight(t->font, t->shadow, t->offset)) / 2;
132 text = g_string_new(t->string);
133 l = g_utf8_strlen(text->str, -1);
134 font_measure_full(t->font, text->str, t->shadow, t->offset, &mw, &mh);
135 while (l && mw > area->width) {
137 /* remove a character from the middle */
138 text = g_string_erase(text, l-- / 2, 1);
139 em = ELIPSES_LENGTH(t->font, t->shadow, t->offset);
140 /* if the elipses are too large, don't show them at all */
141 if (em > area->width)
143 font_measure_full(t->font, text->str, t->shadow, t->offset, &mw, &mh);
147 text = g_string_insert(text, (l + 1) / 2, ELIPSES);
152 switch (t->justify) {
153 case RR_JUSTIFY_LEFT:
156 case RR_JUSTIFY_RIGHT:
157 x = area->x + (w - mw);
159 case RR_JUSTIFY_CENTER:
160 x = area->x + (w - mw) / 2;
164 l = strlen(text->str); /* number of bytes */
171 c.color.alpha = 0xffff * t->tint / 100; /* transparent shadow */
172 c.pixel = BlackPixel(RrDisplay(t->font->inst),
173 RrScreen(t->font->inst));
175 c.color.red = 0xffff * -t->tint / 100;
176 c.color.green = 0xffff * -t->tint / 100;
177 c.color.blue = 0xffff * -t->tint / 100;
178 c.color.alpha = 0xffff * -t->tint / 100; /* transparent shadow */
179 c.pixel = WhitePixel(RrDisplay(t->font->inst),
180 RrScreen(t->font->inst));
182 XftDrawStringUtf8(d, &c, t->font->xftfont, x + t->offset,
183 t->font->xftfont->ascent + y + t->offset,
184 (FcChar8*)text->str, l);
186 c.color.red = t->color->r | t->color->r << 8;
187 c.color.green = t->color->g | t->color->g << 8;
188 c.color.blue = t->color->b | t->color->b << 8;
189 c.color.alpha = 0xff | 0xff << 8; /* fully opaque text */
190 c.pixel = t->color->pixel;
192 XftDrawStringUtf8(d, &c, t->font->xftfont, x,
193 t->font->xftfont->ascent + y,
194 (FcChar8*)text->str, l);