Annotation of 43BSD/contrib/X/libvs100/font.c, revision 1.1.1.1

1.1       root        1: /* $Header: font.c,v 10.3 86/02/01 15:46:53 tony Rel $ */
                      2: /* font.c      Reads a font from a file and stores it on the workstation
                      3:  *
                      4:  *     GetFont         Takes a font name and stores it
                      5:  *     FreeFont        Frees the storage taken by a font
                      6:  *
                      7:  */
                      8: 
                      9: /****************************************************************************
                     10:  *                                                                         *
                     11:  *  Copyright (c) 1983, 1984 by                                                    *
                     12:  *  DIGITAL EQUIPMENT CORPORATION, Maynard, Massachusetts.                 *
                     13:  *  All rights reserved.                                                   *
                     14:  *                                                                         *
                     15:  *  This software is furnished on an as-is basis and may be used and copied *
                     16:  *  only with inclusion of the above copyright notice. This software or any *
                     17:  *  other copies thereof may be provided or otherwise made available to     *
                     18:  *  others only for non-commercial purposes.  No title to or ownership of   *
                     19:  *  the software is hereby transferred.                                            *
                     20:  *                                                                         *
                     21:  *  The information in this software is  subject to change without notice   *
                     22:  *  and  should  not  be  construed as  a commitment by DIGITAL EQUIPMENT   *
                     23:  *  CORPORATION.                                                           *
                     24:  *                                                                         *
                     25:  *  DIGITAL assumes no responsibility for the use  or  reliability of its   *
                     26:  *  software on equipment which is not supplied by DIGITAL.                *
                     27:  *                                                                         *
                     28:  *                                                                         *
                     29:  ****************************************************************************/
                     30: 
                     31: #include "vs100.h"
                     32: #include "vssite.h"
                     33: #include <errno.h>
                     34: 
                     35: extern int errno;
                     36: 
                     37: char *Xalloc(), *AllocateSpace(), *strcpy(), *strcat();
                     38: long lseek();
                     39: VSArea *VSAlloc();
                     40: 
                     41: FONT *GetFont (name)
                     42:        char *name;
                     43: {
                     44:        char fontname[256];
                     45:        int fontfile;
                     46:        FontData font;
                     47:        register FontData *fontCopy;
                     48: #define chars ((BitMap *) font.f_characters)
                     49:        int fontsize, leftsize, width;
                     50:        char *fontarea;
                     51:        register short *leftarea;
                     52:        register FONT *fd;
                     53:        FontPriv *fpriv;
                     54:        register BitMap *mychars;
                     55: 
                     56:        strcpy (fontname, DEFAULT_FONT_DIRECTORY);
                     57:        strcat (fontname, name);
                     58:        strcat (fontname, DEFAULT_FONT_SUFFIX);
                     59: 
                     60:        if ((fontfile = open (fontname, 0)) == -1 &&
                     61:            (errno != ENOENT || (fontfile = open (name, 0)) == -1)) {
                     62:            errno = EINVAL;
                     63:            return (NULL);
                     64:        }
                     65: 
                     66:        if (read (fontfile, (caddr_t) &font, sizeof (FontData)) != sizeof (FontData)) {
                     67:            close (fontfile);
                     68:            errno = EINVAL;
                     69:            return (NULL);
                     70:        }
                     71: 
                     72:        fontsize = BitmapSize(chars->bm_width, chars->bm_height);
                     73:        fontarea = (char *) Xalloc (fontsize);
                     74:        lseek (fontfile, (long) font.f_characters[0], 0);
                     75:        if (read (fontfile, fontarea, fontsize) != fontsize) {
                     76:            close (fontfile);
                     77:            free (fontarea);
                     78:            errno = EINVAL;
                     79:            return (NULL);
                     80:        }
                     81: 
                     82:        if (font.f_fixedWidth == 0) {
                     83:            leftsize = (font.f_lastChar - font.f_firstChar + 2) * sizeof (short);
                     84:            leftarea = (short *) Xalloc (leftsize);
                     85:            lseek (fontfile, (long) font.f_leftArray[0], 0);
                     86:            if (read (fontfile, (caddr_t) leftarea, leftsize) != leftsize) {
                     87:                close (fontfile);
                     88:                free (fontarea);
                     89:                free ((caddr_t) leftarea);
                     90:                errno = EINVAL;
                     91:                return (NULL);
                     92:            }
                     93:        } else {
                     94:            leftsize = 0;
                     95:            leftarea = NULL;
                     96:        }
                     97: 
                     98:        close (fontfile);
                     99: 
                    100:        if ((fontCopy = (FontData *)
                    101:                                AllocateSpace (sizeof (FontData))) == NULL) {
                    102:            free (fontarea);
                    103:            if (leftarea)
                    104:                free ((caddr_t) leftarea);
                    105:            errno = ENOMEM;
                    106:            return (NULL);
                    107:        }
                    108:        mychars = (BitMap *) fontCopy->f_characters;
                    109:        fd = (FONT *) Xalloc (sizeof (FONT));
                    110: 
                    111:        mychars->bm_address[0] = sizeof (FontData);
                    112:        mychars->bm_address[1] = 0;
                    113:        mychars->bm_width = chars->bm_width;
                    114:        fd->height = mychars->bm_height = chars->bm_height;
                    115:        mychars->bm_bitsPerPixel = 1;
                    116:        fd->first = fontCopy->f_firstChar = font.f_firstChar;
                    117:        fd->last = fontCopy->f_lastChar = font.f_lastChar;
                    118:        if (leftarea)
                    119:            fontCopy->f_leftArray[0] = sizeof (FontData) + fontsize;
                    120:        else
                    121:            fontCopy->f_leftArray[0] = 0;
                    122:        fontCopy->f_leftArray[1] = 0;
                    123: 
                    124:        fd->base = fontCopy->f_baseline = font.f_baseline;
                    125:        fd->space = fontCopy->f_spaceIndex = font.f_spaceIndex;
                    126:        fd->space += fd->first;
                    127:        if (fd->avg_width = fontCopy->f_fixedWidth = font.f_fixedWidth)
                    128:            fd->fixed = 1;
                    129:        else
                    130:            fd->fixed = 0;
                    131: 
                    132:        fd->refcnt = 1;
                    133:        fpriv = (FontPriv *) Xalloc (sizeof (FontPriv));
                    134:        fd->data = (caddr_t) fpriv;
                    135:        fpriv->widths = leftarea;
                    136: 
                    137:        if ((fpriv->remote = (VSArea *) VSAlloc (sizeof (FontData) + fontsize + leftsize,
                    138:                                                    FONT_TYPE)) == NULL) {
                    139:            free (fontarea);
                    140:            if (leftarea)
                    141:                free ((caddr_t) leftarea);
                    142:            free ((caddr_t) fd);
                    143:            free ((caddr_t) fpriv);
                    144:            return (NULL);
                    145:        }
                    146:        fd->name = (char *) Xalloc (strlen (name) + 1);
                    147:        strcpy (fd->name, name);
                    148: 
                    149:        if (MoveObjectDown ((caddr_t) fontCopy, fpriv->remote->vsPtr, sizeof (FontData)) ||
                    150:            MoveBufferDown (fontarea,
                    151:                            fpriv->remote->vsPtr + sizeof (FontData),
                    152:                            fontsize) ||
                    153:            (leftarea &&
                    154:             MoveBufferDown ((caddr_t) leftarea,
                    155:                             fpriv->remote->vsPtr + sizeof (FontData) + fontsize,
                    156:                             leftsize))) {
                    157:            free (fontarea);
                    158:            FreeFont (fd);
                    159:            errno = ENOMEM;
                    160:            return (NULL);
                    161:        }
                    162: 
                    163:        if (leftarea) {
                    164:            leftsize = font.f_lastChar - font.f_firstChar + 1;
                    165:            width = 0;
                    166:            fontsize = 0;
                    167:            while (--leftsize >= 0) {
                    168:                *leftarea = leftarea[1] - *leftarea;
                    169:                if (*leftarea) {
                    170:                    width += *leftarea;
                    171:                    fontsize++;
                    172:                }
                    173:                leftarea++;
                    174:            }
                    175:            fd->avg_width = width / fontsize;
                    176:        }
                    177:        free (fontarea);
                    178:        return (fd);
                    179: #undef chars
                    180: }
                    181: 
                    182: FreeFont (font)
                    183:        register FONT *font;
                    184: {
                    185:        register FontPriv *data;
                    186: 
                    187:        data = FDATA(font);
                    188:        if (data->widths) free ((caddr_t) data->widths);
                    189:        VSFree (data->remote);
                    190:        free ((caddr_t) data);
                    191:        free (font->name);
                    192:        free ((caddr_t) font);
                    193: }

unix.superglobalmegacorp.com

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