]> icculus.org git repositories - mikachu/openbox.git/blob - render/font.c
made all the gradients work with integer math. removed the pipecross and rectangle...
[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     w = area->width;
129     h = area->height;
130
131     text = g_string_new(t->string);
132     l = g_utf8_strlen(text->str, -1);
133     font_measure_full(t->font, text->str, t->shadow, t->offset, &mw, &mh);
134     while (l && mw > area->width) {
135         shortened = TRUE;
136         /* remove a character from the middle */
137         text = g_string_erase(text, l-- / 2, 1);
138         em = ELIPSES_LENGTH(t->font, t->shadow, t->offset);
139         /* if the elipses are too large, don't show them at all */
140         if (em > area->width)
141             shortened = FALSE;
142         font_measure_full(t->font, text->str, t->shadow, t->offset, &mw, &mh);
143         mw += em;
144     }
145     if (shortened) {
146         text = g_string_insert(text, (l + 1) / 2, ELIPSES);
147         l += 3;
148     }
149     if (!l) return;
150
151     switch (t->justify) {
152     case RR_JUSTIFY_LEFT:
153         x = area->x;
154         break;
155     case RR_JUSTIFY_RIGHT:
156         x = area->x + (w - mw);
157         break;
158     case RR_JUSTIFY_CENTER:
159         x = area->x + (w - mw) / 2;
160         break;
161     }
162
163     l = strlen(text->str); /* number of bytes */
164
165     if (t->shadow) {
166         if (t->tint >= 0) {
167             c.color.red = 0;
168             c.color.green = 0;
169             c.color.blue = 0;
170             c.color.alpha = 0xffff * t->tint / 100; /* transparent shadow */
171             c.pixel = BlackPixel(RrDisplay(t->font->inst),
172                                  RrScreen(t->font->inst));
173         } else {
174             c.color.red = 0xffff * -t->tint / 100;
175             c.color.green = 0xffff * -t->tint / 100;
176             c.color.blue = 0xffff * -t->tint / 100;
177             c.color.alpha = 0xffff * -t->tint / 100; /* transparent shadow */
178             c.pixel = WhitePixel(RrDisplay(t->font->inst),
179                                  RrScreen(t->font->inst));
180         }  
181         XftDrawStringUtf8(d, &c, t->font->xftfont, x + t->offset,
182                           t->font->xftfont->ascent + y + t->offset,
183                           (FcChar8*)text->str, l);
184     }  
185     c.color.red = t->color->r | t->color->r << 8;
186     c.color.green = t->color->g | t->color->g << 8;
187     c.color.blue = t->color->b | t->color->b << 8;
188     c.color.alpha = 0xff | 0xff << 8; /* fully opaque text */
189     c.pixel = t->color->pixel;
190                      
191     XftDrawStringUtf8(d, &c, t->font->xftfont, x,
192                       t->font->xftfont->ascent + y,
193                       (FcChar8*)text->str, l);
194     return;
195 }