-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathunifont.c
152 lines (133 loc) · 2.49 KB
/
unifont.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
#include <stdio.h>
#include <stdlib.h>
#include <zlib.h>
struct unifont
{
int count;
unsigned short *low;
unsigned short *high;
unsigned char *width;
int *offset;
unsigned char *data;
};
static inline int read32(FILE *f)
{
int a = getc(f);
int b = getc(f);
int c = getc(f);
int d = getc(f);
return a << 24 | b << 16 | c << 8 | d;
}
static inline int read16(FILE *f)
{
int a = getc(f);
int b = getc(f);
return a << 8 | b;
}
struct unifont *
ui_load_unifont(char *filename)
{
struct unifont *u;
unsigned char *cdata;
int count, csize;
uLongf usize;
int i;
FILE *f;
f = fopen(filename, "rb");
if (!f)
return NULL;
u = malloc(sizeof(struct unifont));
count = read32(f);
csize = read32(f);
usize = read32(f);
u->count = count;
u->low = malloc(count * sizeof(unsigned short));
u->high = malloc(count * sizeof(unsigned short));
u->width = malloc(count * sizeof(unsigned char));
u->offset = malloc(count * sizeof(int));
u->data = malloc(usize);
for (i = 0; i < u->count; ++i) {
u->low[i] = read16(f);
u->high[i] = read16(f);
u->width[i] = getc(f);
u->offset[i] = read32(f);
}
cdata = malloc(csize);
fread(cdata, 1, csize, f);
uncompress(u->data, &usize, cdata, csize);
free(cdata);
fclose(f);
return u;
}
static int
ui_find_unifont_range(struct unifont *u, int ucs)
{
int l = 0;
int r = u->count - 1;
int m;
while (l <= r) {
m = (l + r) >> 1;
if (ucs < u->low[m])
r = m - 1;
else if (ucs > u->high[m])
l = m + 1;
else
return m;
}
return -1;
}
int
ui_measure_unifont_char(struct unifont *u, int ucs)
{
int m = ui_find_unifont_range(u, ucs);
if (m > -1)
return u->width[m] * 8;
return 0;
}
static void
ui_draw_unifont_glyph(unsigned char *p, int w, int dx, int dy)
{
int x, k, y;
for (y = 0; y < 16; ++y) {
for (k = 0; k < w; ++k) {
for (x = 0; x < 8; ++x) {
if ((*p >> (7 - x)) & 1) {
putchar('#');
putchar('#');
} else {
putchar('.');
putchar('.');
}
}
++p;
}
putchar('\n');
}
}
int
ui_draw_unifont_char(struct unifont *u, int ucs, int x, int y)
{
int m = ui_find_unifont_range(u, ucs);
if (m > -1) {
int i = ucs - u->low[m];
int w = u->width[m];
unsigned char *p = u->data + u->offset[m] + i * w * 16;
ui_draw_unifont_glyph(p, w, x, y);
return w * 8;
}
return 0;
}
int
main(int argc, char **argv)
{
struct unifont *u;
int c, i;
u = ui_load_unifont("unifont.dat");
for (i = 1; i < argc; ++i)
{
c = strtol(argv[i], NULL, 16);
ui_draw_unifont_char(u, c, 0, 0);
putchar('\n');
}
return 0;
}