~aleteoryx/9c

ref: eff663502098cb6d657e23577997bea7cef00a3e 9c/ufx2font.c -rw-r--r-- 11.9 KiB
eff66350 — glenda use memdraw properly, checkmapfile 11 days ago
                                                                                
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <draw.h>
#include <memdraw.h>


/* entry has 1 byte in widtab + 8 bytes per tile */
#define ufxlen(n) ((n)*(n)*8)
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define warn(fmt, ...) fprint(2, "%s: " fmt "\n", argv0, __VA_ARGS__)


enum{
	MR_BLANK,
	MR_SINGLE,
	MR_RANGE
};
struct MapRule{
	struct MapRule *next;

	uint linum;

	uchar ty;
	uint start, end;
};
struct Mapping{
	struct Mapping *next;

	uint linum;

	Rune start;
	union {Rune end; uint idx;};
	struct MapRule *rules;
};
struct MapFile{
	struct MapFile *next;

	char *file;

	struct Mapping *mappings;
};


struct GlyphInfo{
	uchar top, bottom, left, width;
};

struct RawSubfont{
	char n[12], height[12], ascent[12];
	uchar entries[][6];
};


uint fwidth, nglyphs, glyphlen, glyphedge, baseline;

uchar setconv, donaive, doholes;
struct MapFile *mapfiles, *lastmf;

uchar wasmferr, wasreaderr;

struct GlyphInfo *glyphs;
Memimage *glyphimg;


