]> Dogcows Code - chaz/openbox/blob - render/font.c
fix the dpi stuff with pango by reading the right ascent and descent stuff
[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 out->pango_font_metrics = pango_fontset_get_metrics(pfs);
132 #endif /* USE_PANGO */
133
134 if (FcPatternGetBool(match, OB_SHADOW, 0, &out->shadow) != FcResultMatch)
135 out->shadow = FALSE;
136
137 if (FcPatternGetInteger(match, OB_SHADOW_OFFSET, 0, &out->offset) !=
138 FcResultMatch)
139 out->offset = 1;
140
141 if (FcPatternGetInteger(match, OB_SHADOW_ALPHA, 0, &tint) != FcResultMatch)
142 tint = 25;
143 if (tint > 100) tint = 100;
144 else if (tint < -100) tint = -100;
145 out->tint = tint;
146
147 font = XftFontOpenPattern(RrDisplay(inst), match);
148 if (!font) {
149 FcPatternDestroy(match);
150 g_free(out);
151 return NULL;
152 } else
153 out->xftfont = font;
154
155 #ifdef USE_PANGO
156 /* FcPatternDestroy(match); */
157 #endif /* USE_PANGO */
158 measure_font(out);
159
160 return out;
161 }
162
163 RrFont *RrFontOpen(const RrInstance *inst, gchar *fontstring)
164 {
165 RrFont *out;
166
167 if (!started) {
168 font_startup();
169 started = TRUE;
170 }
171
172 if ((out = openfont(inst, fontstring)))
173 return out;
174 g_warning(_("Unable to load font: %s\n"), fontstring);
175 g_warning(_("Trying fallback font: %s\n"), "sans");
176
177 if ((out = openfont(inst, "sans")))
178 return out;
179 g_warning(_("Unable to load font: %s\n"), "sans");
180
181 return NULL;
182 }
183
184 void RrFontClose(RrFont *f)
185 {
186 if (f) {
187 XftFontClose(RrDisplay(f->inst), f->xftfont);
188 g_free(f);
189 }
190 #ifdef USE_PANGO
191 pango_font_metrics_unref(f->pango_font_metrics);
192 pango_font_description_free(f->pango_font_description);
193 #endif
194 }
195
196 static void font_measure_full(const RrFont *f, const gchar *str,
197 gint *x, gint *y)
198 {
199 #ifdef USE_PANGO
200 PangoLayout *pl;
201 PangoRectangle rect;
202 pl = pango_layout_new (context);
203 pango_layout_set_text(pl, str, -1);
204 pango_layout_set_font_description(pl, f->pango_font_description);
205 pango_layout_set_single_paragraph_mode(pl, TRUE);
206 pango_layout_get_pixel_extents(pl, NULL, &rect);
207 *x = rect.width + (f->shadow ? ABS(f->offset) : 0);
208 *y = rect.height + (f->shadow ? ABS(f->offset) : 0);
209 g_object_unref(pl);
210
211 #else
212 XGlyphInfo info;
213
214 XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
215 (const FcChar8*)str, strlen(str), &info);
216
217 *x = (signed) info.xOff + (f->shadow ? ABS(f->offset) : 0);
218 *y = info.height + (f->shadow ? ABS(f->offset) : 0);
219 #endif /* USE_PANGO */
220 }
221
222 RrSize *RrFontMeasureString(const RrFont *f, const gchar *str)
223 {
224 RrSize *size;
225 size = g_new(RrSize, 1);
226 font_measure_full (f, str, &size->width, &size->height);
227 return size;
228 }
229
230 gint RrFontHeight(const RrFont *f)
231 {
232 #ifdef USE_PANGO
233 int ascent, descent;
234 ascent = pango_font_metrics_get_ascent(f->pango_font_metrics);
235 descent = pango_font_metrics_get_descent(f->pango_font_metrics);
236 return (ascent + descent) / PANGO_SCALE;
237 #else
238 return f->xftfont->ascent + f->xftfont->descent +
239 (f->shadow ? f->offset : 0);
240 #endif
241 }
242
243 gint RrFontMaxCharWidth(const RrFont *f)
244 {
245 return (signed) f->xftfont->max_advance_width;
246 }
247
248 void RrFontDraw(XftDraw *d, RrTextureText *t, RrRect *area)
249 {
250 gint x,y,w,h;
251 XftColor c;
252 GString *text;
253 gint mw, mh;
254 #ifndef USE_PANGO
255 size_t l;
256 gboolean shortened = FALSE;
257 #else
258 PangoLayout *pl;
259 PangoRectangle rect;
260
261 pl = pango_layout_new (context);
262 #endif /* USE_PANGO */
263
264 /* center vertically */
265 #ifndef USE_PANGO /* We have to wait for the text string with pango */
266 y = area->y +
267 (area->height - RrFontHeight(t->font)) / 2;
268 #endif
269 /* the +2 and -4 leave a small blank edge on the sides */
270 x = area->x + 2;
271 w = area->width - 4;
272 h = area->height;
273
274 text = g_string_new(t->string);
275 #ifndef USE_PANGO
276 l = g_utf8_strlen(text->str, -1);
277 font_measure_full(t->font, text->str, &mw, &mh);
278 while (l && mw > area->width) {
279 shortened = TRUE;
280 /* remove a character from the middle */
281 text = g_string_erase(text, l-- / 2, 1);
282 /* if the elipses are too large, don't show them at all */
283 if (ELIPSES_LENGTH(t->font) > area->width)
284 shortened = FALSE;
285 font_measure_full(t->font, text->str, &mw, &mh);
286 mw += ELIPSES_LENGTH(t->font);
287 }
288 if (shortened) {
289 text = g_string_insert(text, (l + 1) / 2, ELIPSES);
290 l += 3;
291 }
292 if (!l) return;
293
294 switch (t->justify) {
295 case RR_JUSTIFY_LEFT:
296 break;
297 case RR_JUSTIFY_RIGHT:
298 x += (w - mw);
299 break;
300 case RR_JUSTIFY_CENTER:
301 x += (w - mw) / 2;
302 break;
303 }
304
305 l = strlen(text->str); /* number of bytes */
306
307 #else
308 pango_layout_set_text(pl, text->str, -1);
309 pango_layout_set_font_description(pl, t->font->pango_font_description);
310 pango_layout_set_single_paragraph_mode(pl, TRUE);
311 pango_layout_set_width(pl, w * PANGO_SCALE);
312 pango_layout_set_ellipsize(pl, PANGO_ELLIPSIZE_MIDDLE);
313 pango_layout_set_alignment(pl, (PangoAlignment)(t->justify));
314 pango_layout_get_pixel_extents(pl, NULL, &rect);
315 y = area->y +
316 (area->height - rect.height) / 2;
317
318 #endif /* USE_PANGO */
319
320 if (t->font->shadow) {
321 if (t->font->tint >= 0) {
322 c.color.red = 0;
323 c.color.green = 0;
324 c.color.blue = 0;
325 c.color.alpha = 0xffff * t->font->tint / 100;
326 c.pixel = BlackPixel(RrDisplay(t->font->inst),
327 RrScreen(t->font->inst));
328 } else {
329 c.color.red = 0xffff;
330 c.color.green = 0xffff;
331 c.color.blue = 0xffff;
332 c.color.alpha = 0xffff * -t->font->tint / 100;
333 c.pixel = WhitePixel(RrDisplay(t->font->inst),
334 RrScreen(t->font->inst));
335 }
336 #ifndef USE_PANGO
337 XftDrawStringUtf8(d, &c, t->font->xftfont, x + t->font->offset,
338 t->font->xftfont->ascent + y + t->font->offset,
339 (FcChar8*)text->str, l);
340 #else /* USE_PANGO */
341 pango_xft_render_layout(d, &c, pl, (x + t->font->offset) * PANGO_SCALE,
342 (y + t->font->offset) * PANGO_SCALE);
343 #endif /* USE_PANGO */
344 }
345 c.color.red = t->color->r | t->color->r << 8;
346 c.color.green = t->color->g | t->color->g << 8;
347 c.color.blue = t->color->b | t->color->b << 8;
348 c.color.alpha = 0xff | 0xff << 8; /* fully opaque text */
349 c.pixel = t->color->pixel;
350
351 #ifndef USE_PANGO
352 XftDrawStringUtf8(d, &c, t->font->xftfont, x,
353 t->font->xftfont->ascent + y,
354 (FcChar8*)text->str, l);
355 #else /* USE_PANGO */
356 pango_xft_render_layout(d, &c, pl, x * PANGO_SCALE, y * PANGO_SCALE);
357 g_object_unref(pl);
358 #endif
359
360 g_string_free(text, TRUE);
361 return;
362 }
This page took 0.055197 seconds and 5 git commands to generate.