summaryrefslogtreecommitdiff
path: root/test/oldfminit.c
blob: 99726c16c26910ddfaa61a2c0610031ade55e338 (plain)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <ftw.h>
#include <libgen.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "rufl.h"

/* dirty! */
#include "../src/rufl_internal.h"

#include "harness.h"
#include "testutils.h"

struct expumap {
	char *fontname;
	size_t num_umaps;
};

struct cfg {
	const char *datadir;

	struct expumap *expumaps;
	size_t n_expumaps;
};

static char template[] = "/tmp/oldfminitXXXXXX";
static const char *ptmp = NULL;
static struct cfg cfg;

static int ftw_cb(const char *path, const struct stat *sb,
		int typeflag, struct FTW *ftwbuf)
{
	(void) sb;
	(void) typeflag;
	(void) ftwbuf;

	remove(path);

	return 0;
}

static void cleanup(void)
{
	if (cfg.expumaps != NULL) {
		size_t i;

		for (i = 0; i < cfg.n_expumaps; i++) {
			free(cfg.expumaps[i].fontname);
		}
		free(cfg.expumaps);
	}

	if (ptmp == NULL)
		return;

	nftw(ptmp, ftw_cb, FOPEN_MAX, FTW_DEPTH | FTW_MOUNT | FTW_PHYS);
}

static void parse_cfg(const char *path, struct cfg *cfg,
		void (*cb)(struct cfg *cfg, const char *line, size_t len))
{
	FILE *fp;
	char wbuf[4096];
	size_t nleft = 0;

	fp = fopen(path, "r");
	assert(fp != NULL);

	while (!feof(fp)) {
		char buf[2048];
		size_t nread;
		const char *p, *s;

		nread = fread(buf, 1, sizeof(buf), fp);
		if (nread != sizeof(buf)) {
			assert(ferror(fp) == 0);
		}

		memcpy(wbuf + nleft, buf, nread);
		nleft += nread;

		for (p = s = wbuf; p < wbuf + nleft; p++) {
			if (*p == '\n') {
				cb(cfg, s, p - s);
				s = p+1;
			}
		}
		if (s != wbuf + nleft) {
			memmove(wbuf, s, p - s);
			nleft = p - s;
		} else {
			nleft = 0;
		}
		assert(nleft < sizeof(buf));
	}
	assert(nleft == 0);

	fclose(fp);
}

static void parse_expumaps(struct cfg *cfg, char *data, size_t len)
{
	char *p, *s;
	const char *font = NULL, *count = NULL;
	struct expumap *e;
	size_t num_umaps;

	for (p = s = data; p < data+len; p++) {
		if (*p == ' ' || *p == '\t') {
			*p = '\0';
			if (s != p) {
				if (font == NULL)
					font = s;
				else if (count == NULL)
					count = s;
			}
			s = p+1;
		}
	}
	if (count == NULL)
		count = s;

	num_umaps = strtoul(count, &p, 10);
	assert((size_t)(p-count) == strlen(count));

	e = realloc(cfg->expumaps, (cfg->n_expumaps + 1) * sizeof(*e));
	assert(e != NULL);

	cfg->expumaps = e;
	cfg->expumaps[cfg->n_expumaps].fontname = strdup(font);
	assert(cfg->expumaps[cfg->n_expumaps].fontname != NULL);
	cfg->expumaps[cfg->n_expumaps].num_umaps = num_umaps;
	cfg->n_expumaps++;
}

static void parse_directive(struct cfg *cfg, char *linecpy, size_t len)
{
	char *p, *s;
	const char *directive = NULL;

	for (p = s = linecpy; p < linecpy+len; p++) {
		if (*p == ' ' || *p == '\t') {
			*p = '\0';
			if (s != p && directive == NULL) {
				directive = s;
				s = p+1;
				break;
			}
			s = p+1;
		}
	}
	if (directive == NULL)
		directive = s;

	if (strcmp("\%expumaps", directive) == 0) {
		parse_expumaps(cfg, s, len - (s - linecpy));
	}
}

static void parse_encoding(struct cfg *cfg, char *linecpy, size_t len)
{
	char *p, *s;
	const char *font = NULL, *encoding = NULL, *file = NULL;
	char *path;

	for (p = s = linecpy; p < linecpy+len; p++) {
		if (*p == ' ' || *p == '\t') {
			*p = '\0';
			if (s != p) {
				if (font == NULL)
					font = s;
				else if (encoding == NULL)
					encoding = s;
				else if (file == NULL)
					file = s;
			}
			s = p+1;
		}
	}
	if (file == NULL)
		file = s;

	assert(font != NULL);
	assert(encoding != NULL);
	assert(file != NULL);

	path = malloc(strlen(cfg->datadir) + strlen(file) + 2);
	assert(path != NULL);
	strcpy(path, cfg->datadir);
	path[strlen(cfg->datadir)] = '/'; //XXX: platform-agnostic dirsep?
	strcpy(path+strlen(cfg->datadir)+1, file);

	rufl_test_harness_set_font_encoding(font, encoding, path);

	free(path);
}