void
bioerr(char *s)
{
	wasreaderr = 1;
	wasmferr = 1;
	fprint(2, "%s: %s\n", argv0, s);
}
char *
readtoken(Biobuf *b, char **line, uint *linum)
{
	char *ret, *p;

	if(b->aux != nil){
		ret = b->aux;
		b->aux = nil;
		return ret;
	}

	while(*line != nil){
		p = *line;
		while(p[0] != '\0' && strchr("\t\r ", p[0]) != nil)
			p++;

		if(strchr("#;\n", p[0]) != nil){
			*line = Brdline(b, '\n');
			(*linum)++;
			continue;
		}
		ret = p;

		while(strchr("\t\r\n ", p[0]) == nil)
			p++;

		*line = p;
		if(p[0] != '\n')
			(*line)++;
		p[0] = '\0';

		return ret;
	}

	return nil;
}
void
unreadtoken(Biobuf *b, char *tok)
{
	b->aux = tok;
}
#pragma varargck argpos mferr 3
void
mferr(char *file, uint linum, char *fmt, ...)
{
	char buf[1024];
	va_list va;

	va_start(va, fmt);
	vsnprint(buf, sizeof(buf), fmt, va);
	va_end(va);
	if(linum != 0)
		fprint(2, "%s: %s:%ud: %s\n", argv0, file, linum, buf);
	else
		fprint(2, "%s: %s: %s\n", argv0, file, buf);

	wasmferr = 1;
}
struct MapRule *
parsemaprule(char *file, Biobuf *b, char **line, uint *linum)
{
	struct MapRule *ret;
	char *tok, *p;

	tok = readtoken(b, line, linum);
	if(tok == nil)
		return nil;

	ret = malloc(sizeof(struct MapRule));
	ret->next = nil;
	ret->linum = *linum;

	if(strcmp(tok, "blank") == 0){
		ret->ty = MR_BLANK;
		return ret;
	}else if(tok[0] == '-'){	/* e.g. -0x4f */
		tok++;
		ret->ty = MR_RANGE;
		ret->start = 0;
		ret->end = strtoul(tok, &p, 0);
		if(tok == p || p[0] != '\0'){
			mferr(file, *linum, "bad upper glyph bound");
			free(ret);
			return nil;
		}
		return ret;
	}


	ret->start = strtoul(tok, &p, 0);
	if(tok == p){
		unreadtoken(b, tok);
		free(ret);
		return nil;
	}else if(p[0] == '\0'){
		ret->ty = MR_SINGLE;
		return ret;
	}else if(p[0] != '-'){
		mferr(file, *linum, "bad lower glyph bound");
		free(ret);
		return nil;
	}


	ret->ty = MR_RANGE;

	tok = p+1;
	if(tok[0] == '\0'){	/* e.g. 0x80- */
		ret->end = nglyphs - 1;
		return ret;
	}
	ret->end = strtoul(tok, &p, 0);
	if(tok == p || p[0] != '\0'){
		mferr(file, *linum, "bad upper glyph bound");
		free(ret);
		return nil;
	}

	return ret;
}
struct Mapping *
parsemapping(char *file, Biobuf *b, char **line, uint *linum)
{
	struct Mapping *ret;
	struct MapRule *rlast = nil, *r;
	char *tok, *p;

	tok = readtoken(b, line, linum);
	if(tok == nil){
		mferr(file, *linum, "unexpected eof");
		return nil;
	}

	ret = malloc(sizeof(struct Mapping));
	ret->linum = *linum;
	ret->rules = nil;
	ret->next = nil;

	ret->start = strtoul(tok, &p, 0);
	if(tok == p){
		mferr(file, *linum, "empty lower rune bound");
		free(ret);
		return nil;
	}
	tok = p;

	if(tok[0] == '\0'){	/* 1:1 mapping */
		tok = readtoken(b, line, linum);
		if(tok == nil){
			mferr(file, *linum, "unexpected eof");
			free(ret);
			return nil;
		}
		ret->idx = strtoul(tok, &p, 0);
		
		if(tok == p || p[0] != '\0'){
			mferr(file, *linum, "bad glyph index");
			free(ret);
			return nil;
		}

		return ret;
	}else if(tok[0] != '-'){
		mferr(file, *linum, "bad rune range");
		free(ret);
		return nil;
	}
	tok++;

	/* many:many mapping */
	ret->end = strtoul(tok, &p, 0);
	if(tok == p){
		mferr(file, *linum, "empty upper rune bound");
		free(ret);
		return nil;
	}else if(p[0] != '\0'){
		mferr(file, *linum, "bad upper rune bound");
		free(ret);
		return nil;
	}

	while((r = parsemaprule(file, b, line, linum)) != nil){
		if(rlast == nil)
			ret->rules = rlast = r;
		else
			rlast = rlast->next = r;
	}

	if(rlast == nil){
		mferr(file, *linum, "unexpected eof");
		free(ret);
		return nil;
	}

	return ret;
}
void
_parsemapfile(Biobuf *b, struct MapFile *mf)
{
	char *line, *tok;
	uint linum = 1;
	struct Mapping *mlast = nil, *m;

	wasreaderr = 0;
	line = Brdline(b, '\n');
	if(line == nil){
		if(!wasreaderr)
			warn("warn: %s: empty file\n", mf->file);
		return;
	}

	while((tok = readtoken(b, &line, &linum)) != nil){
		if(strcmp(tok, "mapping") == 0){
			m = parsemapping(mf->file, b, &line, &linum);
			if(m == nil)
				continue;

			if(mlast == nil)
				mf->mappings = mlast = m;
			else
				mlast = mlast->next = m;
		}else{
			mferr(mf->file, linum, "unknown verb %s", tok);
			return;
		}
	}
}
void
parsemapfile(struct MapFile *mf)
{
	Biobuf *b;

	b = Bopen(mf->file, OREAD);
	if(b == nil){
		fprint(2, "%s: %r\n", argv0);
		wasmferr = 1;
		return;
	}
	Blethal(b, bioerr);

	_parsemapfile(b, mf);

	Bterm(b);
}
void
printmapfile(struct MapFile *mf)
{
	struct Mapping *mp;
	struct MapRule *rp;

	print("mapfile: %s\n", mf->file);

	for(mp = mf->mappings; mp != nil; mp = mp->next){
		if(mp->rules == nil)
			print("\t%ud: %ux => %ux\n", mp->linum, mp->start, mp->idx);
		else{
			print("\t%ud: %ux - %ux =>\n", mp->linum, mp->start, mp->end);
			for(rp = mp->rules; rp != nil; rp = rp->next){
				switch(rp->ty){
				case MR_SINGLE:
					print("\t\t%ux\n", rp->start);
					break;
				case MR_RANGE:
					print("\t\t%ux - %ux\n", rp->start, rp->end);
					break;
				case MR_BLANK:
					print("\t\tblank\n");
					break;
				}
			}
		}
	}
}
void
addmapfile(char *file)
{
	if(lastmf == nil)
		mapfiles = lastmf = malloc(sizeof(struct MapFile));
	else
		lastmf = lastmf->next = malloc(sizeof(struct MapFile));
	lastmf->file = file;
	lastmf->next = nil;
	lastmf->mappings = nil;

	parsemapfile(lastmf);
}
void
testmappingoverlap(char *file, struct Mapping *a, struct Mapping *b)
{
	uchar res = 0;
	struct Mapping *tmp;

	if(a->rules == nil){
		tmp = a; a = b; b = tmp;
	}

	if(a->rules == nil){
		if(a->start == b->start)
			res = 1;
	}else if(b->rules == nil){
		if(a->start <= b->start && b->start <= a->end)
			res = 1;
	}else{
		if(a->start > b->start){
			tmp = a; a = b; b = tmp;
		}
		if(a->end >= b->start)
			res = 1;
	}

	if(res)
		mferr(file, a->linum, "overlaps mapping on line %ud", b->linum);
}
void
testmappingrange(char *file, struct Mapping *m)
{
	struct MapRule *r;
	int nrunes, n = 0;

	for(r = m->rules; r != nil; r = r->next){
		if((r->ty != MR_BLANK && r->start >= nglyphs) ||
			(r->ty == MR_RANGE && r->end >= nglyphs))
			mferr(file, r->linum, "glyphs out of range");

		if(r->ty == MR_RANGE)
			n += r->end - r->start;
		n++;
	}

	nrunes = (m->end-m->start+1);

	if(nrunes != n)
		mferr(file, m->linum, "# runes != # glyphs (%d != %d)", nrunes, n);
}
void
checkmapfiles(void)
{
	struct MapFile *mf;
	struct Mapping *p1, *p2;

	if(wasmferr)
		exits("mapfile");

	for(mf = mapfiles; mf != nil; mf = mf->next){
		for(p1 = mf->mappings; p1 != nil; p1 = p1->next){
			if(p1->rules != nil && p1->start > p1->end)
				mferr(mf->file, p1->linum, "rune start > rune end");

			if(p1->rules != nil)
				testmappingrange(mf->file, p1);
			else if(p1->idx >= nglyphs)
				mferr(mf->file, p1->linum, "glyph out of range");

			for(p2 = p1->next; p2 != nil; p2 = p2->next)
				testmappingoverlap(mf->file, p1, p2);
		}
	}

	if(wasmferr)
		exits("mapfile");
}


