Annotation of coherent/b/bin/troff/fonts.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * fonts.c
                      3:  * Troff.
                      4:  * Font loading.
                      5:  */
                      6: 
                      7: #include <ctype.h>
                      8: #include <canon.h>
                      9: #include "roff.h"
                     10: 
                     11: static int     errflag;                /* font table read error flag */
                     12: int            nfonts;                 /* number of fonts available    */
                     13: 
                     14: /*
                     15:  * Map user font names to font numbers.
                     16:  * Several troff font names can map to the same font number.
                     17:  * Entries get added by .lf or .rf.
                     18:  */
                     19: FTB    fontab[NFNAMES];
                     20: 
                     21: /*
                     22:  * Font width table pointers, indexed by font number.
                     23:  * The order of built-in entries corresponds to the indices in "fonts.h".
                     24:  * The .lf request adds additional entries to this table.
                     25:  */
                     26: FWTAB  *fwptab[NFONTS];
                     27: 
                     28: /* Read a canonical short from file. */
                     29: int
                     30: get_short(fp) register FILE *fp;
                     31: {
                     32:        short s;
                     33: 
                     34:        if (fread(&s, sizeof s, 1, fp) != 1)
                     35:                errflag = 1;
                     36:        canint(s);
                     37:        return (int)s;
                     38: }
                     39: 
                     40: /*
                     41:  * Read a NUL-terminated string from file.
                     42:  * Return a pointer to a newly allocated copy of it.
                     43:  * Die if it is too long.
                     44:  */
                     45: #define        NBUF    256
                     46: char *
                     47: get_str(fp) register FILE *fp;
                     48: {
                     49:        static char buf[NBUF];
                     50:        register char *s;
                     51:        register int c;
                     52: 
                     53:        for (s = buf; s < &buf[NBUF]; ) {
                     54:                c = fgetc(fp);
                     55:                *s++ = c;
                     56:                if (c == '\0')
                     57:                        break;
                     58:                else if (c == EOF) {
                     59:                        errflag = 1;
                     60:                        return NULL;
                     61:                }
                     62:        }
                     63:        if (s == &buf[NBUF]) {
                     64:                errflag = 1;
                     65:                return;
                     66:        }
                     67:        s = nalloc(strlen(buf) + 1);
                     68:        strcpy(s, buf);
                     69:        return s;
                     70: }
                     71: 
                     72: void
                     73: load_font(s, file) char *s, *file;
                     74: {
                     75:        register FWTAB *p;
                     76:        register FILE *fp;
                     77:        int i, new, newflag;
                     78: 
                     79:        if ((fp = fopen(file, "rb")) == NULL) {
                     80:                /* Not found, look in default fwt directory. */
                     81:                sprintf(miscbuf, "%s%sfwt/%s",
                     82:                        LIBDIR, (pflag) ? TPSDIR : TPCLDIR, file);
                     83:                if ((fp = fopen(miscbuf, "rb")) == NULL) {
                     84:                        printe(".lf: cannot open file \"%s\"", file);
                     85:                        return;
                     86:                }
                     87:        }
                     88:        if ((new = font_num(s)) == -1) {
                     89:                /* Font does not already exist, allocate new FWTAB entry. */
                     90:                if (nfonts >= NFONTS) {
                     91:                        printe(".lf: cannot load more than %d fonts", NFONTS);
                     92:                        return;
                     93:                }
                     94:                newflag = 1;
                     95:                new = nfonts;                   /* assign new font number */
                     96:                fwptab[new] = nalloc(sizeof(FWTAB)); /* allocate new FWTAB */
                     97:        } else
                     98:                newflag = 0;
                     99:        errflag = 0;
                    100:        p = fwptab[new];
                    101:        p->f_descr = get_str(fp);
                    102:        p->f_PSname = get_str(fp);
                    103:        p->f_flags = get_short(fp);
                    104:        p->f_fonttype = get_short(fp);
                    105:        p->f_orientation = get_short(fp);
                    106:        p->f_spacing = get_short(fp);
                    107:        p->f_symset = get_short(fp);
                    108:        p->f_pitch = get_short(fp);
                    109:        p->f_psz = get_short(fp);
                    110:        p->f_style= get_short(fp);
                    111:        p->f_weight = get_short(fp);
                    112:        p->f_face = get_short(fp);
                    113:        p->f_num = get_short(fp);
                    114:        p->f_den = get_short(fp);
                    115:        for (i = 0; i < NWIDTH; i++)
                    116:                p->f_width[i] = fgetc(fp);
                    117:        i = fgetc(fp);                          /* should return EOF */
                    118:        fclose(fp);
                    119:        if (i != EOF || errflag) {
                    120:                printe(".lf: %s, file \"%s\"",
                    121:                        (i != EOF) ? "bad font width table" : "read error",
                    122:                        file);
                    123:                if (newflag)
                    124:                        free(p);
                    125:                return;
                    126:        } else if ((p->f_flags & F_PCL) == 0 && !pflag)
                    127:                printe(".lf: \"%s\" is not a PCL font width table", file);
                    128:        else if ((p->f_flags & F_PS) == 0 && pflag)
                    129:                printe(".lf: \"%s\" is not a PostScript font width table", file);
                    130:        fpsz[new] = p->f_psz;                   /* enter pointsize in env */
                    131:        fcsz[new] = 0;                          /* enter const char size in env */
                    132:        assign_font(s, new);                    /* and assign desired name */
                    133:        if (newflag)
                    134:                ++nfonts;
                    135: }
                    136: 
                    137: /*
                    138:  * List all the font names and descriptions in this version.
                    139:  */
                    140: void
                    141: font_display()
                    142: {
                    143:        register FTB *p;
                    144:        register int a, b;
                    145: 
                    146:        fprintf(stderr, "Fonts available in this version:\n");
                    147:        for (p = fontab; p < &fontab[NFNAMES]; p++) {
                    148:                if ((a = p->f_name[0]) == 0)
                    149:                        break;
                    150:                if ((b = p->f_name[1]) == 0)
                    151:                        b = ' ';
                    152:                fprintf(stderr," %c%c %s\n", a, b, fwptab[p->f_font]->f_descr);
                    153:        }
                    154:        fprintf(stderr,
                    155: "Additional fonts may be loaded with the .lf request.\n"
                    156: "Fonts may be renamed with the .rf request.\n"
                    157:                );
                    158: }
                    159: 
                    160: /*
                    161:  * Return fontname associated with font number n.
                    162:  * Because the mapping is many->one, the user might have
                    163:  * specified the font with a different name.
                    164:  * The returned value points to a statically allocated buffer.
                    165:  */
                    166: char *
                    167: fontname(n) register int n;
                    168: {
                    169:        static char buf[3];
                    170:        register FTB *p;
                    171: 
                    172:        for (p = fontab; p < &fontab[NFNAMES]; p++) {
                    173:                if (p->f_font == n) {
                    174:                        buf[0] = p->f_name[0];
                    175:                        buf[1] = p->f_name[1];
                    176:                        buf[2] = '\0';
                    177:                        return buf;
                    178:                }
                    179:        }
                    180:        return NULL;
                    181: }
                    182: 
                    183: /* end of fonts.c */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.