Annotation of 43BSDTahoe/new/X/libis/font.c, revision 1.1

1.1     ! root        1: /*
        !             2:  *     $Source: /u1/X/libis/RCS/font.c,v $
        !             3:  *     $Header: font.c,v 1.1 86/11/17 14:34:11 swick Rel $
        !             4:  */
        !             5: 
        !             6: #ifndef lint
        !             7: static char *rcsid_font_c = "$Header: font.c,v 1.1 86/11/17 14:34:11 swick Rel $";
        !             8: #endif lint
        !             9: 
        !            10: #include "is-copyright.h"
        !            11: 
        !            12: /*     font.c
        !            13:  *
        !            14:  *     GetFont         Takes a font name and stores it
        !            15:  *     FreeFont        Frees the storage taken by a font
        !            16:  *     MakeFontPixmap  Convert font bitmap to pixmap
        !            17:  *
        !            18:  *     Copyright (c) 1986, Integrated Solutions, Inc.
        !            19:  */
        !            20: 
        !            21: #include "Xis.h"
        !            22: #include "vssite.h"
        !            23: #include <errno.h>
        !            24: #include <sys/file.h>
        !            25: 
        !            26: extern int errno;
        !            27: 
        !            28: extern char *strcpy();
        !            29: extern char *strcat();
        !            30: extern long lseek();
        !            31: extern PIXMAP *MakePixmap();
        !            32: 
        !            33: /*
        !            34:  * BitMap and FontData typedefs come from ../libvs100/param.h
        !            35:  */
        !            36: 
        !            37: /* BitMap typedefs */
        !            38: typedef short a_BitmapEntryPtr[2];
        !            39: 
        !            40: typedef struct _Bitmap {
        !            41:     a_BitmapEntryPtr bm_address;
        !            42:     short bm_width;
        !            43:     short bm_height;
        !            44:     short bm_bitsPerPixel;
        !            45: } BitMap;
        !            46: 
        !            47: typedef short a_Bitmap[5];
        !            48: 
        !            49: /* FontData typedefs */
        !            50: typedef short a_FontWidthEntryPtr[2];
        !            51: 
        !            52: typedef struct _FontData {
        !            53:     a_Bitmap   f_characters;
        !            54:     short      f_firstChar;
        !            55:     short      f_lastChar;
        !            56:     a_FontWidthEntryPtr f_leftArray;
        !            57:     short      f_baseline;
        !            58:     short      f_spaceIndex;
        !            59:     short      f_fixedWidth;
        !            60: } FontData;
        !            61: 
        !            62: /*
        !            63:  *     GetFont
        !            64:  */
        !            65: FONT *GetFont(name)
        !            66: char   *name;
        !            67: {
        !            68:     char pathname[1024];       /* font pathname                        */
        !            69:     int file;                  /* file descriptor                      */
        !            70:     FontData hdr;              /* font file header                     */
        !            71: #define chars ((BitMap *) hdr.f_characters)
        !            72:     int mask_size;             /* size of mask bitmap                  */
        !            73:     int xpos_size;             /* size of x pos array                  */
        !            74:     char *mask;                        /* character mask bitmap                */
        !            75:     register short *xpos;      /* x position of characters in mask     */
        !            76:     register FONT *font;       /* font                                 */
        !            77:     register FontPriv *fpriv;  /* font "private" parts                 */
        !            78: 
        !            79: #ifdef DEBUG
        !            80: if (debug & D_Font)
        !            81:     printf("GetFont(name=\"%s\")\n", name);
        !            82: #endif DEBUG
        !            83: 
        !            84:     /* build pathname */
        !            85:     strcpy(pathname, DEFAULT_FONT_DIRECTORY);
        !            86:     strcat(pathname, name);
        !            87:     strcat(pathname, DEFAULT_FONT_SUFFIX);
        !            88: 
        !            89:     /* open as "pathname", if open fails try "name" */
        !            90:     if ((file = open(pathname, O_RDONLY, 0)) == -1 &&
        !            91:        (errno != ENOENT || (file = open(name, O_RDONLY, 0)) == -1)) {
        !            92:        errno = EINVAL;
        !            93:        return (NULL);
        !            94:     }
        !            95: 
        !            96:     /* read header and swap bytes in shorts */
        !            97:     if (read(file, (caddr_t) &hdr, sizeof(FontData)) != sizeof(FontData)) {
        !            98:        close(file);
        !            99:        errno = EINVAL;
        !           100:        return (NULL);
        !           101:     }
        !           102:     SwapShorts((short *) &hdr, sizeof(FontData));
        !           103: 
        !           104:     /* read font mask, and swap bits in bytes */
        !           105:     mask_size = BitmapSize(chars->bm_width, chars->bm_height);
        !           106:     mask = (char *) Xalloc(mask_size);
        !           107:     lseek(file, (long) hdr.f_characters[0], 0);
        !           108:     if (read(file, mask, mask_size) != mask_size) {
        !           109:        close(file);
        !           110:        free(mask);
        !           111:        errno = EINVAL;
        !           112:        return (NULL);
        !           113:     }
        !           114:     SwapBits((short *) mask, mask_size);
        !           115: 
        !           116:     /* read x position array */
        !           117:     if (hdr.f_fixedWidth == 0) {
        !           118:        xpos_size = (hdr.f_lastChar - hdr.f_firstChar + 2) * sizeof(short);
        !           119:        xpos = (short *) Xalloc(xpos_size);
        !           120:        lseek(file, (long) hdr.f_leftArray[0], 0);
        !           121:        if (read(file, (caddr_t) xpos, xpos_size) != xpos_size) {
        !           122:            close(file);
        !           123:            free(mask);
        !           124:            free((caddr_t) xpos);
        !           125:            errno = EINVAL;
        !           126:            return (NULL);
        !           127:        }
        !           128:        SwapShorts(xpos, xpos_size);
        !           129:     } else {
        !           130:        xpos_size = 0;
        !           131:        xpos = NULL;
        !           132:     }
        !           133: 
        !           134:     close(file);
        !           135: 
        !           136:     /* complete "font" struct with info from file */
        !           137:     font = (FONT *) Xalloc(sizeof (FONT));
        !           138: 
        !           139:     font->name = (char *) Xalloc(strlen(name) + 1);
        !           140:     strcpy(font->name, name);
        !           141:     font->first        = hdr.f_firstChar;
        !           142:     font->last = hdr.f_lastChar;
        !           143:     font->space        = hdr.f_spaceIndex;
        !           144:     font->space        += font->first;
        !           145:     font->height = chars->bm_height;
        !           146:     if (hdr.f_fixedWidth) {
        !           147:        font->avg_width = hdr.f_fixedWidth;
        !           148:        font->fixed = 1;
        !           149:     } else {
        !           150:        font->avg_width = (xpos[font->last] - xpos[font->first]) /
        !           151:            (font->last - font->first);
        !           152:        font->fixed = 0;
        !           153:     }
        !           154:     font->base = hdr.f_baseline;
        !           155:     font->refcnt = 1;
        !           156:     fpriv = (FontPriv *) Xalloc(sizeof(FontPriv));
        !           157:     font->data = (caddr_t) fpriv;
        !           158: 
        !           159:     /* complete "fpriv" struct */
        !           160:     {
        !           161:        BITMAP *bitmap = (BITMAP *) Xalloc(sizeof(BITMAP));
        !           162:        RASTER *raster = (RASTER *) Xalloc(sizeof(RASTER));
        !           163: 
        !           164:        bitmap->width   = chars->bm_width;
        !           165:        bitmap->height  = chars->bm_height;
        !           166:        bitmap->refcnt  = 1;
        !           167:        bitmap->kind    = (char) 0;
        !           168:        bitmap->data    = (caddr_t) raster;
        !           169: 
        !           170:        raster->width   = (short) ((chars->bm_width+15)>>4)<<1;
        !           171:        raster->address = (short *) mask;
        !           172: 
        !           173:        fpriv->mask     = (BITMAP *) bitmap;
        !           174:        fpriv->xpos     = xpos;
        !           175: 
        !           176:        /* if x position array exists use it to create width array */
        !           177:        if (xpos) {
        !           178:            register short *p, *limitp;
        !           179: 
        !           180:            fpriv->widths = (short *) Xalloc (xpos_size);
        !           181: 
        !           182:            bcopy((char *)xpos, fpriv->widths, xpos_size);
        !           183: 
        !           184:            limitp = &fpriv->widths[font->last];
        !           185:            for (p = &fpriv->widths[font->first]; p <= limitp; ++p) {
        !           186:                *p = p[1] - *p;
        !           187:            }
        !           188:        } else {
        !           189:            fpriv->widths = NULL;
        !           190:        }
        !           191:     }
        !           192: 
        !           193:     /* initialize font_pixmaps */
        !           194:     {
        !           195:        register int i;
        !           196:        for (i=0; i<FONTPIXMAPS; ++i) {
        !           197:            fpriv->font_pixmaps[i].p = NULL;
        !           198:        }
        !           199:        fpriv->next_pixmap = 0;
        !           200:     }
        !           201: 
        !           202:     return (font);
        !           203: #undef chars
        !           204: }
        !           205: 
        !           206: /*
        !           207:  *     FreeFont
        !           208:  */
        !           209: FreeFont(font)
        !           210: register FONT  *font;
        !           211: {
        !           212:     register FontPriv  *fpriv = FDATA(font);
        !           213:     BITMAP             *bitmap = (BITMAP *)fpriv->mask;
        !           214:     register RASTER    *raster = (RASTER *)bitmap->data;
        !           215: 
        !           216: #ifdef DEBUG
        !           217: if (debug & D_Font)
        !           218:     printf("FreeFont(font=0x%x)\n", font);
        !           219: #endif DEBUG
        !           220: 
        !           221:     /* free text bitmap */
        !           222:     free((caddr_t) raster->address);
        !           223:     free((caddr_t) raster);
        !           224:     free((caddr_t) bitmap);
        !           225: 
        !           226:     /* free xpos and widths arrays */
        !           227:     if (fpriv->xpos) {
        !           228:        free((caddr_t) fpriv->xpos);
        !           229:        free((caddr_t) fpriv->widths);
        !           230:     }
        !           231: 
        !           232:     /* free font pixmaps */
        !           233:     {
        !           234:        register int i;
        !           235:        register struct _font_pixmaps *font_pixmaps = &fpriv->font_pixmaps[0];
        !           236:        for (i = 0; i < FONTPIXMAPS; ++i, ++font_pixmaps) {
        !           237:            if (font_pixmaps->p) {
        !           238:                FreePixmap(font_pixmaps->p);
        !           239:            }
        !           240:        }
        !           241:     }
        !           242: 
        !           243:     /* free remainder of font data */
        !           244:     free((caddr_t) fpriv);
        !           245:     free(font->name);
        !           246:     free((caddr_t) font);
        !           247: }
        !           248: 
        !           249: /*
        !           250:  *     MakeFontPixmap
        !           251:  */
        !           252: PIXMAP *MakeFontPixmap(font, fore, back)
        !           253: FONT *font;
        !           254: register int   fore, back;
        !           255: {
        !           256:     register FontPriv *fdata = FDATA(font);
        !           257:     register struct _font_pixmaps *font_pixmap = &fdata->font_pixmaps[0];
        !           258:     register int i;
        !           259: 
        !           260: #ifdef DEBUG
        !           261: if (debug & D_FontPixmap)
        !           262:     printf("MakeFontPixmap(font=0x%x, fore=%d, back=%d)\n", font, fore, back);
        !           263: #endif DEBUG
        !           264: 
        !           265:     /* search font_pixmaps for match */
        !           266:     for (i = 0; i < FONTPIXMAPS; ++i) {
        !           267:        if (font_pixmap->p &&
        !           268:            font_pixmap->fore == fore && font_pixmap->back == back) {
        !           269:            /* found match */
        !           270:            return (font_pixmap->p);
        !           271:        }
        !           272:     }
        !           273: 
        !           274:     /* free entry if necessary */
        !           275:     font_pixmap = &fdata->font_pixmaps[fdata->next_pixmap];
        !           276:     if (font_pixmap->p != NULL) {
        !           277:        FreePixmap(font_pixmap->p);
        !           278:     }
        !           279: 
        !           280:     /* create pixmap and save in font_pixmaps */
        !           281:     font_pixmap->fore = fore;
        !           282:     font_pixmap->back = back;
        !           283:     font_pixmap->p = MakePixmap(fdata->mask, fore, back);
        !           284:     fdata->next_pixmap = (fdata->next_pixmap + 1) % FONTPIXMAPS;
        !           285: 
        !           286:     return (font_pixmap->p);
        !           287: }

unix.superglobalmegacorp.com

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