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