]> Dogcows Code - chaz/openbox/blob - render/font.c
more namespacing with Rr*
[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, shadow, offset) \
15 (font->elipses_length + (shadow ? offset : 0))
16
17 static gboolean started = FALSE;
18
19 static void font_startup(void)
20 {
21 #ifdef DEBUG
22 int version;
23 #endif /* DEBUG */
24 if (!XftInit(0)) {
25 g_warning(_("Couldn't initialize Xft.\n"));
26 exit(3);
27 }
28 #ifdef DEBUG
29 version = XftGetVersion();
30 g_message("Using Xft %d.%d.%d (Built against %d.%d.%d).",
31 version / 10000 % 100, version / 100 % 100, version % 100,
32 XFT_MAJOR, XFT_MINOR, XFT_REVISION);
33 #endif
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 RrFont *RrFontOpen(const RrInstance *inst, char *fontstring)
47 {
48 RrFont *out;
49 XftFont *xf;
50
51 if (!started) {
52 font_startup();
53 started = TRUE;
54 }
55
56 if ((xf = XftFontOpenName(RrDisplay(inst), RrScreen(inst), fontstring))) {
57 out = g_new(RrFont, 1);
58 out->inst = inst;
59 out->xftfont = xf;
60 measure_font(out);
61 return out;
62 }
63 g_warning(_("Unable to load font: %s\n"), fontstring);
64 g_warning(_("Trying fallback font: %s\n"), "sans");
65
66 if ((xf = XftFontOpenName(RrDisplay(inst), RrScreen(inst), "sans"))) {
67 out = g_new(RrFont, 1);
68 out->inst = inst;
69 out->xftfont = xf;
70 measure_font(out);
71 return out;
72 }
73 g_warning(_("Unable to load font: %s\n"), "sans");
74 g_warning(_("Aborting!.\n"));
75
76 exit(3); /* can't continue without a font */
77 }
78
79 void RrFontClose(RrFont *f)
80 {
81 if (f) {
82 XftFontClose(RrDisplay(f->inst), f->xftfont);
83 g_free(f);
84 }
85 }
86
87 static void font_measure_full(const RrFont *f, const gchar *str,
88 gint shadow, gint offset, gint *x, gint *y)
89 {
90 XGlyphInfo info;
91
92 XftTextExtentsUtf8(RrDisplay(f->inst), f->xftfont,
93 (const FcChar8*)str, strlen(str), &info);
94
95 *x = (signed) info.xOff + (shadow ? ABS(offset) : 0);
96 *y = info.height + (shadow ? ABS(offset) : 0);
97 }
98
99 int RrFontMeasureString(const RrFont *f, const gchar *str,
100 gint shadow, gint offset)
101 {
102 gint x, y;
103 font_measure_full (f, str, shadow, offset, &x, &y);
104 return x;
105 }
106
107 int RrFontHeight(const RrFont *f, gint shadow, gint offset)
108 {
109 return f->xftfont->ascent + f->xftfont->descent + (shadow ? offset : 0);
110 }
111
112 int RrFontMaxCharWidth(const RrFont *f)
113 {
114 return (signed) f->xftfont->max_advance_width;
115 }
116
117 void RrFontDraw(XftDraw *d, RrTextureText *t, Rect *area)
118 {
119 gint x,y,w,h;
120 XftColor c;
121 GString *text;
122 gint mw, em, mh;
123 size_t l;
124 gboolean shortened = FALSE;
125
126 /* center vertically */
127 y = area->y +
128 (area->height - RrFontHeight(t->font, t->shadow, t->offset)) / 2;
129 w = area->width;
130 h = area->height;
131
132 text = g_string_new(t->string);
133 l = g_utf8_strlen(text->str, -1);
134 font_measure_full(t->font, text->str, t->shadow, t->offset, &mw, &mh);
135 while (l && mw > area->width) {
136 shortened = TRUE;
137 /* remove a character from the middle */
138 text = g_string_erase(text, l-- / 2, 1);
139 em = ELIPSES_LENGTH(t->font, t->shadow, t->offset);
140 /* if the elipses are too large, don't show them at all */
141 if (em > area->width)
142 shortened = FALSE;
143 font_measure_full(t->font, text->str, t->shadow, t->offset, &mw, &mh);
144 mw += em;
145 }
146 if (shortened) {
147 text = g_string_insert(text, (l + 1) / 2, ELIPSES);
148 l += 3;
149 }
150 if (!l) return;
151
152 switch (t->justify) {
153 case RR_JUSTIFY_LEFT:
154 x = area->x;
155 break;
156 case RR_JUSTIFY_RIGHT:
157 x = area->x + (w - mw);
158 break;
159 case RR_JUSTIFY_CENTER:
160 x = area->x + (w - mw) / 2;
161 break;
162 }
163
164 l = strlen(text->str); /* number of bytes */
165
166 if (t->shadow) {
167 if (t->tint >= 0) {
168 c.color.red = 0;
169 c.color.green = 0;
170 c.color.blue = 0;
171 c.color.alpha = 0xffff * t->tint / 100; /* transparent shadow */
172 c.pixel = BlackPixel(RrDisplay(t->font->inst),
173 RrScreen(t->font->inst));
174 } else {
175 c.color.red = 0xffff * -t->tint / 100;
176 c.color.green = 0xffff * -t->tint / 100;
177 c.color.blue = 0xffff * -t->tint / 100;
178 c.color.alpha = 0xffff * -t->tint / 100; /* transparent shadow */
179 c.pixel = WhitePixel(RrDisplay(t->font->inst),
180 RrScreen(t->font->inst));
181 }
182 XftDrawStringUtf8(d, &c, t->font->xftfont, x + t->offset,
183 t->font->xftfont->ascent + y + t->offset,
184 (FcChar8*)text->str, l);
185 }
186 c.color.red = t->color->r | t->color->r << 8;
187 c.color.green = t->color->g | t->color->g << 8;
188 c.color.blue = t->color->b | t->color->b << 8;
189 c.color.alpha = 0xff | 0xff << 8; /* fully opaque text */
190 c.pixel = t->color->pixel;
191
192 XftDrawStringUtf8(d, &c, t->font->xftfont, x,
193 t->font->xftfont->ascent + y,
194 (FcChar8*)text->str, l);
195 return;
196 }
This page took 0.043717 seconds and 5 git commands to generate.