xref: /plan9-contrib/sys/src/cmd/postscript/tr2post/chartab.c (revision 9a747e4fd48b9f4522c70c07e8f882a15030f964)
1 /*    Unicode   |     PostScript
2  *  start  end  | offset  font name
3  * 0x0000 0x00ff  0x00   LucidaSansUnicode00
4  */
5 #include <u.h>
6 #include <libc.h>
7 #include <bio.h>
8 #include "common.h"
9 #include "tr2post.h"
10 #include "comments.h"
11 #include "path.h"
12 
13 /* Postscript font names, e.g., `LucidaSansUnicode00'
14  * names may only be added because reference to the
15  * names is made by indexing into this table.
16  */
17 static struct pfnament *pfnafontmtab = 0;
18 static int pfnamcnt = 0;
19 int curpostfontid = -1;
20 int curfontsize = -1;
21 int curtrofffontid = -1;
22 static int curfontpos = -1;
23 static int fontheight = 0;
24 static int fontslant = 0;
25 
26 /* This is troffs mounted font table.  It is an anachronism resulting
27  * from the design of the APS typesetter.  fontmnt is the
28  * number of positions available.  fontmnt is really 11, but
29  * should not be limited.
30  */
31 int fontmnt = 0;
32 char **fontmtab;
33 
34 struct troffont *troffontab = 0;
35 
36 int troffontcnt = 0;
37 
38 void
39 mountfont(int pos, char *fontname) {
40 	int i;
41 
42 	if (debug) Bprint(Bstderr, "mountfont(%d, %s)\n", pos, fontname);
43 	if (pos < 0 || pos >= fontmnt)
44 		error(FATAL, "cannot mount a font at position %d,\n  can only mount into postions 0-%d\n",
45 			pos, fontmnt-1);
46 
47 	i = strlen(fontname);
48 	fontmtab[pos] = galloc(fontmtab[pos], i+1, "mountfont():fontmtab");
49 	strcpy(fontmtab[pos], fontname);
50 	if (curfontpos == pos)	curfontpos = -1;
51 }
52 
53 void
54 settrfont(void) {
55 	if (curfontpos == fontpos) return;
56 
57 	if (fontmtab[fontpos] == 0)
58 		error(FATAL, "Font at position %d was not initialized, botch!\n", fontpos);
59 
60 	curtrofffontid = findtfn(fontmtab[fontpos], 1);
61 	if (debug) Bprint(Bstderr, "settrfont()-> curtrofffontid=%d\n", curtrofffontid);
62 	curfontpos = fontpos;
63 	if (curtrofffontid < 0) {
64 		int i;
65 
66 		error(WARNING, "fontpos=%d\n", fontpos);
67 		for (i=0; i<fontmnt; i++)
68 			if (fontmtab[i] == 0)
69 				error(WARNING, "fontmtab[%d]=0x0\n", i);
70 			else
71 				error(WARNING, "fontmtab[%d]=%s\n", i, fontmtab[i]);
72 		exits("settrfont()");
73 	}
74 }
75 
76 void
77 setpsfont(int psftid, int fontsize) {
78 	if (psftid == curpostfontid && fontsize == curfontsize) return;
79 	if (psftid >= pfnamcnt)
80 		error(FATAL, "Postscript font index=%d used but not defined, there are only %d fonts\n",
81 			psftid, pfnamcnt);
82 
83 	endstring();
84 	if (pageon()) {
85 		Bprint(Bstdout, "%d /%s f\n", fontsize, pfnafontmtab[psftid].str);
86 		if ( fontheight != 0 || fontslant != 0 )
87 			Bprint(Bstdout, "%d %d changefont\n", fontslant, (fontheight != 0) ? fontheight : fontsize);
88 		pfnafontmtab[psftid].used = 1;
89 		curpostfontid = psftid;
90 		curfontsize = fontsize;
91 	}
92 }
93 
94 /* find index of PostScript font name in table
95  * returns -1 if name is not in table
96  * If insflg is not zero
97  * and the name is not found in the table, insert it.
98  */
99 int
100 findpfn(char *fontname, int insflg) {
101 	char *tp;
102 	int i;
103 
104 	for (i=0; i<pfnamcnt; i++) {
105 		if (strcmp(pfnafontmtab[i].str, fontname) == 0)
106 			return(i);
107 	}
108 	if (insflg) {
109 		tp = galloc(pfnafontmtab, sizeof(struct pfnament)*(pfnamcnt+1), "findpfn():pfnafontmtab");
110 		if (tp == 0)
111 			return(-2);
112 		pfnafontmtab = (struct pfnament *)tp;
113 		i = strlen(fontname);
114 		pfnafontmtab[pfnamcnt].str = galloc(0, i+1, "findpfn():pfnafontmtab[].str");
115 		strncpy(pfnafontmtab[pfnamcnt].str, fontname, i);
116 		pfnafontmtab[pfnamcnt].str[i] = '\0';
117 		pfnafontmtab[pfnamcnt].used = 0;
118 		return(pfnamcnt++);
119 	}
120 	return(-1);
121 }
122 
123 char postroffdirname[] = "/sys/lib/postscript/troff";		/* "/sys/lib/postscript/troff/"; */
124 char troffmetricdirname[] = "/sys/lib/troff/font";	/* "/sys/lib/troff/font/devutf/"; */
125 
126 int
127 readpsfontdesc(char *fontname, int trindex) {
128 	static char *filename = 0;
129 	Biobuf *bfd;
130 	Biobufhdr *Bfd;
131 	int warn = 0, errorflg = 0, line =1, rv;
132 	int start, end, offset;
133 	int startfont, endfont, startchar, endchar, i, pfid;
134 	char psfontnam[128];
135 	struct troffont *tp;
136 	struct charent *cp[];
137 
138 	if (debug) Bprint(Bstderr, "readpsfontdesc(%s,%d)\n", fontname, trindex);
139 	filename=galloc(filename, strlen(postroffdirname)+1+strlen(fontname)+1, "readpsfontdesc: cannot allocate memory\n");
140 	sprint(filename, "%s/%s", postroffdirname, fontname);
141 
142 	bfd = Bopen(filename, OREAD);
143 	if (bfd == 0) {
144 		error(WARNING, "cannot open file %s\n", filename);
145 		return(0);
146 	}
147 	Bfd = &(bfd->Biobufhdr);
148 
149 	do {
150 		offset = 0;
151 		if ((rv=Bgetfield(Bfd, 'd', &start, 0)) == 0) {
152 			errorflg = 1;
153 			error(WARNING, "file %s:%d illegal start value\n", filename, line);
154 		} else if (rv < 0) break;
155 		if ((rv=Bgetfield(Bfd, 'd', &end, 0)) == 0) {
156 			errorflg = 1;
157 			error(WARNING, "file %s:%d illegal end value\n", filename, line);
158 		} else if (rv < 0) break;
159 		if ((rv=Bgetfield(Bfd, 'd', &offset, 0)) < 0) {
160 			errorflg = 1;
161 			error(WARNING, "file %s:%d illegal offset value\n", filename, line);
162 		}
163 		if ((rv=Bgetfield(Bfd, 's', psfontnam, 128)) == 0) {
164 			errorflg = 1;
165 			error(WARNING, "file %s:%d illegal fontname value\n", filename, line);
166 		} else if (rv < 0) break;
167 		Brdline(Bfd, '\n');
168 		if (!errorflg) {
169 			struct psfent *psfentp;
170 			startfont = RUNEGETGROUP(start);
171 			startchar = RUNEGETCHAR(start);
172 			endfont = RUNEGETGROUP(end);
173 			endchar = RUNEGETCHAR(end);
174 			pfid = findpfn(psfontnam, 1);
175 			if (startfont != endfont) {
176 				error(WARNING, "font descriptions must not cross 256 glyph block boundary\n");
177 				errorflg = 1;
178 				break;
179 			}
180 			tp = &(troffontab[trindex]);
181 			tp->psfmap = galloc(tp->psfmap, ++(tp->psfmapsize)*sizeof(struct psfent), "readpsfontdesc():psfmap");
182 			psfentp = &(tp->psfmap[tp->psfmapsize-1]);
183 			psfentp->start = start;
184 			psfentp->end = end;
185 			psfentp->offset = offset;
186 			psfentp->psftid = pfid;
187 			if (debug) {
188 				Bprint(Bstderr, "\tpsfmap->start=0x%x\n", start);
189 				Bprint(Bstderr, "\tpsfmap->end=0x%x\n", end);
190 				Bprint(Bstderr, "\tpsfmap->offset=0x%x\n", offset);
191 				Bprint(Bstderr, "\tpsfmap->pfid=0x%x\n", pfid);
192 			}
193 /*
194 			for (i=startchar; i<=endchar; i++) {
195 				tp->charent[startfont][i].postfontid = pfid;
196 				tp->charent[startfont][i].postcharid = i + offset - startchar;
197 			}
198  */
199 			if (debug) {
200 				Bprint(Bstderr, "%x %x ", start, end);
201 				if (offset) Bprint(Bstderr, "%x ", offset);
202 				Bprint(Bstderr, "%s\n", psfontnam);
203 			}
204 			line++;
205 		}
206 	} while(errorflg != 1);
207 	Bterm(Bfd);
208 	return(1);
209 }
210 
211 int
212 readtroffmetric(char *fontname, int trindex) {
213 	static char *filename = 0;
214 	Biobuf *bfd;
215 	Biobufhdr *Bfd;
216 	int warn = 0, errorflg = 0, line =1, rv;
217 	struct troffont *tp;
218 	struct charent **cp;
219 	char stoken[128], *str;
220 	int ntoken;
221 	Rune troffchar, quote;
222 	int width, flag, charnum, thisfont, thischar;
223 	BOOLEAN specharflag;
224 
225 	if (debug) Bprint(Bstderr, "readtroffmetric(%s,%d)\n", fontname, trindex);
226 	filename=galloc(filename, strlen(troffmetricdirname)+4+strlen(devname)+1+strlen(fontname)+1, "readtroffmetric():filename");
227 	sprint(filename, "%s/dev%s/%s", troffmetricdirname, devname, fontname);
228 
229 	bfd = Bopen(filename, OREAD);
230 	if (bfd == 0) {
231 		error(WARNING, "cannot open file %s\n", filename);
232 		return(0);
233 	}
234 	Bfd = &(bfd->Biobufhdr);
235 	do {
236 		/* deal with the few lines at the beginning of the
237 		 * troff font metric files.
238 		 */
239 		if ((rv=Bgetfield(Bfd, 's', stoken, 128)) == 0) {
240 			errorflg = 1;
241 			error(WARNING, "file %s:%d illegal token\n", filename, line);
242 		} else if (rv < 0) break;
243 		if (debug) {
244 			Bprint(Bstderr, "%s\n", stoken);
245 		}
246 
247 		if (strcmp(stoken, "name") == 0) {
248 			if ((rv=Bgetfield(Bfd, 's', stoken, 128)) == 0) {
249 				errorflg = 1;
250 				error(WARNING, "file %s:%d illegal token\n", filename, line);
251 			} else if (rv < 0) break;
252 		} else if (strcmp(stoken, "named") == 0) {
253 			Brdline(Bfd, '\n');
254 		} else if (strcmp(stoken, "fontname") == 0) {
255 			if ((rv=Bgetfield(Bfd, 's', stoken, 128)) == 0) {
256 				errorflg = 1;
257 				error(WARNING, "file %s:%d illegal token\n", filename, line);
258 			} else if (rv < 0) break;
259 		} else if (strcmp(stoken, "spacewidth") == 0) {
260 			if ((rv=Bgetfield(Bfd, 'd', &ntoken, 0)) == 0) {
261 				errorflg = 1;
262 				error(WARNING, "file %s:%d illegal token\n", filename, line);
263 			} else if (rv < 0) break;
264 			troffontab[trindex].spacewidth = ntoken;
265 			thisfont = RUNEGETGROUP(' ');
266 			thischar = RUNEGETCHAR(' ');
267 			for (cp = &(troffontab[trindex].charent[thisfont][thischar]); *cp != 0; cp = &((*cp)->next))
268 				if ((*cp)->name)
269 					if  (strcmp((*cp)->name, " ") == 0)
270 						break;
271 
272 			if (*cp == 0) *cp = galloc(0, sizeof(struct charent), "readtroffmetric:charent");
273 			(*cp)->postfontid = thisfont;
274 			(*cp)->postcharid = thischar;
275 			(*cp)->troffcharwidth = ntoken;
276 			(*cp)->name = galloc(0, 2, "readtroffmetric: char name");
277 			(*cp)->next = 0;
278 			strcpy((*cp)->name, " ");
279 		} else if (strcmp(stoken, "special") == 0) {
280 			troffontab[trindex].special = TRUE;
281 		} else if (strcmp(stoken, "charset") == 0) {
282 			line++;
283 			break;
284 		}
285 		if (!errorflg) {
286 			line++;
287 		}
288 	} while(!errorflg && rv>=0);
289 	while(!errorflg && rv>=0) {
290 		if ((rv=Bgetfield(Bfd, 's', stoken, 128)) == 0) {
291 			errorflg = 1;
292 			error(WARNING, "file %s:%d illegal rune token <0x%x> rv=%d\n", filename, line, troffchar, rv);
293 		} else if (rv < 0) break;
294 		if (utflen(stoken) > 1) specharflag = TRUE;
295 		else specharflag = FALSE;
296 		/* if this character is a quote we have to use the previous characters info */
297 		if ((rv=Bgetfield(Bfd, 'r', &quote, 0)) == 0) {
298 			errorflg = 1;
299 			error(WARNING, "file %s:%d illegal width or quote token <0x%x> rv=%d\n", filename, line, quote, rv);
300 		} else if (rv < 0) break;
301 		if (quote == '"') {
302 			/* need some code here */
303 
304 			goto flush;
305 		} else {
306 			Bungetrune(Bfd);
307 		}
308 
309 		if ((rv=Bgetfield(Bfd, 'd', &width, 0)) == 0) {
310 			errorflg = 1;
311 			error(WARNING, "file %s:%d illegal width token <0x%x> rv=%d\n", filename, line, troffchar, rv);
312 		} else if (rv < 0) break;
313 		if ((rv=Bgetfield(Bfd, 'd', &flag, 0)) == 0) {
314 			errorflg = 1;
315 			error(WARNING, "file %s:%d illegal flag token <0x%x> rv=%d\n", filename, line, troffchar, rv);
316 		} else if (rv < 0) break;
317 		if ((rv=Bgetfield(Bfd, 'd', &charnum, 0)) == 0) {
318 			errorflg = 1;
319 			error(WARNING, "file %s:%d illegal character number token <0x%x> rv=%d\n", filename, line, troffchar, rv);
320 		} else if (rv < 0) break;
321 flush:
322 		str = Brdline(Bfd, '\n');
323 		/* stash the crap from the end of the line for debugging */
324 		if (debug) {
325 			if (str == 0) {
326 				Bprint(Bstderr, "premature EOF\n");
327 				return(0);
328 			}
329 			str[Blinelen(Bfd)-1] = '\0';
330 		}
331 		line++;
332 		chartorune(&troffchar, stoken);
333 		if (specharflag) {
334 			if (debug)
335 				Bprint(Bstderr, "%s %d  %d 0x%x %s # special\n",stoken, width, flag, charnum, str);
336 		}
337 		if (strcmp(stoken, "---") == 0) {
338 			thisfont = RUNEGETGROUP(charnum);
339 			thischar = RUNEGETCHAR(charnum);
340 			stoken[0] = '\0';
341 		} else {
342 			thisfont = RUNEGETGROUP(troffchar);
343 			thischar = RUNEGETCHAR(troffchar);
344 		}
345 		for (cp = &(troffontab[trindex].charent[thisfont][thischar]); *cp != 0; cp = &((*cp)->next))
346 			if ((*cp)->name) {
347 				if (debug) Bprint(Bstderr, "installing <%s>, found <%s>\n", stoken, (*cp)->name);
348 				if  (strcmp((*cp)->name, stoken) == 0)
349 					break;
350 			}
351 		if (*cp == 0) *cp = galloc(0, sizeof(struct charent), "readtroffmetric:charent");
352 		(*cp)->postfontid = RUNEGETGROUP(charnum);
353 		(*cp)->postcharid = RUNEGETCHAR(charnum);
354 		(*cp)->troffcharwidth = width;
355 		(*cp)->name = galloc(0, strlen(stoken)+1, "readtroffmetric: char name");
356 		(*cp)->next = 0;
357 		strcpy((*cp)->name, stoken);
358 		if (debug) {
359 			if (specharflag)
360 				Bprint(Bstderr, "%s", stoken);
361 			else
362 				Bputrune(Bstderr, troffchar);
363 			Bprint(Bstderr, " %d  %d 0x%x %s # psfontid=0x%x pscharid=0x%x thisfont=0x%x thischar=0x%x\n",
364 				width, flag, charnum, str,
365 				(*cp)->postfontid,
366 				(*cp)->postcharid,
367 				thisfont, thischar);
368 		}
369 	}
370 	Bterm(Bfd);
371 	Bflush(Bstderr);
372 	return(1);
373 }
374 
375 /* find index of troff font name in table
376  * returns -1 if name is not in table
377  * returns -2 if it cannot allocate memory
378  * returns -3 if there is a font mapping problem
379  * If insflg is not zero
380  * and the name is not found in the table, insert it.
381  */
382 int
383 findtfn(char *fontname, BOOLEAN insflg) {
384 	struct troffont *tp;
385 	int i, j;
386 
387 	if (debug) {
388 		if (fontname==0) fprint(2, "findtfn(0x%x,%d)\n", fontname, insflg);
389 		else fprint(2, "findtfn(%s,%d)\n", fontname, insflg);
390 	}
391 	for (i=0; i<troffontcnt; i++) {
392 		if (troffontab[i].trfontid==0) {
393 			error(WARNING, "findtfn:troffontab[%d].trfontid=0x%x, botch!\n",
394 				i, troffontab[i].trfontid);
395 			continue;
396 		}
397 		if (strcmp(troffontab[i].trfontid, fontname) == 0)
398 			return(i);
399 	}
400 	if (insflg) {
401 		tp = (struct troffont *)galloc(troffontab, sizeof(struct troffont)*(troffontcnt+1), "findtfn: struct troffont:");
402 		if (tp == 0)
403 			return(-2);
404 		troffontab = tp;
405 		tp = &(troffontab[troffontcnt]);
406 		i = strlen(fontname);
407 		tp->trfontid = galloc(0, i+1, "findtfn: trfontid:");
408 
409 		/* initialize new troff font entry with name and numeric fields to 0 */
410 		strncpy(tp->trfontid, fontname, i);
411 		tp->trfontid[i] = '\0';
412 		tp->special = FALSE;
413 		tp->spacewidth = 0;
414 		tp->psfmapsize = 0;
415 		tp->psfmap = 0;
416 		for (i=0; i<NUMOFONTS; i++)
417 			for (j=0; j<FONTSIZE; j++)
418 				tp->charent[i][j] = 0;
419 		troffontcnt++;
420 		if (!readtroffmetric(fontname, troffontcnt-1))
421 			return(-3);
422 		if (!readpsfontdesc(fontname, troffontcnt-1))
423 			return(-3);
424 		return(troffontcnt-1);
425 	}
426 	return(-1);
427 }
428 
429 void
430 finish(void) {
431 	int i;
432 
433 	Bprint(Bstdout, "%s", TRAILER);
434 	Bprint(Bstdout, "done\n");
435 	Bprint(Bstdout, "%s", DOCUMENTFONTS);
436 
437 	for (i=0; i<pfnamcnt; i++)
438 		if (pfnafontmtab[i].used)
439 			Bprint(Bstdout, " %s", pfnafontmtab[i].str);
440 	Bprint(Bstdout, "\n");
441 
442 	Bprint(Bstdout, "%s %d\n", PAGES, pages_printed);
443 
444 }
445 
446 /* Set slant to n degrees. Disable slanting if n is 0. */
447 void
448 t_slant(int n) {
449 	fontslant = n;
450 	curpostfontid = -1;
451 }
452 
453 /* Set character height to n points. Disabled if n is 0 or the current size. */
454 
455 void
456 t_charht(int n) {
457 	fontheight = (n == fontsize) ? 0 : n;
458 	curpostfontid = -1;
459 }
460