1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 font.c for the Openbox window manager
4 Copyright (c) 2003 Ben Jansens
5 Copyright (c) 2003 Derek Foreman
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 See the COPYING file for a copy of the GNU General Public License.
26 #include <X11/Xft/Xft.h>
32 #define ELIPSES_LENGTH(font) \
33 (font->elipses_length + (font->shadow ? font->offset : 0))
35 #define OB_SHADOW "shadow"
36 #define OB_SHADOW_OFFSET "shadowoffset"
37 #define OB_SHADOW_ALPHA "shadowtint"
39 FcObjectType objs[] = {
40 { OB_SHADOW, FcTypeBool },
41 { OB_SHADOW_OFFSET, FcTypeInteger },
42 { OB_SHADOW_ALPHA, FcTypeInteger }
45 static gboolean started = FALSE;
47 static void font_startup(void)
50 g_warning(_("Couldn't initialize Xft."));
56 #endif /* USE_PANGO */
57 /* Here we are teaching xft about the shadow, shadowoffset & shadowtint */
58 FcNameRegisterObjectTypes(objs, (sizeof(objs) / sizeof(objs[0])));
61 static void measure_font(RrFont *f)
63 /* xOff, yOff is the normal spacing to the next glyph. */
66 /* measure an elipses */
68 XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
69 (FcChar8*)ELIPSES, strlen(ELIPSES), &info);
70 f->elipses_length = (signed) info.xOff;
72 XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
73 (FcChar8*)ELIPSES, strlen(ELIPSES), &info);
74 f->elipses_length = (signed) info.xOff;
75 #endif /* USE_PANGO */
78 static RrFont *openfont(const RrInstance *inst, gchar *fontstring)
80 /* This function is called for each font in the theme file. */
81 /* It returns a pointer to a RrFont struct after filling it. */
83 FcPattern *pat, *match;
88 gchar *tmp_string = NULL;
90 #endif /* USE_PANGO */
92 if (!(pat = XftNameParse(fontstring)))
95 match = XftFontMatch(RrDisplay(inst), RrScreen(inst), pat, &res);
96 FcPatternDestroy(pat);
100 out = g_new(RrFont, 1);
103 /* printf("\n\n%s\n\n",fontstring);
104 FcPatternPrint(match); */
106 out->pango_font_description = pango_font_description_new();
108 if (FcPatternGetString(match, "family", 0, &tmp_string) != FcResultTypeMismatch) {
109 pango_font_description_set_family(out->pango_font_description, tmp_string);
112 if (FcPatternGetString(match, "style", 0, &tmp_string) != FcResultTypeMismatch) {
114 if (!strcasecmp("bold", tmp_string)) {
115 pango_font_description_set_weight(out->pango_font_description, PANGO_WEIGHT_BOLD);
118 else if (!strcasecmp("italic", tmp_string)) {
119 pango_font_description_set_style(out->pango_font_description, PANGO_STYLE_ITALIC);
124 if (FcPatternGetInteger(match, "pixelsize", 0, &tmp_int) != FcResultTypeMismatch) {
125 /* TODO: is PANGO_SCALE correct ?? */
126 pango_font_description_set_size(out->pango_font_description, tmp_int*PANGO_SCALE);
128 #endif /* USE_PANGO */
130 if (FcPatternGetBool(match, OB_SHADOW, 0, &out->shadow) != FcResultMatch)
133 if (FcPatternGetInteger(match, OB_SHADOW_OFFSET, 0, &out->offset) !=
137 if (FcPatternGetInteger(match, OB_SHADOW_ALPHA, 0, &tint) != FcResultMatch)
139 if (tint > 100) tint = 100;
140 else if (tint < -100) tint = -100;
143 font = XftFontOpenPattern(RrDisplay(inst), match);
145 FcPatternDestroy(match);
152 /* FcPatternDestroy(match); */
153 #endif /* USE_PANGO */
159 RrFont *RrFontOpen(const RrInstance *inst, gchar *fontstring)
168 if ((out = openfont(inst, fontstring)))
170 g_warning(_("Unable to load font: %s\n"), fontstring);
171 g_warning(_("Trying fallback font: %s\n"), "sans");
173 if ((out = openfont(inst, "sans")))
175 g_warning(_("Unable to load font: %s\n"), "sans");
180 void RrFontClose(RrFont *f)
183 XftFontClose(RrDisplay(f->inst), f->xftfont);
188 static void font_measure_full(const RrFont *f, const gchar *str,
192 PangoContext *context;
195 context = pango_xft_get_context (RrDisplay(f->inst), RrScreen(f->inst));
196 pl = pango_layout_new (context);
197 pango_layout_set_text(pl, str, -1);
198 pango_layout_set_font_description(pl, f->pango_font_description);
199 pango_layout_set_single_paragraph_mode(pl, TRUE);
200 pango_layout_get_pixel_extents(pl, NULL, &rect);
201 *x = rect.width + (f->shadow ? ABS(f->offset) : 0);
202 *y = rect.height + (f->shadow ? ABS(f->offset) : 0);
204 g_object_unref(context);
209 XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
210 (const FcChar8*)str, strlen(str), &info);
212 *x = (signed) info.xOff + (f->shadow ? ABS(f->offset) : 0);
213 *y = info.height + (f->shadow ? ABS(f->offset) : 0);
214 #endif /* USE_PANGO */
217 gint RrFontMeasureString(const RrFont *f, const gchar *str)
220 font_measure_full (f, str, &x, &y);
224 gint RrFontHeight(const RrFont *f)
227 return f->xftfont->ascent + f->xftfont->descent +
228 (f->shadow ? f->offset : 0);
229 #else /* USE_PANGO */
231 PangoContext *context = pango_context_new ();
233 PangoFontMetrics *metrics = pango_context_get_metrics(context, f->pango_font, NULL);
235 gint result = pango_font_metrics_get_ascent (metrics) +
236 pango_font_metrics_get_descent(metrics) +
237 (f->shadow ? f->offset : 0);
239 pango_font_metrics_unref(metrics);
240 g_object_unref(context);
243 return f->xftfont->ascent + f->xftfont->descent +
244 (f->shadow ? f->offset : 0);
246 #endif /* USE_PANGO */
249 gint RrFontMaxCharWidth(const RrFont *f)
251 return (signed) f->xftfont->max_advance_width;
254 void RrFontDraw(XftDraw *d, RrTextureText *t, RrRect *area)
261 gboolean shortened = FALSE;
265 PangoLayoutLine *pll;
266 PangoContext *context;
269 context = pango_xft_get_context (RrDisplay(t->font->inst), RrScreen(t->font->inst));
270 pl = pango_layout_new (context);
271 #endif /* USE_PANGO */
273 /* center vertically */
275 (area->height - RrFontHeight(t->font)) / 2;
276 /* the +2 and -4 leave a small blank edge on the sides */
281 text = g_string_new(t->string);
282 l = g_utf8_strlen(text->str, -1);
283 font_measure_full(t->font, text->str, &mw, &mh);
284 while (l && mw > area->width) {
286 /* remove a character from the middle */
287 text = g_string_erase(text, l-- / 2, 1);
288 /* if the elipses are too large, don't show them at all */
289 if (ELIPSES_LENGTH(t->font) > area->width)
291 font_measure_full(t->font, text->str, &mw, &mh);
292 mw += ELIPSES_LENGTH(t->font);
295 text = g_string_insert(text, (l + 1) / 2, ELIPSES);
300 switch (t->justify) {
301 case RR_JUSTIFY_LEFT:
303 case RR_JUSTIFY_RIGHT:
306 case RR_JUSTIFY_CENTER:
311 l = strlen(text->str); /* number of bytes */
314 pango_layout_set_text(pl, text->str, l);
315 pango_layout_set_font_description(pl, t->font->pango_font_description);
316 pango_layout_set_single_paragraph_mode(pl, TRUE);
317 pll = pango_layout_get_line(pl, 0);
318 #endif /* USE_PANGO */
320 if (t->font->shadow) {
323 #endif /* USE_PANGO */
324 if (t->font->tint >= 0) {
328 c.color.alpha = 0xffff * t->font->tint / 100;
329 c.pixel = BlackPixel(RrDisplay(t->font->inst),
330 RrScreen(t->font->inst));
332 c.color.red = 0xffff;
333 c.color.green = 0xffff;
334 c.color.blue = 0xffff;
335 c.color.alpha = 0xffff * -t->font->tint / 100;
336 c.pixel = WhitePixel(RrDisplay(t->font->inst),
337 RrScreen(t->font->inst));
340 XftDrawStringUtf8(d, &c, t->font->xftfont, x + t->font->offset,
341 t->font->xftfont->ascent + y + t->font->offset,
342 (FcChar8*)text->str, l);
344 #else /* USE_PANGO */
347 for (p = pll->runs; p != NULL; p = p->next)
349 PangoLayoutRun *run = p->data;
350 PangoFont *font = run->item->analysis.font;
351 PangoGlyphString *glyphs = run->glyphs;
354 pango_glyph_string_extents (glyphs, font, NULL, &rect);
355 pango_xft_render (d, &c, font, glyphs, x2 + t->font->offset,
356 t->font->xftfont->ascent + y + t->font->offset);
357 x2 += rect.width / PANGO_SCALE;
360 #endif /* USE_PANGO */
361 c.color.red = t->color->r | t->color->r << 8;
362 c.color.green = t->color->g | t->color->g << 8;
363 c.color.blue = t->color->b | t->color->b << 8;
364 c.color.alpha = 0xff | 0xff << 8; /* fully opaque text */
365 c.pixel = t->color->pixel;
368 XftDrawStringUtf8(d, &c, t->font->xftfont, x,
369 t->font->xftfont->ascent + y,
370 (FcChar8*)text->str, l);
371 #else /* USE_PANGO */
372 for (p = pll->runs; p != NULL; p = p->next)
374 PangoLayoutRun *run = p->data;
375 PangoFont *font = run->item->analysis.font;
376 PangoGlyphString *glyphs = run->glyphs;
379 pango_glyph_string_extents (glyphs, font, NULL, &rect);
380 pango_xft_render (d, &c, font, glyphs, x, t->font->xftfont->ascent + y);
381 x += rect.width / PANGO_SCALE;
384 // pango_layout_line_unref(pll);
386 g_object_unref(context);
387 #endif /* USE_PANGO */
389 g_string_free(text, TRUE);