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