Blob


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"
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;
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;
34 struct troffont *troffontab = 0;
36 int troffontcnt = 0;
38 void
39 mountfont(int pos, char *fontname) {
40 int i;
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);
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 }
53 void
54 settrfont(void) {
55 if (curfontpos == fontpos) return;
57 if (fontmtab[fontpos] == 0)
58 error(FATAL, "Font at position %d was not initialized, botch!\n", fontpos);
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;
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 }
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);
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 }
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;
104 for (i=0; i<pfnamcnt; i++) {
105 if (strcmp(pfnafontmtab[i].str, fontname) == 0)
106 return(i);
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++);
120 return(-1);
123 char postroffdirname[] = "#9/postscript/troff"; /* "/sys/lib/postscript/troff/"; */
124 char troffmetricdirname[] = "#9/troff/font"; /* "/sys/lib/troff/font/devutf/"; */
126 int
127 readpsfontdesc(char *fontname, int trindex) {
128 static char *filename = 0;
129 Biobuf *bfd;
130 Biobuf *Bfd;
131 int errorflg = 0, line =1, rv;
132 int start, end, offset;
133 int startfont, endfont, startchar, endchar, pfid;
134 char psfontnam[128];
135 struct troffont *tp;
136 /* struct charent *cp[]; */
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);
142 bfd = Bopen(unsharp(filename), OREAD);
143 if (bfd == 0) {
144 error(WARNING, "cannot open file %s\n", filename);
145 return(0);
147 Bfd = bfd; /* &(bfd->Biobufhdr); */
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);
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;
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);
193 /*
194 for (i=startchar; i<=endchar; i++) {
195 tp->charent[startfont][i].postfontid = pfid;
196 tp->charent[startfont][i].postcharid = i + offset - startchar;
198 */
199 if (debug) {
200 Bprint(Bstderr, "%x %x ", start, end);
201 if (offset) Bprint(Bstderr, "%x ", offset);
202 Bprint(Bstderr, "%s\n", psfontnam);
204 line++;
206 } while(errorflg != 1);
207 Bterm(Bfd);
208 return(1);
211 int
212 readtroffmetric(char *fontname, int trindex) {
213 static char *filename = 0;
214 Biobuf *bfd;
215 Biobuf *Bfd;
216 int errorflg = 0, line =1, rv;
217 struct charent **cp;
218 char stoken[128], *str;
219 int ntoken;
220 Rune troffchar, quote;
221 int width, flag, charnum, thisfont, thischar;
222 BOOLEAN specharflag;
224 if (debug) Bprint(Bstderr, "readtroffmetric(%s,%d)\n", fontname, trindex);
225 filename=galloc(filename, strlen(troffmetricdirname)+4+strlen(devname)+1+strlen(fontname)+1, "readtroffmetric():filename");
226 sprint(filename, "%s/dev%s/%s", troffmetricdirname, devname, fontname);
228 bfd = Bopen(unsharp(filename), OREAD);
229 if (bfd == 0) {
230 error(WARNING, "cannot open file %s\n", filename);
231 return(0);
233 Bfd = bfd; /* &(bfd->Biobufhdr); */
234 do {
235 /* deal with the few lines at the beginning of the
236 * troff font metric files.
237 */
238 if ((rv=Bgetfield(Bfd, 's', stoken, 128)) == 0) {
239 errorflg = 1;
240 error(WARNING, "file %s:%d illegal token\n", filename, line);
241 } else if (rv < 0) break;
242 if (debug) {
243 Bprint(Bstderr, "%s\n", stoken);
246 if (strcmp(stoken, "name") == 0) {
247 if ((rv=Bgetfield(Bfd, 's', stoken, 128)) == 0) {
248 errorflg = 1;
249 error(WARNING, "file %s:%d illegal token\n", filename, line);
250 } else if (rv < 0) break;
251 } else if (strcmp(stoken, "named") == 0) {
252 Brdline(Bfd, '\n');
253 } else if (strcmp(stoken, "fontname") == 0) {
254 if ((rv=Bgetfield(Bfd, 's', stoken, 128)) == 0) {
255 errorflg = 1;
256 error(WARNING, "file %s:%d illegal token\n", filename, line);
257 } else if (rv < 0) break;
258 } else if (strcmp(stoken, "spacewidth") == 0) {
259 if ((rv=Bgetfield(Bfd, 'd', &ntoken, 0)) == 0) {
260 errorflg = 1;
261 error(WARNING, "file %s:%d illegal token\n", filename, line);
262 } else if (rv < 0) break;
263 troffontab[trindex].spacewidth = ntoken;
264 thisfont = RUNEGETGROUP(' ');
265 thischar = RUNEGETCHAR(' ');
266 for (cp = &(troffontab[trindex].charent[thisfont][thischar]); *cp != 0; cp = &((*cp)->next))
267 if ((*cp)->name)
268 if (strcmp((*cp)->name, " ") == 0)
269 break;
271 if (*cp == 0) *cp = galloc(0, sizeof(struct charent), "readtroffmetric:charent");
272 (*cp)->postfontid = thisfont;
273 (*cp)->postcharid = thischar;
274 (*cp)->troffcharwidth = ntoken;
275 (*cp)->name = galloc(0, 2, "readtroffmetric: char name");
276 (*cp)->next = 0;
277 strcpy((*cp)->name, " ");
278 } else if (strcmp(stoken, "special") == 0) {
279 troffontab[trindex].special = TRUE;
280 } else if (strcmp(stoken, "charset") == 0) {
281 line++;
282 break;
284 if (!errorflg) {
285 line++;
287 } while(!errorflg && rv>=0);
288 while(!errorflg && rv>=0) {
289 if ((rv=Bgetfield(Bfd, 's', stoken, 128)) == 0) {
290 errorflg = 1;
291 error(WARNING, "file %s:%d illegal rune token <0x%x> rv=%d\n", filename, line, troffchar, rv);
292 } else if (rv < 0) break;
293 if (utflen(stoken) > 1) specharflag = TRUE;
294 else specharflag = FALSE;
295 /* if this character is a quote we have to use the previous characters info */
296 if ((rv=Bgetfield(Bfd, 'r', &quote, 0)) == 0) {
297 errorflg = 1;
298 error(WARNING, "file %s:%d illegal width or quote token <0x%x> rv=%d\n", filename, line, quote, rv);
299 } else if (rv < 0) break;
300 if (quote == '"') {
301 /* need some code here */
303 goto flush;
304 } else {
305 Bungetrune(Bfd);
308 if ((rv=Bgetfield(Bfd, 'd', &width, 0)) == 0) {
309 errorflg = 1;
310 error(WARNING, "file %s:%d illegal width token <0x%x> rv=%d\n", filename, line, troffchar, rv);
311 } else if (rv < 0) break;
312 if ((rv=Bgetfield(Bfd, 'd', &flag, 0)) == 0) {
313 errorflg = 1;
314 error(WARNING, "file %s:%d illegal flag token <0x%x> rv=%d\n", filename, line, troffchar, rv);
315 } else if (rv < 0) break;
316 if ((rv=Bgetfield(Bfd, 'd', &charnum, 0)) == 0) {
317 errorflg = 1;
318 error(WARNING, "file %s:%d illegal character number token <0x%x> rv=%d\n", filename, line, troffchar, rv);
319 } else if (rv < 0) break;
320 flush:
321 str = Brdline(Bfd, '\n');
322 /* stash the crap from the end of the line for debugging */
323 if (debug) {
324 if (str == 0) {
325 Bprint(Bstderr, "premature EOF\n");
326 return(0);
328 str[Blinelen(Bfd)-1] = '\0';
330 line++;
331 chartorune(&troffchar, stoken);
332 if (specharflag) {
333 if (debug)
334 Bprint(Bstderr, "%s %d %d 0x%x %s # special\n",stoken, width, flag, charnum, str);
336 if (strcmp(stoken, "---") == 0) {
337 thisfont = RUNEGETGROUP(charnum);
338 thischar = RUNEGETCHAR(charnum);
339 stoken[0] = '\0';
340 } else {
341 thisfont = RUNEGETGROUP(troffchar);
342 thischar = RUNEGETCHAR(troffchar);
344 for (cp = &(troffontab[trindex].charent[thisfont][thischar]); *cp != 0; cp = &((*cp)->next))
345 if ((*cp)->name) {
346 if (debug) Bprint(Bstderr, "installing <%s>, found <%s>\n", stoken, (*cp)->name);
347 if (strcmp((*cp)->name, stoken) == 0)
348 break;
350 if (*cp == 0) *cp = galloc(0, sizeof(struct charent), "readtroffmetric:charent");
351 (*cp)->postfontid = RUNEGETGROUP(charnum);
352 (*cp)->postcharid = RUNEGETCHAR(charnum);
353 (*cp)->troffcharwidth = width;
354 (*cp)->name = galloc(0, strlen(stoken)+1, "readtroffmetric: char name");
355 (*cp)->next = 0;
356 strcpy((*cp)->name, stoken);
357 if (debug) {
358 if (specharflag)
359 Bprint(Bstderr, "%s", stoken);
360 else
361 Bputrune(Bstderr, troffchar);
362 Bprint(Bstderr, " %d %d 0x%x %s # psfontid=0x%x pscharid=0x%x thisfont=0x%x thischar=0x%x\n",
363 width, flag, charnum, str,
364 (*cp)->postfontid,
365 (*cp)->postcharid,
366 thisfont, thischar);
369 Bterm(Bfd);
370 Bflush(Bstderr);
371 return(1);
374 /* find index of troff font name in table
375 * returns -1 if name is not in table
376 * returns -2 if it cannot allocate memory
377 * returns -3 if there is a font mapping problem
378 * If insflg is not zero
379 * and the name is not found in the table, insert it.
380 */
381 int
382 findtfn(char *fontname, BOOLEAN insflg) {
383 struct troffont *tp;
384 int i, j;
386 if (debug) {
387 if (fontname==0) fprint(2, "findtfn(0x%x,%d)\n", fontname, insflg);
388 else fprint(2, "findtfn(%s,%d)\n", fontname, insflg);
390 for (i=0; i<troffontcnt; i++) {
391 if (troffontab[i].trfontid==0) {
392 error(WARNING, "findtfn:troffontab[%d].trfontid=0x%x, botch!\n",
393 i, troffontab[i].trfontid);
394 continue;
396 if (strcmp(troffontab[i].trfontid, fontname) == 0)
397 return(i);
399 if (insflg) {
400 tp = (struct troffont *)galloc(troffontab, sizeof(struct troffont)*(troffontcnt+1), "findtfn: struct troffont:");
401 if (tp == 0)
402 return(-2);
403 troffontab = tp;
404 tp = &(troffontab[troffontcnt]);
405 i = strlen(fontname);
406 tp->trfontid = galloc(0, i+1, "findtfn: trfontid:");
408 /* initialize new troff font entry with name and numeric fields to 0 */
409 strncpy(tp->trfontid, fontname, i);
410 tp->trfontid[i] = '\0';
411 tp->special = FALSE;
412 tp->spacewidth = 0;
413 tp->psfmapsize = 0;
414 tp->psfmap = 0;
415 for (i=0; i<NUMOFONTS; i++)
416 for (j=0; j<FONTSIZE; j++)
417 tp->charent[i][j] = 0;
418 troffontcnt++;
419 if (!readtroffmetric(fontname, troffontcnt-1))
420 return(-3);
421 if (!readpsfontdesc(fontname, troffontcnt-1))
422 return(-3);
423 return(troffontcnt-1);
425 return(-1);
428 void
429 finish(void) {
430 int i;
432 Bprint(Bstdout, "%s", TRAILER);
433 Bprint(Bstdout, "done\n");
434 Bprint(Bstdout, "%s", DOCUMENTFONTS);
436 for (i=0; i<pfnamcnt; i++)
437 if (pfnafontmtab[i].used)
438 Bprint(Bstdout, " %s", pfnafontmtab[i].str);
439 Bprint(Bstdout, "\n");
441 Bprint(Bstdout, "%s %d\n", PAGES, pages_printed);
445 /* Set slant to n degrees. Disable slanting if n is 0. */
446 void
447 t_slant(int n) {
448 fontslant = n;
449 curpostfontid = -1;
452 /* Set character height to n points. Disabled if n is 0 or the current size. */
454 void
455 t_charht(int n) {
456 fontheight = (n == fontsize) ? 0 : n;
457 curpostfontid = -1;