aboutsummaryrefslogtreecommitdiff
path: root/dist/troff/afm2troff.c
blob: 0bebb989621c520725981dc4ef7e15c92ca94bc2 (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
#include <u.h>
#include <libc.h>
#include <bio.h>

int didname;
int didfontname;
int offset;
void run(char*, int);
Biobuf bout;

void
usage(void)
{
	fprint(2, "usage: afm2troff [-h] [-o offset] [file...]\n");
	exits("usage");
}

void
main(int argc, char **argv)
{
	int i, fd;
	
	ARGBEGIN{
	case 'h':
		didname = 1;
		didfontname = 1;
		break;
	case 'o':
		offset = atoi(EARGF(usage()));
		break;
	default:
		usage();
	}ARGEND
	
	Binit(&bout, 1, OWRITE);
	if(argc == 0)
		run("<stdin>", 0);
	else{
		for(i=0; i<argc; i++){
			if((fd = open(argv[i], OREAD)) < 0)
				sysfatal("open %s: %r", argv[i]);
			run(argv[i], fd);
		}
	}
	Bflush(&bout);
}

void
run(char *name, int fd)
{
	char *p, *q, *f[100];
	int nf, code, wid, ad;
	Biobuf b;
	Fmt fmt;
	
	fmtstrinit(&fmt);
	Binit(&b, fd, OREAD);
	while((p = Brdline(&b, '\n')) != nil){
		p[Blinelen(&b)-1] = 0;
		q = strchr(p, ' ');
		if(q == nil)
			continue;
		*q++ = 0;
		while(*q == ' ' || *q == '\t')
			q++;
		if(*q == 0)
			continue;
		if(strcmp(p, "FontName") == 0 && didname++ == 0)
			 Bprint(&bout, "name %s\n", q);
		if(strcmp(p, "FullName") == 0 && didfontname++ == 0)
			 Bprint(&bout, "fontname %s\n", q);
		if(strcmp(p, "C") == 0){
			nf = getfields(q, f, nelem(f), 1, "\t\r\n\v ");
			if(nf < 5 || strcmp(f[1], ";") != 0 || strcmp(f[2], "WX") != 0)
				continue;
			code = strtol(f[0], 0, 10);
			wid = strtol(f[3], 0, 10);
			wid = (wid+5)/10;
			if(code == 0)
				continue;
			code += offset;
			ad = 0;
			if(nf < 6 || strcmp(f[nf-6], "B") != 0)
				continue;
			if(atoi(f[nf-4]) < -50)
				ad |= 1;
			if(atoi(f[nf-2]) > 600)
				ad |= 2;
			if(nf >= 7 && strcmp(f[5], "N") == 0 && strcmp(f[6], "space") == 0)
				code = ' ';
			if(code == ' ')
				Bprint(&bout, "spacewidth %d\ncharset\n", wid);
			else
				fmtprint(&fmt, "%C\t%d\t%d\t%d %04x\n",
					code, wid, ad, code, code);
		}
	}
	Bprint(&bout, "%s", fmtstrflush(&fmt));
}