void
guesswidth(char *file)
{
	uint len;
	char c;

	len = strlen(file);
	c = file[len-1];

	if('1' <= c && c <= '8' && strncmp(file+len-4, ".uf", 3) == 0){
		fwidth = c - '0';
	}
}
void
guesslength(char *file, Dir *d)
{
	if(nglyphs == 0){
		if(d->length % (1+glyphlen) != 0)
			sysfatal("%s: can't determine file dimensions", file);
		nglyphs = d->length / (1+glyphlen);
	}else if(nglyphs * (1+glyphlen) > d->length)
		sysfatal("%s: file too short", file);
}
void
guessdims(char *file)
{
	Dir *d;

	if(fwidth < 1)
		guesswidth(file);
	if(fwidth < 1)
		sysfatal("%s: can't determine glyph width", file);
	
	glyphlen = ufxlen(fwidth);
	glyphedge = fwidth * 8;	


	d = dirstat(file);
	if(d == nil)
		sysfatal("%r");
	guesslength(file, d);
	free(d);
}


void
loadwidths(Biobuf *b, char *file)
{
	uint i;
	int n;

	for(i = 0; i < nglyphs; i++){
		n = Bgetc(b);
		if(n < 0)
			sysfatal("%r");
		
		if(n > glyphedge){
			warn("%s: 0x%02x: bad width %d, truncating\n", file, i, n);
			n = glyphedge;
		}

		glyphs[i].width = n;
	}
}

void
allocglyphimg(void)
{
	Rectangle dims = Rect(0,0, nglyphs*fwidth*8,glyphedge);

	glyphimg = allocmemimage(dims, strtochan("k1"));
	if(glyphimg == nil)
		sysfatal("%r");
}

