]> Dogcows Code - chaz/openbox/blob - render/font.c
center text vertically. use the font's height because all that space is needed for...
[chaz/openbox] / render / font.c
1 #include "font.h"
2 #include "kernel/openbox.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 void font_startup(void)
16 {
17 #ifdef DEBUG
18 int version;
19 #endif /* DEBUG */
20 if (!XftInit(0)) {
21 g_warning(_("Couldn't initialize Xft.\n"));
22 exit(3);
23 }
24 #ifdef DEBUG
25 version = XftGetVersion();
26 g_message("Using Xft %d.%d.%d (Built against %d.%d.%d).",
27 version / 10000 % 100, version / 100 % 100, version % 100,
28 XFT_MAJOR, XFT_MINOR, XFT_REVISION);
29 #endif
30 }
31
32 static void measure_height(ObFont *f)
33 {
34 XGlyphInfo info;
35
36 /* measure an elipses */
37 XftTextExtentsUtf8(ob_display, f->xftfont,
38 (FcChar8*)ELIPSES, strlen(ELIPSES), &info);
39 f->elipses_length = (signed) info.xOff;
40 }
41
42 ObFont *font_open(char *fontstring)
43 {
44 ObFont *out;
45 XftFont *xf;
46
47 if ((xf = XftFontOpenName(ob_display, ob_screen, fontstring))) {
48 out = g_new(ObFont, 1);
49 out->xftfont = xf;
50 measure_height(out);
51 return out;
52 }
53 g_warning(_("Unable to load font: %s\n"), fontstring);
54 g_warning(_("Trying fallback font: %s\n"), "sans");
55
56 if ((xf = XftFontOpenName(ob_display, ob_screen, "sans"))) {
57 out = g_new(ObFont, 1);
58 out->xftfont = xf;
59 measure_height(out);
60 return out;
61 }
62 g_warning(_("Unable to load font: %s\n"), "sans");
63 g_warning(_("Aborting!.\n"));
64
65 exit(3); /* can't continue without a font */
66 }
67
68 void font_close(ObFont *f)
69 {
70 if (f) {
71 XftFontClose(ob_display, f->xftfont);
72 g_free(f);
73 }
74 }
75
76 void font_measure_full(ObFont *f, char *str, int shadow, int offset,
77 int *x, int *y)
78 {
79 XGlyphInfo info;
80
81 XftTextExtentsUtf8(ob_display, f->xftfont,
82 (FcChar8*)str, strlen(str), &info);
83
84 *x = (signed) info.xOff + (shadow ? ABS(offset) : 0);
85 *y = info.height + (shadow ? ABS(offset) : 0);
86 }
87
88 int font_measure_string(ObFont *f, char *str, int shadow, int offset)
89 {
90 int x, y;
91 font_measure_full (f, str, shadow, offset, &x, &y);
92 return x;
93 }
94
95 int font_height(ObFont *f, int shadow, int offset)
96 {
97 return f->xftfont->ascent + f->xftfont->descent + (shadow ? offset : 0);
98 }
99
100 int font_max_char_width(ObFont *f)
101 {
102 return (signed) f->xftfont->max_advance_width;
103 }
104
105 void font_draw(XftDraw *d, TextureText *t, Rect *position)
106 {
107 int x,y,w,h;
108 XftColor c;
109 GString *text;
110 int mw, em, mh;
111 size_t l;
112 gboolean shortened = FALSE;
113
114 y = position->y;
115 w = position->width;
116 h = position->height;
117
118 text = g_string_new(t->string);
119 l = g_utf8_strlen(text->str, -1);
120 font_measure_full(t->font, text->str, t->shadow, t->offset, &mw, &mh);
121 while (l && mw > position->width) {
122 shortened = TRUE;
123 /* remove a character from the middle */
124 text = g_string_erase(text, l-- / 2, 1);
125 em = ELIPSES_LENGTH(t->font, t->shadow, t->offset);
126 /* if the elipses are too large, don't show them at all */
127 if (em > position->width)
128 shortened = FALSE;
129 font_measure_full(t->font, text->str, t->shadow, t->offset, &mw, &mh);
130 mw += em;
131 }
132 if (shortened) {
133 text = g_string_insert(text, (l + 1) / 2, ELIPSES);
134 l += 3;
135 }
136 if (!l) return;
137
138 /* center vertically */
139 y -= ((t->font->xftfont->ascent + t->font->xftfont->descent) - mh) / 2;
140
141 switch (t->justify) {
142 case Justify_Left:
143 x = position->x;
144 break;
145 case Justify_Right:
146 x = position->x + (w - mw);
147 break;
148 case Justify_Center:
149 x = position->x + (w - mw) / 2;
150 break;
151 }
152
153 l = strlen(text->str); /* number of bytes */
154
155 if (t->shadow) {
156 if (t->tint >= 0) {
157 c.color.red = 0;
158 c.color.green = 0;
159 c.color.blue = 0;
160 c.color.alpha = 0xffff * t->tint / 100; /* transparent shadow */
161 c.pixel = BlackPixel(ob_display, ob_screen);
162 } else {
163 c.color.red = 0xffff * -t->tint / 100;
164 c.color.green = 0xffff * -t->tint / 100;
165 c.color.blue = 0xffff * -t->tint / 100;
166 c.color.alpha = 0xffff * -t->tint / 100; /* transparent shadow */
167 c.pixel = WhitePixel(ob_display, ob_screen);
168 }
169 XftDrawStringUtf8(d, &c, t->font->xftfont, x + t->offset,
170 t->font->xftfont->ascent + y + t->offset,
171 (FcChar8*)text->str, l);
172 }
173 c.color.red = t->color->r | t->color->r << 8;
174 c.color.green = t->color->g | t->color->g << 8;
175 c.color.blue = t->color->b | t->color->b << 8;
176 c.color.alpha = 0xff | 0xff << 8; /* fully opaque text */
177 c.pixel = t->color->pixel;
178
179 XftDrawStringUtf8(d, &c, t->font->xftfont, x,
180 t->font->xftfont->ascent + y,
181 (FcChar8*)text->str, l);
182 return;
183 }
This page took 0.045833 seconds and 5 git commands to generate.