plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

afm2troff.c (1896B)


      1 #include <u.h>
      2 #include <libc.h>
      3 #include <bio.h>
      4 
      5 int didname;
      6 int didfontname;
      7 int offset;
      8 void run(char*, int);
      9 Biobuf bout;
     10 
     11 void
     12 usage(void)
     13 {
     14 	fprint(2, "usage: afm2troff [-h] [-o offset] [file...]\n");
     15 	exits("usage");
     16 }
     17 
     18 void
     19 main(int argc, char **argv)
     20 {
     21 	int i, fd;
     22 	
     23 	ARGBEGIN{
     24 	case 'h':
     25 		didname = 1;
     26 		didfontname = 1;
     27 		break;
     28 	case 'o':
     29 		offset = atoi(EARGF(usage()));
     30 		break;
     31 	default:
     32 		usage();
     33 	}ARGEND
     34 	
     35 	Binit(&bout, 1, OWRITE);
     36 	if(argc == 0)
     37 		run("<stdin>", 0);
     38 	else{
     39 		for(i=0; i<argc; i++){
     40 			if((fd = open(argv[i], OREAD)) < 0)
     41 				sysfatal("open %s: %r", argv[i]);
     42 			run(argv[i], fd);
     43 		}
     44 	}
     45 	Bflush(&bout);
     46 }
     47 
     48 void
     49 run(char *name, int fd)
     50 {
     51 	char *p, *q, *f[100];
     52 	int nf, code, wid, ad;
     53 	Biobuf b;
     54 	Fmt fmt;
     55 	
     56 	fmtstrinit(&fmt);
     57 	Binit(&b, fd, OREAD);
     58 	while((p = Brdline(&b, '\n')) != nil){
     59 		p[Blinelen(&b)-1] = 0;
     60 		q = strchr(p, ' ');
     61 		if(q == nil)
     62 			continue;
     63 		*q++ = 0;
     64 		while(*q == ' ' || *q == '\t')
     65 			q++;
     66 		if(*q == 0)
     67 			continue;
     68 		if(strcmp(p, "FontName") == 0 && didname++ == 0)
     69 			 Bprint(&bout, "name %s\n", q);
     70 		if(strcmp(p, "FullName") == 0 && didfontname++ == 0)
     71 			 Bprint(&bout, "fontname %s\n", q);
     72 		if(strcmp(p, "C") == 0){
     73 			nf = getfields(q, f, nelem(f), 1, "\t\r\n\v ");
     74 			if(nf < 5 || strcmp(f[1], ";") != 0 || strcmp(f[2], "WX") != 0)
     75 				continue;
     76 			code = strtol(f[0], 0, 10);
     77 			wid = strtol(f[3], 0, 10);
     78 			wid = (wid+5)/10;
     79 			if(code == 0)
     80 				continue;
     81 			code += offset;
     82 			ad = 0;
     83 			if(nf < 6 || strcmp(f[nf-6], "B") != 0)
     84 				continue;
     85 			if(atoi(f[nf-4]) < -50)
     86 				ad |= 1;
     87 			if(atoi(f[nf-2]) > 600)
     88 				ad |= 2;
     89 			if(nf >= 7 && strcmp(f[5], "N") == 0 && strcmp(f[6], "space") == 0)
     90 				code = ' ';
     91 			if(code == ' ')
     92 				Bprint(&bout, "spacewidth %d\ncharset\n", wid);
     93 			else
     94 				fmtprint(&fmt, "%C\t%d\t%d\t%d %04x\n",
     95 					code, wid, ad, code, code);
     96 		}
     97 	}
     98 	Bprint(&bout, "%s", fmtstrflush(&fmt));
     99 }