]> Dogcows Code - chaz/openbox/blob - render/font.c
oops typo
[chaz/openbox] / render / font.c
1 #include "font.h"
2 #include "theme.h"
3 #include "kernel/openbox.h"
4 #include "kernel/geom.h"
5 #include "kernel/gettext.h"
6 #define _(str) gettext(str)
7
8 #include <X11/Xft/Xft.h>
9 #include <glib.h>
10 #include <string.h>
11
12 #define ELIPSES "..."
13 #define ELIPSES_LENGTH(font, shadow, offset) \
14 (font->elipses_length + (shadow ? offset : 0))
15
16 void font_startup(void)
17 {
18 #ifdef DEBUG
19 int version;
20 #endif /* DEBUG */
21 if (!XftInit(0)) {
22 g_warning(_("Couldn't initialize Xft.\n"));
23 exit(3);
24 }
25 #ifdef DEBUG
26 version = XftGetVersion();
27 g_message("Using Xft %d.%d.%d (Built against %d.%d.%d).",
28 version / 10000 % 100, version / 100 % 100, version % 100,
29 XFT_MAJOR, XFT_MINOR, XFT_REVISION);
30 #endif
31 }
32
33 static void measure_height(ObFont *f)
34 {
35 XGlyphInfo info;
36
37 /* measure an elipses */
38 XftTextExtentsUtf8(ob_display, f->xftfont,
39 (FcChar8*)ELIPSES, strlen(ELIPSES), &info);
40 f->elipses_length = (signed) info.xOff;
41 }
42
43 ObFont *font_open(char *fontstring)
44 {
45 ObFont *out;
46 XftFont *xf;
47
48 if ((xf = XftFontOpenName(ob_display, ob_screen, fontstring))) {
49 out = g_new(ObFont, 1);
50 out->xftfont = xf;
51 measure_height(out);
52 return out;
53 }
54 g_warning(_("Unable to load font: %s\n"), fontstring);
55 g_warning(_("Trying fallback font: %s\n"), "sans");
56
57 if ((xf = XftFontOpenName(ob_display, ob_screen, "sans"))) {
58 out = g_new(ObFont, 1);
59 out->xftfont = xf;
60 measure_height(out);
61 return out;
62 }
63 g_warning(_("Unable to load font: %s\n"), "sans");
64 g_warning(_("Aborting!.\n"));
65
66 exit(3); /* can't continue without a font */
67 }
68
69 void font_close(ObFont *f)
70 {
71 if (f) {
72 XftFontClose(ob_display, f->xftfont);
73 g_free(f);
74 }
75 }
76
77 void font_measure_full(ObFont *f, char *str, int shadow, int offset,
78 int *x, int *y)
79 {
80 XGlyphInfo info;
81
82 XftTextExtentsUtf8(ob_display, f->xftfont,
83 (FcChar8*)str, strlen(str), &info);
84
85 *x = (signed) info.xOff + (shadow ? ABS(offset) : 0);
86 *y = info.height + (shadow ? ABS(offset) : 0);
87 }
88
89 int font_measure_string(ObFont *f, char *str, int shadow, int offset)
90 {
91 int x, y;
92 font_measure_full (f, str, shadow, offset, &x, &y);
93 return x;
94 }
95
96 int font_height(ObFont *f, int shadow, int offset)
97 {
98 return f->xftfont->ascent + f->xftfont->descent + (shadow ? offset : 0);
99 }
100
101 int font_max_char_width(ObFont *f)
102 {
103 return (signed) f->xftfont->max_advance_width;
104 }
105
106 void font_draw(XftDraw *d, TextureText *t, Rect *area)
107 {
108 int x,y,w,h;
109 XftColor c;
110 GString *text;
111 int mw, em, mh;
112 size_t l;
113 gboolean shortened = FALSE;
114
115 /* center vertically */
116 y = area->y +
117 (area->height - font_height(t->font, t->shadow, t->offset)) / 2;
118 w = area->width - theme_bevel * 2;
119 h = area->height;
120
121 text = g_string_new(t->string);
122 l = g_utf8_strlen(text->str, -1);
123 font_measure_full(t->font, text->str, t->shadow, t->offset, &mw, &mh);
124 while (l && mw > area->width) {
125 shortened = TRUE;
126 /* remove a character from the middle */
127 text = g_string_erase(text, l-- / 2, 1);
128 em = ELIPSES_LENGTH(t->font, t->shadow, t->offset);
129 /* if the elipses are too large, don't show them at all */
130 if (em > area->width)
131 shortened = FALSE;
132 font_measure_full(t->font, text->str, t->shadow, t->offset, &mw, &mh);
133 mw += em;
134 }
135 if (shortened) {
136 text = g_string_insert(text, (l + 1) / 2, ELIPSES);
137 l += 3;
138 }
139 if (!l) return;
140
141 switch (t->justify) {
142 case Justify_Left:
143 x = area->x + theme_bevel;
144 break;
145 case Justify_Right:
146 x = area->x + (w - mw) - theme_bevel;
147 break;
148 case Justify_Center:
149 x = area->x + (w - mw) / 2;
150 break;
151 }
152
153 l = strlen(text->str); /* number of bytes */
154
155 if (t->shadow) {
156 if (t->tint >= 0) {
157 c.color.red = 0;
158 c.color.green = 0;
159 c.color.blue = 0;
160 c.color.alpha = 0xffff * t->tint / 100; /* transparent shadow */
161 c.pixel = BlackPixel(ob_display, ob_screen);
162 } else {
163 c.color.red = 0xffff * -t->tint / 100;
164 c.color.green = 0xffff * -t->tint / 100;
165 c.color.blue = 0xffff * -t->tint / 100;
166 c.color.alpha = 0xffff * -t->tint / 100; /* transparent shadow */
167 c.pixel = WhitePixel(ob_display, ob_screen);
168 }
169 XftDrawStringUtf8(d, &c, t->font->xftfont, x + t->offset,
170 t->font->xftfont->ascent + y + t->offset,
171 (FcChar8*)text->str, l);
172 }
173 c.color.red = t->color->r | t->color->r << 8;
174 c.color.green = t->color->g | t->color->g << 8;
175 c.color.blue = t->color->b | t->color->b << 8;
176 c.color.alpha = 0xff | 0xff << 8; /* fully opaque text */
177 c.pixel = t->color->pixel;
178
179 XftDrawStringUtf8(d, &c, t->font->xftfont, x,
180 t->font->xftfont->ascent + y,
181 (FcChar8*)text->str, l);
182 return;
183 }
This page took 0.043884 seconds and 5 git commands to generate.