]> Dogcows Code - chaz/openbox/blob - render/font.c
destroy 'pat' always
[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."));
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 FcPatternDestroy(pat);
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 g_message("freeing %p", 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 + 4;
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, RrRect *area)
153 {
154 gint x,y,w,h;
155 XftColor c;
156 GString *text;
157 gint mw, 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 /* if the elipses are too large, don't show them at all */
177 if (ELIPSES_LENGTH(t->font) > area->width)
178 shortened = FALSE;
179 font_measure_full(t->font, text->str, &mw, &mh);
180 mw += ELIPSES_LENGTH(t->font);
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
231 g_string_free(text, TRUE);
232 return;
233 }
This page took 0.046616 seconds and 5 git commands to generate.