static void line_cb(struct cfg *cfg, const char *line, size_t len)
{
	char *linecpy;

	if (len == 0 || line[0] == '#')
		return;

	linecpy = malloc(len + 1);
	assert(linecpy != NULL);
	memcpy(linecpy, line, len);
	linecpy[len] = '\0';

	if (line[0] == '%')
		parse_directive(cfg, linecpy, len);
	else
		parse_encoding(cfg, linecpy, len);

	free(linecpy);
}

static void read_config(const char *path, struct cfg *cfg)
{
	char *pathcpy;

	pathcpy = strdup(path);
	assert(pathcpy != NULL);

	cfg->datadir = dirname(pathcpy);

	parse_cfg(path, cfg, line_cb);

	free(pathcpy);
	cfg->datadir = NULL;
}

int main(int argc, const char **argv)
{
	int width, x;
	size_t offset;
	int32_t xkern, ykern, italic, ascent, descent, xheight, cap_height;
	int8_t uline_position;
	uint8_t uline_thickness;
	os_box bbox;

	assert(2 == argc);

	ptmp = mkdtemp(template);
	assert(NULL != ptmp);
	atexit(cleanup);
	chdir(ptmp);

	rufl_test_harness_init(339, false, true);

	read_config(argv[1], &cfg);

	assert(rufl_OK == rufl_init());
	assert(NULL == rufl_fm_error);
	assert(3 == rufl_family_list_entries);
	assert(NULL != rufl_family_menu);

	if (cfg.expumaps != NULL) {
		size_t i, j;
		for (i = 0; i != cfg.n_expumaps; i++) {
			for (j = 0; j != rufl_font_list_entries; j++) {
				if (strcmp(cfg.expumaps[i].fontname, rufl_font_list[j].identifier) == 0) {
					assert(cfg.expumaps[i].num_umaps == rufl_font_list[j].num_umaps);
				}
			}
		}
	}

	assert(rufl_OK == rufl_font_metrics("Corpus", rufl_WEIGHT_500,
			&bbox, &xkern, &ykern, &italic,
			&ascent, &descent, &xheight, &cap_height,
			&uline_position, &uline_thickness));
	assert(0 == bbox.x0);
	assert(2 == bbox.x1);
	assert(0 == bbox.y0);
	assert(2 == bbox.y1);
	assert(0 == xkern);
	assert(0 == ykern);
	assert(0 == italic);
	assert(0 == ascent);
	assert(0 == descent);
	assert((bbox.y1 - bbox.y0) == cap_height);
	assert((cap_height / 2) == xheight);
	assert(0 == uline_position);
	assert(0 == uline_thickness);

	assert(rufl_OK == rufl_width("Corpus", rufl_WEIGHT_500, 160,
			(const uint8_t *) "!\xc2\xa0", 3, &width));
	assert(50 == width);

	/* Place caret after first character */
	assert(rufl_OK == rufl_x_to_offset("Homerton", rufl_WEIGHT_500, 160,
			(const uint8_t *) "!\xc2\xa0", 3, 25,
			&offset, &x));
	assert(1 == offset);
	assert(25 == x);

	/* Attempt to split after first character. As the split point is
	 * coincident with the start of the second character, however,
	 * the split point is placed after it. */
	assert(rufl_OK == rufl_split("Trinity", rufl_WEIGHT_500, 160,
			(const uint8_t *) "!\xc2\xa0", 3, 25,
			&offset, &x));
	assert(3 == offset);
	assert(50 == x);

	/* Compute width of replacement character */
	assert(rufl_OK == rufl_width("Corpus", rufl_WEIGHT_500, 160,
			(const uint8_t *) "\xef\xbf\xbd", 3, &width));
	assert(17 == width);
	assert(rufl_OK == rufl_width("Corpus", rufl_WEIGHT_500, 160,
			(const uint8_t *) "\xf0\xa0\x80\xa5", 4, &width));
	assert(26 == width);

	rufl_dump_state(true);

	rufl_quit();

	/* Reinit -- should load cache */
	assert(rufl_OK == rufl_init());
	assert(NULL == rufl_fm_error);
	assert(3 == rufl_family_list_entries);
	assert(NULL != rufl_family_menu);
	/* Done for real this time */
	rufl_quit();

	printf("PASS\n");

	return 0;
}