File:  [LON-CAPA] / loncom / cgi / mimeTeX / gfuntype.c
Revision 1.3: download - view: text, annotated - select for diffs
Tue Oct 9 21:41:41 2007 UTC (16 years, 9 months ago) by albertel
Branches: MAIN
CVS tags: version_2_7_X, version_2_7_1, version_2_7_0, version_2_6_X, version_2_6_99_1, version_2_6_99_0, version_2_6_3, version_2_6_2, version_2_6_1, version_2_6_0, version_2_5_99_1, version_2_5_99_0, HEAD
- latest revison of mimetex

    1: /****************************************************************************
    2:  *
    3:  * Copyright (c) 2002, John Forkosh Associates, Inc.  All rights reserved.
    4:  * --------------------------------------------------------------------------
    5:  * This file is part of mimeTeX, which is free software. You may redistribute
    6:  * and/or modify it under the terms of the GNU General Public License,
    7:  * version 2 or later, as published by the Free Software Foundation.
    8:  *      MimeTeX is distributed in the hope that it will be useful, but
    9:  * WITHOUT ANY WARRANTY, not even the implied warranty of MERCHANTABILITY.
   10:  * See the GNU General Public License for specific details.
   11:  *      By using mimeTeX, you warrant that you have read, understood and
   12:  * agreed to these terms and conditions, and that you are at least 18 years
   13:  * of age and possess the legal right and ability to enter into this
   14:  * agreement and to use mimeTeX in accordance with it.
   15:  *      Your mimeTeX distribution should contain a copy of the GNU General
   16:  * Public License.  If not, write to the Free Software Foundation, Inc.,
   17:  * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   18:  * --------------------------------------------------------------------------
   19:  *
   20:  * Program:	gfuntype  [-g gformat]  [-u isnoname] [-m msglevel]
   21:  *		[-n fontname]  [infile [outfile]]
   22:  *
   23:  * Purpose:	Parses output from  gftype -i
   24:  *		and writes pixel bitmap data of the characters
   25:  *		in a format suitable for a C header file, etc.
   26:  *
   27:  * --------------------------------------------------------------------------
   28:  *
   29:  * Command-line Arguments:
   30:  *		--- args can be in any order ---
   31:  *		infile		name of input file
   32:  *				(defaults to stdin if no filenames given)
   33:  *		outfile		name of output file
   34:  *				(defaults to stdout if <2 filenames given)
   35:  *		-g gformat	gformat=1(default) for bitmap representation,
   36:  *				or 2,3 for 8-bit,4-bit .gf-like compression,
   37:  *				or 0 to choose smallest format.
   38:  *				Add 10 (gformat=10,12,13,14) to embed scan
   39:  *				line repeat counts in format.
   40:  *		-u isnoname	isnoname=1(default) to output symbols not
   41:  *				defined/named in mimetex.h, or 0 to omit them
   42:  *		-m msglevel	verbose if msglevel>=9 (vv if >=99)
   43:  *		-n fontname	string used for fontname
   44:  *				(defaults to noname)
   45:  *
   46:  * Exits:	0=success,  1=some error
   47:  *
   48:  * Notes:     o	To compile
   49:  *		cc gfuntype.c mimetex.c -lm -o gfuntype
   50:  *		needs mimetex.c and mimetex.h
   51:  *
   52:  * Source:	gfuntype.c
   53:  *
   54:  * --------------------------------------------------------------------------
   55:  * Revision History:
   56:  * 09/22/02	J.Forkosh	Installation.
   57:  * 10/11/05	J.Forkosh	.gf-style format options added.
   58:  *
   59:  ****************************************************************************/
   60: 
   61: /* --------------------------------------------------------------------------
   62: standard headers, program parameters, global data and macros
   63: -------------------------------------------------------------------------- */
   64: /* --- standard headers --- */
   65: #include <stdio.h>
   66: #include <stdlib.h>
   67: #include <string.h>
   68: #include <ctype.h>
   69: /* --- application headers --- */
   70: /* #define SIGNEDCHAR */
   71: #include "mimetex.h"
   72: /* --- parameters either -D defined on cc line, or defaulted here --- */
   73: #ifndef	MSGLEVEL
   74:   #define MSGLEVEL 0
   75: #endif
   76: #ifndef	GFORMAT
   77:   #define GFORMAT 1
   78: #endif
   79: #ifndef ISREPEAT
   80:   #define ISREPEAT 1
   81: #endif
   82: /* --- message level (verbose test) --- */
   83: static	int msglevel = MSGLEVEL;	/* verbose if msglevel >= 9 */
   84: static	FILE *msgfp;			/* verbose output goes here */
   85: /* --- output file format --- */
   86: static	int isnoname = 1;		/* true to output unnamed symbols */
   87: static	char *noname = "(noname)";	/* char name used if lookup fails */
   88: static	int gformat = GFORMAT;		/* 1=bitmap, 2=.gf-like */
   89: static	int isrepeat = ISREPEAT;	/* true to store line repeat counts*/
   90: /* extern int imageformat; */		/* as per gformat, 1=bitmap,2=.gf */
   91: /* --- miscellaneous other data --- */
   92: #define	CORNER_STUB ".<--"		/* start of upper,lower-left line */
   93: #define	BLANKCHAR_STUB "character is entirely blank" /* signals blank char */
   94: #define	TYPECAST    "(pixbyte *)"	/* typecast for pixmap string */
   95: 
   96: /* ==========================================================================
   97:  * Function:	main() for gfuntype.c
   98:  * Purpose:	interprets command-line args, etc
   99:  * --------------------------------------------------------------------------
  100:  * Command-Line Arguments:
  101:  *		See above
  102:  * --------------------------------------------------------------------------
  103:  * Returns:	0=success, 1=some error
  104:  * --------------------------------------------------------------------------
  105:  * Notes:     o
  106:  * ======================================================================= */
  107: /* --- entry point --- */
  108: int	main ( int argc, char *argv[] )
  109: {
  110: /* --------------------------------------------------------------------------
  111: Allocations and Declarations
  112: -------------------------------------------------------------------------- */
  113: int	argnum = 0;		/* argv[] index for command-line args */
  114: int	inarg=0, outarg=0;	/* argv[] indexes for infile, outfile */
  115: int	iserror = 1;		/* error signal */
  116: int	charnum,		/* character number (nextchar->charnum) */
  117: 	nchars = 0;		/* #chars in font */
  118: char	fontname[99] = "noname", /* font name */
  119: 	*getcharname();		/* get character name from its number */
  120: FILE	/* *fopen(),*/ *infp=stdin, *outfp=stdout; /* init file pointers */
  121: chardef	*getnextchar(), *nextchar, /* read and parse next char in infp */
  122: 	*fontdef[256];		/* chars stored using charnum as index */
  123: int	cstruct_chardef();	/* emit C struct for a character map */
  124: int	type_raster();		/* display debugging output */
  125: char	*copyright =		/* copyright, gnu/gpl notice */
  126:  "+-----------------------------------------------------------------------+\n"
  127:  "|gfuntype ver 1.00, Copyright(c) 2002-2003, John Forkosh Associates, Inc|\n"
  128:  "+-----------------------------------------------------------------------+\n"
  129:  "| gfuntype is free software licensed to you under terms of the GNU/GPL, |\n"
  130:  "|           and comes with absolutely no warranty whatsoever.           |\n"
  131:  "+-----------------------------------------------------------------------+";
  132: /* --------------------------------------------------------------------------
  133: interpret command-line arguments
  134: -------------------------------------------------------------------------- */
  135: while ( argc > ++argnum )	/* check for flags and filenames */
  136:     if ( *argv[argnum] == '-' )	/* got some '-' flag */
  137:       {
  138:       char flag = tolower(*(argv[argnum]+1)); /* char following '-' */
  139:       argnum++;			/* arg following flag is usually its value */
  140:       switch ( flag )		/* see what user wants to tell us */
  141: 	{
  142: 	/* --- no usage for clueless users yet --- */
  143: 	default:  exit(iserror); /* exit quietly for unrecognized input */
  144: 	/* --- adjustable program parameters (not checking input) --- */
  145: 	case 'g': gformat  = atoi(argv[argnum]);
  146: 		  isrepeat = (gformat>=10?1:0);
  147: 		  gformat  = gformat%10;         break;
  148: 	case 'u': isnoname = atoi(argv[argnum]); break;
  149: 	case 'm': msglevel = atoi(argv[argnum]); break;
  150: 	case 'n': strcpy(fontname,argv[argnum]); break;
  151: 	} /* --- end-of-switch() --- */
  152:       } /* --- end-of-if(*argv[]=='-') --- */
  153:     else			/* this arg not a -flag, so it must be... */
  154:       if ( inarg == 0 )		/* no infile arg yet */
  155: 	inarg = argnum;		/* so use this one */
  156:       else			/* we already have an infile arg */
  157: 	if ( outarg == 0 )	/* but no outfile arg yet */
  158: 	  outarg = argnum;	/* so use this one */
  159: /* --- set verbose file ptr --- */
  160: msgfp = (outarg>0? stdout : stderr); /* use stdout or stderr */
  161: /* --- emit copyright, gnu/gpl notice --- */
  162: fprintf(msgfp,"%s\n",copyright); /* display copyright, gnu/gpl info */
  163: /* --- display input args if verbose output --- */
  164: if ( msglevel >= 9 )		/* verbose output requested */
  165:   fprintf(msgfp,"gfuntype> infile=%s outfile=%s, fontname=%s format=%d.%d\n",
  166:   (inarg>0?argv[inarg]:"stdin"), (outarg>0?argv[outarg]:"stdout"),
  167:   fontname, gformat,isrepeat);
  168: /* --------------------------------------------------------------------------
  169: initialization
  170: -------------------------------------------------------------------------- */
  171: /* --- initialize font[] array --- */
  172: for ( charnum=0; charnum<256; charnum++ ) /*for each possible char in font*/
  173:   fontdef[charnum] = (chardef *)NULL;	/* char doesn't exist yet */
  174: /* --- open input file (if necessary) --- */
  175: if ( inarg > 0 )		/* input from file, not from stdin */
  176:   if ( (infp = fopen(argv[inarg],"r")) == NULL ) /*try to open input file*/
  177:     { fprintf(msgfp,"gfuntype> can't open %s for read\n",argv[inarg]);
  178:       goto end_of_job; }	/* report error and quit */
  179: /* --- set format for mimetex.c functions --- */
  180: if ( gformat<0 || gformat>3 ) gformat=1; /* sanity check */
  181: /* if ( gformat == 1 ) imageformat = 1;	*/ /* force bitmap format */
  182: /* else gformat = imageformat = 2; */	/* or force .gf format */
  183: /* --------------------------------------------------------------------------
  184: process input file
  185: -------------------------------------------------------------------------- */
  186: while ( (nextchar=getnextchar(infp)) != NULL ) /* get each char in file */
  187:   {
  188:   /* --- display character info --- */
  189:   if ( msglevel >= 9 )			/* verbose output requested */
  190:     fprintf(msgfp,"gfuntype> Char#%3d, loc %4d: ul=(%d,%d) ll=(%d,%d)\n",
  191:     nextchar->charnum, nextchar->location,
  192:     nextchar->topleftcol,nextchar->toprow,
  193:     nextchar->botleftcol,nextchar->botrow);
  194:   if ( msglevel >= 19 )			/* if a bit more verbose */
  195:     type_raster(&(nextchar->image),msgfp); /*display ascii image of raster*/
  196:   /* --- store character in font */
  197:   charnum = nextchar->charnum;		/* get char number of char in font */
  198:   if ( charnum>=0 && charnum<=255 )	/* check for valid range */
  199:     fontdef[charnum] = nextchar;	/* store char in font */
  200:   } /* --- end-of-while(charnum>0) --- */
  201: /* --------------------------------------------------------------------------
  202: generate output file
  203: -------------------------------------------------------------------------- */
  204: /* --- open output file (if necessary) --- */
  205: if ( outarg > 0 )		/* output to a file, not to stdout */
  206:   if ( (outfp = fopen(argv[outarg],"w")) == NULL ) /*try to open output file*/
  207:     { fprintf(msgfp,"gfuntype> can't open %s for write\n",argv[outarg]);
  208:       goto end_of_job; }	/* report error and quit */
  209: /* --- header lines --- */
  210: fprintf(outfp,"/%c --- fontdef for %s --- %c/\n", '*',fontname,'*');
  211: fprintf(outfp,"static\tchardef %c%s[] =\n   {\n", ' ',fontname);
  212: /* --- write characters comprising font --- */
  213: for ( charnum=0; charnum<256; charnum++ ) /*for each possible char in font*/
  214:  if ( fontdef[charnum] != (chardef *)NULL ) /*check if char exists in font*/
  215:   { char *charname = getcharname(fontname,charnum);
  216:     if ( charname!=NULL || isnoname ) {	/* char defined or want undefined */
  217:      if ( ++nchars > 1 )		/* bump count */
  218:       fprintf(outfp,",\n");		/* and terminate preceding chardef */
  219:      fprintf(outfp,"      /%c --- pixel bitmap for %s char#%d %s --- %c/\n",
  220:       '*',fontname,charnum,(charname==NULL?noname:charname),'*');
  221:      cstruct_chardef(fontdef[charnum],outfp,6); } /*emit chardef struct*/
  222:     else
  223:      if(0)fprintf(outfp,"NULL");	/* no character in this position */
  224:   } /* --- end-of-if(fontdef[]!=NULL) --- */
  225:  else
  226:   if(0)fprintf(outfp,"NULL");		/* no character in this position */
  227: /* --- write trailer chardef and closing brace --- */
  228: fprintf(outfp,",\n");			/* finish up last map from loop */
  229: fprintf(outfp,"      /%c --- trailer  --- %c/\n",'*','*'); /* trailer... */
  230: fprintf(outfp,"      { -99, -999,  0,0,0,0, { 0,0,0,0, %s\"\\0\" }  }\n",
  231:      TYPECAST);
  232: fprintf(outfp,"   } ;\n");		/* terminating }; for fontdef */
  233: /* --------------------------------------------------------------------------
  234: end-of-job
  235: -------------------------------------------------------------------------- */
  236: /* --- reset error status for okay exit --- */
  237: iserror = 0;
  238: /* --- close files (if they're open and not stdin/out) --- */
  239: end_of_job:
  240:   if (  infp!=NULL &&  infp!=stdin  ) fclose( infp);
  241:   if ( outfp!=NULL && outfp!=stdout ) fclose(outfp);
  242: exit ( iserror );
  243: } /* --- end-of-function main() --- */
  244: 
  245: 
  246: /* ==========================================================================
  247:  * Function:	getnextchar ( fp )
  248:  * Purpose:	Reads and parses the next character definition on fp,
  249:  *		and returns a new chardef struct describing that character.
  250:  * --------------------------------------------------------------------------
  251:  * Arguments:	fp (I)		FILE *  to input file
  252:  *				(containing output from  gftype -i)
  253:  * Returns:	( chardef * )	ptr to chardef struct describing character,
  254:  *				or NULL for eof or any error
  255:  * --------------------------------------------------------------------------
  256:  * Notes:     o	fp is left so the next line read from it will be
  257:  *		the one following the final .<-- line.
  258:  * ======================================================================= */
  259: /* --- entry point --- */
  260: chardef	*getnextchar ( FILE *fp )
  261: {
  262: /* --------------------------------------------------------------------------
  263: Allocations and Declarations
  264: -------------------------------------------------------------------------- */
  265: chardef	*new_chardef(), *nextchar=(chardef *)NULL; /*ptr returned to caller*/
  266: int	delete_chardef();		/* free allocated memory if error */
  267: int	findnextchar(), charnum,location; /* get header line for next char */
  268: int	rasterizechar();		/* ascii image --> raster pixmap */
  269: int	parsestat=(-999), parsecorner(); /* get col,row from ".<--" line */
  270: char	*readaline();			/* read next line from fp */
  271: /* --------------------------------------------------------------------------
  272: initialization
  273: -------------------------------------------------------------------------- */
  274: while ( parsestat == (-999) ) {		/* flush entirely blank characters */
  275:   /* --- find and interpret header line for next character --- */
  276:   charnum = findnextchar(fp,&location);	/* read and parse header line */
  277:   if ( charnum < 0 ) goto error;	/* eof or error, no more chars */
  278:   /* --- allocate a new chardef struct and begin populating it --- */
  279:   if ( nextchar == (chardef *)NULL )	/* haven't allocated chardef yet */
  280:     if ( (nextchar=new_chardef())	/* allocate a new chardef */
  281:     ==   (chardef *)NULL ) goto error;	/* and quit if we failed */
  282:   nextchar->charnum = charnum;		/* store charnum in struct */
  283:   nextchar->location = location;	/* and location */
  284:   /* --- get upper-left corner line --- */
  285:   parsestat = parsecorner(readaline(fp), /* parse corner line */
  286:     &(nextchar->toprow),&(nextchar->topleftcol)); /* row and col from line */
  287:   } /* --- end-of-while(parsestat)  --- */
  288: if ( !parsestat ) goto error;		/* quit if parsecorner() failed */
  289: /* --------------------------------------------------------------------------
  290: interpret character image (and parse terminating corner line)
  291: -------------------------------------------------------------------------- */
  292: /* --- read ascii character image and interpret as integer bitmap --- */
  293: if ( rasterizechar(fp,&nextchar->image) != 1 ) /* parse image of char */
  294:   goto error;				/* and quit if failed */
  295: /* --- get lower-left corner line --- */
  296: if ( !parsecorner(readaline(NULL),	/* reread and parse corner line */
  297: &(nextchar->botrow),&(nextchar->botleftcol)) ) /* row and col from line */
  298:   goto error;				/* and quit if failed */
  299: /* --------------------------------------------------------------------------
  300: done
  301: -------------------------------------------------------------------------- */
  302: goto end_of_job;			/* skip error return if successful */
  303: error:
  304:   if ( nextchar != (chardef *)NULL )	/* have an allocated chardef */
  305:     delete_chardef(nextchar);		/* so deallocate it */
  306:   nextchar = (chardef *)NULL;		/* and reset ptr to null for error */
  307: end_of_job:
  308:   return ( nextchar );			/* back with chardef or null */
  309: } /* --- end-of-function getnextchar() --- */
  310: 
  311: 
  312: /* ==========================================================================
  313:  * Function:	getcharname ( fontname, charnum )
  314:  * Purpose:	Looks up charnum for the family specified by fontname
  315:  *		and returns the corresponding charname.
  316:  * --------------------------------------------------------------------------
  317:  * Arguments:	fontname (I)	char * containing fontname for font family
  318:  *				(from -n switch on command line)
  319:  *		charnum (I)	int containing the character number
  320:  *				whose corresponding name is wanted.
  321:  * Returns:	( char * )	ptr to character name
  322:  *				or NULL if charnum not found in table
  323:  * --------------------------------------------------------------------------
  324:  * Notes:     o
  325:  * ======================================================================= */
  326: /* --- entry point --- */
  327: char	*getcharname ( char *fontname, int charnum )
  328: {
  329: /* --------------------------------------------------------------------------
  330: Allocations and Declarations
  331: -------------------------------------------------------------------------- */
  332: /* --- recognized font family names and our corresponding numbers --- */
  333: static	char *fnames[] =	/*font name from -n switch on command line*/
  334: 	{ "cmr","cmmib","cmmi","cmsy","cmex","bbold","rsfs",
  335: 	  "stmary","cyr", NULL };
  336: static	int    fnums[] =	/* corresponding mimetex fontfamily number*/
  337: 	{ CMR10,CMMIB10,CMMI10,CMSY10,CMEX10,BBOLD10,RSFS10,
  338:        STMARY10,  CYR10,    -1 };
  339: static	int    offsets[] =	/* symtable[ichar].charnum = charnum-offset*/
  340: 	{     0,      0,     0,     0,     0,      0,    65,
  341: 	      0,      0,    -1 };
  342: /* --- other local declarations --- */
  343: char	*charname = NULL;	/* character name returned to caller */
  344: char	flower[99] = "noname";	/* lowercase caller's fontname */
  345: int	ifamily = 0,		/* fnames[] (and fnums[],offsets[]) index */
  346: 	offset = 0,		/* offsets[ifamily] */
  347: 	ichar = 0;		/* loop index */
  348: /* --------------------------------------------------------------------------
  349: lowercase caller's fontname and look it up in fnames[]
  350: -------------------------------------------------------------------------- */
  351: /* --- lowercase caller's fontname --- */
  352: for ( ichar=0; *fontname!='\000'; ichar++,fontname++ )/*lowercase each char*/
  353:   flower[ichar] = (isalpha(*fontname)? tolower(*fontname) : *fontname);
  354: flower[ichar] = '\000';		/* null-terminate lowercase fontname */
  355: if ( strlen(flower) < 2 ) goto end_of_job; /* no lookup match possible */
  356: /* --- look up lowercase fontname in our fnames[] table --- */
  357: for ( ifamily=0; ;ifamily++ )	/* check fnames[] for flower */
  358:   if ( fnames[ifamily] == NULL ) goto end_of_job; /* quit at end-of-table */
  359:   else if ( strstr(flower,fnames[ifamily]) != NULL ) break; /* found it */
  360: offset = offsets[ifamily];	/* symtable[ichar].charnum = charnum-offset*/
  361: ifamily = fnums[ifamily];	/* xlate index to font family number */
  362: /* --------------------------------------------------------------------------
  363: now look up name for caller's charnum in ifamily, and return it to caller
  364: -------------------------------------------------------------------------- */
  365: /* --- search symtable[] for charnum in ifamily --- */
  366: for ( ichar=0; ;ichar++ )	/*search symtable[] for charnum in ifamily*/
  367:   if ( symtable[ichar].symbol == NULL ) goto end_of_job; /* end-of-table */
  368:   else
  369:     if ( symtable[ichar].family == ifamily /* found desired family */
  370:     &&   symtable[ichar].handler == NULL ) /* and char isn't a "dummy" */
  371:       if ( symtable[ichar].charnum == charnum-offset ) break; /*got charnum*/
  372: /* --- return corresponding charname to caller --- */
  373: charname = symtable[ichar].symbol; /* pointer to symbol name in table */
  374: end_of_job:
  375:   if ( charname==NULL && isnoname ) /* want unnamed/undefined chars */
  376:     charname = noname;		/* so replace null return with noname */
  377:   return ( charname );
  378: } /* --- end-of-function getcharname() --- */
  379: 
  380: 
  381: /* ==========================================================================
  382:  * Function:	findnextchar ( fp, location )
  383:  * Purpose:	Finds next "beginning of char" line in fp
  384:  *		and returns the character number,
  385:  *		and (optionally) location if arg provided.
  386:  * --------------------------------------------------------------------------
  387:  * Arguments:	fp (I)		FILE *  to input file
  388:  *				(containing output from  gftype -i)
  389:  *		location (O)	int *  returning "location" of character
  390:  *				(or pass NULL and it won't be returned)
  391:  * Returns:	( int )		character number,
  392:  *				or -1 for eof or any error
  393:  * --------------------------------------------------------------------------
  394:  * Notes:     o	fp is left so the next line read from it will be
  395:  *		the one following the "beginning of char" line
  396:  * ======================================================================= */
  397: /* --- entry point --- */
  398: int	findnextchar ( FILE *fp, int *location )
  399: {
  400: /* --------------------------------------------------------------------------
  401: Allocations and Declarations
  402: -------------------------------------------------------------------------- */
  403: static	char keyword[99]="beginning of char "; /*signals start of next char*/
  404: char	*readaline(), *line;	/* read next line from fp */
  405: char	*strstr(), *strchr(), *delim; /* search line for substring, char */
  406: char	token[99];		/* token extracted from line */
  407: int	charnum = (-1);		/* character number returned to caller */
  408: /* --------------------------------------------------------------------------
  409: keep reading lines until eof or keyword found
  410: -------------------------------------------------------------------------- */
  411: while ( (line=readaline(fp)) != NULL ) /* read lines until eof */
  412:   {
  413:   if ( msglevel >= 999 )	/* very, very verbose output requested */
  414:     fprintf(msgfp,"nextchar> line = %s\n",line);
  415:   if ( (delim=strstr(line,keyword)) != NULL ) /* found keyword on line */
  416:     {
  417:     /* --- get character number from line --- */
  418:     strcpy(token,delim+strlen(keyword)); /* char num follows keyword */
  419:     charnum = atoi(token);	/* interpret token as integer charnum */
  420:     /* --- get location at beginning of line --- */
  421:     if ( location != (int *)NULL )  /* caller wants location returned */
  422:       if ( (delim=strchr(line,':')) != NULL ) /* location precedes colon */
  423: 	{ *delim = '\000';	/* terminate line after location */
  424: 	  *location = atoi(line); } /* interpret location as integer */
  425:     break;			/* back to caller with charnum */
  426:     } /* --- end-of-if(delim!=NULL) --- */
  427:   } /* --- end-of-while(line!=NULL) --- */
  428: return ( charnum );		/* back to caller with char number or -1 */
  429: } /* --- end-of-function findnextchar() --- */
  430: 
  431: 
  432: /* ==========================================================================
  433:  * Function:	rasterizechar ( fp, rp )
  434:  * Purpose:	Reads and parses subsequent lines from fp
  435:  *		(until a terminating ".<--" line),
  436:  *		representing the ascii image of the character in fp,
  437:  *		and returns the results in raster struct rp
  438:  * --------------------------------------------------------------------------
  439:  * Arguments:	fp (I)		FILE *  to input file
  440:  *				(containing output from  gftype -i)
  441:  *				positioned immediately after top .<-- line,
  442:  *				ready to read first line of ascii image
  443:  *		rp (O)		raster *  returning the rasterized
  444:  *				character represented on fp as an ascii image
  445:  * Returns:	( int )		1=okay, or 0=eof or any error
  446:  * --------------------------------------------------------------------------
  447:  * Notes:     o	fp is left so the last line (already) read from it
  448:  *		contains the terminating .<-- corner information
  449:  *		(readaline(NULL) will reread this last line)
  450:  *	      o	char images on fp can be no wider than 31 pixels
  451:  * ======================================================================= */
  452: /* --- entry point --- */
  453: int	rasterizechar ( FILE *fp, raster *image )
  454: {
  455: /* --------------------------------------------------------------------------
  456: Allocations and Declarations
  457: -------------------------------------------------------------------------- */
  458: char	*readaline(), *line;	/* read next scan line for char from fp */
  459: unsigned char bitvec[1024][128]; /* scan lines parsed up to 1024x1024 bits */
  460: int	bitcmp();		/* compare bit strings */
  461: int	height = 0,		/* #scan lines in fp comprising char */
  462: 	width = 0;		/* #chars on longest scan line */
  463: int	iscan,			/* bitvec[] index */
  464: 	ibit;			/* bit along scan (i.e., 0...width-1) */
  465: int	isokay = 0;		/* returned status, init for failure */
  466: /* --- bitmap and .gf-formatted image info (we'll choose smallest) --- */
  467: int	iformat = gformat;	/*0=best, 1=bitmap, 2=8-bit.gf, 3=4-bit.gf*/
  468: unsigned char gfpixcount[2][65536]; /* .gf black/white flips (max=64K) */
  469: int	npixcounts[2] = {9999999,9999999}; /* #counts for 8-bit,4-bit .gf */
  470: int	nbytes1=9999999,nbytes2=9999999,nbytes3=9999999;/*#bytes for format*/
  471: /* --------------------------------------------------------------------------
  472: read lines till ".<--" terminator, and construct one vector[] int per line
  473: -------------------------------------------------------------------------- */
  474: memset(bitvec,0,128*1024);	/* zero-fill bitvec[] */
  475: while ( (line=readaline(fp)) != NULL ) /* read lines until eof */
  476:   {
  477:   /* --- allocations and declarations --- */
  478:   int	icol, ncols=strlen(line); /* line[] column index, #cols in line[] */
  479:   /* --- check for end-of-char (when we encounter corner line) --- */
  480:   if ( memcmp(line,CORNER_STUB,strlen(CORNER_STUB)) == 0 ) /* corner line */
  481:     break;			/* so done with loop */
  482:   /* --- parse line (encode asterisks comprising character image) --- */
  483:   memset(bitvec[height],0,128);	/* first zero out all bits */
  484:   for ( icol=0; icol<ncols; icol++ ) /* now check line[] for asterisks */
  485:     if ( line[icol] == '*' )	/* we want to set this bit */
  486:       {	setlongbit(bitvec[height],icol); /* set bit */
  487: 	if ( icol >= width ) width=icol+1; } /* and check for new width */
  488:   height++;			/* bump character height */
  489:   } /* --- end-of-while(line!=NULL) --- */
  490: if ( height<1 || width<1 )	/* some problem parsing character */
  491:   goto end_of_job;		/* so quit */
  492: /* --------------------------------------------------------------------------
  493: init image values
  494: -------------------------------------------------------------------------- */
  495: if ( image->pixmap != NULL )	/* hmm, somebody already allocated memory */
  496:   free((void *)image->pixmap);	/* so just free it */
  497: image->width = width;		/* set image width within raster struct */
  498: image->height = height;		/* and height */
  499: image->format = gformat;	/* set format (will be reset below) */
  500: image->pixsz = 1;		/* #bits per pixel (or #counts in .gf fmt) */
  501: if ( gformat==0 || gformat==1 )	/* bitmap representation allowed */
  502:   { nbytes1 = pixmapsz(image);	/* #bytes needed for bitmap */
  503:     iformat = 1; }		/* default to bitmap format */
  504: /* --------------------------------------------------------------------------
  505: perform .gf-like compression on image in bitvec
  506: -------------------------------------------------------------------------- */
  507: if ( gformat == 0		/* choose optimal/smallest respresentation */
  508: ||   gformat==2 || gformat==3 )	/* .gf-like compressed representation */
  509:  {
  510:  /* --- try both 8-bits/count and 4-bits/count for best compression --- */
  511:  int	maxbitcount[2] = {254,14}; /* don't count too much in one byte */
  512:  int	repeatcmds[2]  = {255,15}; /* opcode for repeat/duplicate count */
  513:  int	minbytes = 0;		/* #bytes needed for smallest format */
  514:  for ( iformat=2; iformat<=3; iformat++ ) { /* 2=8-bit packing, 3=4-bit */
  515:   int	gfbitcount = 0,		/* count of consecutive gfbitval's */
  516: 	gfbitval = 0,		/* begin with count of leading 0's */
  517: 	pixcount = 0;		/* #packed bytes (#black/white flips) */
  518:   unsigned char *gfcount = gfpixcount[iformat-2]; /*counts for this format*/
  519:   if ( gformat!=0 && gformat!=iformat ) /* this format not allowed */
  520:     continue;			/* so just skip it */
  521:   for ( iscan=0; iscan<height; iscan++ ) /* for each integer in bitvec[] */
  522:    {
  523:    int	bitval = 0;		/* current actual pixel value */
  524:    int	nrepeats=0, nextreps=0;	/* #duplicate lines below current,next line*/
  525:    /* --- check for repeated/duplicate scan lines --- */
  526:    if ( isrepeat		/* we're storing scan line repeat counts */
  527:    &&   iscan < height-1 ) {	/* current scan line isn't the last line */
  528:     /* --- count repeats --- */
  529:     int jscan = iscan;		/* compare current scan with lines below it*/
  530:     while ( ++jscan < height ) { /* until last scan line */
  531:      if (nrepeats == jscan-iscan-1) /*no intervening non-identical lines*/
  532:       if ( bitcmp(bitvec[iscan],bitvec[jscan],width) == 0 ) /* identical */
  533:        nrepeats++;		/* so bump repeat count */
  534:      if ( jscan > iscan+1 )	/* we're below next line */
  535:       if (nextreps == jscan-iscan-2) /*no intervening non-identical lines*/
  536:        if ( bitcmp(bitvec[iscan+1],bitvec[jscan],width) == 0 )/*identical*/
  537: 	nextreps++; }		/* so bump next lline repeat count */
  538:     /* --- set repeat command and count --- */
  539:     if ( nrepeats > 0 ) {	/* found repeated lines below current */
  540:      int maxrepeats = maxbitcount[iformat-2]; /*max count/repeats per byte*/
  541:      if ( nrepeats > maxrepeats ) nrepeats=maxrepeats; /* don't exceed max */
  542:      {setbyfmt(iformat,gfcount,pixcount,repeatcmds[iformat-2]);} /*set cmd*/
  543:      {setbyfmt(iformat,gfcount,pixcount+1,nrepeats);} /* set #repeats */
  544:      pixcount += 2; }		/* don't bump pixcount within macros */
  545:     } /* --- end-of-if(isrepeat) --- */
  546:    /* --- set bit counts for current scan line --- */
  547:    for ( ibit=0; ibit<width; ibit++ )	/* for all bits in this scanline */
  548:     {
  549:     bitval = getlongbit(bitvec[iscan],ibit); /* check actual pixel value */
  550:     if ( bitval != gfbitval ) {	/* black-to-white edge (or vice versa) */
  551:       {setbyfmt(iformat,gfcount,pixcount,gfbitcount);} /*set byte or nibble*/
  552:       pixcount++;		/* don't bump pixcount within macro */
  553:       gfbitcount = 0;		/* reset consecutive bit count */
  554:       gfbitval = 1-gfbitval; }	/* flip bit to be counted */
  555:     else			/* check count if continuing with same val */
  556:      if ( gfbitcount >= maxbitcount[iformat-2] ) { /* max count per byte */
  557:       {setbyfmt(iformat,gfcount,pixcount,gfbitcount);} /*set byte or nibble*/
  558:       clearbyfmt(iformat,gfcount,pixcount+1); /*followed by dummy 0 count*/
  559:       pixcount += 2;		/* don't bump pixcount within macros */
  560:       gfbitcount = 0; }		/* reset consecutive bit count */
  561:     if ( bitval == gfbitval )	/* same bit val as preceding, or first new */
  562:       gfbitcount++;		/* so just count another pixel */
  563:     } /* --- end-of-for(ibit) --- */
  564:    /* --- adjust for repeated scan lines --- */
  565:    iscan += nrepeats;		/* skip repeated/duplicate scan lines */
  566:    if ( nrepeats>0 || nextreps>0 ) /* emit count to align on full scan */
  567:     if ( iscan < height-1 )	/* have another scan line below this one */
  568:      if ( gfbitcount > 0 ) {	/* should always have some final count */
  569:       {setbyfmt(iformat,gfcount,pixcount,gfbitcount);} /*set byte or nibble*/
  570:       pixcount++;		/* don't bump pixcount within macro */
  571:       gfbitcount = 0;		/* reset consecutive bit count */
  572:       if ( bitval == getlongbit(bitvec[iscan+1],0) ) { /* same bit value */
  573:        clearbyfmt(iformat,gfcount,pixcount); /*so we need a dummy 0 count*/
  574:        pixcount++; }		/* don't bump pixcount within macros */
  575:       else			/* bitval flips at start of next line */
  576:        gfbitval = 1-gfbitval;	/* so flip bit to be counted */
  577:       } /* --- end-of-if(nrepeats...gfbitcount>0) --- */
  578:    } /* --- end-of-for(iscan) --- */
  579:    /* --- store final count --- */
  580:    if ( gfbitcount > 0 ) {	/* have a final count */
  581:      {setbyfmt(iformat,gfcount,pixcount,gfbitcount);} /*set byte or nibble*/
  582:      pixcount++; }		/* don't bump pixcount within macro */
  583:    else				/* ended exactly after maxbitcount? */
  584:     if ( getbyfmt(iformat,gfcount,pixcount-1) == 0 )/*have dummy 0 trailer?*/
  585:      pixcount--;		/* remove unneeded dummy trailer */
  586:    /* --- save count to choose smallest --- */
  587:    npixcounts[iformat-2] = pixcount; /* save count */
  588:    } /* --- end-of-for(iformat) --- */
  589:  /* --- check for optimal/smallest format --- */
  590:  nbytes2=npixcounts[0];  nbytes3=(1+npixcounts[1])/2; /* #bytes for count */
  591:  iformat = (nbytes2<nbytes3? 2:3); /* choose smallest format */
  592:  minbytes = (iformat==2?nbytes2:nbytes3); /* #bytes for smallest format */
  593:  if ( gformat == 0 )		/* bitmap representation also permitted */
  594:   if ( nbytes1 <= minbytes )	/* and it's the optimal/smallest format */
  595:    iformat = 1;			/* so flip format */
  596:  /* --- move results to returned image --- */
  597:  if ( iformat != 1 ) {		/* using a .gf format */
  598:   if ( (image->pixmap = (unsigned char *)malloc(minbytes)) /* alloc pixmap */
  599:   == NULL ) goto end_of_job;	/* quit if failed to allocate pixmap */
  600:   memcpy(image->pixmap,gfpixcount[iformat-2],minbytes); /*copy local counts*/
  601:   image->format = iformat;	/* signal byte counts or nibble counts */
  602:   image->pixsz = npixcounts[iformat-2]; /*#counts in pixmap for gformat=2,3*/
  603:   } /* --- end-of-if(iformat!=1) --- */
  604:  } /* --- end-of-if(gformat==2) --- */
  605: /* --------------------------------------------------------------------------
  606: copy each integer in bitvec[] to raster pixmap, bit by bit
  607: -------------------------------------------------------------------------- */
  608: if ( iformat == 1 )		/* bit-by-bit representation of image */
  609:  {
  610:  int	ipixel = 0;		/* pixmap index */
  611:  /* --- first allocate image raster pixmap for character --- */
  612:  if ( (image->pixmap = (unsigned char *)malloc(pixmapsz(image)))
  613:  == NULL ) goto end_of_job;	/* quit if failed to allocate pixmap */
  614:  image->format = iformat;	/* reset format */
  615:  /* --- now store bit image in allocated raster --- */
  616:  for ( iscan=0; iscan<height; iscan++ )	/* for each integer in bitvec[] */
  617:   for ( ibit=0; ibit<width; ibit++ )	/* for all bits in this scanline */
  618:     {
  619:     if ( getlongbit(bitvec[iscan],ibit) != 0 ) /* check current scan pixel */
  620:       { setlongbit(image->pixmap,ipixel); }
  621:     else				/*turn off corresponding raster bit*/
  622:       { unsetlongbit(image->pixmap,ipixel); }
  623:     ipixel++;				/* bump image raster pixel */
  624:     } /* --- end-of-for(iscan,ibit) --- */
  625:  } /* --- end-of-if(gformat==1) --- */
  626: /* --------------------------------------------------------------------------
  627: done
  628: -------------------------------------------------------------------------- */
  629: isokay = 1;				/* reset flag for success */
  630:  end_of_job:
  631:   return ( isokay );			/* back with 1=success, 0=failure */
  632: } /* --- end-of-function rasterizechar() --- */
  633: 
  634: 
  635: /* ==========================================================================
  636:  * Function:	parsecorner ( line, row, col )
  637:  * Purpose:	Parses a "pixel corner" line (upper left or lower left)
  638:  *		and returns the (col,row) information on it as integers.
  639:  * --------------------------------------------------------------------------
  640:  * Arguments:	line (I)	char *  to input line containing
  641:  *				".<--This pixel's..." to be parsed
  642:  *		row (O)		int *  returning the (,row)
  643:  *		col (O)		int *  returning the (col,)
  644:  * Returns:	( int )		1 if successful, or 0 for any error
  645:  * --------------------------------------------------------------------------
  646:  * Notes:     o
  647:  * ======================================================================= */
  648: /* --- entry point --- */
  649: int	parsecorner ( char *line, int *row, int *col )
  650: {
  651: /* --------------------------------------------------------------------------
  652: Allocations and Declarations
  653: -------------------------------------------------------------------------- */
  654: int	isokay = 0;		/* success/fail flag, init for failure */
  655: char	field[99], *delim;	/*(col,row) field and ptr to various delims*/
  656: /* --------------------------------------------------------------------------
  657: extract (col,row) field from line, and interpret col and row as integers
  658: -------------------------------------------------------------------------- */
  659: /* --- first, check beginning of line --- */
  660: if ( line == (char *)NULL ) goto end_of_job; /* no line supplied by caller */
  661: /* --- check for blank line --- */
  662: if ( strstr(line,BLANKCHAR_STUB) != NULL ) /* got entirely blank character */
  663:   return ( -999 );			/* so return special -999 signal */
  664: /* --- check for corner --- */
  665: if ( memcmp(line,CORNER_STUB,strlen(CORNER_STUB)) != 0 ) /*not valid corner*/
  666:   goto end_of_job;			/* so quit */
  667: /* --- extract  col,row  field from line --- */
  668: if ( (delim=strchr(line,'(')) == NULL ) goto end_of_job; /*find open paren*/
  669: strncpy(field,delim+1,10);		/* extract next 10 chars */
  670: field[10] = '\000';			/* and null-terminate field */
  671: if ( (delim=strchr(field,')')) == NULL ) goto end_of_job; /*find close paren*/
  672: *delim = '\000';			/* terminate field at close paren */
  673: /* --- interpret col,row as integers --- */
  674: if ( (delim=strchr(field,',')) == NULL ) goto end_of_job; /* find comma */
  675: *delim = '\000';			/* break field into col and row */
  676: if ( col != (int *)NULL )		/* caller gave us ptr for col */
  677:   *col = atoi(field);			/* so return it to him */
  678: if ( row != (int *)NULL )		/* caller gave us ptr for row */
  679:   *row = atoi(delim+1);			/* so return it to him */
  680: /* --------------------------------------------------------------------------
  681: done
  682: -------------------------------------------------------------------------- */
  683: isokay = 1;				/* reset flag for success */
  684:  end_of_job:
  685:   return ( isokay );			/* back with success/fail flag */
  686: } /* --- end-of-function parsecorner() --- */
  687: 
  688: 
  689: /* ==========================================================================
  690:  * Function:	readaline ( fp )
  691:  * Purpose:	Reads a line from fp, strips terminating newline,
  692:  *		and returns ptr to internal buffer
  693:  * --------------------------------------------------------------------------
  694:  * Arguments:	fp (I)		FILE *  to input file to be read.
  695:  *				If null, returns line previously read.
  696:  * Returns:	( char * )	internal buffer containing line read,
  697:  *				or NULL for eof or error.
  698:  * --------------------------------------------------------------------------
  699:  * Notes:     o	fp is left on the line following the returned line
  700:  * ======================================================================= */
  701: /* --- entry point --- */
  702: char	*readaline ( FILE *fp )
  703: {
  704: /* --------------------------------------------------------------------------
  705: Allocations and Declarations
  706: -------------------------------------------------------------------------- */
  707: static	char buffer[2048];	/* static buffer returned to caller */
  708: char	*fgets(), *bufptr=buffer; /* read line from fp */
  709: char	*strchr(), *delim;	/* remove terminating newline */
  710: /* --------------------------------------------------------------------------
  711: Read line and strip trailing newline
  712: -------------------------------------------------------------------------- */
  713: if ( fp != NULL )			/*if null, return previous line read*/
  714:   if ( (bufptr=fgets(buffer,2047,fp))	/* read next line from fp */
  715:   != NULL )				/* and check that we succeeded */
  716:     {
  717:     if ( (delim=strchr(bufptr,'\n'))	/* look for terminating newline */
  718:     != NULL )				/* and check that we found it */
  719:       *delim = '\000';			/* truncate line at newline */
  720:     } /* --- end-of-if(fgets()!=NULL) --- */
  721: return ( bufptr );			/*back to caller with buffer or null*/
  722: } /* --- end-of-function readaline() --- */
  723: 
  724: 
  725: /* ==========================================================================
  726:  * Function:	bitcmp ( bs1, bs2, n )
  727:  * Purpose:	compares the first n bits of two strings
  728:  * --------------------------------------------------------------------------
  729:  * Arguments:	bs1 (I)		unsigned char * to first bit string
  730:  *		bs2 (I)		unsigned char * to second bit string
  731:  *		n (I)		int containing #bits to compare
  732:  * Returns:	( int )		0 if first n bits are identical
  733:  *				-1 if first unmatching bit of bs1 is 0
  734:  *				+1 if first unmatching bit of bs2 id 0
  735:  * --------------------------------------------------------------------------
  736:  * Notes:     o
  737:  * ======================================================================= */
  738: /* --- entry point --- */
  739: int	bitcmp ( unsigned char *bs1, unsigned char *bs2, int n )
  740: {
  741: /* --------------------------------------------------------------------------
  742: Allocations and Declarations
  743: -------------------------------------------------------------------------- */
  744: int	icmp = 0;		/* returned to caller */
  745: int	nbytes = n/8,		/* #full bytes we can compare with memcmp()*/
  746: 	nbits  = n%8,  ibit=0;	/* #trailing bits in last byte, index */
  747: /* --------------------------------------------------------------------------
  748: compare leading bytes, then trailing bits
  749: -------------------------------------------------------------------------- */
  750: if ( nbytes > 0 ) icmp = memcmp(bs1,bs2,nbytes); /* compare leading bytes */
  751: if ( icmp == 0 )		/* leading bytes identical */
  752:  if ( nbits > 0 )		/* and we have trailing bits */
  753:   for ( ibit=0; ibit<nbits; ibit++ ) /* check each bit */
  754:    { icmp = (int)get1bit(bs1[nbytes],ibit) - (int)get1bit(bs2[nbytes],ibit);
  755:      if ( icmp != 0 ) break; }	/* done at first unmatched bit */
  756: return ( icmp );		/* back to caller with -1,0,+1 */
  757: } /* --- end-of-function bitcmp() --- */
  758: /* --- end-of-file gfuntype.c --- */
  759: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>