uchar
testscanline(uchar *bitmap, int rowlen)
{
	uchar mask, x;
	for(x = 0; x <= (rowlen-1)/8; x++){
		/* ignore the right edge of the last byte */
		mask = 0xff << (8-min(8, rowlen-x*8));
		if((bitmap[x*glyphedge] & mask) != 0)
			return 1;
	}

	return 0;	
}
void
parseglyph(uchar *bitmap, struct GlyphInfo *ginfo, uint *botfreqs)
{
	uint top, bot;
	int rowlen = ginfo->width;

	for(top = 0; top < glyphedge; top++)
		if(testscanline(bitmap+top, rowlen))
			break;
	for(bot = glyphedge-1; bot >= top; bot--)
		if(testscanline(bitmap+bot, rowlen))
			break;

	if(botfreqs != nil && top <= bot)	/* don't count empty glyphs */
		botfreqs[bot]++;
	ginfo->top = top; ginfo->bottom = bot;
}
void
storeglyph(uint i, uchar *bitmap)
{
	uint y, x;
	ulong srcidx;
	Point dst;

	for(x = 0; x < fwidth; x++){
		for(y = 0; y < glyphedge; y++){
			srcidx = y+(x*glyphedge);
			dst = Pt((i*fwidth+x)*8, y);
			*byteaddr(glyphimg, dst) = bitmap[srcidx];
		}
	}
}
void
readglyphs(Biobuf *b, uint *botfreqs)
{
	uint i;
	uchar *bitmap;
	long n;

	bitmap = malloc(glyphlen);

	for(i = 0; i < nglyphs; i++){
		n = Bread(b, bitmap, glyphlen);
		if(n < 0)
			sysfatal("%r");
		if(n != glyphlen)
			sysfatal("unexpected eof");
		parseglyph(bitmap, glyphs+i, botfreqs);
		storeglyph(i, bitmap);
	}

	free(bitmap);
}
void
loadglyphs(Biobuf *b)
{
	uint i, *botfreqs, bestfreq;

	if(baseline < 1){
		botfreqs = calloc(glyphedge, sizeof(uint));

		readglyphs(b, botfreqs);

		bestfreq = 0;
		for(i = 0; i < glyphedge; i++){
			if(botfreqs[i] > bestfreq){
				baseline = i;
				bestfreq = botfreqs[i];
			}
		}
	}else
		readglyphs(b, nil);
}

void
loadufx(char *infile)
{
	Biobuf *b;

	glyphs = malloc(nglyphs * sizeof(struct GlyphInfo));
	b = Bopen(infile, OREAD);

	allocglyphimg();

	loadwidths(b, infile);
	loadglyphs(b);

	Bterm(b);
}


void
makenaive(void)
{
	int fd;
	struct RawSubfont s;
}


void
usage(void)
{
	fprint(2,
		"usage: %s [-nh] [-b baseline] [-m mapfile]"
			"[-l length] [-x width] in.ufx out/\n",
		argv0);
	exits("usage");
}

void
main(int argc, char **argv)
{
	char *infile, *outdir;
	int fd;

	ARGBEGIN{
	case 'l':
		nglyphs = atoi(EARGF(usage()));
		break;
	case 'x':
		fwidth = atoi(EARGF(usage()));
		if(1 > fwidth || fwidth > 8)
			sysfatal("-x: width must be in range 1-8");
		break;
	case 'n':
		setconv = 1;
		donaive = 1;
		break;
	case 'h':
		setconv = 1;
		doholes = 1;
		break;
	case 'm':
		setconv = 1;
		addmapfile(EARGF(usage()));
		break;
	case 'b':
		baseline = atoi(EARGF(usage()));
		if(baseline < 1)
			sysfatal("-b: baseline must be in range 1-width*8");
		break;
	default:
		usage();
	}ARGEND;


	if(argv0 == nil)
		argv0 = "ufx2font";
	if(argc != 2)
		usage();

	infile = argv[0];
	outdir = argv[1];


	guessdims(infile);
	if(baseline >= glyphedge)
		sysfatal("-b: baseline must be in range 1-width*8");

	checkmapfiles();

	loadufx(infile);


	fd = create(outdir, OWRITE, 0777|DMDIR);
	if(fd < 0)
		sysfatal("%r");
	close(fd);
	chdir(outdir);

	fd = create("test", OWRITE, 0777);
	writememimage(fd, glyphimg);
	close(fd);

	if(!setconv || donaive)
		makenaive();
/*	if(!setconv || doholes)
		makeholes();
	makemapped();*/
}