7 #include <X11/Xft/Xft.h>
13 #define ELIPSES_LENGTH(font) \
14 (font->elipses_length + (font->shadow ? font->offset : 0))
16 #define OB_SHADOW "shadow"
17 #define OB_SHADOW_OFFSET "shadowoffset"
18 #define OB_SHADOW_ALPHA "shadowtint"
20 FcObjectType objs[] = {
21 { OB_SHADOW, FcTypeBool },
22 { OB_SHADOW_OFFSET, FcTypeInteger },
23 { OB_SHADOW_ALPHA, FcTypeInteger }
26 static gboolean started = FALSE;
28 static void font_startup(void)
31 g_warning(_("Couldn't initialize Xft.\n"));
34 FcNameRegisterObjectTypes(objs, (sizeof(objs) / sizeof(objs[0])));
37 static void measure_font(RrFont *f)
41 /* measure an elipses */
42 XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
43 (FcChar8*)ELIPSES, strlen(ELIPSES), &info);
44 f->elipses_length = (signed) info.xOff;
47 static RrFont *openfont(const RrInstance *inst, char *fontstring)
50 FcPattern *pat, *match;
55 if (!(pat = XftNameParse(fontstring)))
58 match = XftFontMatch(RrDisplay(inst), RrScreen(inst), pat, &res);
62 out = g_new(RrFont, 1);
65 if (FcPatternGetBool(match, OB_SHADOW, 0, &out->shadow) != FcResultMatch)
68 if (FcPatternGetInteger(match, OB_SHADOW_OFFSET, 0, &out->offset) !=
72 if (FcPatternGetInteger(match, OB_SHADOW_ALPHA, 0, &tint) != FcResultMatch)
74 if (tint > 100) tint = 100;
75 else if (tint < -100) tint = -100;
78 font = XftFontOpenPattern(RrDisplay(inst), match);
80 FcPatternDestroy(match);
91 RrFont *RrFontOpen(const RrInstance *inst, char *fontstring)
100 if ((out = openfont(inst, fontstring)))
102 g_warning(_("Unable to load font: %s\n"), fontstring);
103 g_warning(_("Trying fallback font: %s\n"), "sans");
105 if ((out = openfont(inst, "sans")))
107 g_warning(_("Unable to load font: %s\n"), "sans");
112 void RrFontClose(RrFont *f)
115 XftFontClose(RrDisplay(f->inst), f->xftfont);
120 static void font_measure_full(const RrFont *f, const gchar *str,
125 XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
126 (const FcChar8*)str, strlen(str), &info);
128 *x = (signed) info.xOff + (f->shadow ? ABS(f->offset) : 0);
129 *y = info.height + (f->shadow ? ABS(f->offset) : 0);
132 int RrFontMeasureString(const RrFont *f, const gchar *str)
135 font_measure_full (f, str, &x, &y);
139 int RrFontHeight(const RrFont *f)
141 return f->xftfont->ascent + f->xftfont->descent +
142 (f->shadow ? f->offset : 0);
145 int RrFontMaxCharWidth(const RrFont *f)
147 return (signed) f->xftfont->max_advance_width;
150 void RrFontDraw(XftDraw *d, RrTextureText *t, RrRect *area)
157 gboolean shortened = FALSE;
159 /* center vertically */
161 (area->height - RrFontHeight(t->font)) / 2;
162 /* the +2 and -4 leave a small blank edge on the sides */
167 text = g_string_new(t->string);
168 l = g_utf8_strlen(text->str, -1);
169 font_measure_full(t->font, text->str, &mw, &mh);
170 while (l && mw > area->width) {
172 /* remove a character from the middle */
173 text = g_string_erase(text, l-- / 2, 1);
174 /* if the elipses are too large, don't show them at all */
175 if (ELIPSES_LENGTH(t->font) > area->width)
177 font_measure_full(t->font, text->str, &mw, &mh);
178 mw += ELIPSES_LENGTH(t->font);
181 text = g_string_insert(text, (l + 1) / 2, ELIPSES);
186 switch (t->justify) {
187 case RR_JUSTIFY_LEFT:
189 case RR_JUSTIFY_RIGHT:
192 case RR_JUSTIFY_CENTER:
197 l = strlen(text->str); /* number of bytes */
199 if (t->font->shadow) {
200 if (t->font->tint >= 0) {
204 c.color.alpha = 0xffff * t->font->tint / 100;
205 c.pixel = BlackPixel(RrDisplay(t->font->inst),
206 RrScreen(t->font->inst));
208 c.color.red = 0xffff;
209 c.color.green = 0xffff;
210 c.color.blue = 0xffff;
211 c.color.alpha = 0xffff * -t->font->tint / 100;
212 c.pixel = WhitePixel(RrDisplay(t->font->inst),
213 RrScreen(t->font->inst));
215 XftDrawStringUtf8(d, &c, t->font->xftfont, x + t->font->offset,
216 t->font->xftfont->ascent + y + t->font->offset,
217 (FcChar8*)text->str, l);
219 c.color.red = t->color->r | t->color->r << 8;
220 c.color.green = t->color->g | t->color->g << 8;
221 c.color.blue = t->color->b | t->color->b << 8;
222 c.color.alpha = 0xff | 0xff << 8; /* fully opaque text */
223 c.pixel = t->color->pixel;
225 XftDrawStringUtf8(d, &c, t->font->xftfont, x,
226 t->font->xftfont->ascent + y,
227 (FcChar8*)text->str, l);
229 g_string_free(text, TRUE);