wdvi

network DVI viewer
Log | Files | Refs

vf.c (4490B)


      1 /*========================================================================*\
      2 
      3 Copyright (c) 1992-1999  Paul Vojta
      4 
      5 Permission is hereby granted, free of charge, to any person obtaining a copy
      6 of this software and associated documentation files (the "Software"), to
      7 deal in the Software without restriction, including without limitation the
      8 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
      9 sell copies of the Software, and to permit persons to whom the Software is
     10 furnished to do so, subject to the following conditions:
     11 
     12 The above copyright notice and this permission notice shall be included in
     13 all copies or substantial portions of the Software.
     14 
     15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     18 PAUL VOJTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
     19 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
     20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     21 
     22 \*========================================================================*/
     23 
     24 /*
     25  *	VF font reading routines.
     26  *	Public routine is read_index---because virtual characters are presumed
     27  *	to be short, we read the whole virtual font in at once, instead of
     28  *	faulting in characters as needed.
     29  */
     30 
     31 #include <err.h>
     32 
     33 #include "font.h"	/* struct font */
     34 #include "xdvi.h"
     35 #include "dvi.h"
     36 #include "util.h"	/* xmalloc() */
     37 
     38 #define	LONG_CHAR	242
     39 
     40 /*
     41  *	These are parameters which determine whether macros are combined for
     42  *	storage allocation purposes.  Small macros ( <= VF_PARM_1 bytes) are
     43  *	combined into chunks of size VF_PARM_2.
     44  */
     45 
     46 #ifndef	VF_PARM_1
     47 #define	VF_PARM_1	20
     48 #endif
     49 #ifndef	VF_PARM_2
     50 #define	VF_PARM_2	256
     51 #endif
     52 
     53 /*
     54  *	The main routine
     55  */
     56 
     57 void
     58 read_VF_index(struct font *fontp, Boolean hushcs)
     59 {
     60 	FILE	*VF_file = fontp->file;
     61 	unsigned char	cmnd;
     62 	unsigned char	*avail, *availend; /* available space for macros */
     63 	long	checksum;
     64 
     65 	fontp->read_char = NULL;
     66 	fontp->flags |= FONT_VIRTUAL;
     67 	fontp->set_char_p = set_vf_char;
     68 	if (debug & DBG_PK)
     69 	    printf("Reading VF pixel file %s\n", fontp->filename);
     70 /*
     71  *	Read preamble.
     72  */
     73 	fseek(VF_file, (long) one(VF_file), 1);	/* skip comment */
     74 	checksum = four(VF_file);
     75 	if (checksum != fontp->checksum && checksum != 0 && fontp->checksum != 0
     76 		&& !hushcs)
     77 	    warnx("Checksum mismatch (dvi = %lu, vf = %lu) in font file '%s'",
     78 		fontp->checksum, checksum, fontp->filename);
     79 	(void) four(VF_file);		/* skip design size */
     80 /*
     81  *	Read the fonts.
     82  */
     83 	fontp->vf_table = xmalloc(VFTABLELEN * sizeof(struct font *));
     84 	bzero((char *) fontp->vf_table, VFTABLELEN * sizeof(struct font *));
     85 	fontp->vf_chain = NULL;
     86 	fontp->first_font = NULL;
     87 	while ((cmnd = one(VF_file)) >= FNTDEF1 && cmnd <= FNTDEF4) {
     88 	    struct font *newfontp = define_font(VF_file, cmnd, fontp,
     89 		fontp->vf_table, VFTABLELEN, &fontp->vf_chain, 0);
     90 	    if (fontp->first_font == NULL) fontp->first_font = newfontp;
     91 	}
     92 /*
     93  *	Prepare macro array.
     94  */
     95 	fontp->macro = xmalloc(256 * sizeof(struct macro));
     96 	bzero((char *) fontp->macro, 256 * sizeof(struct macro));
     97 /*
     98  *	Read macros.
     99  */
    100 	avail = availend = NULL;
    101 	for (; cmnd <= LONG_CHAR; cmnd = one(VF_file)) {
    102 	    struct macro *m;
    103 	    int len;
    104 	    unsigned long cc;
    105 	    long width;
    106 
    107 	    if (cmnd == LONG_CHAR) {	/* long form packet */
    108 		len = four(VF_file);
    109 		cc = four(VF_file);
    110 		width = four(VF_file);
    111 		if (cc >= 256) {
    112 		    warnx("Virtual character %lu in font '%s' ignored.",
    113 			cc, fontp->fontname);
    114 		    fseek(VF_file, (long) len, 1);
    115 		    continue;
    116 		}
    117 	    }
    118 	    else {	/* short form packet */
    119 		len = cmnd;
    120 		cc = one(VF_file);
    121 		width = num(VF_file, 3);
    122 	    }
    123 	    m = &fontp->macro[cc];
    124 	    m->dvi_adv = width * fontp->dimconv;
    125 	    if (len > 0) {
    126 		if (len <= availend - avail) {
    127 		    m->pos = avail;
    128 		    avail += len;
    129 		}
    130 		else {
    131 		    m->free_me = True;
    132 		    if (len <= VF_PARM_1) {
    133 			m->pos = avail = xmalloc(VF_PARM_2);
    134 			availend = avail + VF_PARM_2;
    135 			avail += len;
    136 		    }
    137 		    else m->pos = xmalloc((unsigned) len);
    138 		}
    139 		fread((char *) m->pos, 1, len, VF_file);
    140 		m->end = m->pos + len;
    141 	    }
    142 	    if (debug & DBG_PK)
    143 		printf("Read VF macro for character %lu; dy = %ld, length = %d\n",
    144 			cc, m->dvi_adv, len);
    145 	}
    146 	if (cmnd != POST)
    147 	    errx(1, "Wrong command byte found in VF macro list:  %d", cmnd);
    148 
    149 	fclose(VF_file);
    150 	fontp->file = NULL;
    151 }