-
Notifications
You must be signed in to change notification settings - Fork 7
/
font.c
194 lines (178 loc) · 4.23 KB
/
font.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* font handling */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "post.h"
struct font {
char name[FNLEN];
char desc[1024];
char fontname[FNLEN];
char fontpath[1024];
int spacewid;
struct glyph *gl; /* glyphs present in the font */
int gl_n, gl_sz; /* number of glyphs in the font */
struct dict *gl_dict; /* mapping from gl[i].id to i */
struct dict *ch_dict; /* charset mapping */
struct dict *ch_map; /* character aliases */
};
/* find a glyph by its name */
struct glyph *font_find(struct font *fn, char *name)
{
int i = dict_get(fn->ch_dict, name);
if (i < 0) /* maybe a character alias */
i = dict_get(fn->ch_map, name);
return i >= 0 ? fn->gl + i : NULL;
}
/* find a glyph by its device-dependent identifier */
struct glyph *font_glyph(struct font *fn, char *id)
{
int i = dict_get(fn->gl_dict, id);
return i >= 0 ? &fn->gl[i] : NULL;
}
static int font_glyphput(struct font *fn, char *id, char *name, int type)
{
struct glyph *g;
if (fn->gl_n == fn->gl_sz) {
fn->gl_sz = fn->gl_sz + 1024;
fn->gl = mextend(fn->gl, fn->gl_n, fn->gl_sz, sizeof(fn->gl[0]));
}
g = &fn->gl[fn->gl_n];
snprintf(g->id, sizeof(g->id), "%s", id);
snprintf(g->name, sizeof(g->name), "%s", name);
g->type = type;
g->font = fn;
dict_put(fn->gl_dict, g->id, fn->gl_n);
return fn->gl_n++;
}
static void tilleol(FILE *fin, char *s)
{
int c = fgetc(fin);
while (c != EOF && c != '\n') {
*s++ = c;
c = fgetc(fin);
}
*s = '\0';
if (c != EOF)
ungetc(c, fin);
}
static int font_readchar(struct font *fn, FILE *fin, int *n, int *gid)
{
struct glyph *g;
char tok[128];
char name[GNLEN];
char id[GNLEN];
int type;
if (fscanf(fin, GNFMT " %127s", name, tok) != 2)
return 1;
if (!strcmp("---", name))
sprintf(name, "c%04d", *n);
if (strcmp("\"", tok)) {
if (fscanf(fin, "%d " GNFMT, &type, id) != 2)
return 1;
*gid = font_glyphput(fn, id, name, type);
g = &fn->gl[*gid];
sscanf(tok, "%d", &g->wid);
tilleol(fin, tok);
if (sscanf(tok, "%d", &g->pos) != 1)
g->pos = 0;
dict_put(fn->ch_dict, name, *gid);
(*n)++;
} else {
dict_put(fn->ch_map, name, *gid);
}
return 0;
}
static void skipline(FILE* filp)
{
int c;
do {
c = getc(filp);
} while (c != '\n' && c != EOF);
}
struct font *font_open(char *path)
{
struct font *fn;
int ch_g = -1; /* last glyph in the charset */
int ch_n = 0; /* number of glyphs in the charset */
char tok[128];
FILE *fin;
fin = fopen(path, "r");
if (!fin)
return NULL;
fn = malloc(sizeof(*fn));
if (!fn) {
fclose(fin);
return NULL;
}
memset(fn, 0, sizeof(*fn));
snprintf(fn->desc, sizeof(fn->desc), "%s", path);
fn->gl_dict = dict_make(-1, 1, 0);
fn->ch_dict = dict_make(-1, 1, 0);
fn->ch_map = dict_make(-1, 1, 0);
while (fscanf(fin, "%127s", tok) == 1) {
if (!strcmp("char", tok)) {
font_readchar(fn, fin, &ch_n, &ch_g);
} else if (!strcmp("spacewidth", tok)) {
fscanf(fin, "%d", &fn->spacewid);
} else if (!strcmp("name", tok)) {
fscanf(fin, "%s", fn->name);
} else if (!strcmp("fontname", tok)) {
fscanf(fin, "%s", fn->fontname);
} else if (!strcmp("fontpath", tok)) {
int c = fgetc(fin);
while (c == ' ')
c = fgetc(fin);
ungetc(c, fin);
tilleol(fin, fn->fontpath);
} else if (!strcmp("ligatures", tok)) {
while (fscanf(fin, "%s", tok) == 1)
if (!strcmp("0", tok))
break;
} else if (!strcmp("charset", tok)) {
while (!font_readchar(fn, fin, &ch_n, &ch_g))
;
break;
}
skipline(fin);
}
fclose(fin);
return fn;
}
void font_close(struct font *fn)
{
dict_free(fn->gl_dict);
dict_free(fn->ch_dict);
dict_free(fn->ch_map);
free(fn->gl);
free(fn);
}
/* return width w for the given font and size */
int font_wid(struct font *fn, int sz, int w)
{
return (w * sz + dev_uwid / 2) / dev_uwid;
}
/* space width for the give word space or sentence space */
int font_swid(struct font *fn, int sz)
{
return font_wid(fn, sz, fn->spacewid);
}
char *font_name(struct font *fn)
{
return fn->fontname[0] ? fn->fontname : fn->name;
}
char *font_path(struct font *fn)
{
return fn->fontpath;
}
int font_glnum(struct font *fn, struct glyph *g)
{
return g - fn->gl;
}
struct glyph *font_glget(struct font *fn, int id)
{
return id >= 0 && id < fn->gl_n ? &fn->gl[id] : NULL;
}
char *font_desc(struct font *fn)
{
return fn->desc;
}