Back to index

tetex-bin  3.0
tfmload.c
Go to the documentation of this file.
00001 /* This code was stolen from dvips for xdvi use.  The file itself did
00002  * not carry any licensing info.  According to other filed the code
00003  * either public domain, or, in the worst case, GPL.
00004  *
00005  * Loads a tfm file.  It marks the characters as undefined.
00006  *
00007  * Modified for use by xdvi/t1 by Nicolai Langfeldt */
00008 
00009 #include <stdio.h>
00010 
00011 #include "xdvi-config.h"
00012 #include "xdvi.h"
00013 #include "util.h"
00014 #include "tfmload.h"
00015 
00016 #ifdef T1LIB
00017 
00018 #include "kpathsea/tex-file.h"
00019 
00020 int fallbacktfm = 0;
00021 
00022 Boolean
00023 tfmload(const char *name, long *design, long *widths, long *fontdimen2)
00024 {
00025     int i;
00026     int li;
00027     int nw, hd;
00028     int bc, ec;
00029     int f_param_off, f_param_num;
00030     long wtab[256];
00031     unsigned short chardat[256];
00032     char *filename;
00033     FILE *curfnt;
00034 
00035     fallbacktfm = 0;
00036 
00037     filename = kpse_find_tfm(name);
00038 
00039     if (filename == NULL) {
00040       filename = kpse_find_tfm("cmr10.tfm");
00041       if (fallbacktfm == 0)
00042          fallbacktfm = 1;
00043     }
00044 
00045     if (filename == NULL)
00046        return False;
00047 
00048     curfnt = XFOPEN(filename, "r");
00049 
00050     if (curfnt == NULL)
00051        return False;
00052 
00053     /* Next, we read the font data from the tfm file, and store it in
00054      * our own array.  */
00055 
00056     /* Lengths section, 16 bit unsigned quanities */
00057     li = get_bytes(curfnt, 2);     /* lf = length of file, in words(4 bytes) */
00058     hd = get_bytes(curfnt, 2);     /* lh = length of header data, in words */
00059     bc = get_bytes(curfnt, 2);     /* bc = lowest charcode */
00060     ec = get_bytes(curfnt, 2);     /* ec = largest charcode */
00061     nw = get_bytes(curfnt, 2);     /* nw = number of width words */
00062     f_param_off = get_bytes(curfnt, 2);   /* nh = number of height words */
00063     f_param_off += get_bytes(curfnt, 2);  /* nd = number of depth words */
00064     f_param_off += get_bytes(curfnt, 2);  /* ni = number of italic words */
00065     f_param_off += get_bytes(curfnt, 2);  /* nl = number of words in lig/kern table */
00066     f_param_off += get_bytes(curfnt, 2);  /* nk = number of words in the kern table */
00067     f_param_off += get_bytes(curfnt, 2);  /* ne = number of words in the extensible char tab */
00068     f_param_num = get_bytes(curfnt, 2);   /* np = number of font parameter words */
00069 
00070     /* Header, 32 bit signed quantities */
00071     get_bytes(curfnt, 4);   /* header[0]: checksum */
00072     *design = get_bytes(curfnt, 4);       /* header[1]: design size */
00073 
00074     /* Skip the rest: header[2..(hd-1)] */
00075     for (i = 2; i < hd; i++)
00076        li = get_bytes(curfnt, 4);
00077 
00078     /* Initialize to undef value */
00079     for (i = 0; i < 256; i++) {
00080        chardat[i] = 256;
00081        widths[i] = 0;
00082     }
00083 
00084     /* The charinfo array: */
00085     for (i = bc; i <= ec; i++) {
00086        chardat[i] = get_byte(curfnt);     /* 8 bits of width index */
00087        get_byte(curfnt);    /* skip */
00088        get_bytes(curfnt, 2);       /* skip, jump */
00089     }
00090 
00091     /* The (compressed!) width table */
00092     for (i = 0; i < nw; i++)
00093        wtab[i] = get_bytes(curfnt, 4);
00094 
00095     /* skip to font parameter words */
00096     for (i = 0; i < f_param_off; i++)
00097        get_bytes(curfnt, 4);
00098 
00099     /* read fontdimen2 (width of space) */
00100     for (i = 0; i < f_param_num; i++) {
00101        long l = get_bytes(curfnt, 4);
00102        if (i == 1) {
00103            *fontdimen2 = l;
00104            if (globals.debug & DBG_T1)
00105               fprintf(stderr, "fontdimen2 of %s: %ld\n", filename, *fontdimen2);
00106        }
00107            
00108     }
00109     (void)fclose(curfnt);
00110 
00111     /* Now we have the table and the indexes.  Expand into a directly
00112        mapped width array */
00113     for (i = bc; i <= ec; i++)
00114        if (chardat[i] != 256)
00115            widths[i] = wtab[chardat[i]];
00116 
00117     return True;
00118 }
00119 
00120 #endif /* T1LIB */