vfont.c

Go to the documentation of this file.
00001 /*                         V F O N T . C
00002  * BRL-CAD
00003  *
00004  * Copyright (c) 2004-2006 United States Government as represented by
00005  * the U.S. Army Research Laboratory.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * as published by the Free Software Foundation; either version 2 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this file; see the file named COPYING for more
00019  * information.
00020  */
00021 
00022 /** \addtogroup vfont */
00023 /*@{*/
00024 /** @file vfont.c
00025  *
00026  * @brief Berkely Vector Fonts
00027  *
00028  *      Provide a machine-independent interface to files containing
00029  *      Berkeley VFONT format fonts, stored with VAX byte ordering
00030  *      and word alignment.
00031  *
00032  *  @author
00033  *      Michael John Muuss
00034  *
00035  *  @par Source -
00036  *      SECAD/VLD Computing Consortium, Bldg 394
00037  *@n    The U. S. Army Ballistic Research Laboratory
00038  *@n    Aberdeen Proving Ground, Maryland  21005-5066
00039  */
00040 
00041 
00042 #ifndef lint
00043 static const char libbu_vfont_RCSid[] = "@(#)$Header: /cvsroot/brlcad/brlcad/src/libbu/vfont.c,v 14.15 2006/09/03 15:14:07 lbutler Exp $ (BRL)";
00044 #endif
00045 
00046 #include "common.h"
00047 
00048 #include <stdio.h>
00049 #include "machine.h"
00050 #include "vfont-if.h"
00051 #include "bu.h"
00052 
00053 #define FONTDIR2        "/usr/lib/vfont"
00054 #define DEFAULT_FONT    "nonie.r.12"
00055 #define FONTNAMESZ      128
00056 
00057 /*
00058  * Forward Definitions
00059  */
00060 static int vax_gshort(unsigned char *);
00061 
00062 /**
00063  *                      V F O N T _ G E T
00064  *
00065  *  Fetch the named font, and return a struct vfont pointer.
00066  *
00067  *  First the filename provided is used, then the BRL-CAD font
00068  *  directory is searched (for places where "system" directories
00069  *  are considered sacred), and then finally the ordinary
00070  *  font directory is searched.
00071  *
00072  *  The font files are treated as pure byte streams, and are expected
00073  *  to be in VAX order.
00074  *
00075  *  VFONT_NULL is returned on error.  On ordinary errors, the function
00076  *  is silent.  On extraordinary errors, a remark is placed on stderr.
00077  */
00078 struct vfont *
00079 vfont_get(char *font)
00080 {
00081         register struct vfont   *vfp = VFONT_NULL;
00082         register FILE           *fp = NULL;
00083         register int    i;
00084         char            fname[FONTNAMESZ];
00085         unsigned char   header[2*5];            /* 5 16-bit vax shorts */
00086         unsigned char   dispatch[10*256];       /* 256 10-byte structs */
00087         int             magic;
00088         int             size;
00089 
00090         if( font == NULL )
00091                 font = DEFAULT_FONT;
00092 
00093         /* Open the file and read in the header information. */
00094         if( (fp = fopen( font, "r" )) == NULL )  {
00095                 sprintf( fname, "%s/%s", (char *)bu_brlcad_data("vfont", 0), font );
00096                 if( (fp = fopen( fname, "r" )) == NULL )  {
00097                         sprintf( fname, "%s/%s", FONTDIR2, font );
00098                         if( (fp = fopen( fname, "r" )) == NULL )  {
00099                                 return(VFONT_NULL);
00100                         }
00101                 }
00102         }
00103         if( fread( (char *)header, sizeof(header), 1, fp ) != 1 ||
00104             fread( (char *)dispatch, sizeof(dispatch), 1, fp ) != 1 )  {
00105                 fprintf(stderr, "vfont_get(%s):  header read error\n", fname );
00106                 fclose(fp);
00107                 return(VFONT_NULL);
00108         }
00109         magic = vax_gshort( &header[0*2] ) & 0xFFFF;
00110         size = vax_gshort( &header[1*2] ) & 0xFFFF;     /* unsigned short */
00111 
00112         if( magic != 0436 )  {
00113                 fprintf(stderr, "vfont_get(%s):  bad magic number 0%o\n",
00114                         fname, magic );
00115                 fclose(fp);
00116                 return(VFONT_NULL);
00117         }
00118 
00119         if( (vfp = (struct vfont *)bu_malloc(sizeof(struct vfont), "vfont")) == VFONT_NULL )  {
00120                 fprintf(stderr,"vfont_get(%s):  malloc failure 1\n", fname );
00121                 fclose(fp);
00122                 return(VFONT_NULL);
00123         }
00124 
00125         /* Read in the bit maps */
00126         if( (vfp->vf_bits = (char *)bu_malloc(size, "vfont bits")) == (char *)0 )  {
00127                 fprintf(stderr,"vfont_get(%s):  malloc failure 2 (%d)\n",
00128                         fname, size);
00129                 fclose(fp);
00130                 bu_free( (char *)vfp, "vfont");
00131                 return(VFONT_NULL);
00132         }
00133         if( fread( vfp->vf_bits, size, 1, fp ) != 1 )  {
00134                 fprintf(stderr,"vfont_get(%s):  bitmap read error\n", fname );
00135                 fclose(fp);
00136                 bu_free( vfp->vf_bits, "vfont bits" );
00137                 bu_free( (char *)vfp, "vfont" );
00138                 return(VFONT_NULL);
00139         }
00140 
00141         /*
00142          *  Convert VAX data in header[] and dispatch[] arrays to
00143          *  native machine form.
00144          */
00145         vfp->vf_maxx = vax_gshort( &header[2*2] );
00146         vfp->vf_maxy = vax_gshort( &header[3*2] );
00147         vfp->vf_xtend = vax_gshort( &header[4*2] );
00148 
00149         for( i=0; i<255; i++ )  {
00150                 register struct vfont_dispatch  *vdp = &(vfp->vf_dispatch[i]);
00151                 register unsigned char          *cp = &dispatch[i*10];
00152 
00153                 vdp->vd_addr = vax_gshort( &cp[0] );
00154                 vdp->vd_nbytes = vax_gshort( &cp[2] );
00155                 vdp->vd_up = SXT( cp[4] );
00156                 vdp->vd_down = SXT( cp[5] );
00157                 vdp->vd_left = SXT( cp[6] );
00158                 vdp->vd_right = SXT( cp[7] );
00159                 vdp->vd_width = vax_gshort( &cp[8] );
00160         }
00161         fclose(fp);
00162         return(vfp);
00163 }
00164 
00165 /**
00166  *                      V A X _ G S H O R T
00167  *
00168  *  Obtain a 16-bit signed integer from two adjacent characters,
00169  *  stored in VAX order, regardless of word alignment.
00170  */
00171 static int
00172 vax_gshort(unsigned char *msgp)
00173 {
00174         register unsigned char *p = (unsigned char *) msgp;
00175         register int    i;
00176 
00177         if( (i = (p[1] << 8) | p[0]) & 0x8000 )
00178                 return(i | ~0xFFFF);    /* Sign extend */
00179         return(i);
00180 }
00181 
00182 /**
00183  *                      V F O N T _ F R E E
00184  *
00185  *  Return the storage associated with a struct vfont
00186  */
00187 void
00188 vfont_free(register struct vfont *vfp)
00189 {
00190         bu_free( vfp->vf_bits, "vfont bits" );
00191         bu_free( (char *)vfp, "vfont" );
00192 }
00193 /*@}*/
00194 /*
00195  * Local Variables:
00196  * mode: C
00197  * tab-width: 8
00198  * c-basic-offset: 4
00199  * indent-tabs-mode: t
00200  * End:
00201  * ex: shiftwidth=4 tabstop=8
00202  */

Generated on Mon Sep 18 01:24:48 2006 for BRL-CAD by  doxygen 1.4.6