Annotation of researchv10no/cmd/dimpress/rast.c, revision 1.1

1.1     ! root        1: 
        !             2: /*
        !             3:  *
        !             4:  * A few routines that read and process raster files supplied by Imagen.
        !             5:  *
        !             6:  * Impress, at least at the time these routines were written, only allowed
        !             7:  * us to use families 0 to 95. That imposes a limit on the total number of
        !             8:  * raster files that any one job can use, unless we do some kind of memory
        !             9:  * management, and that in turn restricts the number of entries in array
        !            10:  * fam_data[] to 96. Unless this limit is changed MAXFAMILY should never be
        !            11:  * defined to be greater than 95.
        !            12:  *
        !            13:  */
        !            14: 
        !            15: 
        !            16: #include <stdio.h>
        !            17: #include <sys/types.h>
        !            18: #include <sys/stat.h>
        !            19: 
        !            20: #include "ext.h"                       /* external variable definitions */
        !            21: #include "gen.h"                       /* definitions used by everyone */
        !            22: #include "rast.h"                      /* raster file definitions */
        !            23: #include "impcodes.h"                  /* Impress 2.0 opcodes */
        !            24: 
        !            25: 
        !            26: Rst            rst[] = RST_INIT;       /* describes raster file structure */
        !            27: 
        !            28: Rastdata       fam_data[MAXFAMILY+1];  /* data about raster files we've read */
        !            29: 
        !            30: int            fam_num = 0;            /* use this as next family number */
        !            31: int            next_fam = 0;           /* next free spot in fam_data[] */
        !            32: int            cur_fam = 0;            /* family number we're using right now */
        !            33: int            last_fam = MAXFAMILY+1; /* last one we told printer about */
        !            34: Rastdata       *fam = NULL;            /* &fam_data[cur_fam] */
        !            35: 
        !            36: int            rast_res = RAST_RES;    /* raster table resolution */
        !            37: 
        !            38: 
        !            39: /*****************************************************************************/
        !            40: 
        !            41: 
        !            42: getfamdata(f, s)
        !            43: 
        !            44: 
        !            45:     char       *f;                     /* find data for this font */
        !            46:     int                s;                      /* in this point size */
        !            47: 
        !            48: 
        !            49: {
        !            50: 
        !            51: 
        !            52:     int                i;                      /* check fam_data[i] next */
        !            53: 
        !            54: 
        !            55: /*
        !            56:  *
        !            57:  * Looks for the family data for font *f and size s in the fam_data[]
        !            58:  * data structure. Returns the index where it was found, or next_fam
        !            59:  * if not found.
        !            60:  *
        !            61:  */
        !            62: 
        !            63: 
        !            64:     for ( i = 0; i < next_fam; i++ )
        !            65:        if ( fam_data[i].size == s  &&  strncmp(f, fam_data[i].name, L_FNAME) == 0 )
        !            66:            break;
        !            67: 
        !            68:     return(i);
        !            69: 
        !            70: }   /* End of getfamdata */
        !            71: 
        !            72: 
        !            73: /*****************************************************************************/
        !            74: 
        !            75: 
        !            76: getrastdata(f, s)
        !            77:     char       *f;                     /* use this font's raster file */
        !            78:     int                s;                      /* in this point size */
        !            79: {
        !            80:     int                i;                      /* loop index for setting families */
        !            81: /*
        !            82:  * Called to find the raster file info for font f, size s in fam_data[].
        !            83:  * If it's not there it will be read in from the appropriate file in
        !            84:  * *rastdir and all the required info will be added to location
        !            85:  * next_fam in fam_data[].
        !            86:  *
        !            87:  * I'm assuming that each raster file contains at most 128 glyphs. If
        !            88:  * that's not the case we'll need to change the way things are done
        !            89:  * because Impress only allows 128 members in each family. Brian took
        !            90:  * account of of larger raster files in di10.c and it wouldn't be all
        !            91:  * that difficult to do it here too, but I don't think it's necessary.
        !            92:  */
        !            93:     cur_fam = getfamdata(f, s);                /* use this family next */
        !            94:     fam = &fam_data[cur_fam];          /* its entry in fam_data[] */
        !            95: 
        !            96:     if ( cur_fam >= next_fam )  {      /* didn't find it */
        !            97:        if ( next_fam > MAXFAMILY )     /* only use families 0 to 95 */
        !            98:            error(FATAL, "job uses too many raster files");
        !            99: 
        !           100:        strncpy(fam->name, f, L_FNAME); /* initialze some of the fields */
        !           101:        fam->name[L_FNAME-1] = '\0';
        !           102:        fam->size = s;
        !           103:        fam->rst = NULL;
        !           104:        fam->advance = (int *) rastalloc((MAXMEMBER+1) * sizeof(int), FALSE);
        !           105: 
        !           106:        for ( i = 0; i < ROT_COUNT; i++ )  {
        !           107:            fam->chused[i] = (unsigned char *) rastalloc((MAXMEMBER+BYTE)/BYTE, TRUE, 0);
        !           108:            fam->fam[i] = -1;
        !           109:        }   /* End for */
        !           110: 
        !           111:        next_fam++;                     /* can only use it once */
        !           112:     }   /* End if */
        !           113: 
        !           114:     if ( fam->rst == NULL )            /* need to read the whole raster file */
        !           115:        readrastfile(f, s);
        !           116: 
        !           117: }   /* End of getrastdata */
        !           118: 
        !           119: 
        !           120: /*****************************************************************************/
        !           121: 
        !           122: 
        !           123: readrastfile(f, s)
        !           124: 
        !           125: 
        !           126:     char       *f;                     /* name of the raster file */
        !           127:     int                s;                      /* in this point size */
        !           128: 
        !           129: 
        !           130: {
        !           131: 
        !           132: 
        !           133:     char       name[100];              /* raster file pathname built up here */
        !           134:     int                fd;                     /* file descriptor for *name */
        !           135:     struct stat buf;                   /* used to get raster file size */
        !           136: 
        !           137: 
        !           138: /*
        !           139:  *
        !           140:  * Reads the raster file for font f in size s from directory *rastdir
        !           141:  * and initializes some of the required fields in fam_data[cur_fam].
        !           142:  *
        !           143:  * First we try to open the appropriate raster file. Then we determine
        !           144:  * its size in bytes and try to allocate enough memory for the whole file.
        !           145:  * After we've successfully read it we close the file and then initialize
        !           146:  * the rest of the fields in fam_data[cur_fam].
        !           147:  *
        !           148:  * I've made allowances for two types of raster file naming conventions.
        !           149:  * The most common is the one that troff's post-processor will be using.
        !           150:  * The raster file names all begin with the string "font." and end with
        !           151:  * the suffix size. For example the raster table for the Roman font in size
        !           152:  * 10 would be called R.10. The other convention is the one you'll find
        !           153:  * in all the raster files supplied directly from Imagen (as of 9/15 anyway).
        !           154:  * In this case the file names all begin with the prefix string "*font.r"
        !           155:  * and again end with the size. For example the raster file for font cmti
        !           156:  * in size 12 would be cmti.r12. Since this routine is used by programs that
        !           157:  * expect both conventions I've written it to try the two different raster
        !           158:  * file names before giving up.
        !           159:  *
        !           160:  */
        !           161: 
        !           162: 
        !           163:     sprintf(name, "%s/%.10s.%d", rastdir, f, s);
        !           164:     if ( (fd = open(name, 0)) == -1 )  {
        !           165:        sprintf(name, "%s/%.10s.r%d", rastdir, f, s);
        !           166:        if ( (fd = open(name, 0)) == -1 )
        !           167:            error(FATAL, "can't open raster file for font %.10s, size %d", f, s);
        !           168:     }  /* End if */
        !           169: 
        !           170:     if ( fstat(fd, &buf) == -1 )
        !           171:        error(FATAL, "can't get file size for %s", name);
        !           172: 
        !           173:     fam->rst = rastalloc(buf.st_size, FALSE);
        !           174: 
        !           175:     if ( read(fd, fam->rst, buf.st_size) != buf.st_size )
        !           176:        error(FATAL, "can't read raster file %.10s", name);
        !           177: 
        !           178:     close(fd);                         /* all done with it - for now anyway */
        !           179: 
        !           180:     fam->glyphdir = getvalue(P_GLYDIR);
        !           181:     fam->first = getvalue(P_FIRSTGLY);
        !           182:     fam->last = getvalue(P_LASTGLY);
        !           183: 
        !           184:     if ( (fam->mag = getvalue(P_MAG)) <= 0 )
        !           185:        fam->mag = 1000;
        !           186: 
        !           187: }   /* End of readrastfile */
        !           188: 
        !           189: 
        !           190: /*****************************************************************************/
        !           191: 
        !           192: 
        !           193: freerast(count)
        !           194: 
        !           195: 
        !           196:     int                count;                  /* max number of calls to free() */
        !           197: 
        !           198: 
        !           199: {
        !           200: 
        !           201: 
        !           202:     int                called;                 /* number of calls made so far */
        !           203:     int                i;                      /* loop index */
        !           204: 
        !           205: 
        !           206: /*
        !           207:  *
        !           208:  * Tries to free all the memory used to store raster file data for
        !           209:  * at most count entires in array fam_data[]. The actual number of
        !           210:  * calls to free() is returned to the caller. A return value of 0
        !           211:  * obviously means there's no more memory to release. We don't want to
        !           212:  * free the memory used to keep track of the characters we've downloaded
        !           213:  * because we may want to use this raster file later on and we'll need to
        !           214:  * know which glyphs have been downloaded.
        !           215:  *
        !           216:  * Although I don't expect to use this routine often, some of the
        !           217:  * raster files are huge and there will undoubtedly be a few jobs
        !           218:  * that won't be able to keep all the required raster data in memory
        !           219:  * at the same time.
        !           220:  *
        !           221:  */
        !           222: 
        !           223: 
        !           224:     for ( called = 0, i = 0; i < next_fam  &&  called < count; i++ )
        !           225:        if ( fam_data[i].rst != NULL )  {
        !           226:            free(fam_data[i].rst);
        !           227:            fam_data[i].rst = NULL;
        !           228:            called++;
        !           229:        }   /* End if */
        !           230: 
        !           231:     return(called);
        !           232: 
        !           233: }   /* End of freerast */
        !           234: 
        !           235: 
        !           236: /*****************************************************************************/
        !           237: 
        !           238: 
        !           239: resetrast()
        !           240: 
        !           241: 
        !           242: {
        !           243: 
        !           244: 
        !           245:     int                i, j;                   /* loop indices - family and angle */
        !           246: 
        !           247: 
        !           248: /*
        !           249:  *
        !           250:  * Called when we want to clear the fam_data[] array, free all the memory
        !           251:  * associated with each raster file, and reset all the variables used to
        !           252:  * access the raster file info. Not used when we're generating Impress
        !           253:  * commands used to print files, but it may be useful when we build and
        !           254:  * edit raster files and troff's ASCII font files - maybe it shouldn't
        !           255:  * be in this file.
        !           256:  *
        !           257:  */
        !           258: 
        !           259: 
        !           260:     for ( i = 0; i < next_fam; i++ )  {
        !           261:        free(fam_data[i].rst);
        !           262:        free(fam_data[i].advance);
        !           263:        fam_data[i].name[0] = '\0';
        !           264:        fam_data[i].size = 0;
        !           265:        for ( j = 0; j < ROT_COUNT; j++ )
        !           266:            free(fam_data[i].chused[j]);
        !           267:     }  /* End for */
        !           268: 
        !           269:     next_fam = 0;
        !           270:     cur_fam = 0;
        !           271:     last_fam = -1;
        !           272:     fam = NULL;
        !           273: 
        !           274: }   /* End of resetrast */
        !           275: 
        !           276: 
        !           277: /*****************************************************************************/
        !           278: 
        !           279: 
        !           280: char *rastalloc(size, init, val)
        !           281: 
        !           282: 
        !           283:     unsigned   size;                   /* allocate this many bytes */
        !           284:     int                init;                   /* if TRUE initialize bytes */
        !           285:     int                val;                    /* to these values */
        !           286: 
        !           287: 
        !           288: {
        !           289: 
        !           290: 
        !           291:     char       *ptr;                   /* block of memory we just got */
        !           292:     int                i;                      /* loop index for initialization */
        !           293: 
        !           294: 
        !           295: /*
        !           296:  *
        !           297:  * Tries to allocate a block of memory that's large enough to be used for
        !           298:  * storage of size bytes. If init is TRUE the memory will be initialized
        !           299:  * to val. It's assumed that the memory is going to be used to store
        !           300:  * a raster file or other related data. We'll keep trying to get enough
        !           301:  * memory as long as some is being used for other raster files.
        !           302:  *
        !           303:  */
        !           304: 
        !           305: 
        !           306:     while ( (ptr = (char *) malloc(size)) == NULL )
        !           307:        if ( freerast(1) == 0 )
        !           308:            error(FATAL, "can't allocate memory for raster file");
        !           309: 
        !           310:     if ( init == TRUE )                        /* set all bytes to val */
        !           311:        for ( i = 0; i < size; i++ )
        !           312:            *(ptr + i) = val;
        !           313: 
        !           314:     return(ptr);
        !           315: 
        !           316: }   /* End of rastalloc */
        !           317: 
        !           318: 
        !           319: /*****************************************************************************/
        !           320: 
        !           321: 
        !           322: download(c, advance, angle, fp)
        !           323: 
        !           324: 
        !           325:     int                c;                      /* download this glyph? */
        !           326:     int                advance;                /* using this as the advance width */
        !           327:     int                angle;                  /* glyph rotation - just 2 bits */
        !           328:     FILE       *fp;                    /* Impress commands written here */
        !           329: 
        !           330: 
        !           331: {
        !           332: 
        !           333: 
        !           334:     Glyph      glyph;                  /* all of c's important data */
        !           335:     int                map_size;               /* bytes needed to store the bitmap */
        !           336:     int                i;                      /* loop index for downloading glyph */
        !           337: 
        !           338: 
        !           339: /*
        !           340:  *
        !           341:  * Downloads glyph number c, using the raster file fam->rst and advance
        !           342:  * as the character advance. All the Impress commands are written to FILE
        !           343:  * *fp. If advance is greater than or equal to zero we'll assume the caller
        !           344:  * wants to use the value as the character advance for the glyph, otherwise
        !           345:  * we'll get the value from the raster file. Returns the member number in
        !           346:  * the current family that corresponds to glyph c.
        !           347:  *
        !           348:  * I've added glyph rotation capabilities to this routine. The parameter
        !           349:  * angle selects the absolute orientation of the glyph. Each angle needs
        !           350:  * to be handled as a separate family. That means we'll want separate
        !           351:  * chused bitmaps and family numbers for each orientation. I've added
        !           352:  * the necessary fields to Rastdata. The fam[] field is new and really
        !           353:  * needed so we don't waste our limited family numbers on orientations
        !           354:  * that will never be used in the document. The Impress family is no
        !           355:  * longer the font's index in fam_data[].
        !           356:  *
        !           357:  * Does much of the stuff handled by xychar() in di10.c except for the
        !           358:  * positioning of the reference point and printing the member. I've tried
        !           359:  * to make things a little more general so I'm assuming that whoever calls
        !           360:  * it has done the appropriate positioning and will take care of printing
        !           361:  * the glyph.
        !           362:  *
        !           363:  */
        !           364: 
        !           365: 
        !           366:     if ( c < fam->first  ||  c > fam->last )
        !           367:        error(FATAL, "glyph %d out of range for font %.10s", c, fam->name);
        !           368: 
        !           369:     if ( fam->fam[angle] == -1 || fam->fam[angle] + c/128 != last_fam )  {     /* new family */
        !           370:        if ( fam->fam[angle] == -1 )  {
        !           371:            fam->fam[angle] = fam_num;
        !           372:            if ( (fam_num += 1 + fam->last/128) > MAXFAMILY )
        !           373:                error(FATAL, "job uses too many families");
        !           374:        }   /* End if */
        !           375:        putc(ASF, fp);
        !           376:        putc(last_fam = fam->fam[angle] + c/128, fp);
        !           377:     }  /* End if */
        !           378: 
        !           379:     if ( ! checkbit(fam->chused[angle], c) )  {        /* first use of this glyph */
        !           380:        setbit(fam->chused[angle], c);          /* don't download it again */
        !           381: 
        !           382:        fam->advance[c] = (advance < 0 ) ? PIXEL_WIDTH(getvalue(G_CHWIDTH, c), rast_res) : advance;
        !           383: 
        !           384:        glyph.xref = getvalue(G_XREF, c);       /* we'll need these soon */
        !           385:        glyph.yref = getvalue(G_YREF, c);
        !           386:        glyph.width = getvalue(G_WIDTH, c);
        !           387:        glyph.height = getvalue(G_HEIGHT, c);
        !           388:        glyph.advance = fam->advance[c];
        !           389:        glyph.bptr = fam->rst + getvalue(G_BPTR, c);
        !           390: 
        !           391:        if ( angle != ROT_0 )
        !           392:            rotate(&glyph, angle);
        !           393: 
        !           394:        if ( (map_size = glyph.height * ((glyph.width + BYTE - 1) / BYTE)) > 0 )  {
        !           395:            putc(ABGLY, fp);            /* define the glyph */
        !           396:            putint((angle << 14) | (last_fam << 7) | c % 128, fp);
        !           397:            putint(glyph.advance, fp);
        !           398:            putint(glyph.width, fp);
        !           399:            putint(glyph.xref, fp);
        !           400:            putint(glyph.height, fp);
        !           401:            putint(glyph.yref, fp);
        !           402: 
        !           403:            for ( i = map_size; i--; )  /* followed by its bitmap */
        !           404:                putc(*glyph.bptr++, fp);
        !           405:        } else error(NON_FATAL, "Bad glyph - font %.10s, glyph %d", fam->name, c);
        !           406:     }  /* End if */
        !           407: 
        !           408:     return(c % 128);
        !           409: 
        !           410: }   /* End of download */
        !           411: 
        !           412: 
        !           413: /*****************************************************************************/
        !           414: 
        !           415: 
        !           416: unsigned getvalue(field, ch)
        !           417: 
        !           418: 
        !           419:     int                        field;          /* position in rst[] */
        !           420:     int                        ch;             /* character number - for glyphs */
        !           421: 
        !           422: 
        !           423: {
        !           424: 
        !           425: 
        !           426:     register int       n;              /* number of bytes in the field */
        !           427:     register char      *p;             /* start of the field */
        !           428:     register unsigned  value;          /* result after decoding field */
        !           429: 
        !           430: 
        !           431: /*
        !           432:  *
        !           433:  * Called to figure out the value field in the current family's raster file.
        !           434:  * Right now things don't work for strings, but they're not really important.
        !           435:  *
        !           436:  * You'll see quite a few calls to this routine without the parameter ch.
        !           437:  * It's only used when we're accessing the glyph directory so I've omitted
        !           438:  * passing a value when we're looking at the preamble.
        !           439:  *
        !           440:  */
        !           441: 
        !           442: 
        !           443:     n = rst[field].size;               /* number of bytes we want to grab */
        !           444: 
        !           445:     p = fam->rst + rst[field].offset;  /* right except for glyph directory */
        !           446: 
        !           447:     if ( rst[field].glyphdir == TRUE ) /* it's really part of the directory */
        !           448:        p += GLYPH_PTR(ch);
        !           449: 
        !           450:     value = (rst[field].type == INTEGER && (*p & 0200)) ? ~0 : 0;
        !           451: 
        !           452:     for ( ; n > 0; n--, p++ )
        !           453:        value = (value << BYTE) | (*p & BMASK);
        !           454: 
        !           455:     return(value);
        !           456: 
        !           457: }   /* End of getvalue */
        !           458: 
        !           459: 
        !           460: /*****************************************************************************/
        !           461: 
        !           462: 
        !           463: checkbit(p, n)
        !           464: 
        !           465: 
        !           466:     char       *p;                     /* start of the bitmap */
        !           467:     int                n;                      /* see if this bit is set */
        !           468: 
        !           469: 
        !           470: {
        !           471: 
        !           472: 
        !           473: /*
        !           474:  *
        !           475:  * Returns the value (0 or 1) set in the n-th bit in the bitmap that
        !           476:  * starts at address p. Primarily used to keep track of which glyphs
        !           477:  * have already been downloaded, but it's also needed in the routine
        !           478:  * that dumps bitmaps out to files. It probably should be defined as
        !           479:  * a macro.
        !           480:  *
        !           481:  */
        !           482: 
        !           483: 
        !           484:     return(((p[n / BYTE] >> ((BYTE - 1) - n % BYTE)) & 01));
        !           485: 
        !           486: }   /* End of checkbit */
        !           487: 
        !           488: 
        !           489: /*****************************************************************************/
        !           490: 
        !           491: 
        !           492: setbit(p, n)
        !           493: 
        !           494: 
        !           495:     char       *p;                     /* start of the bitmap - again */
        !           496:     int                n;                      /* bit that we want to set in p[] */
        !           497: 
        !           498: 
        !           499: {
        !           500: 
        !           501: 
        !           502: /*
        !           503:  *
        !           504:  * Turns on the n-th bit in the bitmap that starts at address p. This
        !           505:  * routine is really only used to keep track of the glyphs that have
        !           506:  * already been downloaded. Again it could easily be defined as a macro.
        !           507:  *
        !           508:  */
        !           509: 
        !           510: 
        !           511:     p[n / BYTE] |= 01 << ((BYTE - 1) - n % BYTE);
        !           512: 
        !           513: }   /* End of setbit */
        !           514: 
        !           515: 
        !           516: /*****************************************************************************/
        !           517: 
        !           518: 
        !           519: dump_glyph(n, fp)
        !           520: 
        !           521: 
        !           522:     int                n;                      /* glyph number we want to display */
        !           523:     FILE       *fp;                    /* everything's written here */
        !           524: 
        !           525: 
        !           526: {
        !           527: 
        !           528: 
        !           529:     int                height;                 /* pixel height of the bitmap */
        !           530:     int                width;                  /* width of the rows in pixels */
        !           531:     int                xref;                   /* x coordinate of reference point */
        !           532:     int                yref;                   /* same but for y coordinate */
        !           533:     int                chwidth;                /* width of the character */
        !           534:     int                rwid;                   /* number of bytes per bitmap row */
        !           535:     int                i, j;                   /* loop indices for the bitmap */
        !           536:     char       *bptr;                  /* start of the glyph's bitmap */
        !           537: 
        !           538: 
        !           539: /*
        !           540:  *
        !           541:  * Really just a debugging routine that's used to dump the bitmap and
        !           542:  * other glyph data for character n out to FILE *fp.
        !           543:  *
        !           544:  */
        !           545: 
        !           546: 
        !           547:     height = getvalue(G_HEIGHT, n);
        !           548:     width = getvalue(G_WIDTH, n);
        !           549:     xref = getvalue(G_XREF, n);
        !           550:     yref = getvalue(G_YREF, n);
        !           551:     chwidth = getvalue(G_CHWIDTH, n);
        !           552:     bptr = fam->rst + getvalue(G_BPTR, n);
        !           553: 
        !           554:     rwid = (width + BYTE - 1) / BYTE;
        !           555: 
        !           556:     fprintf(fp, "GLYPH NUMBER %d  Character Width = %d\n", n, PIXEL_WIDTH(chwidth, rast_res));
        !           557:     fprintf(fp, " Raster Data: height = %d, width = %d, xref = %d, yref = %d, chwidth = %d\n",height, width, xref, yref, chwidth);
        !           558: 
        !           559:     for ( j = 0; j < height; j++ )  {
        !           560:        for ( i = 0; i < width; i++ )
        !           561:            if ( checkbit(bptr, j * rwid * BYTE + i) )
        !           562:                putc('X', fp);
        !           563:            else putc('.', fp);
        !           564:        putc('\n', fp);
        !           565:     }   /* End for */
        !           566: 
        !           567: }   /* End of dump_glyph */
        !           568: 
        !           569: 
        !           570: /*****************************************************************************/
        !           571: 
        !           572: 

unix.superglobalmegacorp.com

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