]> icculus.org git repositories - mikachu/openbox.git/blob - render/font.c
read fonts and titlebar layout from theme files
[mikachu/openbox.git] / render / font.c
1 #include "font.h"
2 #include "color.h"
3 #include "mask.h"
4 #include "theme.h"
5 #include "kernel/geom.h"
6 #include "kernel/gettext.h"
7 #define _(str) gettext(str)
8
9 #include <X11/Xft/Xft.h>
10 #include <glib.h>
11 #include <string.h>
12
13 #define ELIPSES "..."
14 #define ELIPSES_LENGTH(font) \
15     (font->elipses_length + (font->shadow ? font->offset : 0))
16
17 #define OB_SHADOW "shadow"
18 #define OB_SHADOW_OFFSET "shadowoffset"
19 #define OB_SHADOW_ALPHA "shadowtint"
20
21 FcObjectType objs[] = {
22     { OB_SHADOW,        FcTypeBool    },
23     { OB_SHADOW_OFFSET, FcTypeInteger },
24     { OB_SHADOW_ALPHA,  FcTypeInteger  }
25 };
26
27 static gboolean started = FALSE;
28
29 static void font_startup(void)
30 {
31     if (!XftInit(0)) {
32         g_warning(_("Couldn't initialize Xft.\n"));
33         exit(EXIT_FAILURE);
34     }
35     FcNameRegisterObjectTypes(objs, (sizeof(objs) / sizeof(objs[0])));
36 }
37
38 static void measure_font(RrFont *f)
39 {
40     XGlyphInfo info;
41
42     /* measure an elipses */
43     XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
44                        (FcChar8*)ELIPSES, strlen(ELIPSES), &info);
45     f->elipses_length = (signed) info.xOff;
46 }
47
48 static RrFont *openfont(const RrInstance *inst, char *fontstring)
49 {
50     RrFont *out;
51     FcPattern *pat, *match;
52     XftFont *font;
53     FcResult res;
54     gint tint;
55
56     if (!(pat = XftNameParse(fontstring)))
57         return NULL;
58
59     match = XftFontMatch(RrDisplay(inst), RrScreen(inst), pat, &res);
60     FcPatternDestroy(pat);
61     if (!match)
62         return NULL;
63
64     out = g_new(RrFont, 1);
65     out->inst = inst;
66
67     if (FcPatternGetBool(match, OB_SHADOW, 0, &out->shadow) != FcResultMatch)
68         out->shadow = FALSE;
69     g_message("shadow %d", out->shadow);
70
71     if (FcPatternGetInteger(match, OB_SHADOW_OFFSET, 0, &out->offset) !=
72         FcResultMatch)
73         out->offset = 1;
74
75     if (FcPatternGetInteger(match, OB_SHADOW_ALPHA, 0, &tint) != FcResultMatch)
76         tint = 25;
77     if (tint > 100) tint = 100;
78     else if (tint < -100) tint = -100;
79     out->tint = tint;
80
81     font = XftFontOpenPattern(RrDisplay(inst), match);
82     FcPatternDestroy(match);
83     if (!font) {
84         g_free(out);
85         return NULL;
86     } else
87         out->xftfont = font;
88
89     measure_font(out);
90
91     return out;
92 }
93
94 RrFont *RrFontOpen(const RrInstance *inst, char *fontstring)
95 {
96     RrFont *out;
97
98     if (!started) {
99         font_startup();
100         started = TRUE;
101     }
102
103     if ((out = openfont(inst, fontstring)))
104         return out;
105     g_warning(_("Unable to load font: %s\n"), fontstring);
106     g_warning(_("Trying fallback font: %s\n"), "sans");
107
108     if ((out = openfont(inst, "sans")))
109         return out;
110     g_warning(_("Unable to load font: %s\n"), "sans");
111
112     return NULL;
113 }
114
115 void RrFontClose(RrFont *f)
116 {
117     if (f) {
118         XftFontClose(RrDisplay(f->inst), f->xftfont);
119         g_free(f);
120     }
121 }
122
123 static void font_measure_full(const RrFont *f, const gchar *str,
124                               gint *x, gint *y)
125 {
126     XGlyphInfo info;
127
128     XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
129                        (const FcChar8*)str, strlen(str), &info);
130
131     *x = (signed) info.xOff + (f->shadow ? ABS(f->offset) : 0);
132     *y = info.height + (f->shadow ? ABS(f->offset) : 0);
133 }
134
135 int RrFontMeasureString(const RrFont *f, const gchar *str)
136 {
137     gint x, y;
138     font_measure_full (f, str, &x, &y);
139     return x;
140 }
141
142 int RrFontHeight(const RrFont *f)
143 {
144     return f->xftfont->ascent + f->xftfont->descent +
145         (f->shadow ? f->offset : 0);
146 }
147
148 int RrFontMaxCharWidth(const RrFont *f)
149 {
150     return (signed) f->xftfont->max_advance_width;
151 }
152
153 void RrFontDraw(XftDraw *d, RrTextureText *t, Rect *area)
154 {
155     gint x,y,w,h;
156     XftColor c;
157     GString *text;
158     gint mw, em, mh;
159     size_t l;
160     gboolean shortened = FALSE;
161
162     /* center vertically */
163     y = area->y +
164         (area->height - RrFontHeight(t->font)) / 2;
165     /* the +2 and -4 leave a small blank edge on the sides */
166     x = area->x + 2;
167     w = area->width - 4;
168     h = area->height;
169
170     text = g_string_new(t->string);
171     l = g_utf8_strlen(text->str, -1);
172     font_measure_full(t->font, text->str, &mw, &mh);
173     while (l && mw > area->width) {
174         shortened = TRUE;
175         /* remove a character from the middle */
176         text = g_string_erase(text, l-- / 2, 1);
177         em = ELIPSES_LENGTH(t->font);
178         /* if the elipses are too large, don't show them at all */
179         if (em > area->width)
180             shortened = FALSE;
181         font_measure_full(t->font, text->str, &mw, &mh);
182         mw += em;
183     }
184     if (shortened) {
185         text = g_string_insert(text, (l + 1) / 2, ELIPSES);
186         l += 3;
187     }
188     if (!l) return;
189
190     switch (t->justify) {
191     case RR_JUSTIFY_LEFT:
192         break;
193     case RR_JUSTIFY_RIGHT:
194         x += (w - mw);
195         break;
196     case RR_JUSTIFY_CENTER:
197         x += (w - mw) / 2;
198         break;
199     }
200
201     l = strlen(text->str); /* number of bytes */
202
203     if (t->font->shadow) {
204         if (t->font->tint >= 0) {
205             c.color.red = 0;
206             c.color.green = 0;
207             c.color.blue = 0;
208             c.color.alpha = 0xffff * t->font->tint / 100;
209             c.pixel = BlackPixel(RrDisplay(t->font->inst),
210                                  RrScreen(t->font->inst));
211         } else {
212             c.color.red = 0xffff;
213             c.color.green = 0xffff;
214             c.color.blue = 0xffff;
215             c.color.alpha = 0xffff * -t->font->tint / 100;
216             c.pixel = WhitePixel(RrDisplay(t->font->inst),
217                                  RrScreen(t->font->inst));
218         }  
219         XftDrawStringUtf8(d, &c, t->font->xftfont, x + t->font->offset,
220                           t->font->xftfont->ascent + y + t->font->offset,
221                           (FcChar8*)text->str, l);
222     }  
223     c.color.red = t->color->r | t->color->r << 8;
224     c.color.green = t->color->g | t->color->g << 8;
225     c.color.blue = t->color->b | t->color->b << 8;
226     c.color.alpha = 0xff | 0xff << 8; /* fully opaque text */
227     c.pixel = t->color->pixel;
228
229     XftDrawStringUtf8(d, &c, t->font->xftfont, x,
230                       t->font->xftfont->ascent + y,
231                       (FcChar8*)text->str, l);
232     return;
233 }