]> Dogcows Code - chaz/openbox/blob - render/font.c
include stdlib for exit()
[chaz/openbox] / 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 #include <stdlib.h>
13
14 #define ELIPSES "..."
15 #define ELIPSES_LENGTH(font) \
16 (font->elipses_length + (font->shadow ? font->offset : 0))
17
18 #define OB_SHADOW "shadow"
19 #define OB_SHADOW_OFFSET "shadowoffset"
20 #define OB_SHADOW_ALPHA "shadowtint"
21
22 FcObjectType objs[] = {
23 { OB_SHADOW, FcTypeBool },
24 { OB_SHADOW_OFFSET, FcTypeInteger },
25 { OB_SHADOW_ALPHA, FcTypeInteger }
26 };
27
28 static gboolean started = FALSE;
29
30 static void font_startup(void)
31 {
32 if (!XftInit(0)) {
33 g_warning(_("Couldn't initialize Xft.\n"));
34 exit(EXIT_FAILURE);
35 }
36 FcNameRegisterObjectTypes(objs, (sizeof(objs) / sizeof(objs[0])));
37 }
38
39 static void measure_font(RrFont *f)
40 {
41 XGlyphInfo info;
42
43 /* measure an elipses */
44 XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
45 (FcChar8*)ELIPSES, strlen(ELIPSES), &info);
46 f->elipses_length = (signed) info.xOff;
47 }
48
49 static RrFont *openfont(const RrInstance *inst, char *fontstring)
50 {
51 RrFont *out;
52 FcPattern *pat, *match;
53 XftFont *font;
54 FcResult res;
55 gint tint;
56
57 if (!(pat = XftNameParse(fontstring)))
58 return NULL;
59
60 match = XftFontMatch(RrDisplay(inst), RrScreen(inst), pat, &res);
61 if (!match)
62 return NULL;
63
64 out = g_new(RrFont, 1);
65 out->inst = inst;
66
67 if (FcPatternGetBool(match, OB_SHADOW, 0, &out->shadow) != FcResultMatch)
68 out->shadow = FALSE;
69
70 if (FcPatternGetInteger(match, OB_SHADOW_OFFSET, 0, &out->offset) !=
71 FcResultMatch)
72 out->offset = 1;
73
74 if (FcPatternGetInteger(match, OB_SHADOW_ALPHA, 0, &tint) != FcResultMatch)
75 tint = 25;
76 if (tint > 100) tint = 100;
77 else if (tint < -100) tint = -100;
78 out->tint = tint;
79
80 font = XftFontOpenPattern(RrDisplay(inst), match);
81 if (!font) {
82 FcPatternDestroy(match);
83 g_free(out);
84 return NULL;
85 } else
86 out->xftfont = font;
87
88 measure_font(out);
89
90 return out;
91 }
92
93 RrFont *RrFontOpen(const RrInstance *inst, char *fontstring)
94 {
95 RrFont *out;
96
97 if (!started) {
98 font_startup();
99 started = TRUE;
100 }
101
102 if ((out = openfont(inst, fontstring)))
103 return out;
104 g_warning(_("Unable to load font: %s\n"), fontstring);
105 g_warning(_("Trying fallback font: %s\n"), "sans");
106
107 if ((out = openfont(inst, "sans")))
108 return out;
109 g_warning(_("Unable to load font: %s\n"), "sans");
110
111 return NULL;
112 }
113
114 void RrFontClose(RrFont *f)
115 {
116 if (f) {
117 XftFontClose(RrDisplay(f->inst), f->xftfont);
118 g_free(f);
119 }
120 }
121
122 static void font_measure_full(const RrFont *f, const gchar *str,
123 gint *x, gint *y)
124 {
125 XGlyphInfo info;
126
127 XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
128 (const FcChar8*)str, strlen(str), &info);
129
130 *x = (signed) info.xOff + (f->shadow ? ABS(f->offset) : 0);
131 *y = info.height + (f->shadow ? ABS(f->offset) : 0);
132 }
133
134 int RrFontMeasureString(const RrFont *f, const gchar *str)
135 {
136 gint x, y;
137 font_measure_full (f, str, &x, &y);
138 return x;
139 }
140
141 int RrFontHeight(const RrFont *f)
142 {
143 return f->xftfont->ascent + f->xftfont->descent +
144 (f->shadow ? f->offset : 0);
145 }
146
147 int RrFontMaxCharWidth(const RrFont *f)
148 {
149 return (signed) f->xftfont->max_advance_width;
150 }
151
152 void RrFontDraw(XftDraw *d, RrTextureText *t, Rect *area)
153 {
154 gint x,y,w,h;
155 XftColor c;
156 GString *text;
157 gint mw, em, mh;
158 size_t l;
159 gboolean shortened = FALSE;
160
161 /* center vertically */
162 y = area->y +
163 (area->height - RrFontHeight(t->font)) / 2;
164 /* the +2 and -4 leave a small blank edge on the sides */
165 x = area->x + 2;
166 w = area->width - 4;
167 h = area->height;
168
169 text = g_string_new(t->string);
170 l = g_utf8_strlen(text->str, -1);
171 font_measure_full(t->font, text->str, &mw, &mh);
172 while (l && mw > area->width) {
173 shortened = TRUE;
174 /* remove a character from the middle */
175 text = g_string_erase(text, l-- / 2, 1);
176 em = ELIPSES_LENGTH(t->font);
177 /* if the elipses are too large, don't show them at all */
178 if (em > area->width)
179 shortened = FALSE;
180 font_measure_full(t->font, text->str, &mw, &mh);
181 mw += em;
182 }
183 if (shortened) {
184 text = g_string_insert(text, (l + 1) / 2, ELIPSES);
185 l += 3;
186 }
187 if (!l) return;
188
189 switch (t->justify) {
190 case RR_JUSTIFY_LEFT:
191 break;
192 case RR_JUSTIFY_RIGHT:
193 x += (w - mw);
194 break;
195 case RR_JUSTIFY_CENTER:
196 x += (w - mw) / 2;
197 break;
198 }
199
200 l = strlen(text->str); /* number of bytes */
201
202 if (t->font->shadow) {
203 if (t->font->tint >= 0) {
204 c.color.red = 0;
205 c.color.green = 0;
206 c.color.blue = 0;
207 c.color.alpha = 0xffff * t->font->tint / 100;
208 c.pixel = BlackPixel(RrDisplay(t->font->inst),
209 RrScreen(t->font->inst));
210 } else {
211 c.color.red = 0xffff;
212 c.color.green = 0xffff;
213 c.color.blue = 0xffff;
214 c.color.alpha = 0xffff * -t->font->tint / 100;
215 c.pixel = WhitePixel(RrDisplay(t->font->inst),
216 RrScreen(t->font->inst));
217 }
218 XftDrawStringUtf8(d, &c, t->font->xftfont, x + t->font->offset,
219 t->font->xftfont->ascent + y + t->font->offset,
220 (FcChar8*)text->str, l);
221 }
222 c.color.red = t->color->r | t->color->r << 8;
223 c.color.green = t->color->g | t->color->g << 8;
224 c.color.blue = t->color->b | t->color->b << 8;
225 c.color.alpha = 0xff | 0xff << 8; /* fully opaque text */
226 c.pixel = t->color->pixel;
227
228 XftDrawStringUtf8(d, &c, t->font->xftfont, x,
229 t->font->xftfont->ascent + y,
230 (FcChar8*)text->str, l);
231 return;
232 }
This page took 0.046248 seconds and 5 git commands to generate.