]> Dogcows Code - chaz/openbox/blob - render/font.c
this includes a number of things since my magnificent return....
[chaz/openbox] / render / font.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 font.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003 Ben Jansens
6 Copyright (c) 2003 Derek Foreman
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 See the COPYING file for a copy of the GNU General Public License.
19 */
20
21 #include "font.h"
22 #include "color.h"
23 #include "mask.h"
24 #include "theme.h"
25 #include "geom.h"
26 #include "gettext.h"
27
28 #include <X11/Xft/Xft.h>
29 #include <glib.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #ifdef USE_PANGO
33 #include <locale.h>
34 #endif
35
36 #define ELIPSES "..."
37 #define ELIPSES_LENGTH(font) \
38 (font->elipses_length + (font->shadow ? font->offset : 0))
39
40 #define OB_SHADOW "shadow"
41 #define OB_SHADOW_OFFSET "shadowoffset"
42 #define OB_SHADOW_ALPHA "shadowtint"
43
44 FcObjectType objs[] = {
45 { OB_SHADOW, FcTypeBool },
46 { OB_SHADOW_OFFSET, FcTypeInteger },
47 { OB_SHADOW_ALPHA, FcTypeInteger }
48 };
49
50 #ifdef USE_PANGO
51 static PangoContext *context;
52 #endif
53 static gboolean started = FALSE;
54
55 static void font_startup(void)
56 {
57 if (!XftInit(0)) {
58 g_warning(_("Couldn't initialize Xft."));
59 exit(EXIT_FAILURE);
60 }
61
62 #ifdef USE_PANGO
63 g_type_init();
64 /* these will never be freed, but we will need
65 * them until we shut down anyway */
66 context = pango_xft_get_context(RrDisplay(NULL), RrScreen(NULL));
67 #endif /* USE_PANGO */
68 /* Here we are teaching xft about the shadow, shadowoffset & shadowtint */
69 FcNameRegisterObjectTypes(objs, (sizeof(objs) / sizeof(objs[0])));
70 }
71
72 static void measure_font(RrFont *f)
73 {
74 /* xOff, yOff is the normal spacing to the next glyph. */
75 XGlyphInfo info;
76
77 /* measure an elipses */
78 XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
79 (FcChar8*)ELIPSES, strlen(ELIPSES), &info);
80 f->elipses_length = (signed) info.xOff;
81 }
82
83 static RrFont *openfont(const RrInstance *inst, gchar *fontstring)
84 {
85 /* This function is called for each font in the theme file. */
86 /* It returns a pointer to a RrFont struct after filling it. */
87 RrFont *out;
88 FcPattern *pat, *match;
89 XftFont *font;
90 FcResult res;
91 gint tint;
92 #ifdef USE_PANGO
93 guchar *tmp_string = NULL;
94 gint tmp_int;
95 #endif /* USE_PANGO */
96
97 if (!(pat = XftNameParse(fontstring)))
98 return NULL;
99
100 match = XftFontMatch(RrDisplay(inst), RrScreen(inst), pat, &res);
101 FcPatternDestroy(pat);
102 if (!match)
103 return NULL;
104
105 out = g_new(RrFont, 1);
106 out->inst = inst;
107 #ifdef USE_PANGO
108 out->pango_font_description = pango_font_description_new();
109
110 if (FcPatternGetString(match, "family", 0, &tmp_string) !=
111 FcResultTypeMismatch) {
112 pango_font_description_set_family(out->pango_font_description,
113 (gchar *)tmp_string);
114 tmp_string = NULL;
115 }
116 if (FcPatternGetString(match, "style", 0, &tmp_string) !=
117 FcResultTypeMismatch) {
118 /* Bold ? */
119 if (!g_ascii_strcasecmp("bold", (gchar *)tmp_string)) {
120 pango_font_description_set_weight(out->pango_font_description,
121 PANGO_WEIGHT_BOLD);
122 }
123 /* Italic ? */
124 else if (!g_ascii_strcasecmp("italic", (gchar *)tmp_string)) {
125 pango_font_description_set_style(out->pango_font_description,
126 PANGO_STYLE_ITALIC);
127 }
128 tmp_string = NULL;
129 }
130
131 if (FcPatternGetInteger(match, "pixelsize", 0, &tmp_int) !=
132 FcResultTypeMismatch) {
133 pango_font_description_set_absolute_size(out->pango_font_description,
134 tmp_int*PANGO_SCALE);
135 }
136
137 /* based on gtkmain.c gtk_get_default_language() */
138 {
139 gchar *locale, *p;
140 PangoFontMetrics *metrics;
141
142 locale = g_strdup(setlocale(LC_CTYPE, NULL));
143 if ((p = strchr(locale, '.')))
144 *p = '\0';
145 if ((p = strchr(locale, '@')))
146 *p = '\0';
147 metrics =
148 pango_context_get_metrics(context, out->pango_font_description,
149 pango_language_from_string(locale));
150 out->pango_ascent = pango_font_metrics_get_ascent(metrics);
151 out->pango_descent = pango_font_metrics_get_descent(metrics);
152 g_free(locale);
153 pango_font_metrics_unref(metrics);
154 }
155 #endif /* USE_PANGO */
156
157 if (FcPatternGetBool(match, OB_SHADOW, 0, &out->shadow) != FcResultMatch)
158 out->shadow = FALSE;
159
160 if (FcPatternGetInteger(match, OB_SHADOW_OFFSET, 0, &out->offset) !=
161 FcResultMatch)
162 out->offset = 1;
163
164 if (FcPatternGetInteger(match, OB_SHADOW_ALPHA, 0, &tint) != FcResultMatch)
165 tint = 25;
166 if (tint > 100) tint = 100;
167 else if (tint < -100) tint = -100;
168 out->tint = tint;
169
170 font = XftFontOpenPattern(RrDisplay(inst), match);
171 if (!font) {
172 FcPatternDestroy(match);
173 g_free(out);
174 return NULL;
175 } else
176 out->xftfont = font;
177
178 #ifdef USE_PANGO
179 /* FcPatternDestroy(match); */
180 #endif /* USE_PANGO */
181 measure_font(out);
182
183 return out;
184 }
185
186 RrFont *RrFontOpen(const RrInstance *inst, gchar *fontstring)
187 {
188 RrFont *out;
189
190 if (!started) {
191 font_startup();
192 started = TRUE;
193 }
194
195 if ((out = openfont(inst, fontstring)))
196 return out;
197 g_warning(_("Unable to load font: %s\n"), fontstring);
198 g_warning(_("Trying fallback font: %s\n"), "sans");
199
200 if ((out = openfont(inst, "sans")))
201 return out;
202 g_warning(_("Unable to load font: %s\n"), "sans");
203
204 return NULL;
205 }
206
207 void RrFontClose(RrFont *f)
208 {
209 if (f) {
210 #ifdef USE_PANGO
211 pango_font_description_free(f->pango_font_description);
212 #endif
213 XftFontClose(RrDisplay(f->inst), f->xftfont);
214 g_free(f);
215 }
216 }
217
218 static void font_measure_full(const RrFont *f, const gchar *str,
219 gint *x, gint *y)
220 {
221 #ifdef USE_PANGO
222 PangoLayout *pl;
223 PangoRectangle rect;
224 pl = pango_layout_new (context);
225 pango_layout_set_text(pl, str, -1);
226 pango_layout_set_font_description(pl, f->pango_font_description);
227 pango_layout_set_single_paragraph_mode(pl, TRUE);
228 pango_layout_get_pixel_extents(pl, NULL, &rect);
229 *x = rect.width + (f->shadow ? ABS(f->offset) : 0);
230 *y = rect.height + (f->shadow ? ABS(f->offset) : 0);
231 g_object_unref(pl);
232
233 #else
234 XGlyphInfo info;
235
236 XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
237 (const FcChar8*)str, strlen(str), &info);
238
239 *x = (signed) info.xOff + (f->shadow ? ABS(f->offset) : 0);
240 *y = info.height + (f->shadow ? ABS(f->offset) : 0);
241 #endif /* USE_PANGO */
242 }
243
244 RrSize *RrFontMeasureString(const RrFont *f, const gchar *str)
245 {
246 RrSize *size;
247 size = g_new(RrSize, 1);
248 font_measure_full (f, str, &size->width, &size->height);
249 return size;
250 }
251
252 gint RrFontHeight(const RrFont *f)
253 {
254 #ifdef USE_PANGO
255 return (f->pango_ascent
256 + f->pango_descent
257 ) / PANGO_SCALE +
258 (f->shadow ? f->offset : 0);
259 #else
260 return f->xftfont->ascent + f->xftfont->descent +
261 (f->shadow ? f->offset : 0);
262 #endif
263 }
264
265 gint RrFontMaxCharWidth(const RrFont *f)
266 {
267 return (signed) f->xftfont->max_advance_width;
268 }
269
270 #ifdef USE_PANGO
271 static inline int font_calculate_baseline(RrFont *f, gint height)
272 {
273 /* For my own reference:
274 * _________
275 * ^space/2 ^height ^baseline
276 * v_________|_ |
277 * | ^ascent | _ _
278 * | | | | |_ _____ _| |_ _ _
279 * | | | | _/ -_) \ / _| || |
280 * | v_________v \__\___/_\_\\__|\_, |
281 * | ^descent |__/
282 * __________|_v
283 * ^space/2 |
284 * V_________v
285 */
286 int asc = f->pango_ascent;
287 int ascdesc = asc + f->pango_descent;
288 int space = height * PANGO_SCALE - ascdesc;
289 int baseline = space / 2 + asc;
290 return baseline / PANGO_SCALE;
291 }
292 #endif
293
294 void RrFontDraw(XftDraw *d, RrTextureText *t, RrRect *area)
295 {
296 gint x,y,w,h;
297 XftColor c;
298 GString *text;
299 gint mw;
300 #ifndef USE_PANGO
301 gint mh;
302 size_t l;
303 gboolean shortened = FALSE;
304 #else
305 PangoLayout *pl;
306 PangoRectangle rect;
307
308 pl = pango_layout_new (context);
309 #endif /* USE_PANGO */
310
311 /* center vertically
312 * for xft we pass the top edge of the text for positioning... */
313 #ifndef USE_PANGO
314 y = area->y +
315 (area->height - RrFontHeight(t->font)) / 2;
316 #else
317 /* but for pango we pass the baseline, since different fonts have
318 * different top edges. It looks stupid when the baseline of "normal"
319 * text jumps up and down when a "strange" character is just added
320 * to the end of the text */
321 y = area->y +
322 font_calculate_baseline(t->font, area->height);
323 #endif
324 /* the +2 and -4 leave a small blank edge on the sides */
325 x = area->x + 2;
326 w = area->width - 4;
327 h = area->height;
328
329 text = g_string_new(t->string);
330 #ifndef USE_PANGO
331 l = g_utf8_strlen(text->str, -1);
332 font_measure_full(t->font, text->str, &mw, &mh);
333 while (l && mw > area->width) {
334 shortened = TRUE;
335 /* remove a character from the middle */
336 text = g_string_erase(text, l-- / 2, 1);
337 /* if the elipses are too large, don't show them at all */
338 if (ELIPSES_LENGTH(t->font) > area->width)
339 shortened = FALSE;
340 font_measure_full(t->font, text->str, &mw, &mh);
341 mw += ELIPSES_LENGTH(t->font);
342 }
343 if (shortened) {
344 text = g_string_insert(text, (l + 1) / 2, ELIPSES);
345 l += 3;
346 }
347 if (!l) return;
348
349 l = strlen(text->str); /* number of bytes */
350
351 #else
352 pango_layout_set_text(pl, text->str, -1);
353 pango_layout_set_font_description(pl, t->font->pango_font_description);
354 pango_layout_set_single_paragraph_mode(pl, TRUE);
355 pango_layout_set_width(pl, w * PANGO_SCALE);
356 pango_layout_set_ellipsize(pl, PANGO_ELLIPSIZE_MIDDLE);
357 /* This doesn't work with layout_line() of course */
358 /* pango_layout_set_alignment(pl, (PangoAlignment)(t->justify)); */
359 pango_layout_get_pixel_extents(pl, NULL, &rect);
360 mw = rect.width;
361
362 #endif /* USE_PANGO */
363
364 switch (t->justify) {
365 case RR_JUSTIFY_LEFT:
366 break;
367 case RR_JUSTIFY_RIGHT:
368 x += (w - mw);
369 break;
370 case RR_JUSTIFY_CENTER:
371 x += (w - mw) / 2;
372 break;
373 }
374
375 if (t->font->shadow) {
376 if (t->font->tint >= 0) {
377 c.color.red = 0;
378 c.color.green = 0;
379 c.color.blue = 0;
380 c.color.alpha = 0xffff * t->font->tint / 100;
381 c.pixel = BlackPixel(RrDisplay(t->font->inst),
382 RrScreen(t->font->inst));
383 } else {
384 c.color.red = 0xffff;
385 c.color.green = 0xffff;
386 c.color.blue = 0xffff;
387 c.color.alpha = 0xffff * -t->font->tint / 100;
388 c.pixel = WhitePixel(RrDisplay(t->font->inst),
389 RrScreen(t->font->inst));
390 }
391 #ifndef USE_PANGO
392 XftDrawStringUtf8(d, &c, t->font->xftfont, x + t->font->offset,
393 t->font->xftfont->ascent + y + t->font->offset,
394 (FcChar8*)text->str, l);
395 #else /* USE_PANGO */
396 /* see below... */
397 pango_xft_render_layout_line(d, &c, pango_layout_get_line(pl, 0),
398 (x + t->font->offset) * PANGO_SCALE,
399 (y + t->font->offset) * PANGO_SCALE);
400 #endif /* USE_PANGO */
401 }
402 c.color.red = t->color->r | t->color->r << 8;
403 c.color.green = t->color->g | t->color->g << 8;
404 c.color.blue = t->color->b | t->color->b << 8;
405 c.color.alpha = 0xff | 0xff << 8; /* fully opaque text */
406 c.pixel = t->color->pixel;
407
408 #ifndef USE_PANGO
409 XftDrawStringUtf8(d, &c, t->font->xftfont, x,
410 t->font->xftfont->ascent + y,
411 (FcChar8*)text->str, l);
412 #else /* USE_PANGO */
413 /* layout_line() bases y on the baseline, while layout() bases y on the
414 * top of the ink layout. We want the baseline to always be in the same
415 * place, thusly, we use layout_line()
416 * The actual line doesn't need to be freed (per the pango docs) */
417 pango_xft_render_layout_line(d, &c, pango_layout_get_line(pl, 0),
418 x * PANGO_SCALE, y * PANGO_SCALE);
419 g_object_unref(pl);
420 #endif
421
422 g_string_free(text, TRUE);
423 return;
424 }
This page took 0.0601 seconds and 5 git commands to generate.