-
Notifications
You must be signed in to change notification settings - Fork 1
/
text.c
175 lines (137 loc) · 4.13 KB
/
text.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* cocowm - Column Commander Window Manager for X11 Window System
* Copyright (c) 2023, Tommi Leino <namhas@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "extern.h"
#include <err.h>
static XftFont *ftfont;
static int space_width;
#define TABWIDTH 8
void
font_extents(Display *dpy, const char *text, size_t len, XGlyphInfo *extents)
{
XftTextExtentsUtf8(dpy, ftfont,
(const FcChar8 *) text, len, extents);
}
#define NUM_COLOR 6
static XColor color[NUM_COLOR];
static int has_color[NUM_COLOR];
static const char *colorname[] = {
"black",
"cornflower blue",
"gray",
"red"
};
XColor
query_color(Display *dpy, int i)
{
XColor def, exact;
assert(i < NUM_COLOR);
if (has_color[i])
return color[i];
if (XAllocNamedColor(dpy, XDefaultColormap(dpy,
DefaultScreen(dpy)), colorname[i], &def, &exact) == 0)
errx(1, "couldn't allocate '%s'", colorname[i]);
color[i] = def;
has_color[i] = 1;
return color[i];
}
void
set_ftcolor(Display *dpy, XftColor *dst, int color)
{
XColor xcolor;
xcolor = query_color(dpy, color);
dst->pixel = xcolor.pixel;
dst->color.red = xcolor.red;
dst->color.green = xcolor.green;
dst->color.blue = xcolor.blue;
dst->color.alpha = USHRT_MAX;
}
#ifndef DEFAULT_FONT
#define DEFAULT_FONT "DejaVu Sans Mono:size=12.0"
#endif
#ifndef FALLBACK_FONT
#define FALLBACK_FONT "-misc-fixed-medium-r-normal--20-200-75-75-c-100-iso10646-1"
#endif
XftFont *
font_load(Display *dpy, char *fontname)
{
XGlyphInfo extents;
if (fontname == NULL || fontname[0] == '\0')
fontname = DEFAULT_FONT;
/*
* First try Xlfd form, then Xft font name form.
*/
ftfont = XftFontOpenXlfd(dpy, DefaultScreen(dpy), fontname);
if (ftfont == NULL)
ftfont = XftFontOpenName(dpy, DefaultScreen(dpy),
fontname);
/*
* No success? Try the fallback font.
*/
if (ftfont == NULL && strcmp(fontname, FALLBACK_FONT) != 0) {
warnx("couldn't load font: %s", fontname);
return font_load(dpy, FALLBACK_FONT);
} else if (ftfont == NULL)
errx(1, "couldn't load fallback font: %s", fontname);
warnx("Font: %s", fontname);
font_extents(dpy, " ", 1, &extents);
space_width = extents.xOff;
warnx("Spacewidth: %d", extents.xOff);
return ftfont;
}
void
font_clear(XftDraw *ftdraw, Display *dpy, Window window, XftColor bg, int x, int y, int width)
{
XftDrawRect(ftdraw, &bg, x, y, width, ftfont->height);
}
static int
_font_draw(XftDraw *ftdraw, Display *dpy, Window window, XftColor fg, XftColor bg, int x, int y,
const char *text, size_t len)
{
XGlyphInfo extents;
font_extents(dpy, text, len, &extents);
XftDrawRect(ftdraw, &bg, x, y, extents.xOff,
ftfont->height);
XftDrawStringUtf8(ftdraw, &fg, ftfont, x,
y + ftfont->ascent, (const FcChar8 *) text, len);
return extents.xOff;
}
#include <ctype.h>
int
font_draw(XftDraw *ftdraw, Display *dpy, Window window, XftColor fg, XftColor bg, int x, int sx, int y, const char *text, size_t len)
{
size_t i, j;
int x_out, tabstop, tabwidth, remaining;
x_out = 0;
j = 0;
for (i = 0; i < len; i++) {
if (text[i] == '\t') {
if (j!=i)
x_out += _font_draw(ftdraw, dpy, window, fg, bg, sx+x_out, y,
&text[j], i-j);
j=i+1;
tabwidth = space_width * TABWIDTH;
tabstop = ((x+x_out) / tabwidth);
remaining = tabwidth - ((x+x_out) -
(tabstop * tabwidth));
font_clear(ftdraw, dpy, window, bg, sx+x_out, y, remaining);
x_out += remaining;
}
}
if (j < len)
x_out += _font_draw(ftdraw, dpy, window, fg, bg, sx+x_out, y, &text[j], i-j);
return x_out;
}