xref: /plan9-contrib/sys/src/cmd/postscript/tr2post/readDESC.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <ctype.h>
5 #include "common.h"
6 #include "tr2post.h"
7 #include "comments.h"
8 #include "path.h"
9 
10 char *printdesclang = 0;
11 char *encoding = 0;
12 int devres;
13 int unitwidth;
14 int nspechars = 0;
15 struct charent spechars[MAXSPECHARS];
16 
17 #define NDESCTOKS 9
18 static char *desctoks[NDESCTOKS] = {
19 	"PDL",
20 	"Encoding",
21 	"fonts",
22 	"sizes",
23 	"res",
24 	"hor",
25 	"vert",
26 	"unitwidth",
27 	"charset"
28 };
29 
30 char *spechar[MAXSPECHARS];
31 
32 int
33 hash(char *s, int l) {
34     unsigned i;
35 
36     for (i=0; *s; s++)
37 	i = i*10 + *s;
38     return(i % l);
39 }
40 
41 BOOLEAN
42 readDESC(void) {
43 	char token[MAXTOKENSIZE];
44 	char *descnameformat = "%s/dev%s/DESC";
45 	char *descfilename = 0;
46 	Biobuf *bfd;
47 	Biobufhdr *Bfd;
48 	int i, state = -1;
49 	int fontindex = 0;
50 
51 	if (debug) Bprint(Bstderr, "readDESC()\n");
52 	descfilename = galloc(descfilename, strlen(descnameformat)+strlen(FONTDIR)
53 		+strlen(devname), "readdesc");
54 	sprint(descfilename, descnameformat, FONTDIR, devname);
55 	if ((bfd = Bopen(descfilename, OREAD)) == 0) {
56 		error(WARNING, "cannot open file %s\n", descfilename);
57 		return(0);
58 	}
59 	Bfd = &(bfd->Biobufhdr);
60 
61 	while (Bgetfield(Bfd, 's', token, MAXTOKENSIZE) > 0) {
62 		for (i=0; i<NDESCTOKS; i++) {
63 			if (strcmp(desctoks[i], token) == 0) {
64 				state = i;
65 				break;
66 			}
67 		}
68 		if (i<NDESCTOKS) continue;
69 		switch (state) {
70 		case 0:
71 			printdesclang=galloc(printdesclang, strlen(token)+1, "readdesc:");
72 			strcpy(printdesclang, token);
73 			if (debug) Bprint(Bstderr, "PDL %s\n", token);
74 			break;
75 		case 1:
76 			encoding=galloc(encoding, strlen(token)+1, "readdesc:");
77 			strcpy(encoding, token);
78 			if (debug) Bprint(Bstderr, "encoding %s\n", token);
79 			break;
80 		case 2:
81 			if (fontmnt <=0) {
82 				if (!isdigit(*token)) {
83 					error(WARNING, "readdesc: expecting number of fonts in mount table.\n");
84 					return(FALSE);
85 				}
86 				fontmnt = atoi(token) + 1;
87 				fontmtab = galloc(fontmtab, fontmnt*sizeof(char *), "readdesc:");
88 
89 				for (i=0; i<fontmnt; i++)
90 					fontmtab[i] = 0;
91 				fontindex = 0;
92 			} else {
93 				mountfont(++fontindex, token);
94 				findtfn(token, TRUE);
95 			}
96 			break;
97 		case 3:
98 			/* I don't really care about sizes */
99 			break;
100 		case 4:
101 			/* device resolution in dots per inch */
102 			if (!isdigit(*token)) {
103 				error(WARNING, "readdesc: expecting device resolution.\n");
104 				return(FALSE);
105 			}
106 			devres = atoi(token);
107 			if (debug) Bprint(Bstderr, "res %d\n", devres);
108 			break;
109 		case 5:
110 			/* I don't really care about horizontal motion resolution */
111 			if (debug) Bprint(Bstderr, "ignoring horizontal resolution\n");
112 			break;
113 		case 6:
114 			/* I don't really care about vertical motion resolution */
115 			if (debug) Bprint(Bstderr, "ignoring vertical resolution\n");
116 			break;
117 		case 7:
118 			/* unitwidth is the font size at which the character widths are 1:1 */
119 			if (!isdigit(*token)) {
120 				error(WARNING, "readdesc: expecting unitwidth.\n");
121 				return(FALSE);
122 			}
123 			unitwidth = atoi(token);
124 			if (debug) Bprint(Bstderr, "unitwidth %d\n", unitwidth);
125 			break;
126 		case 8:
127 			/* I don't really care about this list of special characters */
128 			if (debug) Bprint(Bstderr, "ignoring special character <%s>\n", token);
129 			break;
130 		default:
131 			if (*token == '#')
132 				Brdline(Bfd, '\n');
133 			else
134 				error(WARNING, "unknown token %s in DESC file.\n", token);
135 			break;
136 		}
137 	}
138 	Bterm(Bfd);
139 }
140