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