]> Dogcows Code - chaz/openbox/blob - render/font.c
fix a warning
[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 "gettext.h"
25
26 #include <X11/Xft/Xft.h>
27 #include <glib.h>
28 #include <string.h>
29 #include <stdlib.h>
30
31 #define ELIPSES "..."
32 #define ELIPSES_LENGTH(font) \
33 (font->elipses_length + (font->shadow ? font->offset : 0))
34
35 #define OB_SHADOW "shadow"
36 #define OB_SHADOW_OFFSET "shadowoffset"
37 #define OB_SHADOW_ALPHA "shadowtint"
38
39 FcObjectType objs[] = {
40 { OB_SHADOW, FcTypeBool },
41 { OB_SHADOW_OFFSET, FcTypeInteger },
42 { OB_SHADOW_ALPHA, FcTypeInteger }
43 };
44
45 static gboolean started = FALSE;
46
47 static void font_startup(void)
48 {
49 if (!XftInit(0)) {
50 g_warning(_("Couldn't initialize Xft."));
51 exit(EXIT_FAILURE);
52 }
53
54 #ifdef USE_PANGO
55 g_type_init();
56 #endif /* USE_PANGO */
57 /* Here we are teaching xft about the shadow, shadowoffset & shadowtint */
58 FcNameRegisterObjectTypes(objs, (sizeof(objs) / sizeof(objs[0])));
59 }
60
61 static void measure_font(RrFont *f)
62 {
63 /* xOff, yOff is the normal spacing to the next glyph. */
64 XGlyphInfo info;
65
66 /* measure an elipses */
67 XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
68 (FcChar8*)ELIPSES, strlen(ELIPSES), &info);
69 f->elipses_length = (signed) info.xOff;
70 }
71
72 static RrFont *openfont(const RrInstance *inst, gchar *fontstring)
73 {
74 /* This function is called for each font in the theme file. */
75 /* It returns a pointer to a RrFont struct after filling it. */
76 RrFont *out;
77 FcPattern *pat, *match;
78 XftFont *font;
79 FcResult res;
80 gint tint;
81 #ifdef USE_PANGO
82 guchar *tmp_string = NULL;
83 gint tmp_int;
84 #endif /* USE_PANGO */
85
86 if (!(pat = XftNameParse(fontstring)))
87 return NULL;
88
89 match = XftFontMatch(RrDisplay(inst), RrScreen(inst), pat, &res);
90 FcPatternDestroy(pat);
91 if (!match)
92 return NULL;
93
94 out = g_new(RrFont, 1);
95 out->inst = inst;
96 #ifdef USE_PANGO
97 /* printf("\n\n%s\n\n",fontstring);
98 FcPatternPrint(match); */
99
100 out->pango_font_description = pango_font_description_new();
101
102 if (FcPatternGetString(match, "family", 0, &tmp_string) != FcResultTypeMismatch) {
103 pango_font_description_set_family(out->pango_font_description, (gchar *)tmp_string);
104 tmp_string = NULL;
105 }
106 if (FcPatternGetString(match, "style", 0, &tmp_string) != FcResultTypeMismatch) {
107 /* Bold ? */
108 if (!strcasecmp("bold", (gchar *)tmp_string)) {
109 pango_font_description_set_weight(out->pango_font_description, PANGO_WEIGHT_BOLD);
110 }
111 /* Italic ? */
112 else if (!strcasecmp("italic", (gchar *)tmp_string)) {
113 pango_font_description_set_style(out->pango_font_description, PANGO_STYLE_ITALIC);
114 }
115 tmp_string = NULL;
116 }
117
118 if (FcPatternGetInteger(match, "pixelsize", 0, &tmp_int) != FcResultTypeMismatch) {
119 /* TODO: is PANGO_SCALE correct ?? */
120 pango_font_description_set_size(out->pango_font_description, tmp_int*PANGO_SCALE);
121 }
122 #endif /* USE_PANGO */
123
124 if (FcPatternGetBool(match, OB_SHADOW, 0, &out->shadow) != FcResultMatch)
125 out->shadow = FALSE;
126
127 if (FcPatternGetInteger(match, OB_SHADOW_OFFSET, 0, &out->offset) !=
128 FcResultMatch)
129 out->offset = 1;
130
131 if (FcPatternGetInteger(match, OB_SHADOW_ALPHA, 0, &tint) != FcResultMatch)
132 tint = 25;
133 if (tint > 100) tint = 100;
134 else if (tint < -100) tint = -100;
135 out->tint = tint;
136
137 font = XftFontOpenPattern(RrDisplay(inst), match);
138 if (!font) {
139 FcPatternDestroy(match);
140 g_free(out);
141 return NULL;
142 } else
143 out->xftfont = font;
144
145 #ifdef USE_PANGO
146 /* FcPatternDestroy(match); */
147 #endif /* USE_PANGO */
148 measure_font(out);
149
150 return out;
151 }
152
153 RrFont *RrFontOpen(const RrInstance *inst, gchar *fontstring)
154 {
155 RrFont *out;
156
157 if (!started) {
158 font_startup();
159 started = TRUE;
160 }
161
162 if ((out = openfont(inst, fontstring)))
163 return out;
164 g_warning(_("Unable to load font: %s\n"), fontstring);
165 g_warning(_("Trying fallback font: %s\n"), "sans");
166
167 if ((out = openfont(inst, "sans")))
168 return out;
169 g_warning(_("Unable to load font: %s\n"), "sans");
170
171 return NULL;
172 }
173
174 void RrFontClose(RrFont *f)
175 {
176 if (f) {
177 XftFontClose(RrDisplay(f->inst), f->xftfont);
178 g_free(f);
179 }
180 }
181
182 static void font_measure_full(const RrFont *f, const gchar *str,
183 gint *x, gint *y)
184 {
185 #ifdef USE_PANGO
186 PangoContext *context;
187 PangoLayout *pl;
188 PangoRectangle rect;
189 context = pango_xft_get_context (RrDisplay(f->inst), RrScreen(f->inst));
190 pl = pango_layout_new (context);
191 pango_layout_set_text(pl, str, -1);
192 pango_layout_set_font_description(pl, f->pango_font_description);
193 pango_layout_set_single_paragraph_mode(pl, TRUE);
194 pango_layout_get_pixel_extents(pl, NULL, &rect);
195 *x = rect.width + (f->shadow ? ABS(f->offset) : 0);
196 *y = rect.height + (f->shadow ? ABS(f->offset) : 0);
197 g_object_unref(pl);
198 g_object_unref(context);
199
200 #else
201 XGlyphInfo info;
202
203 XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
204 (const FcChar8*)str, strlen(str), &info);
205
206 *x = (signed) info.xOff + (f->shadow ? ABS(f->offset) : 0);
207 *y = info.height + (f->shadow ? ABS(f->offset) : 0);
208 #endif /* USE_PANGO */
209 }
210
211 gint RrFontMeasureString(const RrFont *f, const gchar *str)
212 {
213 gint x, y;
214 font_measure_full (f, str, &x, &y);
215 return x + 4;
216 }
217
218 gint RrFontHeight(const RrFont *f)
219 {
220 #ifndef USE_PANGO
221 return f->xftfont->ascent + f->xftfont->descent +
222 (f->shadow ? f->offset : 0);
223 #else /* USE_PANGO */
224 /*
225 PangoContext *context = pango_context_new ();
226
227 PangoFontMetrics *metrics = pango_context_get_metrics(context, f->pango_font, NULL);
228
229 gint result = pango_font_metrics_get_ascent (metrics) +
230 pango_font_metrics_get_descent(metrics) +
231 (f->shadow ? f->offset : 0);
232
233 pango_font_metrics_unref(metrics);
234 g_object_unref(context);
235 return result;
236 */
237 return f->xftfont->ascent + f->xftfont->descent +
238 (f->shadow ? f->offset : 0);
239
240 #endif /* USE_PANGO */
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 PangoContext *context;
260
261 context = pango_xft_get_context (RrDisplay(t->font->inst), RrScreen(t->font->inst));
262 pl = pango_layout_new (context);
263 #endif /* USE_PANGO */
264
265 /* center vertically */
266 y = area->y +
267 (area->height - RrFontHeight(t->font)) / 2;
268 /* the +2 and -4 leave a small blank edge on the sides */
269 x = area->x + 2;
270 w = area->width - 4;
271 h = area->height;
272
273 text = g_string_new(t->string);
274 #ifndef USE_PANGO
275 l = g_utf8_strlen(text->str, -1);
276 font_measure_full(t->font, text->str, &mw, &mh);
277 while (l && mw > area->width) {
278 shortened = TRUE;
279 /* remove a character from the middle */
280 text = g_string_erase(text, l-- / 2, 1);
281 /* if the elipses are too large, don't show them at all */
282 if (ELIPSES_LENGTH(t->font) > area->width)
283 shortened = FALSE;
284 font_measure_full(t->font, text->str, &mw, &mh);
285 mw += ELIPSES_LENGTH(t->font);
286 }
287 if (shortened) {
288 text = g_string_insert(text, (l + 1) / 2, ELIPSES);
289 l += 3;
290 }
291 if (!l) return;
292
293 switch (t->justify) {
294 case RR_JUSTIFY_LEFT:
295 break;
296 case RR_JUSTIFY_RIGHT:
297 x += (w - mw);
298 break;
299 case RR_JUSTIFY_CENTER:
300 x += (w - mw) / 2;
301 break;
302 }
303
304 l = strlen(text->str); /* number of bytes */
305
306 #else
307 pango_layout_set_text(pl, text->str, -1);
308 pango_layout_set_font_description(pl, t->font->pango_font_description);
309 pango_layout_set_single_paragraph_mode(pl, TRUE);
310 pango_layout_set_width(pl, w * PANGO_SCALE);
311 pango_layout_set_ellipsize(pl, PANGO_ELLIPSIZE_MIDDLE);
312 pango_layout_set_alignment(pl, (PangoAlignment)(t->justify));
313 #endif /* USE_PANGO */
314
315 if (t->font->shadow) {
316 if (t->font->tint >= 0) {
317 c.color.red = 0;
318 c.color.green = 0;
319 c.color.blue = 0;
320 c.color.alpha = 0xffff * t->font->tint / 100;
321 c.pixel = BlackPixel(RrDisplay(t->font->inst),
322 RrScreen(t->font->inst));
323 } else {
324 c.color.red = 0xffff;
325 c.color.green = 0xffff;
326 c.color.blue = 0xffff;
327 c.color.alpha = 0xffff * -t->font->tint / 100;
328 c.pixel = WhitePixel(RrDisplay(t->font->inst),
329 RrScreen(t->font->inst));
330 #ifndef USE_PANGO
331 }
332 XftDrawStringUtf8(d, &c, t->font->xftfont, x + t->font->offset,
333 t->font->xftfont->ascent + y + t->font->offset,
334 (FcChar8*)text->str, l);
335 }
336 #else /* USE_PANGO */
337 }
338 pango_xft_render_layout(d, &c, pl, (x + t->font->offset) * PANGO_SCALE,
339 (y + t->font->offset) * PANGO_SCALE);
340 }
341 #endif /* USE_PANGO */
342 c.color.red = t->color->r | t->color->r << 8;
343 c.color.green = t->color->g | t->color->g << 8;
344 c.color.blue = t->color->b | t->color->b << 8;
345 c.color.alpha = 0xff | 0xff << 8; /* fully opaque text */
346 c.pixel = t->color->pixel;
347
348 #ifndef USE_PANGO
349 XftDrawStringUtf8(d, &c, t->font->xftfont, x,
350 t->font->xftfont->ascent + y,
351 (FcChar8*)text->str, l);
352 #else /* USE_PANGO */
353 pango_xft_render_layout(d, &c, pl, x * PANGO_SCALE, y * PANGO_SCALE);
354 g_object_unref(pl);
355 g_object_unref(context);
356 #endif
357
358 g_string_free(text, TRUE);
359 return;
360 }
This page took 0.053005 seconds and 5 git commands to generate.