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