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