4 * width functions, sizes and fonts
11 int fontlab[MAXFONTS+1];
12 int cstab[MAXFONTS+1];
13 int ccstab[MAXFONTS+1];
14 int bdtab[MAXFONTS+1];
46 if (sfbits(j) == oldbits) {
51 if (i < nchnames + ALPHABET && widcache[i].fontpts == (xfont<<8) + xpts && !setwdf)
52 k = widcache[i].width;
65 * clear width cache-- s means just space
72 widcache[' '].fontpts = 0;
75 for (i=0; i<NWIDCACHE; i++)
76 widcache[i].fontpts = 0;
80 onfont(int n, int f) /* is char n on font f? */
88 if (fp->wp[n].num == n) /* ascii at front */
93 cp = &fp->wp[ALPHABET];
94 ep = &fp->wp[fp->nchars];
95 for ( ; cp < ep; cp++) /* search others */
97 return cp - &fp->wp[0];
98 /* maybe it was a \N... */
101 i = atoi(np+1); /* sscanf(np+1, "%d", &i); */
103 ep = &fp->wp[fp->nchars];
104 for ( ; cp < ep; cp++) { /* search others */
106 return cp - &fp->wp[0];
108 return -2; /* a \N that doesn't have an entry */
110 return -1; /* vanilla not found */
123 if (i == ' ') { /* a blank */
124 k = (fp->spacewidth * spacesz + 6) / 12;
125 /* this nonsense because .ss cmd uses 1/36 em as its units */
126 /* and default is 12 */
127 } else if ((n = onfont(i, xfont)) >= 0) { /* on this font at n */
130 numtabp[CT].val |= fp->wp[n].kern;
131 } else if (n == -2) { /* \N with default width */
133 k = fp->defaultwidth;
134 } else { /* not on current font */
136 k = fp->defaultwidth; /* default-size space */
139 for (ii=smnt, jj=0; jj < nfonts; jj++, ii=ii % nfonts + 1) {
140 if ((n = onfont(i, ii)) >= 0) {
141 k = fonts[ii].wp[n].wid;
145 numtabp[CT].val |= fonts[ii].wp[n].kern;
153 if (cs = cstab[xfont]) {
155 if (ccs = ccstab[xfont])
159 cs = (cs * EMPTS(x)) / 36;
161 /* was (k & BYTEMASK); since .wid is unsigned, should never happen */
163 ERROR "can't happen: negative width %d in getcw %d\n", k, i WARN;
164 k = (k * xpts + (Unitwidth / 2)) / Unitwidth;
166 widcache[i].fontpts = 0;
168 widcache[i].fontpts = (xfont<<8) + xpts;
169 widcache[i].width = k;
172 /* Unitwidth is Units/Point, where
173 /* Units is the fundamental digitization
174 /* of the character set widths, and
175 /* Point is the number of goobies in a point
176 /* e.g., for cat, Units=36, Point=6, so Unitwidth=36/6=6
177 /* In effect, it's the size at which the widths
178 /* translate directly into units.
182 void xbits(Tchar i, int bitf)
213 /* these next two functions ought to be the same in troff and nroff, */
214 /* but the data structures they search are different. */
215 /* silly historical problem. */
226 if (c == '(') { /* \(xx */
227 if ((*s++ = getach()) == 0 || (*s++ = getach()) == 0)
229 } else { /* \C'...' */
231 while ((*s = getach()) != c && *s != 0 && s < temp + sizeof(temp) - 1)
236 return chadd(temp, Troffchar, Install) | chbits; /* add name even if haven't seen it */
239 j = chadd(temp, Troffchar, Lookup);
245 return chadd(temp, Troffchar, Install) | chbits; /* add name even if haven't seen it */
250 Tchar t_setabs(void) /* set absolute char from \N'...' */
261 sprintf(temp, "%d", n); /* convert into "#n" */
262 n = chadd(temp, Number, Install);
268 * fontlab[] is a cache that contains font information
270 * fontlab[] contains the 1- or 2-character name of the
271 * font current associated with that font.
272 * fonts 1..nfonts correspond to the mounted fonts;
273 * the last of these are the special fonts.
274 * If we don't use the (named) font in one of the
275 * standard positions, we install the name in the next
276 * free slot of fontlab[] and font[].
277 * Whenever we need info about the font, we
278 * read in the data into the next free slot with getfont.
279 * The ptfont() (t10.c) routine will tell
280 * the device filter to put the font always at position
281 * zero if xfont > nfonts, so no need to change these filters.
282 * Yes, this is a bit kludgy.
284 * This gives the new specs of findft:
285 * find the font name i, where i also can be a number.
286 * Installs the font(name) i when not present
287 * returns -1 on error
298 if (isdigit(p[0])) { /* first look for numbers */
300 if (p[1] > 0 && isdigit(p[1]))
301 k = 10 * k + p[1] - '0';
302 if (k > 0 && k <= nfonts && k < smnt)
303 return(k); /* mounted font: .ft 3 */
304 if (fontlab[k] && k <= MAXFONTS) { /* translate */
305 return(k); /*number to a name */
307 fprintf(stderr, "troff: no font at position %d\n", k);
308 return(-1); /* wild number */
313 * Now we look for font names
315 for (k = 1; fontlab[k] != i; k++) {
317 return(-1); /* running out of fontlab space */
318 if (fontlab[k] == 0) { /* passed all existing names */
319 if (setfp(k, i, (char *) 0, 1) == -1)
322 fontlab[k] = i; /* install the name */
327 return(k); /* was one of the existing names */
340 i = inumb(&apts); /* this is a disaster for fractional point sizes */
354 * in olden times, it used to ignore changes to 0 or negative.
355 * this is meant to allow the requested size to be anything,
356 * in particular so eqn can generate lots of \s-3's and still
357 * get back by matching \s+3's.
374 for (j=k=0 ; pstab[j] != 0 ; j++)
375 if (abs(pstab[j]-i) < abs(pstab[k]-i))
387 for (j = 0; i > (k = pstab[j]); j++)
393 setsbits(chbits, ++j);
394 setfbits(chbits, font);
395 sps = width(' ' | chbits);
405 if (isdigit(i)) { /* \sd or \sdd */
407 if (i == 0) /* \s0 */
409 else if (i <= 3 && (ch=getch()) && isdigit(j = cbits(ch))) { /* \sdd */
410 j = 10 * i + j - '0';
414 } else if (i == '(') { /* \s(dd */
415 j = cbits(getch()) - '0';
416 j = 10 * j + cbits(getch()) - '0';
417 if (j == 0) /* \s(00 */
419 } else if (i == '+' || i == '-') { /* \s+, \s- */
421 if (isdigit(j)) { /* \s+d, \s-d */
423 } else if (j == '(') { /* \s+(dd, \s-(dd */
424 j = cbits(getch()) - '0';
425 j = 10 * j + cbits(getch()) - '0';
435 Tchar t_setht(void) /* set character height from \H'...' */
443 if (n == 0 || nonumb)
444 n = apts; /* does this work? */
448 setfbits(c, pts); /* sneaky, CHARHT font bits are size bits */
452 Tchar t_setslant(void) /* set slant from \S'...' */
481 void t_setfont(int a)
489 if (!i || i == 'P') {
493 if (/* i == 'S' || */ i == '0') /* an experiment -- why can't we change to it? */
495 if ((j = findft(i)) == -1)
496 if ((j = setfp(0, i, (char*) 0, 1)) == -1) /* try to put it in position 0 */
510 int savhp, savapts, savapts1, savfont, savfont1, savpts, savpts1;
512 base = numtabp[ST].val = numtabp[SB].val = wid = numtabp[CT].val = 0;
513 if (ismot(i = getch()))
516 savhp = numtabp[HP].val;
525 while (cbits(i = getch()) != delim && !nlflg) {
528 numtabp[HP].val += k;
530 emsz = (INCH/72) * xpts;
531 } else if (isvmot(i)) {
539 if (base < numtabp[SB].val)
540 numtabp[SB].val = base;
541 if ((k = base + emsz) > numtabp[ST].val)
544 setn1(wid, 0, (Tchar) 0);
545 numtabp[HP].val = savhp;
578 getch(); /*eat delim*/
582 i = makem(quant(n, j));
617 if (Hor > 1 && !vflag)
618 j = (j + Hor/2)/Hor * Hor;
635 if ((lf = fonts[fbits(i)].ligfont) == 0) /* font lacks ligatures */
638 if (cbits(j) == 'i' && (lf & LFI))
640 else if (cbits(j) == 'l' && (lf & LFL))
642 else if (cbits(j) == 'f' && (lf & LFF)) {
643 if ((lf & (LFFI|LFFL)) && lg != 2) {
645 if (cbits(k)=='i' && (lf&LFFI))
647 else if (cbits(k)=='l' && (lf&LFFL))
659 return(i & SFMASK | j);
688 i = 10 * i + j - '0';
690 if (i <= 0 || i > nfonts)
691 ERROR "fp: bad font position %d", i WARN;
692 else if (skip() || !(j = getrq()))
693 ERROR "fp: no font name" WARN;
694 else if (skip() || !getname())
695 setfp(i, j, (char*) 0, 1);
696 else /* 3rd argument = filename */
697 setfp(i, j, nextf, 1);
700 char *strdupl(const char *s) /* make a copy of s */
704 t = (char *) malloc(strlen(s) + 1);
706 ERROR "out of space in strdupl(%s)", s FATAL;
712 setfp(int pos, int f, char *truename, int print) /* mount font f at position pos[0...nfonts] */
714 char pathname[NS], shortname[NS], *sl;
719 strcpy(shortname, truename);
721 strcpy(shortname, (char *) unpair(f));
722 if (truename && strrchr(truename, '/')) { /* .fp 1 R dir/file: use verbatim */
723 sprintf(pathname, "%s", truename);
724 if (fonts[pos].truename)
725 free(fonts[pos].truename);
726 fonts[pos].truename = strdupl(truename);
727 } else if (truename) { /* synonym: .fp 1 R Avant */
728 sprintf(pathname, "%s/dev%s/%s", fontdir, devname, truename);
729 truename = 0; /* so doesn't get repeated by ptfpcmd */
730 } else /* vanilla: .fp 5 XX */
731 sprintf(pathname, "%s/dev%s/%s", fontdir, devname, shortname);
732 if (truename == 0 && fonts[pos].truename != 0) {
733 free(fonts[pos].truename);
734 fonts[pos].truename = 0;
736 if (getfont(pathname, pos) < 0) {
737 ERROR "Can't open font file %s", pathname WARN;
740 if (print && !ascii) {
741 ptfpcmd(pos, fonts[pos].longname, truename);
749 if (smnt == 0 && fonts[pos].specfont)
751 bdtab[pos] = cstab[pos] = ccstab[pos] = 0;
756 * .cs request; don't check legality of optional arguments
768 if (!(i = getrq()) || (i = findft(i)) < 0)
777 ccstab[i] = findps(j);
798 if (skip() || !(i = getrq()) || (j = findft(i)) == -1) {
830 dfact = INCH; /* default scaling is points! */
853 sps = width(' ' | chbits);
862 /* stores \x'...' into two successive Tchars.
863 /* the first contains HX, the second the value,
864 /* encoded as a vertical motion.
865 /* decoding is done in n2.c by pchar().
871 i = quant(atoi0(), VERT);
875 *pbp++ = MOT | VMOT | i;
877 *pbp++ = MOT | VMOT | NMOT | -i;
883 static Uchar name[3];
885 name[0] = i & SHORTMASK;
886 name[1] = (i >> SHORT) & SHORTMASK;