]> Dogcows Code - chaz/openbox/blob - render/font.c
convert shit from utf8 to filename before execing it
[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 char *str;
36
37 /* XXX add some extended UTF8 characters in here? */
38 str = "12345678900-qwertyuiopasdfghjklzxcvbnm"
39 "!@#$%^&*()_+QWERTYUIOPASDFGHJKLZXCVBNM"
40 "`~[]\\;',./{}|:\"<>?";
41
42 XftTextExtentsUtf8(ob_display, f->xftfont,
43 (FcChar8*)str, strlen(str), &info);
44 f->height = (signed) info.height;
45
46 /* measure an elipses */
47 XftTextExtentsUtf8(ob_display, f->xftfont,
48 (FcChar8*)ELIPSES, strlen(ELIPSES), &info);
49 f->elipses_length = (signed) info.xOff;
50 }
51
52 ObFont *font_open(char *fontstring)
53 {
54 ObFont *out;
55 XftFont *xf;
56
57 if ((xf = XftFontOpenName(ob_display, ob_screen, fontstring))) {
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"), fontstring);
64 g_warning(_("Trying fallback font: %s\n"), "sans");
65
66 if ((xf = XftFontOpenName(ob_display, ob_screen, "sans"))) {
67 out = g_new(ObFont, 1);
68 out->xftfont = xf;
69 measure_height(out);
70 return out;
71 }
72 g_warning(_("Unable to load font: %s\n"), "sans");
73 g_warning(_("Aborting!.\n"));
74
75 exit(3); /* can't continue without a font */
76 }
77
78 void font_close(ObFont *f)
79 {
80 if (f) {
81 XftFontClose(ob_display, f->xftfont);
82 g_free(f);
83 }
84 }
85
86 int font_measure_string(ObFont *f, char *str, int shadow, int offset)
87 {
88 XGlyphInfo info;
89
90 XftTextExtentsUtf8(ob_display, f->xftfont,
91 (FcChar8*)str, strlen(str), &info);
92
93 return (signed) info.xOff + (shadow ? offset : 0);
94 }
95
96 int font_height(ObFont *f, int shadow, int offset)
97 {
98 return f->height + (shadow ? offset : 0);
99 }
100
101 int font_max_char_width(ObFont *f)
102 {
103 return (signed) f->xftfont->max_advance_width;
104 }
105
106 void font_draw(XftDraw *d, TextureText *t, Rect *position)
107 {
108 int x,y,w,h;
109 XftColor c;
110 GString *text;
111 int m, em;
112 size_t l;
113 gboolean shortened = FALSE;
114
115 y = position->y;
116 w = position->width;
117 h = position->height;
118
119 /* accomidate for areas bigger/smaller than Xft thinks the font is tall */
120 y -= (2 * (t->font->xftfont->ascent + t->font->xftfont->descent) -
121 (t->font->height + h) - 1) / 2;
122
123 text = g_string_new(t->string);
124 l = g_utf8_strlen(text->str, -1);
125 m = font_measure_string(t->font, text->str, t->shadow, t->offset);
126 while (l && m > position->width) {
127 shortened = TRUE;
128 /* remove a character from the middle */
129 text = g_string_erase(text, l-- / 2, 1);
130 em = ELIPSES_LENGTH(t->font, t->shadow, t->offset);
131 /* if the elipses are too large, don't show them at all */
132 if (em > position->width)
133 shortened = FALSE;
134 m = font_measure_string(t->font, text->str, t->shadow, t->offset) + em;
135 }
136 if (shortened) {
137 text = g_string_insert(text, (l + 1) / 2, ELIPSES);
138 l += 3;
139 }
140 if (!l) return;
141
142 switch (t->justify) {
143 case Justify_Left:
144 x = position->x;
145 break;
146 case Justify_Right:
147 x = position->x + (w - m);
148 break;
149 case Justify_Center:
150 x = position->x + (w - m) / 2;
151 break;
152 }
153
154 l = strlen(text->str); /* number of bytes */
155
156 if (t->shadow) {
157 if (t->tint >= 0) {
158 c.color.red = 0;
159 c.color.green = 0;
160 c.color.blue = 0;
161 c.color.alpha = 0xffff * t->tint / 100; /* transparent shadow */
162 c.pixel = BlackPixel(ob_display, ob_screen);
163 } else {
164 c.color.red = 0xffff * -t->tint / 100;
165 c.color.green = 0xffff * -t->tint / 100;
166 c.color.blue = 0xffff * -t->tint / 100;
167 c.color.alpha = 0xffff * -t->tint / 100; /* transparent shadow */
168 c.pixel = WhitePixel(ob_display, ob_screen);
169 }
170 XftDrawStringUtf8(d, &c, t->font->xftfont, x + t->offset,
171 t->font->xftfont->ascent + y + t->offset,
172 (FcChar8*)text->str, l);
173 }
174 c.color.red = t->color->r | t->color->r << 8;
175 c.color.green = t->color->g | t->color->g << 8;
176 c.color.blue = t->color->b | t->color->b << 8;
177 c.color.alpha = 0xff | 0xff << 8; /* fully opaque text */
178 c.pixel = t->color->pixel;
179
180 XftDrawStringUtf8(d, &c, t->font->xftfont, x,
181 t->font->xftfont->ascent + y,
182 (FcChar8*)text->str, l);
183 return;
184 }
This page took 0.039342 seconds and 4 git commands to generate.