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