set teh X so it gets used right
[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, shadow, offset) \
15     (font->elipses_length + (shadow ? offset : 0))
16
17 static gboolean started = FALSE;
18
19 static void font_startup(void)
20 {
21 #ifdef DEBUG
22     int version;
23 #endif /* DEBUG */
24     if (!XftInit(0)) {
25         g_warning(_("Couldn't initialize Xft.\n"));
26         exit(EXIT_FAILURE);
27     }
28 #ifdef DEBUG
29     version = XftGetVersion();
30     g_message("Using Xft %d.%d.%d (Built against %d.%d.%d).",
31               version / 10000 % 100, version / 100 % 100, version % 100,
32               XFT_MAJOR, XFT_MINOR, XFT_REVISION);
33 #endif
34 }
35
36 static void measure_font(RrFont *f)
37 {
38     XGlyphInfo info;
39
40     /* measure an elipses */
41     XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
42                        (FcChar8*)ELIPSES, strlen(ELIPSES), &info);
43     f->elipses_length = (signed) info.xOff;
44 }
45
46 RrFont *RrFontOpen(const RrInstance *inst, char *fontstring)
47 {
48     RrFont *out;
49     XftFont *xf;
50
51     if (!started) {
52         font_startup();
53         started = TRUE;
54     }
55     
56     if ((xf = XftFontOpenName(RrDisplay(inst), RrScreen(inst), fontstring))) {
57         out = g_new(RrFont, 1);
58         out->inst = inst;
59         out->xftfont = xf;
60         measure_font(out);
61         return out;
62     }
63     g_warning(_("Unable to load font: %s\n"), fontstring);
64     g_warning(_("Trying fallback font: %s\n"), "sans");
65
66     if ((xf = XftFontOpenName(RrDisplay(inst), RrScreen(inst), "sans"))) {
67         out = g_new(RrFont, 1);
68         out->inst = inst;
69         out->xftfont = xf;
70         measure_font(out);
71         return out;
72     }
73     g_warning(_("Unable to load font: %s\n"), "sans");
74
75     return NULL;
76 }
77
78 void RrFontClose(RrFont *f)
79 {
80     if (f) {
81         XftFontClose(RrDisplay(f->inst), f->xftfont);
82         g_free(f);
83     }
84 }
85
86 static void font_measure_full(const RrFont *f, const gchar *str,
87                               gint shadow, gint offset, gint *x, gint *y)
88 {
89     XGlyphInfo info;
90
91     XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
92                        (const FcChar8*)str, strlen(str), &info);
93
94     *x = (signed) info.xOff + (shadow ? ABS(offset) : 0);
95     *y = info.height + (shadow ? ABS(offset) : 0);
96 }
97
98 int RrFontMeasureString(const RrFont *f, const gchar *str,
99                         gint shadow, gint offset)
100 {
101     gint x, y;
102     font_measure_full (f, str, shadow, offset, &x, &y);
103     return x;
104 }
105
106 int RrFontHeight(const RrFont *f, gint shadow, gint offset)
107 {
108     return f->xftfont->ascent + f->xftfont->descent + (shadow ? offset : 0);
109 }
110
111 int RrFontMaxCharWidth(const RrFont *f)
112 {
113     return (signed) f->xftfont->max_advance_width;
114 }
115
116 void RrFontDraw(XftDraw *d, RrTextureText *t, Rect *area)
117 {
118     gint x,y,w,h;
119     XftColor c;
120     GString *text;
121     gint mw, em, mh;
122     size_t l;
123     gboolean shortened = FALSE;
124
125     /* center vertically */
126     y = area->y +
127         (area->height - RrFontHeight(t->font, t->shadow, t->offset)) / 2;
128     /* the +2 and -4 leave a small blank edge on the sides */
129     x = area->x + 2;
130     w = area->width - 4;
131     h = area->height;
132
133     text = g_string_new(t->string);
134     l = g_utf8_strlen(text->str, -1);
135     font_measure_full(t->font, text->str, t->shadow, t->offset, &mw, &mh);
136     while (l && mw > area->width) {
137         shortened = TRUE;
138         /* remove a character from the middle */
139         text = g_string_erase(text, l-- / 2, 1);
140         em = ELIPSES_LENGTH(t->font, t->shadow, t->offset);
141         /* if the elipses are too large, don't show them at all */
142         if (em > area->width)
143             shortened = FALSE;
144         font_measure_full(t->font, text->str, t->shadow, t->offset, &mw, &mh);
145         mw += em;
146     }
147     if (shortened) {
148         text = g_string_insert(text, (l + 1) / 2, ELIPSES);
149         l += 3;
150     }
151     if (!l) return;
152
153     switch (t->justify) {
154     case RR_JUSTIFY_LEFT:
155         break;
156     case RR_JUSTIFY_RIGHT:
157         x += (w - mw);
158         break;
159     case RR_JUSTIFY_CENTER:
160         x += (w - mw) / 2;
161         break;
162     }
163
164     l = strlen(text->str); /* number of bytes */
165
166     if (t->shadow) {
167         if (t->tint >= 0) {
168             c.color.red = 0;
169             c.color.green = 0;
170             c.color.blue = 0;
171             c.color.alpha = 0xffff * t->tint / 100; /* transparent shadow */
172             c.pixel = BlackPixel(RrDisplay(t->font->inst),
173                                  RrScreen(t->font->inst));
174         } else {
175             c.color.red = 0xffff * -t->tint / 100;
176             c.color.green = 0xffff * -t->tint / 100;
177             c.color.blue = 0xffff * -t->tint / 100;
178             c.color.alpha = 0xffff * -t->tint / 100; /* transparent shadow */
179             c.pixel = WhitePixel(RrDisplay(t->font->inst),
180                                  RrScreen(t->font->inst));
181         }  
182         XftDrawStringUtf8(d, &c, t->font->xftfont, x + t->offset,
183                           t->font->xftfont->ascent + y + t->offset,
184                           (FcChar8*)text->str, l);
185     }  
186     c.color.red = t->color->r | t->color->r << 8;
187     c.color.green = t->color->g | t->color->g << 8;
188     c.color.blue = t->color->b | t->color->b << 8;
189     c.color.alpha = 0xff | 0xff << 8; /* fully opaque text */
190     c.pixel = t->color->pixel;
191                      
192     XftDrawStringUtf8(d, &c, t->font->xftfont, x,
193                       t->font->xftfont->ascent + y,
194                       (FcChar8*)text->str, l);
195     return;
196 }