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