]> icculus.org git repositories - dana/openbox.git/blob - render/font.c
apparently nobody uses cvs or bothers to tell me about compile errors
[dana/openbox.git] / 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 #ifndef USE_PANGO
68     XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
69                        (FcChar8*)ELIPSES, strlen(ELIPSES), &info);
70     f->elipses_length = (signed) info.xOff;
71 #else /* USE_PANGO */
72     XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
73                        (FcChar8*)ELIPSES, strlen(ELIPSES), &info);
74     f->elipses_length = (signed) info.xOff;
75 #endif /* USE_PANGO */
76 }
77
78 static RrFont *openfont(const RrInstance *inst, gchar *fontstring)
79 {
80     /* This function is called for each font in the theme file. */
81     /* It returns a pointer to a RrFont struct after filling it. */
82     RrFont *out;
83     FcPattern *pat, *match;
84     XftFont *font;
85     FcResult res;
86     gint tint;
87 #ifdef USE_PANGO
88     gchar *tmp_string = NULL;
89     gint tmp_int;
90 #endif /* USE_PANGO */
91
92     if (!(pat = XftNameParse(fontstring)))
93         return NULL;
94
95     match = XftFontMatch(RrDisplay(inst), RrScreen(inst), pat, &res);
96     FcPatternDestroy(pat);
97     if (!match)
98         return NULL;
99
100     out = g_new(RrFont, 1);
101     out->inst = inst;
102 #ifdef USE_PANGO
103     /*    printf("\n\n%s\n\n",fontstring);
104           FcPatternPrint(match); */
105
106     out->pango_font_description = pango_font_description_new();
107
108     if (FcPatternGetString(match, "family", 0, &tmp_string) != FcResultTypeMismatch) {
109         pango_font_description_set_family(out->pango_font_description, tmp_string);
110         tmp_string = NULL;
111     }
112     if (FcPatternGetString(match, "style", 0, &tmp_string) != FcResultTypeMismatch) {
113         /* Bold ? */
114         if (!strcasecmp("bold", tmp_string)) {
115             pango_font_description_set_weight(out->pango_font_description, PANGO_WEIGHT_BOLD);
116         }
117         /* Italic ? */
118         else if (!strcasecmp("italic", tmp_string)) {
119             pango_font_description_set_style(out->pango_font_description, PANGO_STYLE_ITALIC);
120         }
121         tmp_string = NULL;
122     }
123
124     if (FcPatternGetInteger(match, "pixelsize", 0, &tmp_int) != FcResultTypeMismatch) {
125         /* TODO: is PANGO_SCALE correct ?? */
126         pango_font_description_set_size(out->pango_font_description, tmp_int*PANGO_SCALE);
127     }
128 #endif /* USE_PANGO */
129
130     if (FcPatternGetBool(match, OB_SHADOW, 0, &out->shadow) != FcResultMatch)
131         out->shadow = FALSE;
132
133     if (FcPatternGetInteger(match, OB_SHADOW_OFFSET, 0, &out->offset) !=
134         FcResultMatch)
135         out->offset = 1;
136
137     if (FcPatternGetInteger(match, OB_SHADOW_ALPHA, 0, &tint) != FcResultMatch)
138         tint = 25;
139     if (tint > 100) tint = 100;
140     else if (tint < -100) tint = -100;
141     out->tint = tint;
142
143     font = XftFontOpenPattern(RrDisplay(inst), match);
144     if (!font) {
145         FcPatternDestroy(match);
146         g_free(out);
147         return NULL;
148     } else
149         out->xftfont = font;
150
151 #ifdef USE_PANGO
152     /*        FcPatternDestroy(match); */
153 #endif /* USE_PANGO */
154     measure_font(out);
155
156     return out;
157 }
158
159 RrFont *RrFontOpen(const RrInstance *inst, gchar *fontstring)
160 {
161     RrFont *out;
162
163     if (!started) {
164         font_startup();
165         started = TRUE;
166     }
167
168     if ((out = openfont(inst, fontstring)))
169         return out;
170     g_warning(_("Unable to load font: %s\n"), fontstring);
171     g_warning(_("Trying fallback font: %s\n"), "sans");
172
173     if ((out = openfont(inst, "sans")))
174         return out;
175     g_warning(_("Unable to load font: %s\n"), "sans");
176
177     return NULL;
178 }
179
180 void RrFontClose(RrFont *f)
181 {
182     if (f) {
183         XftFontClose(RrDisplay(f->inst), f->xftfont);
184         g_free(f);
185     }
186 }
187
188 static void font_measure_full(const RrFont *f, const gchar *str,
189                               gint *x, gint *y)
190 {
191 #ifdef USE_PANGO
192     PangoContext *context;
193     PangoLayout *pl;
194     PangoRectangle rect;
195     context = pango_xft_get_context (RrDisplay(f->inst), RrScreen(f->inst));
196     pl = pango_layout_new (context);
197     pango_layout_set_text(pl, str, -1);
198     pango_layout_set_font_description(pl, f->pango_font_description);
199     pango_layout_set_single_paragraph_mode(pl, TRUE);
200     pango_layout_get_pixel_extents(pl, NULL, &rect);
201     *x = rect.width + (f->shadow ? ABS(f->offset) : 0);
202     *y = rect.height + (f->shadow ? ABS(f->offset) : 0);
203     g_object_unref(pl);
204     g_object_unref(context);
205
206 #else
207     XGlyphInfo info;
208
209     XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
210                        (const FcChar8*)str, strlen(str), &info);
211
212     *x = (signed) info.xOff + (f->shadow ? ABS(f->offset) : 0);
213     *y = info.height + (f->shadow ? ABS(f->offset) : 0);
214 #endif /* USE_PANGO */
215 }
216
217 gint RrFontMeasureString(const RrFont *f, const gchar *str)
218 {
219     gint x, y;
220     font_measure_full (f, str, &x, &y);
221     return x + 4;
222 }
223
224 gint RrFontHeight(const RrFont *f)
225 {
226 #ifndef USE_PANGO
227     return f->xftfont->ascent + f->xftfont->descent +
228         (f->shadow ? f->offset : 0);
229 #else /* USE_PANGO */
230      /*
231      PangoContext *context = pango_context_new ();
232  
233      PangoFontMetrics *metrics = pango_context_get_metrics(context, f->pango_font, NULL);
234  
235      gint result =  pango_font_metrics_get_ascent (metrics) +
236          pango_font_metrics_get_descent(metrics) +
237          (f->shadow ? f->offset : 0);
238  
239      pango_font_metrics_unref(metrics);
240      g_object_unref(context);
241      return result;
242  */
243     return f->xftfont->ascent + f->xftfont->descent +
244         (f->shadow ? f->offset : 0);
245
246 #endif /* USE_PANGO */
247 }
248
249 gint RrFontMaxCharWidth(const RrFont *f)
250 {
251     return (signed) f->xftfont->max_advance_width;
252 }
253
254 void RrFontDraw(XftDraw *d, RrTextureText *t, RrRect *area)
255 {
256     gint x,y,w,h;
257     XftColor c;
258     GString *text;
259     gint mw, mh;
260     size_t l;
261     gboolean shortened = FALSE;
262
263 #ifdef USE_PANGO
264     PangoLayout *pl;
265     PangoLayoutLine *pll;
266     PangoContext *context;
267     GSList *p;
268
269     context = pango_xft_get_context (RrDisplay(t->font->inst), RrScreen(t->font->inst));
270     pl = pango_layout_new (context);
271 #endif /* USE_PANGO */
272
273     /* center vertically */
274     y = area->y +
275         (area->height - RrFontHeight(t->font)) / 2;
276     /* the +2 and -4 leave a small blank edge on the sides */
277     x = area->x + 2;
278     w = area->width - 4;
279     h = area->height;
280
281     text = g_string_new(t->string);
282     l = g_utf8_strlen(text->str, -1);
283     font_measure_full(t->font, text->str, &mw, &mh);
284     while (l && mw > area->width) {
285         shortened = TRUE;
286         /* remove a character from the middle */
287         text = g_string_erase(text, l-- / 2, 1);
288         /* if the elipses are too large, don't show them at all */
289         if (ELIPSES_LENGTH(t->font) > area->width)
290             shortened = FALSE;
291         font_measure_full(t->font, text->str, &mw, &mh);
292         mw += ELIPSES_LENGTH(t->font);
293     }
294     if (shortened) {
295         text = g_string_insert(text, (l + 1) / 2, ELIPSES);
296         l += 3;
297     }
298     if (!l) return;
299
300     switch (t->justify) {
301     case RR_JUSTIFY_LEFT:
302         break;
303     case RR_JUSTIFY_RIGHT:
304         x += (w - mw);
305         break;
306     case RR_JUSTIFY_CENTER:
307         x += (w - mw) / 2;
308         break;
309     }
310
311     l = strlen(text->str); /* number of bytes */
312
313 #ifdef USE_PANGO
314     pango_layout_set_text(pl, text->str, l);
315     pango_layout_set_font_description(pl, t->font->pango_font_description);
316     pango_layout_set_single_paragraph_mode(pl, TRUE);
317     pll = pango_layout_get_line(pl, 0);
318 #endif /* USE_PANGO */
319
320     if (t->font->shadow) {
321 #ifdef USE_PANGO
322         int x2 = x;
323 #endif /* USE_PANGO */
324         if (t->font->tint >= 0) {
325             c.color.red = 0;
326             c.color.green = 0;
327             c.color.blue = 0;
328             c.color.alpha = 0xffff * t->font->tint / 100;
329             c.pixel = BlackPixel(RrDisplay(t->font->inst),
330                                  RrScreen(t->font->inst));
331         } else {
332             c.color.red = 0xffff;
333             c.color.green = 0xffff;
334             c.color.blue = 0xffff;
335             c.color.alpha = 0xffff * -t->font->tint / 100;
336             c.pixel = WhitePixel(RrDisplay(t->font->inst),
337                                  RrScreen(t->font->inst));
338 #ifndef USE_PANGO
339         }  
340         XftDrawStringUtf8(d, &c, t->font->xftfont, x + t->font->offset,
341                           t->font->xftfont->ascent + y + t->font->offset,
342                           (FcChar8*)text->str, l);
343     }  
344 #else /* USE_PANGO */
345         }
346
347     for (p = pll->runs; p != NULL; p = p->next)
348         {
349             PangoLayoutRun *run = p->data;
350             PangoFont *font = run->item->analysis.font;
351             PangoGlyphString *glyphs = run->glyphs;
352             PangoRectangle rect;
353
354             pango_glyph_string_extents (glyphs, font, NULL, &rect);
355             pango_xft_render (d, &c, font, glyphs, x2 + t->font->offset,
356                               t->font->xftfont->ascent + y + t->font->offset);
357             x2 += rect.width / PANGO_SCALE;
358         }
359     }
360 #endif /* USE_PANGO */
361     c.color.red = t->color->r | t->color->r << 8;
362     c.color.green = t->color->g | t->color->g << 8;
363     c.color.blue = t->color->b | t->color->b << 8;
364     c.color.alpha = 0xff | 0xff << 8; /* fully opaque text */
365     c.pixel = t->color->pixel;
366
367 #ifndef USE_PANGO
368     XftDrawStringUtf8(d, &c, t->font->xftfont, x,
369                       t->font->xftfont->ascent + y,
370                       (FcChar8*)text->str, l);
371 #else /* USE_PANGO */
372     for (p = pll->runs; p != NULL; p = p->next)
373         {
374             PangoLayoutRun *run = p->data;
375             PangoFont *font = run->item->analysis.font;
376             PangoGlyphString *glyphs = run->glyphs;
377             PangoRectangle rect;
378
379             pango_glyph_string_extents (glyphs, font, NULL, &rect);
380             pango_xft_render (d, &c, font, glyphs, x, t->font->xftfont->ascent + y);
381             x += rect.width / PANGO_SCALE;
382         }
383
384     //    pango_layout_line_unref(pll);
385         g_object_unref(pl);
386         g_object_unref(context);
387 #endif /* USE_PANGO */
388
389     g_string_free(text, TRUE);
390     return;
391 }