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.
27 #include <X11/Xft/Xft.h>
31 #if USE_PANGO && !ENABLE_NLS
36 #define ELIPSES_LENGTH(font) \
37 (font->elipses_length + (font->shadow ? font->offset : 0))
39 #define OB_SHADOW "shadow"
40 #define OB_SHADOW_OFFSET "shadowoffset"
41 #define OB_SHADOW_ALPHA "shadowtint"
43 FcObjectType objs[] = {
44 { OB_SHADOW, FcTypeBool },
45 { OB_SHADOW_OFFSET, FcTypeInteger },
46 { OB_SHADOW_ALPHA, FcTypeInteger }
50 static PangoContext *context;
52 static gboolean started = FALSE;
54 static void font_startup(void)
57 g_warning(_("Couldn't initialize Xft."));
63 /* these will never be freed, but we will need
64 * them until we shut down anyway */
65 context = pango_xft_get_context(RrDisplay(NULL), RrScreen(NULL));
66 #endif /* USE_PANGO */
67 /* Here we are teaching xft about the shadow, shadowoffset & shadowtint */
68 FcNameRegisterObjectTypes(objs, (sizeof(objs) / sizeof(objs[0])));
71 static void measure_font(RrFont *f)
73 /* xOff, yOff is the normal spacing to the next glyph. */
76 /* measure an elipses */
77 XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
78 (FcChar8*)ELIPSES, strlen(ELIPSES), &info);
79 f->elipses_length = (signed) info.xOff;
82 static RrFont *openfont(const RrInstance *inst, gchar *fontstring)
84 /* This function is called for each font in the theme file. */
85 /* It returns a pointer to a RrFont struct after filling it. */
87 FcPattern *pat, *match;
92 guchar *tmp_string = NULL;
94 #endif /* USE_PANGO */
96 if (!(pat = XftNameParse(fontstring)))
99 match = XftFontMatch(RrDisplay(inst), RrScreen(inst), pat, &res);
100 FcPatternDestroy(pat);
104 out = g_new(RrFont, 1);
107 out->pango_font_description = pango_font_description_new();
109 if (FcPatternGetString(match, "family", 0, &tmp_string) !=
110 FcResultTypeMismatch) {
111 pango_font_description_set_family(out->pango_font_description,
112 (gchar *)tmp_string);
115 if (FcPatternGetString(match, "style", 0, &tmp_string) !=
116 FcResultTypeMismatch) {
118 if (!strcasecmp("bold", (gchar *)tmp_string)) {
119 pango_font_description_set_weight(out->pango_font_description,
123 else if (!strcasecmp("italic", (gchar *)tmp_string)) {
124 pango_font_description_set_style(out->pango_font_description,
130 if (FcPatternGetInteger(match, "pixelsize", 0, &tmp_int) !=
131 FcResultTypeMismatch) {
132 /* TODO: is PANGO_SCALE correct ?? */
133 pango_font_description_set_size(out->pango_font_description,
134 tmp_int*PANGO_SCALE);
137 /* based on gtkmain.c gtk_get_default_language() */
140 locale = g_strdup(setlocale(LC_CTYPE, NULL));
141 if ((p = strchr(locale, '.')))
143 if ((p = strchr(locale, '@')))
145 PangoFontMetrics *metrics =
146 pango_context_get_metrics(context, out->pango_font_description,
147 ln = pango_language_from_string(locale));
148 out->pango_ascent = pango_font_metrics_get_ascent(metrics);
149 out->pango_descent = pango_font_metrics_get_descent(metrics);
151 pango_font_metrics_unref(metrics);
152 #endif /* USE_PANGO */
154 if (FcPatternGetBool(match, OB_SHADOW, 0, &out->shadow) != FcResultMatch)
157 if (FcPatternGetInteger(match, OB_SHADOW_OFFSET, 0, &out->offset) !=
161 if (FcPatternGetInteger(match, OB_SHADOW_ALPHA, 0, &tint) != FcResultMatch)
163 if (tint > 100) tint = 100;
164 else if (tint < -100) tint = -100;
167 font = XftFontOpenPattern(RrDisplay(inst), match);
169 FcPatternDestroy(match);
176 /* FcPatternDestroy(match); */
177 #endif /* USE_PANGO */
183 RrFont *RrFontOpen(const RrInstance *inst, gchar *fontstring)
192 if ((out = openfont(inst, fontstring)))
194 g_warning(_("Unable to load font: %s\n"), fontstring);
195 g_warning(_("Trying fallback font: %s\n"), "sans");
197 if ((out = openfont(inst, "sans")))
199 g_warning(_("Unable to load font: %s\n"), "sans");
204 void RrFontClose(RrFont *f)
207 XftFontClose(RrDisplay(f->inst), f->xftfont);
211 pango_font_description_free(f->pango_font_description);
215 static void font_measure_full(const RrFont *f, const gchar *str,
221 pl = pango_layout_new (context);
222 pango_layout_set_text(pl, str, -1);
223 pango_layout_set_font_description(pl, f->pango_font_description);
224 pango_layout_set_single_paragraph_mode(pl, TRUE);
225 pango_layout_get_pixel_extents(pl, NULL, &rect);
226 *x = rect.width + (f->shadow ? ABS(f->offset) : 0);
227 *y = rect.height + (f->shadow ? ABS(f->offset) : 0);
233 XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
234 (const FcChar8*)str, strlen(str), &info);
236 *x = (signed) info.xOff + (f->shadow ? ABS(f->offset) : 0);
237 *y = info.height + (f->shadow ? ABS(f->offset) : 0);
238 #endif /* USE_PANGO */
241 RrSize *RrFontMeasureString(const RrFont *f, const gchar *str)
244 size = g_new(RrSize, 1);
245 font_measure_full (f, str, &size->width, &size->height);
249 gint RrFontHeight(const RrFont *f)
252 return (f->pango_ascent
255 (f->shadow ? f->offset : 0);
257 return f->xftfont->ascent + f->xftfont->descent +
258 (f->shadow ? f->offset : 0);
262 gint RrFontMaxCharWidth(const RrFont *f)
264 return (signed) f->xftfont->max_advance_width;
268 static inline int font_calculate_baseline(RrFont *f, gint height)
270 /* For my own reference:
272 * ^space/2 ^height ^baseline
275 * | | | | |_ _____ _| |_ _ _
276 * | | | | _/ -_) \ / _| || |
277 * | v_________v \__\___/_\_\\__|\_, |
283 int asc = f->pango_ascent;
284 int ascdesc = asc + f->pango_descent;
285 int space = height * PANGO_SCALE - ascdesc;
286 int baseline = space / 2 + asc;
287 return baseline / PANGO_SCALE;
291 void RrFontDraw(XftDraw *d, RrTextureText *t, RrRect *area)
299 gboolean shortened = FALSE;
304 pl = pango_layout_new (context);
305 #endif /* USE_PANGO */
308 * for xft we pass the top edge of the text for positioning... */
311 (area->height - RrFontHeight(t->font)) / 2;
313 /* but for pango we pass the baseline, since different fonts have
314 * different top edges. It looks stupid when the baseline of "normal"
315 * text jumps up and down when a "strange" character is just added
316 * to the end of the text */
318 font_calculate_baseline(t->font, area->height);
320 /* the +2 and -4 leave a small blank edge on the sides */
325 text = g_string_new(t->string);
327 l = g_utf8_strlen(text->str, -1);
328 font_measure_full(t->font, text->str, &mw, &mh);
329 while (l && mw > area->width) {
331 /* remove a character from the middle */
332 text = g_string_erase(text, l-- / 2, 1);
333 /* if the elipses are too large, don't show them at all */
334 if (ELIPSES_LENGTH(t->font) > area->width)
336 font_measure_full(t->font, text->str, &mw, &mh);
337 mw += ELIPSES_LENGTH(t->font);
340 text = g_string_insert(text, (l + 1) / 2, ELIPSES);
345 l = strlen(text->str); /* number of bytes */
348 pango_layout_set_text(pl, text->str, -1);
349 pango_layout_set_font_description(pl, t->font->pango_font_description);
350 pango_layout_set_single_paragraph_mode(pl, TRUE);
351 pango_layout_set_width(pl, w * PANGO_SCALE);
352 pango_layout_set_ellipsize(pl, PANGO_ELLIPSIZE_MIDDLE);
353 /* This doesn't work with layout_line() of course */
354 /* pango_layout_set_alignment(pl, (PangoAlignment)(t->justify)); */
355 pango_layout_get_pixel_extents(pl, NULL, &rect);
358 #endif /* USE_PANGO */
360 switch (t->justify) {
361 case RR_JUSTIFY_LEFT:
363 case RR_JUSTIFY_RIGHT:
366 case RR_JUSTIFY_CENTER:
371 if (t->font->shadow) {
372 if (t->font->tint >= 0) {
376 c.color.alpha = 0xffff * t->font->tint / 100;
377 c.pixel = BlackPixel(RrDisplay(t->font->inst),
378 RrScreen(t->font->inst));
380 c.color.red = 0xffff;
381 c.color.green = 0xffff;
382 c.color.blue = 0xffff;
383 c.color.alpha = 0xffff * -t->font->tint / 100;
384 c.pixel = WhitePixel(RrDisplay(t->font->inst),
385 RrScreen(t->font->inst));
388 XftDrawStringUtf8(d, &c, t->font->xftfont, x + t->font->offset,
389 t->font->xftfont->ascent + y + t->font->offset,
390 (FcChar8*)text->str, l);
391 #else /* USE_PANGO */
393 pango_xft_render_layout_line(d, &c, pango_layout_get_line(pl, 0),
394 (x + t->font->offset) * PANGO_SCALE,
395 (y + t->font->offset) * PANGO_SCALE);
396 #endif /* USE_PANGO */
398 c.color.red = t->color->r | t->color->r << 8;
399 c.color.green = t->color->g | t->color->g << 8;
400 c.color.blue = t->color->b | t->color->b << 8;
401 c.color.alpha = 0xff | 0xff << 8; /* fully opaque text */
402 c.pixel = t->color->pixel;
405 XftDrawStringUtf8(d, &c, t->font->xftfont, x,
406 t->font->xftfont->ascent + y,
407 (FcChar8*)text->str, l);
408 #else /* USE_PANGO */
409 /* layout_line() bases y on the baseline, while layout() bases y on the
410 * top of the ink layout. We want the baseline to always be in the same
411 * place, thusly, we use layout_line()
412 * The actual line doesn't need to be freed (per the pango docs) */
413 pango_xft_render_layout_line(d, &c, pango_layout_get_line(pl, 0),
414 x * PANGO_SCALE, y * PANGO_SCALE);
418 g_string_free(text, TRUE);