7ffcb53b2dbcec7aa20166594fc859b2ea6748c2
[dana/openbox-history.git] / render / font.c
1 #include "../kernel/openbox.h"
2 #include "font.h"
3
4 #include "../kernel/gettext.h"
5 #define _(str) gettext(str)
6
7 #include <X11/Xft/Xft.h>
8 #include <glib.h>
9 #include "../kernel/geom.h"
10
11 void font_startup(void)
12 {
13 #ifdef DEBUG
14     int version;
15 #endif /* DEBUG */
16     if (!XftInit(0)) {
17         g_warning(_("Couldn't initialize Xft.\n"));
18         exit(3);
19     }
20 #ifdef DEBUG
21     version = XftGetVersion();
22     g_message("Using Xft %d.%d.%d (Built against %d.%d.%d).",
23               version / 10000 % 100, version / 100 % 100, version % 100,
24               XFT_MAJOR, XFT_MINOR, XFT_REVISION);
25 #endif
26 }
27
28 static void measure_height(ObFont *f)
29 {
30     XGlyphInfo info;
31     char *str;
32
33     /* XXX add some extended UTF8 characters in here? */
34     str = "12345678900-qwertyuiopasdfghjklzxcvbnm"
35         "!@#$%^&*()_+QWERTYUIOPASDFGHJKLZXCVBNM"
36         "`~[]\\;',./{}|:\"<>?";
37
38     XftTextExtentsUtf8(ob_display, f->xftfont,
39                        (FcChar8*)str, strlen(str), &info);
40     f->height = (signed) info.height;
41 }
42
43 ObFont *font_open(char *fontstring)
44 {
45     ObFont *out;
46     XftFont *xf;
47     
48     if ((xf = XftFontOpenName(ob_display, ob_screen, fontstring))) {
49         out = g_new(ObFont, 1);
50         out->xftfont = xf;
51         measure_height(out);
52         return out;
53     }
54     g_warning(_("Unable to load font: %s\n"), fontstring);
55     g_warning(_("Trying fallback font: %s\n"), "sans");
56
57     if ((xf = XftFontOpenName(ob_display, ob_screen, "sans"))) {
58         out = g_new(ObFont, 1);
59         out->xftfont = xf;
60         measure_height(out);
61         return out;
62     }
63     g_warning(_("Unable to load font: %s\n"), "sans");
64     g_warning(_("Aborting!.\n"));
65
66     exit(3); /* can't continue without a font */
67 }
68
69 void font_close(ObFont *f)
70 {
71     if (f) {
72         XftFontClose(ob_display, f->xftfont);
73         g_free(f);
74     }
75 }
76
77 int font_measure_string(ObFont *f, char *str, int shadow, int offset)
78 {
79     XGlyphInfo info;
80
81     XftTextExtentsUtf8(ob_display, f->xftfont,
82                        (FcChar8*)str, strlen(str), &info);
83
84     return (signed) info.xOff + (shadow ? offset : 0);
85 }
86
87 int font_height(ObFont *f, int shadow, int offset)
88 {
89     return f->height + (shadow ? offset : 0);
90 }
91
92 int font_max_char_width(ObFont *f)
93 {
94     return (signed) f->xftfont->max_advance_width;
95 }
96
97 void font_draw(XftDraw *d, TextureText *t, Rect *position)
98 {
99     int x,y,w,h;
100     XftColor c;
101     char *text;
102     int m;
103     size_t l;
104
105     y = position->y;
106     w = position->width;
107     h = position->height;
108
109     /* accomidate for areas bigger/smaller than Xft thinks the font is tall */
110     y -= (2 * (t->font->xftfont->ascent + t->font->xftfont->descent) -
111           (t->font->height + h) - 1) / 2;
112
113     text = g_strdup(t->string);
114     l = strlen(text);
115     m = font_measure_string(t->font, text, t->shadow, t->offset);
116     while (l && m > position->width) {
117         text[--l] = '\0';
118         m = font_measure_string(t->font, text, t->shadow, t->offset);
119     }
120     if (!l) return;
121
122     switch (t->justify) {
123     case Justify_Left:
124         x = position->x;
125         break;
126     case Justify_Right:
127         x = position->x + (w - m);
128         break;
129     case Justify_Center:
130         x = position->x + (w - m) / 2;
131         break;
132     }
133
134     if (t->shadow) {
135         if (t->tint >= 0) {
136             c.color.red = 0;
137             c.color.green = 0;
138             c.color.blue = 0;
139             c.color.alpha = 0xffff * t->tint / 100; /* transparent shadow */
140             c.pixel = BlackPixel(ob_display, ob_screen);
141         } else {
142             c.color.red = 0xffff * -t->tint / 100;
143             c.color.green = 0xffff * -t->tint / 100;
144             c.color.blue = 0xffff * -t->tint / 100;
145             c.color.alpha = 0xffff * -t->tint / 100; /* transparent shadow */
146             c.pixel = WhitePixel(ob_display, ob_screen);
147         }  
148         XftDrawStringUtf8(d, &c, t->font->xftfont, x + t->offset,
149                           t->font->xftfont->ascent + y + t->offset,
150                           (FcChar8*)text, l);
151     }  
152     c.color.red = t->color->r | t->color->r << 8;
153     c.color.green = t->color->g | t->color->g << 8;
154     c.color.blue = t->color->b | t->color->b << 8;
155     c.color.alpha = 0xff | 0xff << 8; /* fully opaque text */
156     c.pixel = t->color->pixel;
157                      
158     XftDrawStringUtf8(d, &c, t->font->xftfont, x,
159                       t->font->xftfont->ascent + y,
160                       (FcChar8*)text, l);
161     return;
162 }