]> icculus.org git repositories - dana/openbox.git/blob - render/font.c
hardcode the button names for themes
[dana/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     if (!font) {
83         g_free(out);
84         return NULL;
85     } else
86         out->xftfont = font;
87
88     measure_font(out);
89
90     return out;
91 }
92
93 RrFont *RrFontOpen(const RrInstance *inst, char *fontstring)
94 {
95     RrFont *out;
96
97     if (!started) {
98         font_startup();
99         started = TRUE;
100     }
101
102     if ((out = openfont(inst, fontstring)))
103         return out;
104     g_warning(_("Unable to load font: %s\n"), fontstring);
105     g_warning(_("Trying fallback font: %s\n"), "sans");
106
107     if ((out = openfont(inst, "sans")))
108         return out;
109     g_warning(_("Unable to load font: %s\n"), "sans");
110
111     return NULL;
112 }
113
114 void RrFontClose(RrFont *f)
115 {
116     if (f) {
117         XftFontClose(RrDisplay(f->inst), f->xftfont);
118         g_free(f);
119     }
120 }
121
122 static void font_measure_full(const RrFont *f, const gchar *str,
123                               gint *x, gint *y)
124 {
125     XGlyphInfo info;
126
127     XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
128                        (const FcChar8*)str, strlen(str), &info);
129
130     *x = (signed) info.xOff + (f->shadow ? ABS(f->offset) : 0);
131     *y = info.height + (f->shadow ? ABS(f->offset) : 0);
132 }
133
134 int RrFontMeasureString(const RrFont *f, const gchar *str)
135 {
136     gint x, y;
137     font_measure_full (f, str, &x, &y);
138     return x;
139 }
140
141 int RrFontHeight(const RrFont *f)
142 {
143     return f->xftfont->ascent + f->xftfont->descent +
144         (f->shadow ? f->offset : 0);
145 }
146
147 int RrFontMaxCharWidth(const RrFont *f)
148 {
149     return (signed) f->xftfont->max_advance_width;
150 }
151
152 void RrFontDraw(XftDraw *d, RrTextureText *t, Rect *area)
153 {
154     gint x,y,w,h;
155     XftColor c;
156     GString *text;
157     gint mw, em, mh;
158     size_t l;
159     gboolean shortened = FALSE;
160
161     /* center vertically */
162     y = area->y +
163         (area->height - RrFontHeight(t->font)) / 2;
164     /* the +2 and -4 leave a small blank edge on the sides */
165     x = area->x + 2;
166     w = area->width - 4;
167     h = area->height;
168
169     text = g_string_new(t->string);
170     l = g_utf8_strlen(text->str, -1);
171     font_measure_full(t->font, text->str, &mw, &mh);
172     while (l && mw > area->width) {
173         shortened = TRUE;
174         /* remove a character from the middle */
175         text = g_string_erase(text, l-- / 2, 1);
176         em = ELIPSES_LENGTH(t->font);
177         /* if the elipses are too large, don't show them at all */
178         if (em > area->width)
179             shortened = FALSE;
180         font_measure_full(t->font, text->str, &mw, &mh);
181         mw += em;
182     }
183     if (shortened) {
184         text = g_string_insert(text, (l + 1) / 2, ELIPSES);
185         l += 3;
186     }
187     if (!l) return;
188
189     switch (t->justify) {
190     case RR_JUSTIFY_LEFT:
191         break;
192     case RR_JUSTIFY_RIGHT:
193         x += (w - mw);
194         break;
195     case RR_JUSTIFY_CENTER:
196         x += (w - mw) / 2;
197         break;
198     }
199
200     l = strlen(text->str); /* number of bytes */
201
202     if (t->font->shadow) {
203         if (t->font->tint >= 0) {
204             c.color.red = 0;
205             c.color.green = 0;
206             c.color.blue = 0;
207             c.color.alpha = 0xffff * t->font->tint / 100;
208             c.pixel = BlackPixel(RrDisplay(t->font->inst),
209                                  RrScreen(t->font->inst));
210         } else {
211             c.color.red = 0xffff;
212             c.color.green = 0xffff;
213             c.color.blue = 0xffff;
214             c.color.alpha = 0xffff * -t->font->tint / 100;
215             c.pixel = WhitePixel(RrDisplay(t->font->inst),
216                                  RrScreen(t->font->inst));
217         }  
218         XftDrawStringUtf8(d, &c, t->font->xftfont, x + t->font->offset,
219                           t->font->xftfont->ascent + y + t->font->offset,
220                           (FcChar8*)text->str, l);
221     }  
222     c.color.red = t->color->r | t->color->r << 8;
223     c.color.green = t->color->g | t->color->g << 8;
224     c.color.blue = t->color->b | t->color->b << 8;
225     c.color.alpha = 0xff | 0xff << 8; /* fully opaque text */
226     c.pixel = t->color->pixel;
227
228     XftDrawStringUtf8(d, &c, t->font->xftfont, x,
229                       t->font->xftfont->ascent + y,
230                       (FcChar8*)text->str, l);
231     return;
232 }