--- loncom/cgi/mimeTeX/mimetex.c 2005/02/28 19:08:11 1.1 +++ loncom/cgi/mimeTeX/mimetex.c 2008/12/04 12:17:13 1.4 @@ -1,10 +1,11 @@ /**************************************************************************** * - * Copyright(c) 2002-2005, John Forkosh Associates, Inc. All rights reserved. + * Copyright(c) 2002-2008, John Forkosh Associates, Inc. All rights reserved. + * http://www.forkosh.com mailto: john@forkosh.com * -------------------------------------------------------------------------- * This file is part of mimeTeX, which is free software. You may redistribute * and/or modify it under the terms of the GNU General Public License, - * version 2 or later, as published by the Free Software Foundation. + * version 3 or later, as published by the Free Software Foundation. * MimeTeX is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY, not even the implied warranty of MERCHANTABILITY. * See the GNU General Public License for specific details. @@ -12,10 +13,11 @@ * agreed to these terms and conditions, and that you possess the legal * right and ability to enter into this agreement and to use mimeTeX * in accordance with it. - * Your mimeTeX distribution should contain a copy of the GNU General - * Public License. If not, write to the Free Software Foundation, Inc., - * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA, - * or point your browser to http://www.gnu.org/licenses/gpl.html + * Your mimetex.zip distribution file should contain the file COPYING, + * an ascii text copy of the GNU General Public License, version 3. + * If not, point your browser to http://www.gnu.org/licenses/ + * or write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * -------------------------------------------------------------------------- * * Purpose: o MimeTeX, licensed under the gpl, lets you easily embed @@ -25,21 +27,29 @@ * entirely separate little program that doesn't use TeX or * its fonts in any way. It's just one cgi that you put in * your site's cgi-bin/ directory, with no other dependencies. - * So mimeTeX is very easy to install. And it's equally easy - * to use. Just place an html tag in your document + * So mimeTeX is very easy to install. And it's equally + * easy to use. Just place an html tag in your document * wherever you want to see the corresponding LaTeX expression. * For example, * * immediately generates the corresponding gif image on-the-fly, * displaying the rendered expression wherever you put that - * tag. MimeTeX doesn't need intermediate dvi-to-gif - * conversion, and it doesn't clutter up your filesystem with - * separate little gif files for each converted expression. + * tag. + * MimeTeX doesn't need intermediate dvi-to-gif conversion, + * and it doesn't clutter up your filesystem with separate + * little gif files for each converted expression. + * But image caching is available by using mimeTeX's + * -DCACHEPATH=\"path/\" compile option (see below). * There's also no inherent need to repeatedly write the * cumbersome tag illustrated above. You can write * your own custom tags, or write a wrapper script around - * mimeTeX to simplify the necessary notation. + * mimeTeX to simplify the notation. + * Further discussion about mimeTeX's features and + * usage is available on its homepage, + * http://www.forkosh.com/mimetex.html + * and similarly in mimetex.html included with your mimetex.zip + * distribution file. * * Functions: ===================== Raster Functions ====================== * PART2 --- raster constructor functions --- @@ -53,12 +63,14 @@ * rastcpy(rp) allocate new copy of rp * subrastcpy(sp) allocate new copy of sp * rastrot(rp) new raster rotated right 90 degrees to rp + * rastref(rp,axis) new raster reflected (axis 1=horz,2=vert) * rastput(target,source,top,left,isopaque) overlay src on trgt * rastcompose(sp1,sp2,offset2,isalign,isfree) sp2 on top of sp1 * rastcat(sp1,sp2,isfree) concatanate sp1||sp2 * rastack(sp1,sp2,base,space,iscenter,isfree)stack sp2 atop sp1 * rastile(tiles,ntiles) create composite raster from tiles - * rastsquash(sp1,sp2,xmin,ymin) calc #squash pixels sp1||sp2 + * rastsmash(sp1,sp2,xmin,ymin) calc #smash pixels sp1||sp2 + * rastsmashcheck(term) check if term is "safe" to smash * --- raster "drawing" functions --- * accent_subraster(accent,width,height) draw \hat\vec\etc * arrow_subraster(width,height,drctn,isBig) left/right arrow @@ -70,20 +82,25 @@ * circle_recurse(rp,row0,col0,row1,col1,thickness,theta0,theta1) * bezier_raster(rp,r0,c0,r1,c1,rt,ct) draw bezier recursively * border_raster(rp,ntop,nbot,isline,isfree)put border around rp + * backspace_raster(rp,nback,pback,minspace,isfree) neg space * --- raster (and chardef) output functions --- * type_raster(rp,fp) emit ascii dump of rp on file ptr fp * type_bytemap(bp,grayscale,width,height,fp) dump bytemap on fp * xbitmap_raster(rp,fp) emit mime xbitmap of rp on fp + * type_pbmpgm(rp,ptype,file) pbm or pgm image of rp to file * cstruct_chardef(cp,fp,col1) emit C struct of cp on fp * cstruct_raster(rp,fp,col1) emit C struct of rp on fp * hex_bitmap(rp,fp,col1,isstr)emit hex dump of rp->pixmap on fp * --- ancillary output functions --- * emit_string(fp,col1,string,comment) emit string and C comment + * gftobitmap(rp) convert .gf-like pixmap to bitmap image * ====================== Font Functions ======================= * --- font lookup functions --- - * get_symdef(symbol) returns mathchardef for symbol - * get_chardef(symdef,size) returns chardef for symdef,size + * get_symdef(symbol) return mathchardef for symbol + * get_ligature(expr,family) return symtable index for ligature + * get_chardef(symdef,size) return chardef for symdef,size * get_charsubraster(symdef,size) wrap subraster around chardef + * get_symsubraster(symbol,size) returns subraster for symbol * --- ancillary font functions --- * get_baseline(gfdata) determine baseline (in our coords) * get_delim(symbol,height,family) delim just larger than height @@ -91,6 +108,7 @@ * ================= Tokenize/Parse Functions ================== * texchar(expression,chartoken) retruns next char or \sequence * texsubexpr(expr,subexpr,maxsubsz,left,right,isescape,isdelim) + * texleft(expr,subexpr,maxsubsz,ldelim,rdelim) \left...\right * texscripts(expression,subscript,superscript,which)get scripts * --- ancillary parse functions --- * isbrace(expression,braces,isescape) check for leading brace @@ -98,6 +116,7 @@ * mimeprep(expression) preprocessor converts \left( to \(, etc. * strchange(nfirst,from,to) change nfirst chars of from to to * strreplace(string,from,to,nreplace) change from to to in str + * strwstr(string,substr,white,sublen) find substr in string * strtexchr(string,texchr) find texchr in string * findbraces(expression,command) find opening { or closing } * PART3 =========== Rasterize an Expression (recursively) =========== @@ -110,6 +129,8 @@ * rastdispmath(expression,size,sp) scripts for displaymath * --- table-driven handlers that rasterize... --- * rastleft(expression,size,basesp,ildelim,arg2,arg3)\left\right + * rastright(expression,size,basesp,ildelim,arg2,arg3) ...\right + * rastmiddle(expression,size,basesp,arg1,arg2,arg3) \middle * rastflags(expression,size,basesp,flag,value,arg3) set flag * rastspace(expression,size,basesp,width,isfill,isheight)\,\:\; * rastnewline(expression,size,basesp,arg1,arg2,arg3) \\ @@ -126,39 +147,62 @@ * rastarray(expression,size,basesp,arg1,arg2,arg3) \array * rastpicture(expression,size,basesp,arg1,arg2,arg3) \picture * rastline(expression,size,basesp,arg1,arg2,arg3) \line + * rastrule(expression,size,basesp,arg1,arg2,arg3) \rule * rastcircle(expression,size,basesp,arg1,arg2,arg3) \circle * rastbezier(expression,size,basesp,arg1,arg2,arg3) \bezier * rastraise(expression,size,basesp,arg1,arg2,arg3) \raisebox * rastrotate(expression,size,basesp,arg1,arg2,arg3) \rotatebox + * rastreflect(expression,size,basesp,arg1,arg2,arg3)\reflectbox * rastfbox(expression,size,basesp,arg1,arg2,arg3) \fbox * rastinput(expression,size,basesp,arg1,arg2,arg3) \input * rastcounter(expression,size,basesp,arg1,arg2,arg3) \counter + * rasttoday(expression,size,basesp,arg1,arg2,arg3) \today + * rastcalendar(expression,size,basesp,arg1,arg2,arg3) \calendar * rastnoop(expression,size,basesp,arg1,arg2,arg3) flush \escape * --- helper functions for handlers --- * rastopenfile(filename,mode) opens filename[.tex] in mode - * rastreadfile(filename,tag,value) read between ... + * rasteditfilename(filename) edit filename (for security) + * rastreadfile(filename,islock,tag,value) read ... * rastwritefile(filename,tag,value,isstrict)write... - * timestamp( ) formats timestamp string (used by rastcounter) - * dtoa(d,npts) double to comma-separated ascii + * calendar(year,month,day) formats one-month calendar string + * timestamp(tzdelta,ifmt) formats timestamp string + * tzadjust(tzdelta,year,month,day,hour) adjust date/time + * daynumber(year,month,day) #days since Monday, Jan 1, 1973 + * dbltoa(d,npts) double to comma-separated ascii * === Anti-alias completed raster (lowpass) or symbols (ss) === * aalowpass(rp,bytemap,grayscale) lowpass grayscale bytemap * aapnm(rp,bytemap,grayscale) lowpass based on pnmalias.c + * aapnmlookup(rp,bytemap,grayscale) aapnm based on aagridnum() + * aapatterns(rp,irow,icol,gridnum,patternum,grayscale) call 19, + * aapattern1124(rp,irow,icol,gridnum,grayscale)antialias pattrn + * aapattern19(rp,irow,icol,gridnum,grayscale) antialias pattern + * aapattern20(rp,irow,icol,gridnum,grayscale) antialias pattern + * aapattern39(rp,irow,icol,gridnum,grayscale) antialias pattern + * aafollowline(rp,irow,icol,direction) looks for a "turn" + * aagridnum(rp,irow,icol) calculates gridnum, 0-511 + * aapatternnum(gridnum) looks up pattern#, 1-51, for gridnum + * aalookup(gridnum) table lookup for all possible 3x3 grids + * aalowpasslookup(rp,bytemap,grayscale) driver for aalookup() * aasupsamp(rp,aa,sf,grayscale) or by supersampling * aacolormap(bytemap,nbytes,colors,colormap)make colors,colormap * aaweights(width,height) builds "canonical" weight matrix * aawtpixel(image,ipixel,weights,rotate) weight image at ipixel + * === miscellaneous === + * mimetexsetmsg(newmsglevel,newmsgfp) set msglevel and msgfp * PART1 ========================== Driver =========================== * main(argc,argv) parses math expression and emits mime xbitmap + * CreateGifFromEq(expression,gifFileName) entry pt for win dll * isstrstr(string,snippets,iscase) are any snippets in string? * ismonth(month) is month current month ("jan"-"dec")? * unescape_url(url,isescape), x2c(what) xlate %xx url-encoded * logger(fp,msglevel,logvars) logs environment variables - * emitcache(cachefile) read cachefile and emit bytes to stdout + * emitcache(cachefile,maxage,valign,isbuffer) emit cachefile + * readcachefile(cachefile,buffer) read cachefile into buffer * md5str(instr) md5 hash library functions * GetPixel(x,y) callback function for gifsave library * * Source: mimetex.c (needs mimetex.h and texfonts.h to compile, - * and also needs gifsave.c if compiled with -DAA or -DGIF) + * and also needs gifsave.c when compiled with -DAA or -DGIF) * * -------------------------------------------------------------------------- * Notes o See bottom of file for main() driver (and "friends"), @@ -175,11 +219,13 @@ * and with -DAA or -DGIF you'll also need gifsave.c * o For gif images, the gifsave.c library by Sverre H. Huseby * slightly modified by me to allow - * (a)sending output to stdout and (b)specifying a transparent - * background color index, is included with mimeTeX, - * and it's documented in mimetex.html#gifsave . + * (a)sending output to stdout or returning it in memory, + * and (b)specifying a transparent background color index, + * is included with mimeTeX, and it's documented in + * mimetex.html#gifsave . * o Optional compile-line -D defined symbols are documented - * in mimetex.html#options . They include... + * in mimetex.html#options . They include (additional -D + * switches are discussed in mimetex.html#options)... * -DAA * Turns on gif anti-aliasing with default values * (CENTERWT=32, ADJACENTWT=3, CORNERWT=1) @@ -191,11 +237,11 @@ * algorithm for anti-aliasing, which is applied to the * existing set of bitmap fonts. This lowpass filter * applies default weights - * 1 3 1 - * 3 32 3 - * 1 3 1 + * 1 2 1 + * 2 8 2 + * 1 2 1 * to neighboring pixels. The defaults weights are - * CENTERWT=32, ADJACENTWT=3 and CORNERWT=1, + * CENTERWT=8, ADJACENTWT=2 and CORNERWT=1, * which you can adjust to control anti-aliasing. * Lower CENTERWT values will blur/spread out lines * while higher values will tend to sharpen lines. @@ -294,6 +340,9 @@ * 02/01/04 J.Forkosh Version 1.40 released. * 10/02/04 J.Forkosh Version 1.50 released. * 11/30/04 J.Forkosh Version 1.60 released. + * 10/11/05 J.Forkosh Version 1.64 released. + * 11/30/06 J.Forkosh Version 1.65 released. + * 09/06/08 J.Forkosh Version 1.70 released. * ****************************************************************************/ @@ -311,8 +360,9 @@ header files and macros /* --- windows-specific header info --- */ #ifndef WINDOWS /* -DWINDOWS not supplied by user */ - #if defined(_WIN32) || defined(WIN32) \ - || defined(DJGPP) /* try to recognize windows */ + #if defined(_WINDOWS) || defined(_WIN32) || defined(WIN32) \ + || defined(DJGPP) /* try to recognize windows compilers */ \ + || defined(_USRDLL) /* must be WINDOWS if compiling for DLL */ #define WINDOWS /* signal windows */ #endif #endif @@ -328,6 +378,11 @@ header files and macros #if defined(_O_BINARY) || defined(O_BINARY) /* setmode() now available */ #define HAVE_SETMODE /* so we'll use setmode() */ #endif + #if defined(_MSC_VER) && defined(_DEBUG) /* MS VC++ in debug mode */ + /* to show source file and line numbers where memory leaks occur... */ + #define _CRTDBG_MAP_ALLOC /* ...include this debug macro */ + #include /* and this debug library */ + #endif #define ISWINDOWS 1 #else #define ISWINDOWS 0 @@ -348,9 +403,12 @@ header files and macros #else #define ISSUPERSAMPLING 0 #ifndef AAALGORITHM - #define AAALGORITHM 2 /* default lowpass algorithm */ + #define AAALGORITHM 3 /*2*/ /* default lowpass algorithm */ #endif #endif +#ifndef MAXFOLLOW + #define MAXFOLLOW 8 /* aafollowline() maxturn default */ +#endif /* --- set aa (and default gif) if any anti-aliasing options specified --- */ #if defined(AA) || defined(GIF) || defined(PNG) \ @@ -388,7 +446,9 @@ header files and macros #define NOREFMAXLEN 9999 /* default to any length query */ #endif #else - #define NOTEXFONTS /* texfonts not required */ + #ifndef TEXFONTS + #define NOTEXFONTS /* texfonts not required */ + #endif #endif /* --- application headers --- */ @@ -396,20 +456,58 @@ header files and macros #define TEXFONTS /* to include texfonts.h */ #endif #include "mimetex.h" +/* --- info needed when gif image returned in memory buffer --- */ +#ifdef GIF /* compiling along with gifsave.c */ + extern int gifSize; + extern int maxgifSize; +#else /* or just set dummy values */ + static int gifSize=0, maxgifSize=0; +#endif +/* --- gamma correction --- */ +#ifndef GAMMA + #define GAMMA 1.25 /*1.75*/ /*2.2*/ +#endif +#ifndef REVERSEGAMMA + #define REVERSEGAMMA 0.5 /* for \reverse white-on-black */ +#endif +/* --- opaque background (default to transparent) --- */ +#ifndef OPAQUE + #define ISTRANSPARENT 1 +#else + #define ISTRANSPARENT 0 +#endif +/* --- internal buffer sizes --- */ +#if !defined(MAXEXPRSZ) + #define MAXEXPRSZ (32768-1) /*max #bytes in input tex expression*/ +#endif +#if !defined(MAXSUBXSZ) + #define MAXSUBXSZ (((MAXEXPRSZ+1)/2)-1)/*max #bytes in input subexpression*/ +#endif +#if !defined(MAXTOKNSZ) + #define MAXTOKNSZ (((MAXSUBXSZ+1)/4)-1) /* max #bytes in input token */ +#endif +#if !defined(MAXFILESZ) + #define MAXFILESZ (65536-1) /*max #bytes in input (output) file*/ +#endif +#if !defined(MAXLINESZ) + #define MAXLINESZ (4096-1) /* max #chars in line from file */ +#endif +#if !defined(MAXGIFSZ) + #define MAXGIFSZ 131072 /* max #bytes in output GIF image */ +#endif /* ------------------------------------------------------------------------- adjustable default values -------------------------------------------------------------------------- */ /* --- anti-aliasing parameters --- */ #ifndef CENTERWT + /*#define CENTERWT 32*/ /* anti-aliasing centerwt default */ /*#define CENTERWT 10*/ /* anti-aliasing centerwt default */ - /*#define CENTERWT 6*/ /* anti-aliasing centerwt default */ - #define CENTERWT 32 /* anti-aliasing centerwt default */ + #define CENTERWT 8 /* anti-aliasing centerwt default */ #endif #ifndef ADJACENTWT /*#define ADJACENTWT 3*/ /* anti-aliasing adjacentwt default*/ - /*#define ADJACENTWT 2*/ /* anti-aliasing adjacentwt default*/ - #define ADJACENTWT 3 /* anti-aliasing adjacentwt default*/ + #define ADJACENTWT 2 /* anti-aliasing adjacentwt default*/ #endif #ifndef CORNERWT #define CORNERWT 1 /* anti-aliasing cornerwt default*/ @@ -428,7 +526,7 @@ GLOBAL(int,minadjacent,MINADJACENT); /* GLOBAL(int,maxadjacent,MAXADJACENT); /* darken if<=adjacent pts black */ GLOBAL(int,weightnum,1); /* font wt, */ GLOBAL(int,maxaaparams,4); /* #entries in table */ -/* --- parameter values by font weight --- */ +/* --- anti-aliasing parameter values by font weight --- */ #define aaparameters struct aaparameters_struct /* typedef */ aaparameters { int centerwt; /* lowpass matrix center pixel wt*/ @@ -450,6 +548,9 @@ STATIC aaparameters aaparams[] /* set p } /* --- end-of-aaparams[] --- */ #endif ; +/* --- anti-aliasing diagnostics (to help improve algorithm) --- */ +STATIC int patternnumcount0[99], patternnumcount1[99], /*aalookup() counts*/ + ispatternnumcount = 1; /* true to accumulate counts */ /* ------------------------------------------------------------------------- other variables @@ -473,20 +574,23 @@ other variables #ifndef FGBLUE #define FGBLUE (ISBLACKONWHITE?0:255) #endif -/* --- "squash" margin (0 means no squashing) --- */ -#ifndef SQUASHMARGIN - #ifdef NOSQUASH - #define SQUASHMARGIN 0 +/* --- "smash" margin (0 means no smashing) --- */ +#ifndef SMASHMARGIN + #ifdef NOSMASH + #define SMASHMARGIN 0 #else - #define SQUASHMARGIN 3 + #define SMASHMARGIN 3 #endif #endif +#ifndef SMASHCHECK + #define SMASHCHECK 0 +#endif /* --- textwidth --- */ #ifndef TEXTWIDTH #define TEXTWIDTH (400) #endif /* --- font "combinations" --- */ -#define CMSYEX (107) /* select CMSY10 _or_ CMEX10 */ +#define CMSYEX (109) /*select CMSY10, CMEX10 or STMARY10*/ /* --- prefix prepended to all expressions --- */ #ifndef PREFIX #define PREFIX "\000" /* default no prepended prefix */ @@ -522,6 +626,20 @@ other variables #ifndef PATHPREFIX #define PATHPREFIX "\000" /* paths relative mimetex.cgi */ #endif +/* --- time zone delta t (in hours) --- */ +#ifndef TZDELTA + #define TZDELTA 0 +#endif +/* --- treat +'s in query string as blanks? --- */ +#ifdef PLUSBLANK /* + always interpreted as blank */ + #define ISPLUSBLANK 1 +#else + #ifdef PLUSNOTBLANK /* + never interpreted as blank */ + #define ISPLUSBLANK 0 + #else /* program tries to determine */ + #define ISPLUSBLANK (-1) + #endif +#endif /* ------------------------------------------------------------------------- debugging and logging / error reporting @@ -555,27 +673,43 @@ control flags and values -------------------------------------------------------------------------- */ GLOBAL(int,recurlevel,0); /* inc/decremented in rasterize() */ GLOBAL(int,scriptlevel,0); /* inc/decremented in rastlimits() */ -GLOBAL(int,istext,0); /* textmode if true,italics=2,bb=3 */ -GLOBAL(int,isstring ,0); /*pixmap is ascii string, not raster*/ +GLOBAL(int,isstring,0); /*pixmap is ascii string, not raster*/ +GLOBAL(int,isligature,0); /* true if ligature found */ +GLOBAL(char,*subexprptr,(char *)NULL); /* ptr within expression to subexpr*/ +/*SHARED(int,imageformat,1);*/ /* image is 1=bitmap, 2=.gf-like */ GLOBAL(int,isdisplaystyle,1); /* displaystyle mode (forced if 2) */ GLOBAL(int,ispreambledollars,0); /* displaystyle mode set by $$...$$ */ +GLOBAL(int,fontnum,0); /* cal=1,scr=2,rm=3,it=4,bb=5,bf=6 */ GLOBAL(int,fontsize,NORMALSIZE); /* current size */ GLOBAL(int,displaysize,DISPLAYSIZE); /* use \displaystyle when fontsize>=*/ GLOBAL(int,shrinkfactor,3); /* shrinkfactors[fontsize] */ GLOBAL(double,unitlength,1.0); /* #pixels per unit (may be <1.0) */ /*GLOBAL(int,textwidth,TEXTWIDTH);*/ /* #pixels across line */ -GLOBAL(int,squashmargin,SQUASHMARGIN); /* minimum "squash" margin */ -GLOBAL(int,issquashdelta,1); /* true if squashmargin is a delta */ -GLOBAL(int,istransparent,1); /*true to set background transparent*/ +GLOBAL(int,isnocatspace,0); /* >0 to not add space in rastcat()*/ +GLOBAL(int,smashmargin,SMASHMARGIN); /* minimum "smash" margin */ +GLOBAL(int,mathsmashmargin,SMASHMARGIN); /* needed for \text{if $n-m$ even}*/ +GLOBAL(int,issmashdelta,1); /* true if smashmargin is a delta */ +GLOBAL(int,isexplicitsmash,0); /* true if \smash explicitly given */ +GLOBAL(int,smashcheck,SMASHCHECK); /* check if terms safe to smash */ +GLOBAL(int,isscripted,0); /* is (lefthand) term text-scripted*/ +GLOBAL(int,isdelimscript,0); /* is \right delim text-scripted */ +GLOBAL(int,issmashokay,0); /*is leading char okay for smashing*/ +#define BLANKSIGNAL (-991234) /*rastsmash signal right-hand blank*/ +GLOBAL(int,blanksignal,BLANKSIGNAL); /*rastsmash signal right-hand blank*/ +GLOBAL(int,blanksymspace,0); /* extra (or too much) space wanted*/ +GLOBAL(int,istransparent,ISTRANSPARENT);/* true sets background transparent*/ GLOBAL(int,fgred,FGRED); GLOBAL(int,fggreen,FGGREEN); GLOBAL(int,fgblue,FGBLUE); /* fg r,g,b */ GLOBAL(int,bgred,BGRED); GLOBAL(int,bggreen,BGGREEN); GLOBAL(int,bgblue,BGBLUE); /* bg r,g,b */ +GLOBAL(double,gammacorrection,GAMMA); /* gamma correction */ +GLOBAL(int,isplusblank,ISPLUSBLANK); /*interpret +'s in query as blanks?*/ GLOBAL(int,isblackonwhite,ISBLACKONWHITE); /*1=black on white,0=reverse*/ GLOBAL(char,exprprefix[256],PREFIX); /* prefix prepended to expressions */ GLOBAL(int,aaalgorithm,AAALGORITHM); /* for lp, 1=aalowpass, 2 =aapnm */ +GLOBAL(int,maxfollow,MAXFOLLOW); /* aafollowline() maxturn parameter*/ GLOBAL(int,fgalias,1); GLOBAL(int,fgonly,0); GLOBAL(int,bgalias,0); @@ -587,8 +721,13 @@ GLOBAL(subraster,*workingbox,(subraster GLOBAL(int,isreplaceleft,0); /* true to replace leftexpression */ GLOBAL(subraster,*leftexpression,(subraster *)NULL); /*rasterized so far*/ GLOBAL(mathchardef,*leftsymdef,NULL); /* mathchardef for preceding symbol*/ +GLOBAL(int,fraccenterline,NOVALUE); /* baseline for punct. after \frac */ +/*GLOBAL(int,currentcharclass,NOVALUE);*/ /*primarily to check for PUNCTION*/ GLOBAL(int,iscaching,ISCACHING); /* true if caching images */ GLOBAL(char,cachepath[256],CACHEPATH); /* relative path to cached files */ +GLOBAL(int,isemitcontenttype,1); /* true to emit mime content-type */ +int iscachecontenttype = 0; /* true to cache mime content-type */ +char contenttype[2048] = "\000"; /* content-type:, etc buffer */ GLOBAL(char,pathprefix[256],PATHPREFIX); /*prefix for \input,\counter paths*/ /*GLOBAL(int,iswindows,ISWINDOWS);*/ /* true if compiled for ms windows */ @@ -606,6 +745,9 @@ miscellaneous macros { char *p; while((p=strchr((s),(c)))!=NULL) strcpy(p,p+1); } else #define slower(s) if ((s)!=NULL) /* lowercase all chars in s */ \ { char *p=(s); while(*p!='\000'){*p=tolower(*p); p++;} } else +/*subraster *subrastcpy();*/ /* need global module declaration */ +/*#define spnosmash(sp) if (sp->type==CHARASTER) sp=subrastcpy(sp); \*/ +/* sp->type=blanksignal*/ /* --- * PART2 @@ -655,6 +797,7 @@ if ( rp == (raster *)NULL ) /* malloc f goto end_of_job; /* return error to caller */ rp->width = width; /* store width in raster struct */ rp->height = height; /* and store height */ +rp->format = 1; /* initialize as bitmap format */ rp->pixsz = pixsz; /* store #bits per pixel */ rp->pixmap = (pixbyte *)NULL; /* init bitmap as null ptr */ /* --- allocate and initialize bitmap array --- */ @@ -775,6 +918,7 @@ cp->charnum = cp->location = 0; /* init cp->toprow = cp->topleftcol = 0; /* init upper-left corner */ cp->botrow = cp->botleftcol = 0; /* init lower-left corner */ cp->image.width = cp->image.height = 0; /* init raster dimensions */ +cp->image.format = 0; /* init raster format */ cp->image.pixsz = 0; /* and #bits per pixel */ cp->image.pixmap = NULL; /* init raster pixmap as null */ /* ------------------------------------------------------------------------- @@ -947,6 +1091,8 @@ newsp->image = newrp; /* new raster im switch ( sp->type ) /* set new raster image type */ { case STRINGRASTER: case CHARASTER: newsp->type = STRINGRASTER; break; case ASCIISTRING: newsp->type = ASCIISTRING; break; + case FRACRASTER: newsp->type = FRACRASTER; break; + case BLANKSIGNAL: newsp->type = blanksignal; break; case IMAGERASTER: default: newsp->type = IMAGERASTER; break; } /* --- return copy of sp to caller --- */ end_of_job: @@ -960,7 +1106,7 @@ end_of_job: * -------------------------------------------------------------------------- * Arguments: rp (I) ptr to raster struct to be rotated * -------------------------------------------------------------------------- - * Returns: ( raster * ) ptr to new raster rotated ralative to rp, + * Returns: ( raster * ) ptr to new raster rotated relative to rp, * or NULL for any error. * -------------------------------------------------------------------------- * Notes: o An underbrace is } rotated 90 degrees clockwise, @@ -993,6 +1139,47 @@ return ( rotated ); /* return rotated /* ========================================================================== + * Function: rastref ( rp, axis ) + * Purpose: reflects rp, horizontally about y-axis |_ becomes _| if axis=1 + * or vertically about x-axis M becomes W if axis=2. + * -------------------------------------------------------------------------- + * Arguments: rp (I) ptr to raster struct to be reflected + * axis (I) int containing 1 for horizontal reflection, + * or 2 for vertical + * -------------------------------------------------------------------------- + * Returns: ( raster * ) ptr to new raster reflected relative to rp, + * or NULL for any error. + * -------------------------------------------------------------------------- + * Notes: o + * ======================================================================= */ +/* --- entry point --- */ +raster *rastref ( raster *rp, int axis ) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +raster *new_raster(), *reflected=NULL; /* reflected raster back to caller */ +int height = rp->height, irow, /* height, row index */ + width = rp->width, icol, /* width, column index */ + pixsz = rp->pixsz; /* #bits per pixel */ +/* ------------------------------------------------------------------------- +allocate reflected raster and fill it +-------------------------------------------------------------------------- */ +/* --- allocate reflected raster with same width, height --- */ +if ( axis==1 || axis==2 ) /* first validate axis arg */ + if ( (reflected = new_raster(width,height,pixsz)) /* same width, height */ + != NULL ) /* check that allocation succeeded */ + /* --- fill reflected raster --- */ + for ( irow=0; irow= 0 ) /* bounds check okay */ - if ( svalue!=0 || isopaque ) /*got dark or opaque source*/ - setpixel(target,irow+top,icol+left,svalue); /*overlay source on target*/ + if ( svalue!=0 || isopaque ) { /*got dark or opaque source*/ + setpixel(target,irow+top,icol+left,svalue); }/*overlay source on targ*/ } /* --- end-of-for(icol) --- */ } /* --- end-of-for(irow) --- */ /* ------------------------------------------------------------------------- @@ -1190,11 +1377,12 @@ int base1 = sp1->baseline, /*baseline pixsz2 = (sp2->image)->pixsz, /* pixsz for right-hand subraster */ type2 = sp2->type; /* image type for right-hand */ int height=0, width=0, pixsz=0, base=0; /*concatted sp1||sp2 composite*/ -int issquash = (squashmargin!=0?1:0), /* true to "squash" sp1||sp2 */ - isopaque = (issquash?0:1), /* not oppaque if squashing */ - rastsquash(), isblank=0, nsquash=0, /* #cols to squash */ - oldsquashmargin = squashmargin; /* save original squashmargin */ -int blanksignal = (-991234); /*rastsquash signal right-hand blank*/ +int issmash = (smashmargin!=0?1:0), /* true to "squash" sp1||sp2 */ + isopaque = (issmash?0:1), /* not oppaque if smashing */ + rastsmash(), isblank=0, nsmash=0, /* #cols to smash */ + oldsmashmargin = smashmargin, /* save original smashmargin */ + oldblanksymspace = blanksymspace, /* save original blanksymspace */ + oldnocatspace = isnocatspace; /* save original isnocatspace */ mathchardef *symdef1 = sp1->symdef, /*mathchardef of last left-hand char*/ *symdef2 = sp2->symdef; /* mathchardef of right-hand char */ int class1 = (symdef1==NULL?ORDINARY:symdef1->class), /* symdef->class */ @@ -1204,6 +1392,8 @@ int class1 = (symdef1==NULL?ORDINARY:sym smash2 = (symdef2!=NULL)&&(class2==ORDINARY||class2==VARIABLE|| class2==OPENING||class2==CLOSING||class2==PUNCTION), space = fontsize/2+1; /* #cols between sp1 and sp2 */ +int isfrac = (type1 == FRACRASTER /* sp1 is a \frac */ + && class2 == PUNCTION); /* and sp2 is punctuation */ /* ------------------------------------------------------------------------- Initialization -------------------------------------------------------------------------- */ @@ -1211,36 +1401,50 @@ Initialization if ( !isstring ) space = max2(2,(symspace[class1][class2] + fontsize-3)); /* space */ else space = 1; /* space for ascii string */ -/* --- determine squash --- */ -if ( !isstring ) /* don't squash strings */ - if ( issquash ) { /* raster squash wanted */ - int maxsquash = rastsquash(sp1,sp2), /* calculate max squash space */ - margin = squashmargin; /* init margin without delta */ +if ( isnocatspace > 0 ) { /* spacing explicitly turned off */ + space = 0; /* reset space */ + isnocatspace--; } /* and decrement isnocatspace flag */ +if ( 0 && sp1->type == BLANKSIGNAL ) space=0; /*implicitly turn off spacing*/ +if ( sp1->type==BLANKSIGNAL && sp2->type==BLANKSIGNAL ) /* both blank */ + space = 0; /* no extra space between spaces */ +if ( sp2->type != BLANKSIGNAL ) /* not a blank space signal */ + if ( blanksymspace != 0 ) { /* and we have a space adjustment */ + space = max2(0,space+blanksymspace); /* adjust as much as possible */ + blanksymspace = 0; } /* and reset adjustment */ +if ( msgfp!=NULL && msglevel>=999 ) /* display space results */ + { fprintf(msgfp,"rastcat> space=%d, blanksymspace=%d, isnocatspace=%d\n", + space,oldblanksymspace,oldnocatspace); fflush(msgfp); } +/* --- determine smash --- */ +if ( !isstring && !isfrac ) /* don't smash strings or \frac's */ + if ( issmash ) { /* raster smash wanted */ + int maxsmash = rastsmash(sp1,sp2), /* calculate max smash space */ + margin = smashmargin; /* init margin without delta */ if ( (1 && smash1 && smash2) /* concatanating two chars */ - || (1 && type1!=IMAGERASTER && type2!=IMAGERASTER) ) - /*maxsquash = 0;*/ /* turn off squash */ - margin = max2(space-1,0); /* force small squashmargin */ + || (1 && type1!=IMAGERASTER && type2!=IMAGERASTER + && type1!=FRACRASTER && type2!=FRACRASTER ) ) + /*maxsmash = 0;*/ /* turn off smash */ + margin = max2(space-1,0); /* force small smashmargin */ else /* adjust for delta if images */ - if ( issquashdelta ) /* squashmargin is a delta value */ + if ( issmashdelta ) /* smashmargin is a delta value */ margin += fontsize; /* add displaystyle base to margin */ - if ( maxsquash == blanksignal ) /* sp2 is intentional blank */ + if ( maxsmash == blanksignal ) /* sp2 is intentional blank */ isblank = 1; /* set blank flag signal */ else /* see how much extra space we have*/ - if ( maxsquash > margin ) /* enough space for adjustment */ - nsquash = maxsquash-margin; /* make adjustment */ - if ( msgfp!=NULL && msglevel>=99 ) /* display squash results */ - { fprintf(msgfp,"rastcat> maxsquash=%d, margin=%d, nsquash=%d\n", - maxsquash,margin,nsquash); + if ( maxsmash > margin ) /* enough space for adjustment */ + nsmash = maxsmash-margin; /* make adjustment */ + if ( msgfp!=NULL && msglevel>=99 ) /* display smash results */ + { fprintf(msgfp,"rastcat> maxsmash=%d, margin=%d, nsmash=%d\n", + maxsmash,margin,nsmash); fprintf(msgfp,"rastcat> type1=%d,2=%d, class1=%d,2=%d\n", type1,type2, (symdef1==NULL?-999:class1),(symdef2==NULL?-999:class2)); fflush(msgfp); } - } /* --- end-of-if(issquash) --- */ + } /* --- end-of-if(issmash) --- */ /* --- determine height, width and baseline of composite raster --- */ if ( !isstring ) { height = max2(base1+1,base2+1) /* max height above baseline */ + max2(height1-base1-1,height2-base2-1); /*+ max descending below*/ - width = width1+width2 + space-nsquash; /*add widths and space-squash*/ - width = max3(width,width1,width2); } /* don't "over-squash" composite */ + width = width1+width2 + space-nsmash; /*add widths and space-smash*/ + width = max3(width,width1,width2); } /* don't "over-smash" composite */ else /* ascii string */ { height = 1; /* default */ width = width1 + width2 + space - 1; } /* no need for two nulls */ @@ -1254,8 +1458,8 @@ if ( msgfp!=NULL && msglevel>=9999 ) /* height2,width2,pixsz2,base2); type_raster(sp2->image,msgfp); /* display right-hand raster */ fprintf(msgfp, - "rastcat> Composite ht,width,squash,pixsz,base = %d,%d,%d,%d,%d\n", - height,width,nsquash,pixsz,base); + "rastcat> Composite ht,width,smash,pixsz,base = %d,%d,%d,%d,%d\n", + height,width,nsmash,pixsz,base); fflush(msgfp); } /* flush msgfp buffer */ /* ------------------------------------------------------------------------- allocate concatted composite subraster @@ -1273,7 +1477,10 @@ if ( (sp=new_subraster(width,height,pixs /* --- initialize subraster parameters --- */ /* sp->type = (!isstring?STRINGRASTER:ASCIISTRING); */ /*concatted string*/ if ( !isstring ) - sp->type = type2;/*(type1==type2?type2:IMAGERASTER);*/ /*string or image*/ + sp->type = /*type2;*//*(type1==type2?type2:IMAGERASTER);*/ + (type2!=CHARASTER? type2 : + (type1!=CHARASTER&&type1!=BLANKSIGNAL + &&type1!=FRACRASTER?type1:IMAGERASTER)); else sp->type = ASCIISTRING; /* concatted ascii string */ sp->symdef = symdef2; /* rightmost char is sp2 */ @@ -1291,15 +1498,22 @@ if ( msgfp!=NULL && msglevel>=9999 ) fflush(msgfp); } /* flush msgfp buffer */ if ( !isstring ) rastput (rp, sp1->image, base-base1, /* overlay left-hand */ - max2(0,nsquash-width1), 1); /* plus any residual squash space */ + max2(0,nsmash-width1), 1); /* plus any residual smash space */ else memcpy(rp->pixmap,(sp1->image)->pixmap,width1-1); /*init left string*/ if ( msgfp!=NULL && msglevel>=9999 ) { type_raster(sp->image,msgfp); /* display composite raster */ fflush(msgfp); } /* flush msgfp buffer */ if ( !isstring ) - rastput (rp, sp2->image, base-base2, /* overlay right-hand */ - max2(0,width1+space-nsquash), isopaque); /* minus any squashed space */ + { int fracbase = ( isfrac? /* baseline for punc after \frac */ + max2(fraccenterline,base2):base ); /*adjust baseline or use original*/ + rastput (rp, sp2->image, fracbase-base2, /* overlay right-hand */ + max2(0,width1+space-nsmash), isopaque); /* minus any smashed space */ + if ( 1 && type1 == FRACRASTER /* we're done with \frac image */ + && type2 != FRACRASTER ) /* unless we have \frac\frac */ + fraccenterline = NOVALUE; /* so reset centerline signal */ + if ( fraccenterline != NOVALUE ) /* sp2 is a fraction */ + fraccenterline += (base-base2); } /* so adjust its centerline */ else { strcpy((char *)(rp->pixmap)+width1-1+space,(char *)((sp2->image)->pixmap)); ((char *)(rp->pixmap))[width1+width2+space-2] = '\000'; } /*null-term*/ @@ -1316,7 +1530,7 @@ if ( isfree > 0 ) /* caller wants inpu Back to caller with pointer to concatted subraster or with null for error -------------------------------------------------------------------------- */ end_of_job: - squashmargin = oldsquashmargin; /* reset original squashmargin */ + smashmargin = oldsmashmargin; /* reset original smashmargin */ return ( sp ); /* back with subraster or null ptr */ } /* --- end-of-function rastcat() --- */ @@ -1487,26 +1701,26 @@ end_of_job: /* ========================================================================== - * Function: rastsquash ( sp1, sp2 ) + * Function: rastsmash ( sp1, sp2 ) * Purpose: When concatanating sp1||sp2, calculate #pixels - * we can "squash sp2 left" + * we can "smash sp2 left" * -------------------------------------------------------------------------- * Arguments: sp1 (I) subraster * to left-hand raster * sp2 (I) subraster * to right-hand raster * -------------------------------------------------------------------------- - * Returns: ( int ) max #pixels we can squash sp1||sp2, + * Returns: ( int ) max #pixels we can smash sp1||sp2, * or "blanksignal" if sp2 intentionally blank, * or 0 for any error. * -------------------------------------------------------------------------- * Notes: o * ======================================================================= */ /* --- entry point --- */ -int rastsquash ( subraster *sp1, subraster *sp2 ) +int rastsmash ( subraster *sp1, subraster *sp2 ) { /* ------------------------------------------------------------------------- Allocations and Declarations -------------------------------------------------------------------------- */ -int nsquash = 0; /* #pixels to squash sp1||sp2 */ +int nsmash = 0; /* #pixels to smash sp1||sp2 */ int base1 = sp1->baseline, /*baseline for left-hand subraster*/ height1 = (sp1->image)->height, /* height for left-hand subraster */ width1 = (sp1->image)->width, /* width for left-hand subraster */ @@ -1520,7 +1734,6 @@ int base = max2(base1,base2), /* max asc int irow1=0,irow2=0, icol=0; /* row,col indexes */ int firstcol1[1025], nfirst1=0, /* 1st sp1 col containing set pixel*/ firstcol2[1025], nfirst2=0; /* 1st sp2 col containing set pixel*/ -int blanksignal = (-991234); /*rastsquash signal right-hand blank*/ int smin=9999, xmin=9999,ymin=9999; /* min separation (s=x+y) */ int type_raster(); /* display debugging output */ /* ------------------------------------------------------------------------- @@ -1528,9 +1741,10 @@ find right edge of sp1 and left edge of -------------------------------------------------------------------------- */ /* --- check args --- */ if ( isstring ) goto end_of_job; /* ignore string rasters */ -if ( height > 1023 ) goto end_of_job; /* don't try to squash huge image */ +if ( 0 && istextmode ) goto end_of_job; /* don't smash in text mode */ +if ( height > 1023 ) goto end_of_job; /* don't try to smash huge image */ if ( sp2->type == blanksignal ) /*blanksignal was propagated to us*/ - goto end_of_job; /* don't squash intentional blank */ + goto end_of_job; /* don't smash intentional blank */ /* --- init firstcol1[], firstcol2[] --- */ for ( irow1=0; irow1type == blanksignal ) /*blanksignal was propagated to us*/ - goto end_of_job; /* don't squash intentional blank */ + goto end_of_job; /* don't smash intentional blank */ /* --- set firstcol1[] indicating right edge of sp1 --- */ for ( irow1=top1; irow1<=bot1; irow1++ ) /* for each row inside sp1 */ for ( icol=width1-1; icol>=0; icol-- ) /* find last non-empty col in row */ @@ -1555,42 +1769,124 @@ for ( irow1=top1; irow1<=bot1; irow1++ ) nfirst1++; /* bump #rows containing set pixels*/ break; } /* and go on to next row */ if ( nfirst1 < 1 ) /*left-hand sp1 is completely blank*/ - goto end_of_job; /* don't squash intentional blanks */ + goto end_of_job; /* don't smash intentional blanks */ /* ------------------------------------------------------------------------- find minimum separation -------------------------------------------------------------------------- */ for ( irow2=top2; irow2<=bot2; irow2++ ) { /* check each row inside sp2 */ int margin1, margin2=firstcol2[irow2]; /* #cols to first set pixel */ - if ( margin2 != blanksignal ) /* irow2 not an empty/blank row */ + if ( margin2 != blanksignal ) { /* irow2 not an empty/blank row */ for ( irow1=max2(irow2-smin,top1); ; irow1++ ) if ( irow1 > min2(irow2+smin,bot1) ) break; /* upper bound check */ else if ( (margin1=firstcol1[irow1]) != blanksignal ) { /*have non-blank row*/ int dx=(margin1+margin2), dy=absval(irow2-irow1), ds=dx+dy; /* deltas */ if ( ds >= smin ) continue; /* min unchanged */ - if ( dy>squashmargin && dxsmashmargin && dx= 99 ) /* display for debugging */ - { fprintf(msgfp,"rastsquash> nsquash=%d, squashmargin=%d\n", - nsquash,squashmargin); + { fprintf(msgfp,"rastsmash> nsmash=%d, smashmargin=%d\n", + nsmash,smashmargin); if ( msglevel >= 999 ) /* also display rasters */ - { fprintf(msgfp,"rastsquash>left-hand image...\n"); + { fprintf(msgfp,"rastsmash>left-hand image...\n"); if(sp1!=NULL) type_raster(sp1->image,msgfp); /* left image */ - fprintf(msgfp,"rastsquash>right-hand image...\n"); + fprintf(msgfp,"rastsmash>right-hand image...\n"); if(sp2!=NULL) type_raster(sp2->image,msgfp); } /* right image */ fflush(msgfp); } - return ( nsquash ); /* back with #squash pixels */ -} /* --- end-of-function rastsquash() --- */ + return ( nsmash ); /* back with #smash pixels */ +} /* --- end-of-function rastsmash() --- */ + + +/* ========================================================================== + * Function: rastsmashcheck ( term ) + * Purpose: Check an exponent term to see if its leading symbol + * would make smashing dangerous + * -------------------------------------------------------------------------- + * Arguments: term (I) char * to null-terminated string + * containing right-hand exponent term about to + * be smashed against existing left-hand. + * -------------------------------------------------------------------------- + * Returns: ( int ) 1 if it's okay to smash term, or + * 0 if smash is dangerous. + * -------------------------------------------------------------------------- + * Notes: o + * ======================================================================= */ +/* --- entry point --- */ +int rastsmashcheck ( char *term ) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +int isokay = 0; /* 1 to signal okay to caller */ +static char nosmashchars[64] = "-.,="; /* don't smash these leading chars */ +static char *nosmashstrs[64] = { "\\frac", NULL }; /* or leading strings */ +static char *grayspace[64] = { "\\tiny", "\\small", "\\normalsize", + "\\large", "\\Large", "\\LARGE", "\\huge", "\\Huge", NULL }; +char *expression = term; /* local ptr to beginning of expression */ +char *token = NULL; int i; /* token = nosmashstrs[i] or grayspace[i] */ +/* ------------------------------------------------------------------------- +see if smash check enabled +-------------------------------------------------------------------------- */ +if ( smashcheck < 1 ) { /* no smash checking wanted */ + if ( smashcheck >= 0 ) /* -1 means check should always fail */ + isokay = 1; /* otherwise (if 0), signal okay to smash */ + goto end_of_job; } /* return to caller */ +/* ------------------------------------------------------------------------- +skip leading white and gray space +-------------------------------------------------------------------------- */ +/* --- first check input --- */ +if ( term == NULL ) goto end_of_job; /* no input so return 0 to caller */ +if ( *term == '\000' ) goto end_of_job; /* ditto for empty string */ +/* --- skip leading white space --- */ +skipwhite(term); /* skip leading white sapce */ +if ( *term == '\000' ) goto end_of_job; /* nothing but white space */ +/* --- skip leading gray space --- */ +skipgray: + for ( i=0; (token=grayspace[i]) != NULL; i++ ) /* check each grayspace */ + if ( strncmp(term,token,strlen(token)) == 0 ) { /* found grayspace */ + term += strlen(token); /* skip past this grayspace token */ + if ( *term == '\000' ) { /* nothing left so quit */ + if ( msgfp!=NULL && msglevel >= 99 ) /* display for debugging */ + fprintf(msgfp,"rastsmashcheck> only grayspace in %.32s\n",expression); + goto end_of_job; } + goto skipgray; } /* restart grayspace check from beginning */ +/* ------------------------------------------------------------------------- +check for leading no-smash single char +-------------------------------------------------------------------------- */ +/* --- don't smash if term begins with a "nosmash" char --- */ +if ( (token=strchr(nosmashchars,*term)) != NULL ) { + if ( msgfp!=NULL && msglevel >= 99 ) /* display for debugging */ + fprintf(msgfp,"rastsmashcheck> char %.1s found in %.32s\n",token,term); + goto end_of_job; } +/* ------------------------------------------------------------------------- +check for leading no-smash token +-------------------------------------------------------------------------- */ +for ( i=0; (token=nosmashstrs[i]) != NULL; i++ ) /* check each nosmashstr */ + if ( strncmp(term,token,strlen(token)) == 0 ) { /* found a nosmashstr */ + if ( msgfp!=NULL && msglevel >= 99 ) /* display for debugging */ + fprintf(msgfp,"rastsmashcheck> token %s found in %.32s\n",token,term); + goto end_of_job; } /* so don't smash term */ +/* ------------------------------------------------------------------------- +back to caller +-------------------------------------------------------------------------- */ +isokay = 1; /* no problem, so signal okay to smash */ +end_of_job: + if ( msgfp!=NULL && msglevel >= 999 ) /* display for debugging */ + fprintf(msgfp,"rastsmashcheck> returning isokay=%d for \"%.32s\"\n", + isokay,(expression==NULL?"":expression)); + return ( isokay ); /* back to caller with 1 if okay to smash */ +} /* --- end-of-function rastsmashcheck() --- */ /* ========================================================================== @@ -1622,6 +1918,7 @@ raster *new_raster(), *rp=NULL; /*raster subraster *new_subraster(), *sp=NULL; /* subraster returning accent */ int delete_raster(), delete_subraster(); /*free allocated raster on err*/ int line_raster(), /* draws lines */ + rule_raster(), /* draw solid boxes */ thickness = 1; /* line thickness */ /*int pixval = (pixsz==1? 1 : (pixsz==8?255:(-1)));*/ /*black pixel value*/ /* --- other working info --- */ @@ -1653,28 +1950,38 @@ switch ( accent ) /* --- bar request --- */ case UNDERBARACCENT: case BARACCENT: - thickness = height-1; /* adjust thickness */ + thickness = 1; /*height-1;*/ /* adjust thickness */ if ( accent == BARACCENT ) /* bar is above expression */ - line_raster(rp,0,0,0,width-1,thickness); /*leave blank line at bot*/ + { row0 = row1 = max2(height-3,0); /* row numbers for overbar */ + line_raster(rp,row0,0,row1,width-1,thickness); } /*blanks at bot*/ else /* underbar is below expression */ - line_raster(rp,1,0,1,width-1,thickness); /*leave blank line at top*/ + { row0 = row1 = min2(2,height-1); /* row numbers for underbar */ + line_raster(rp,row0,0,row1,width-1,thickness); } /*blanks at top*/ break; /* --- dot request --- */ case DOTACCENT: thickness = height-1; /* adjust thickness */ - line_raster(rp,0,width/2,1,(width/2)+1,thickness); /* centered dot */ + /*line_raster(rp,0,width/2,1,(width/2)+1,thickness);*//*centered dot*/ + rule_raster(rp,0,(width+1-thickness)/2,thickness,thickness,3); /*box*/ break; /* --- ddot request --- */ case DDOTACCENT: thickness = height-1; /* adjust thickness */ - col0 = max2(width/3-(thickness-1),0); /* one-third of width */ - col1 = min2((2*width)/3+(thickness-1),width-thickness); /*two thirds*/ - line_raster(rp,0,col0,1,col0+1,thickness); /* set a dot at 1st third*/ - line_raster(rp,0,col1,1,col1+1,thickness); /* and another at 2nd */ + col0 = max2((width+1)/3-(thickness/2)-1,0); /* one-third of width */ + col1 = min2((2*width+1)/3-(thickness/2)+1,width-thickness); /*2/3rds*/ + if ( col0+thickness >= col1 ) /* dots overlap */ + { col0 = max2(col0-1,0); /* try moving left dot more left */ + col1 = min2(col1+1,width-thickness); } /* and right dot right */ + if ( col0+thickness >= col1 ) /* dots _still_ overlap */ + thickness = max2(thickness-1,1); /* so try reducing thickness */ + /*line_raster(rp,0,col0,1,col0+1,thickness);*//*set dot at 1st third*/ + /*line_raster(rp,0,col1,1,col1+1,thickness);*//*and another at 2nd*/ + rule_raster(rp,0,col0,thickness,thickness,3); /*box at 1st third*/ + rule_raster(rp,0,col1,thickness,thickness,3); /*box at 2nd third*/ break; /* --- hat request --- */ case HATACCENT: - thickness = (width<=12? 2 : 3); /* adjust thickness */ + thickness = 1; /*(width<=12? 2 : 3);*/ /* adjust thickness */ line_raster(rp,height-1,0,0,width/2,thickness); /* / part of hat*/ line_raster(rp,0,(width-1)/2,height-1,width-1,thickness); /* \ part*/ break; @@ -1724,14 +2031,15 @@ switch ( accent ) if ( (sp=rastack(new_subraster(1,1,pixsz),accsp,1,0,1,3))/*space below*/ != NULL ) /* have tilde with space below it */ { rp = sp->image; /* "extract" raster with bitmap */ - free((void *)sp); } /* and free subraster "envelope" */ + free((void *)sp); /* and free subraster "envelope" */ + leftsymdef = NULL; } /* so \tilde{x}^2 works properly */ break; } /* --- end-of-outer-switch(accent) --- */ /* ------------------------------------------------------------------------- if we constructed accent raster okay, embed it in a subraster and return it -------------------------------------------------------------------------- */ /* --- if all okay, allocate subraster to contain constructed raster --- */ -if ( rp != NULL ) /* accent raster constructed okay */ +if ( rp != NULL ) { /* accent raster constructed okay */ if ( (sp=new_subraster(0,0,0)) /* allocate subraster "envelope" */ == NULL ) /* and if we fail to allocate */ delete_raster(rp); /* free now-unneeded raster */ @@ -1741,6 +2049,7 @@ if ( rp != NULL ) /* accent raster con sp->image = rp; /* raster we just constructed */ sp->size = (-1); /* can't set font size here */ sp->baseline = 0; } /* can't set baseline here */ + } /* --- end-of-if(rp!=NULL) --- */ /* --- return subraster containing desired accent to caller --- */ return ( sp ); /* return accent or NULL to caller */ } /* --- end-of-function accent_subraster() --- */ @@ -1774,7 +2083,7 @@ Allocations and Declarations subraster *new_subraster(), *arrowsp=NULL; /* allocate arrow subraster */ int rule_raster(); /* draw arrow line */ int irow, midrow=height/2; /* index, midrow is arrowhead apex */ -int icol, thickness=(height>15?2:1); /* arrowhead thickness and index */ +int icol, thickness=(height>15?2:2); /* arrowhead thickness and index */ int pixval = (pixsz==1? 1 : (pixsz==8?255:(-1))); /* black pixel value */ int ipix, /* raster pixmap[] index */ npix = width*height; /* #pixels malloced in pixmap[] */ @@ -1800,22 +2109,22 @@ for ( irow=0; irow= 0 ) /* right arrowhead wanted */ for ( icol=0; icol= 0 ) /* bounds check */ + if ( ipix >= 0 ) { /* bounds check */ if ( pixsz == 1 ) /* have a bitmap */ setlongbit((arrowsp->image)->pixmap,ipix);/*turn on arrowhead bit*/ else /* should have a bytemap */ if ( pixsz == 8 ) /* check pixsz for bytemap */ - ((arrowsp->image)->pixmap)[ipix] = pixval; } /*set arrowhead byte*/ + ((arrowsp->image)->pixmap)[ipix] = pixval; } }/*set arrowhead byte*/ /* --- left arrowhead (same as right except for ipix calculation) --- */ if ( drctn <= 0 ) /* left arrowhead wanted */ for ( icol=0; icolimage)->pixmap,ipix);/*turn on arrowhead bit*/ else /* should have a bytemap */ if ( pixsz == 8 ) /* check pixsz for bytemap */ - ((arrowsp->image)->pixmap)[ipix] = pixval; } /*set arrowhead byte*/ + ((arrowsp->image)->pixmap)[ipix] = pixval; } }/*set arrowhead byte*/ } /* --- end-of-for(irow) --- */ end_of_job: return ( arrowsp ); /*back to caller with arrow or NULL*/ @@ -1850,7 +2159,7 @@ Allocations and Declarations subraster *new_subraster(), *arrowsp=NULL; /* allocate arrow subraster */ int rule_raster(); /* draw arrow line */ int icol, midcol=width/2; /* index, midcol is arrowhead apex */ -int irow, thickness=(width>15?2:1); /* arrowhead thickness and index */ +int irow, thickness=(width>15?2:2); /* arrowhead thickness and index */ int pixval = (pixsz==1? 1 : (pixsz==8?255:(-1))); /* black pixel value */ int ipix, /* raster pixmap[] index */ npix = width*height; /* #pixels malloced in pixmap[] */ @@ -1876,23 +2185,23 @@ for ( icol=0; icol= 0 ) /* up arrowhead wanted */ for ( irow=0; irowimage)->pixmap,ipix);/*turn on arrowhead bit*/ else /* should have a bytemap */ if ( pixsz == 8 ) /* check pixsz for bytemap */ - ((arrowsp->image)->pixmap)[ipix] = pixval; } /*set arrowhead byte*/ + ((arrowsp->image)->pixmap)[ipix] = pixval; } }/*set arrowhead byte*/ /* --- down arrowhead (same as up except for ipix calculation) --- */ if ( drctn <= 0 ) /* down arrowhead wanted */ for ( irow=0; irow 0 ) /* bounds check */ + if ( ipix > 0 ) { /* bounds check */ if ( pixsz == 1 ) /* have a bitmap */ setlongbit((arrowsp->image)->pixmap,ipix);/*turn on arrowhead bit*/ else /* should have a bytemap */ if ( pixsz == 8 ) /* check pixsz for bytemap */ - ((arrowsp->image)->pixmap)[ipix] = pixval; } /*set arrowhead byte*/ - } /* --- end-of-for(irow) --- */ + ((arrowsp->image)->pixmap)[ipix] = pixval; } }/*set arrowhead byte*/ + } /* --- end-of-for(icol) --- */ end_of_job: return ( arrowsp ); /*back to caller with arrow or NULL*/ } /* --- end-of-function uparrow_subraster() --- */ @@ -1913,6 +2222,8 @@ end_of_job: * height (I) int containing number of rows for rule * type (I) int containing 0 for solid rule, * 1 for horizontal dashes, 2 for vertical + * 3 for solid rule with corners removed (bevel) + * 4 for strut (nothing drawn) * -------------------------------------------------------------------------- * Returns: ( int ) 1 if rule drawn okay, * or 0 for any error. @@ -1928,42 +2239,53 @@ int rule_raster ( raster *rp, int top, i /* ------------------------------------------------------------------------- Allocations and Declarations -------------------------------------------------------------------------- */ -int irow, icol; /* indexes over rp raster */ -int ipix, /* raster pixmap[] index */ +int irow=0, icol=0; /* indexes over rp raster */ +int ipix = 0, /* raster pixmap[] index */ npix = rp->width * rp->height; /* #pixels malloced in rp->pixmap[] */ int isfatal = 0; /* true to abend on out-of-bounds error */ -int hdash=1, vdash=2; /* type for horizontal, vertical dashes */ +int hdash=1, vdash=2, /* type for horizontal, vertical dashes */ + bevel=99/*3*/, strut=4; /* type for bevel (turned off), strut */ int dashlen=3, spacelen=2, /* #pixels for dash followed by space */ isdraw=1; /* true when drawing dash (init for solid) */ /* ------------------------------------------------------------------------- Check args -------------------------------------------------------------------------- */ -if ( rp == (raster *)NULL ) /* no raster arg supplied */ +if ( rp == (raster *)NULL ) { /* no raster arg supplied */ if ( workingbox != (subraster *)NULL ) /* see if we have a workingbox */ rp = workingbox->image; /* use workingbox if possible */ - else return ( 0 ); /* otherwise signal error to caller */ + else return ( 0 ); } /* otherwise signal error to caller */ +if ( type == bevel ) /* remove corners of solid box */ + if ( width<3 || height<3 ) type=0; /* too small to remove corners */ /* ------------------------------------------------------------------------- Fill line/box -------------------------------------------------------------------------- */ -for ( irow=top; irow 0 ) /* zero width implies strut*/ + for ( irow=top; irowwidth + left - 1; /*first pixel preceding icol*/ for ( icol=left; icol=left+width-1) /* top-right corner */ + || (irow>=top+height-1 && icol==left) /* bottom-left corner */ + || (irow>=top+height-1 && icol>=left+width-1) ) /* bottom-right */ + isdraw = 0; else isdraw = 1; } /*set isdraw to skip corner*/ if ( type == hdash ) /*set isdraw for horiz dash*/ isdraw = (((icol-left)%(dashlen+spacelen)) < dashlen); if ( ++ipix >= npix ) /* bounds check failed */ if ( isfatal ) goto end_of_job; /* abort if error is fatal */ else break; /*or just go on to next row*/ else /*ibit is within rp bounds*/ - if ( isdraw ) /*and we're drawing this bit*/ + if ( isdraw ) { /*and we're drawing this bit*/ if ( rp->pixsz == 1 ) /* have a bitmap */ setlongbit(rp->pixmap,ipix); /* so turn on bit in line */ else /* should have a bytemap */ if ( rp->pixsz == 8 ) /* check pixsz for bytemap */ - ((unsigned char *)(rp->pixmap))[ipix] = 255; /* set black byte */ + ((unsigned char *)(rp->pixmap))[ipix] = 255; } /* set black byte */ } /* --- end-of-for(icol) --- */ } /* --- end-of-for(irow) --- */ end_of_job: @@ -2008,17 +2330,18 @@ int line_raster ( raster *rp, int row0, /* ------------------------------------------------------------------------- Allocations and Declarations -------------------------------------------------------------------------- */ -int irow, icol, /* indexes over rp raster */ +int irow=0, icol=0, /* indexes over rp raster */ locol=col0, hicol=col1, /* col limits at irow */ lorow=row0, hirow=row1; /* row limits at icol */ -int ipix, /* raster pixmap[] index */ - npix = rp->width * rp->height; /* #pixels malloced in rp->pixmap[] */ +int width=rp->width, height=rp->height; /* dimensions of input raster */ +int ipix = 0, /* raster pixmap[] index */ + npix = width*height; /* #pixels malloced in rp->pixmap[] */ int isfatal = 0; /* true to abend on out-of-bounds error */ int isline=(row1==row0), isbar=(col1==col0); /*true if slope a=0,\infty*/ double dy = row1-row0 /* + (row1>=row0? +1.0 : -1.0) */, /* delta-x */ dx = col1-col0 /* + (col1>=col0? +1.0 : -1.0) */, /* delta-y */ a= (isbar||isline? 0.0 : dy/dx), /* slope = tan(theta) = dy/dx */ - xcol, xrow; /* calculated col at irow, or row at icol */ + xcol=0, xrow=0; /* calculated col at irow, or row at icol */ double ar = ASPECTRATIO, /* aspect ratio width/height of one pixel */ xwidth= (isline? 0.0 : /*#pixels per row to get sloped line thcknss*/ ((double)thickness)*sqrt((dx*dx)+(dy*dy*ar*ar))/fabs(dy*ar)), @@ -2027,22 +2350,28 @@ int line_recurse(), isrecurse=1; /* true /* ------------------------------------------------------------------------- Check args -------------------------------------------------------------------------- */ -if ( rp == (raster *)NULL ) /* no raster arg supplied */ +if ( rp == (raster *)NULL ) { /* no raster arg supplied */ if ( workingbox != (subraster *)NULL ) /* see if we have a workingbox */ rp = workingbox->image; /* use workingbox if possible */ - else return ( 0 ); /* otherwise signal error to caller */ + else return ( 0 ); } /* otherwise signal error to caller */ /* ------------------------------------------------------------------------- Initialization -------------------------------------------------------------------------- */ -if ( msgfp!=NULL && msglevel>=29 ) /* debugging */ +if ( msgfp!=NULL && msglevel>=29 ) { /* debugging */ fprintf(msgfp,"line_raster> row,col0=%d,%d row,col1=%d,%d, thickness=%d\n" "\t dy,dx=%3.1f,%3.1f, a=%4.3f, xwidth=%4.3f\n", - row0,col0, row1,col1, thickness, dy,dx, a, xwidth); + row0,col0, row1,col1, thickness, dy,dx, a, xwidth); fflush(msgfp); } /* --- check for recursive line drawing --- */ -if ( isrecurse ) /* drawing lines recursively */ - { line_recurse(rp,(double)row0,(double)col0, - (double)row1,(double)col1,thickness); - return ( 1 ); } +if ( isrecurse ) { /* drawing lines recursively */ + for ( irow=0; irow(-0.001) && xcol0>(-0.001) /*check line inside raster*/ + && xrow1<((double)(height-1)+0.001) && xcol1<((double)(width-1)+0.001) ) + line_recurse(rp,xrow0,xcol0,xrow1,xcol1,thickness); } + return ( 1 ); } /* --- set params for horizontal line or vertical bar --- */ if ( isline ) /*interpret row as top row*/ row1 = row0 + (thickness-1); /* set bottom row for line */ @@ -2499,11 +2828,11 @@ Allocations and Declarations -------------------------------------------------------------------------- */ raster *new_raster(), *bp=(raster *)NULL; /*raster back to caller*/ int rastput(); /* overlay rp in new bordered raster */ -int width = (rp==NULL?0:rp->width), /* height of raster */ - height = (rp==NULL?0:rp->height), /* width of raster */ - istopneg=0, isbotneg=0, /* true if ntop or nbot negative */ +int width = (rp==NULL?0:rp->width), /* width of raster */ + height = (rp==NULL?0:rp->height), /* height of raster */ + istopneg=0, isbotneg=0, /* true if ntop or nbot negative */ leftmargin = 0; /* adjust width to whole number of bytes */ -int delete_raster(); /* to free input rp if isdelete is true */ +int delete_raster(); /* free input rp if isfree is true */ /* ------------------------------------------------------------------------- Initialization -------------------------------------------------------------------------- */ @@ -2563,6 +2892,85 @@ end_of_job: /* ========================================================================== + * Function: backspace_raster ( rp, nback, pback, minspace, isfree ) + * Purpose: Allocate a new raster containing a copy of input rp, + * but with trailing nback columns removed. + * If minspace>=0 then (at least) that many columns + * of whitespace will be left in place, regardless of nback. + * If minspace<0 then existing black pixels will be deleted + * as required. + * -------------------------------------------------------------------------- + * Arguments: rp (I) raster * to raster on which a border + * is to be placed + * nback (I) int containing number of columns to + * backspace (>=0) + * pback (O) ptr to int returning #pixels actually + * backspaced (or NULL to not use) + * minspace (I) int containing number of columns + * of whitespace to be left in place + * isfree (I) int containing true to free rp before return + * -------------------------------------------------------------------------- + * Returns: ( raster * ) ptr to backspaced raster, + * or NULL for any error. + * -------------------------------------------------------------------------- + * Notes: o For \! negative space, for \hspace{-10}, etc. + * ======================================================================= */ +/* --- entry point --- */ +raster *backspace_raster ( raster *rp, int nback, int *pback, int minspace, + int isfree ) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +raster *new_raster(), *bp=(raster *)NULL; /* raster returned to caller */ +int delete_raster(); /* free input rp if isfree is true */ +int width = (rp==NULL?0:rp->width), /* original width of raster */ + height = (rp==NULL?0:rp->height), /* height of raster */ + mback = nback, /* nback adjusted for minspace */ + newwidth = width, /* adjusted width after backspace */ + icol=0, irow=0; /* col,row index */ +if ( rp == NULL ) goto end_of_job; /* no input given */ +/* ------------------------------------------------------------------------- +locate rightmost column of rp containing ink, and determine backspaced width +-------------------------------------------------------------------------- */ +/* --- locate rightmost column of rp containing ink --- */ +if ( minspace >= 0 ) /* only needed if given minspace */ + for ( icol=width-1; icol>=0; icol-- ) /* find first non-empty col in row */ + for ( irow=0; irow width ) mback = width; /* can't backspace before beginning*/ + newwidth = max2(1,width-mback); /* #cols in backspaced raster */ + if ( pback != NULL ) *pback = width-newwidth; /* caller wants #pixels */ +/* ------------------------------------------------------------------------- +allocate new raster and fill it with leftmost cols of rp +-------------------------------------------------------------------------- */ +/* --- allocate backspaced raster --- */ +if ( (bp=new_raster(newwidth,height,rp->pixsz)) /*allocate backspaced raster*/ +== (raster *)NULL ) goto end_of_job; /* and quit if failed */ +/* --- fill new raster --- */ +if ( width-nback > 0 ) /* don't fill 1-pixel wide empty bp*/ + for ( icol=0; icol=999 ) { fprintf(msgfp, /* diagnostics */ + "backspace_raster> nback=%d,minspace=%d,mback=%d, width:old=%d,new=%d\n", + nback,minspace,mback,width,newwidth); fflush(msgfp); } + if ( isfree && bp!=NULL ) delete_raster(rp); /* free original raster */ + return ( bp ); /* back with backspaced or null ptr*/ +} /* --- end-of-function backspace_raster() --- */ + + +/* ========================================================================== * Function: type_raster ( rp, fp ) * Purpose: Emit an ascii dump representing rp, on fp. * -------------------------------------------------------------------------- @@ -2588,14 +2996,19 @@ static char display_chars[16] = /* displ char scanline[133]; /* ascii image for one scan line */ int scan_width; /* #chars in scan (<=display_width)*/ int irow, locol,hicol=(-1); /* height index, width indexes */ +raster *gftobitmap(), *bitmaprp=rp; /* convert .gf to bitmap if needed */ +int delete_raster(); /*free bitmap converted for display*/ /* -------------------------------------------------------------------------- initialization -------------------------------------------------------------------------- */ /* --- redirect null fp --- */ if ( fp == (FILE *)NULL ) fp = stdout; /* default fp to stdout if null */ +if ( msglevel >= 999 ) { fprintf(fp, /* debugging diagnostics */ + "type_raster> width=%d height=%d ...\n", + rp->width,rp->height); fflush(fp); } /* --- check for ascii string --- */ if ( isstring /* pixmap has string, not raster */ -|| (1 && rp->height==1) ) /* infer input rp is a string */ +|| (0 && rp->height==1) ) /* infer input rp is a string */ { char *string = (char *)(rp->pixmap); /*interpret pixmap as ascii string*/ int width = strlen(string); /* #chars in ascii string */ @@ -2609,7 +3022,11 @@ if ( isstring /* pixmap has string, n /* -------------------------------------------------------------------------- display ascii dump of bitmap image (in segments if display_width < rp->width) -------------------------------------------------------------------------- */ -while ( (locol=hicol+1) < rp->width ) /*start where prev segment left off*/ +if ( rp->format == 2 /* input is .gf-formatted */ +|| rp->format == 3 ) + bitmaprp = gftobitmap(rp); /* so convert it for display */ +if ( bitmaprp != NULL ) /* if we have image for display */ + while ( (locol=hicol+1) < rp->width ) /*start where prev segment left off*/ { /* --- set hicol for this pass (locol set above) --- */ hicol += display_width; /* show as much as display allows */ @@ -2626,11 +3043,11 @@ while ( (locol=hicol+1) < rp->width ) /* lopix = irow*rp->width + locol; /*first pixmap[] pixel in this scan*/ /* --- set chars in scanline[] based on pixels in rp->pixmap[] --- */ for ( ipix=0; ipixpixsz == 1 ) /*' '=0 or '*'=1 to display bitmap*/ - scanline[ipix] = (getlongbit(rp->pixmap,lopix+ipix)==1? '*':'.'); + if ( bitmaprp->pixsz == 1 ) /*' '=0 or '*'=1 to display bitmap*/ + scanline[ipix]=(getlongbit(bitmaprp->pixmap,lopix+ipix)==1? '*':'.'); else /* should have a bytemap */ - if ( rp->pixsz == 8 ) /* double-check pixsz for bytemap */ - { int pixval = (int)((rp->pixmap)[lopix+ipix]), /*pixel's byte value*/ + if ( bitmaprp->pixsz == 8 ) /* double-check pixsz for bytemap */ + { int pixval = (int)((bitmaprp->pixmap)[lopix+ipix]), /*byte value*/ ichar = min2(15,pixval/16); /* index for ' ', '1'...'e', '*' */ scanline[ipix] = display_chars[ichar]; } /*set ' ' for 0-15, etc*/ /* --- display completed scan line --- */ @@ -2640,6 +3057,10 @@ while ( (locol=hicol+1) < rp->width ) /* /* ------------------------------------------------------------------------- Back to caller with 1=okay, 0=failed. -------------------------------------------------------------------------- */ +if ( rp->format == 2 /* input was .gf-format */ +|| rp->format == 3 ) + if ( bitmaprp != NULL ) /* and we converted it for display */ + delete_raster(bitmaprp); /* no longer needed, so free it */ return ( 1 ); } /* --- end-of-function type_raster() --- */ @@ -2782,6 +3203,141 @@ return ( 1 ); /* ========================================================================== + * Function: type_pbmpgm ( rp, ptype, file ) + * Purpose: Write pbm or pgm image of rp to file + * -------------------------------------------------------------------------- + * Arguments: rp (I) ptr to raster struct for which + * a pbm/pgm file is to be written. + * ptype (I) int containing 1 for pbm, 2 for pgm, or + * 0 to determine ptype from values in rp + * file (I) ptr to null-terminated char string + * containing name of fuke to be written + * (see notes below). + * -------------------------------------------------------------------------- + * Returns: ( int ) total #bytes written, + * or 0 for any error. + * -------------------------------------------------------------------------- + * Notes: o (a) If file==NULL, output is written to stdout; + * (b) if *file=='\000' then file is taken as the + * address of an output buffer to which output + * is written (and is followed by a terminating + * '\0' which is not counted in #bytes returned); + * (c) otherwise file is the filename (opened and + * closed internally) to which output is written, + * except that any final .ext extension is replaced + * by .pbm or .pgm depending on ptype. + * ======================================================================= */ +/* --- entry point --- */ +int type_pbmpgm ( raster *rp, int ptype, char *file ) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +int isokay=0, nbytes=0; /* completion flag, total #bytes written */ +int irow=0, jcol=0; /*height(row), width(col) indexes in raster*/ +int pixmin=9999, pixmax=(-9999), /* min, max pixel value in raster */ + ngray = 0; /* #gray scale values */ +FILE /* *fopen(), */ *fp=NULL; /* pointer to output file (or NULL) */ +char outline[1024], outfield[256], /* output line, field */ + cr[16] = "\n\000"; /* cr at end-of-line */ +int maxlinelen = 70; /* maximum allowed line length */ +int pixfrac=6; /* use (pixmax-pixmin)/pixfrac as step */ +static char + *suffix[] = { NULL, ".pbm", ".pgm" }, /* file.suffix[ptype] */ + *magic[] = { NULL, "P1", "P2" }, /*identifying "magic number"*/ + *mode[] = { NULL, "w", "w" }; /* fopen() mode[ptype] */ +/* ------------------------------------------------------------------------- +check input, determine grayscale, and set up output file if necessary +-------------------------------------------------------------------------- */ +/* --- check input args --- */ +if ( rp == NULL ) goto end_of_job; /* no input raster provided */ +if ( ptype != 0 ) /* we'll determine ptype below */ + if ( ptype<1 || ptype>2 ) goto end_of_job; /*invalid output graphic format*/ +/* --- determine largest (and smallest) value in pixmap --- */ +for ( irow=0; irowheight; irow++ ) /* for each row, top-to-bottom */ + for ( jcol=0; jcolwidth; jcol++ ) /* for each col, left-to-right */ + { int pixval = getpixel(rp,irow,jcol); /* value of pixel at irow,jcol */ + pixmin = min2(pixmin,pixval); /* new minimum */ + pixmax = max2(pixmax,pixval); } /* new maximum */ +ngray = 1 + (pixmax-pixmin); /* should be 2 for b/w bitmap */ +if ( ptype == 0 ) /* caller wants us to set ptype */ + ptype = (ngray>=3?2:1); /* use grayscale if >2 shades */ +/* --- open output file if necessary --- */ +if ( file == NULL ) fp = stdout; /*null ptr signals output to stdout*/ +else if ( *file != '\000' ) { /* explicit filename provided, so...*/ + char fname[512], *pdot=NULL; /* file.ext, ptr to last . in fname*/ + strncpy(fname,file,255); /* local copy of file name */ + fname[255] = '\000'; /* make sure it's null terminated */ + if ( (pdot=strrchr(fname,'.')) == NULL ) /*no extension on original name*/ + strcat(fname,suffix[ptype]); /* so add extension */ + else /* we already have an extension */ + strcpy(pdot,suffix[ptype]); /* so replace original extension */ + if ( (fp = fopen(fname,mode[ptype])) /* open output file */ + == (FILE *)NULL ) goto end_of_job; /* quit if failed to open */ + } /* --- ens-of-if(*file!='\0') --- */ +/* ------------------------------------------------------------------------- +format and write header +-------------------------------------------------------------------------- */ +/* --- format header info --- */ +*outline = '\000'; /* initialize line buffer */ +strcat(outline,magic[ptype]); /* begin file with "magic number" */ +strcat(outline,cr); /* followed by cr to end line */ +sprintf(outfield,"%d %d",rp->width,rp->height); /* format width and height */ +strcat(outline,outfield); /* add width and height to header */ +strcat(outline,cr); /* followed by cr to end line */ +if ( ptype == 2 ) /* need max grayscale value */ + { sprintf(outfield,"%d",pixmax); /* format maximum pixel value */ + strcat(outline,outfield); /* add max value to header */ + strcat(outline,cr); } /* followed by cr to end line */ +/* --- write header to file or memory buffer --- */ +if ( fp == NULL ) /* if we have no open file... */ + strcat(file,outline); /* add header to caller's buffer */ +else /* or if we have an open file... */ + if ( fputs(outline,fp) /* try writing header to open file */ + == EOF ) goto end_of_job; /* return with error if failed */ +nbytes += strlen(outline); /* bump output byte count */ +/* ------------------------------------------------------------------------- +format and write pixels +-------------------------------------------------------------------------- */ +*outline = '\000'; /* initialize line buffer */ +for ( irow=0; irow<=rp->height; irow++ ) /* for each row, top-to-bottom */ + for ( jcol=0; jcolwidth; jcol++ ) { /* for each col, left-to-right */ + /* --- format value at irow,jcol--- */ + *outfield = '\000'; /* init empty field */ + if ( irow < rp->height ) { /* check row index */ + int pixval = getpixel(rp,irow,jcol); /* value of pixel at irow,jcol */ + if ( ptype == 1 ) /* pixval must be 1 or 0 */ + pixval = (pixval>pixmin+((pixmax-pixmin)/pixfrac)?1:0); + sprintf(outfield,"%d ",pixval); } /* format pixel value */ + /* --- write line if this value won't fit on it (or last line) --- */ + if ( strlen(outline)+strlen(outfield)+strlen(cr) >= maxlinelen /*won't fit*/ + || irow >= rp->height ) { /* force writing last line */ + strcat(outline,cr); /* add cr to end current line */ + if ( fp == NULL ) /* if we have no open file... */ + strcat(file,outline); /* add header to caller's buffer */ + else /* or if we have an open file... */ + if ( fputs(outline,fp) /* try writing header to open file */ + == EOF ) goto end_of_job; /* return with error if failed */ + nbytes += strlen(outline); /* bump output byte count */ + *outline = '\000'; /* re-initialize line buffer */ + } /* --- end-of-if(strlen>=maxlinelen) --- */ + if ( irow >= rp->height ) break; /* done after writing last line */ + /* --- concatanate value to line -- */ + strcat(outline,outfield); /* concatanate value to line */ + } /* --- end-of-for(jcol,irow) --- */ +isokay = 1; /* signal successful completion */ +/* ------------------------------------------------------------------------- +Back to caller with total #bytes written, or 0=failed. +-------------------------------------------------------------------------- */ +end_of_job: + if ( fp != NULL /* output written to an open file */ + && fp != stdout ) /* and it's not just stdout */ + fclose(fp); /* so close file before returning */ + return ( (isokay?nbytes:0) ); /*back to caller with #bytes written*/ +} /* --- end-of-function type_pbmpgm() --- */ + + +/* ========================================================================== * Function: cstruct_chardef ( cp, fp, col1 ) * Purpose: Emit a C struct of cp on fp, starting in col1. * -------------------------------------------------------------------------- @@ -2813,7 +3369,7 @@ emit charnum, location, name / hirow sprintf(field,"{ %3d,%5d,\n", cp->charnum,cp->location); /*char#,location*/ emit_string ( fp, col1, field, "character number, location"); /* --- toprow, topleftcol, botrow, botleftcol --- */ -sprintf(field," %3d,%2d, %3d,%2d,\n", /* format... */ +sprintf(field," %3d,%2d, %3d,%2d,\n", /* format... */ cp->toprow,cp->topleftcol, /* toprow, topleftcol, */ cp->botrow,cp->botleftcol); /* and botrow, botleftcol */ emit_string ( fp, col1, field, "topleft row,col, and botleft row,col"); @@ -2855,9 +3411,9 @@ int emit_string(); /* emit a string and /* ------------------------------------------------------------------------- emit width and height -------------------------------------------------------------------------- */ -sprintf(field,"{ %2d, %3d,%2d, %s\n", /* format width,height,pixsz */ - rp->width,rp->height,rp->pixsz,typecast); -emit_string ( fp, col1, field, "widthxheight, pixsz,map..."); +sprintf(field,"{ %2d, %3d,%2d,%2d, %s\n", /* format width,height,pixsz */ + rp->width,rp->height,rp->format,rp->pixsz,typecast); +emit_string ( fp, col1, field, "width,ht, fmt,pixsz,map..."); /* ------------------------------------------------------------------------- emit bitmap and closing brace, and return to caller -------------------------------------------------------------------------- */ @@ -2894,7 +3450,8 @@ int hex_bitmap ( raster *rp, FILE *fp, i /* ------------------------------------------------------------------------- Allocations and Declarations -------------------------------------------------------------------------- */ -int ibyte, nbytes=pixmapsz(rp); /* #bytes in raster */ +int ibyte, /* pixmap[ibyte] index */ + nbytes = pixbytes(rp); /*#bytes in bitmap or .gf-formatted*/ char stub[64]=" ";/* col1 leading blanks */ int linewidth = 64, /* (roughly) rightmost column */ colwidth = (isstr? 4:5); /* #cols required for each byte */ @@ -2921,11 +3478,11 @@ for ( ibyte=0; ibytewidth, gf->height, #bits */ +int format=0, icount=0, ncounts=0, /*.gf format, count index, #counts*/ + ibit=0, bitval=0; /* bitmap index, bit value */ +int isrepeat = 1, /* true to process repeat counts */ + repeatcmds[2] = {255,15}, /*opcode for repeat/duplicate count*/ + nrepeats=0, irepeat=0, /* scan line repeat count,index */ + wbits = 0; /* count bits to width of scan line*/ +/* ------------------------------------------------------------------------- +initialization +-------------------------------------------------------------------------- */ +/* --- check args --- */ +if ( gf == NULL ) goto end_of_job; /* input raster not provided */ +format = gf->format; /* 2 or 3 */ +if ( format!=2 && format!=3 ) goto end_of_job; /* invalid raster format */ +ncounts = gf->pixsz; /*pixsz is really #counts in pixmap*/ +/* --- allocate output raster with proper dimensions for bitmap --- */ +width=gf->width; height=gf->height; /* dimensions of raster */ +if ( (rp = new_raster(width,height,1)) /* allocate new raster and bitmap */ +== NULL ) goto end_of_job; /* quit if failed to allocate */ +totbits = width*height; /* total #bits in image */ +/* ------------------------------------------------------------------------- +fill bitmap +-------------------------------------------------------------------------- */ +for ( icount=0,bitval=0; icountpixmap,icount)); /*#bits to set*/ + if ( isrepeat /* we're proxessing repeat counts */ + && nbits == repeatcmds[format-2] ) { /* and repeat opcode found */ + if ( nrepeats == 0 ) /* recursive repeat is error */ + { nrepeats = (int)(getbyfmt(format,gf->pixmap,icount+1));/*repeat count*/ + nbits = (int)(getbyfmt(format,gf->pixmap,icount+2)); /*#bits to set*/ + icount += 2; } /* bump byte/nibble count */ + else /* some internal error occurred */ + if ( msgfp!=NULL && msglevel>=1 ) /* report error */ + fprintf(msgfp,"gftobitmap> found embedded repeat command\n"); } + if ( 0 ) + fprintf(stdout, + "gftobitmap> icount=%d bitval=%d nbits=%d ibit=%d totbits=%d\n", + icount,bitval,nbits,ibit,totbits); + for ( ; nbits>0; nbits-- ) /* count down */ + { if ( ibit >= totbits ) goto end_of_job; /* overflow check */ + for ( irepeat=0; irepeat<=nrepeats; irepeat++ ) + if ( bitval == 1 ) /* set pixel */ + { setlongbit(rp->pixmap,(ibit+irepeat*width)); } + else /* clear pixel */ + { unsetlongbit(rp->pixmap,(ibit+irepeat*width)); } + if ( nrepeats > 0 ) wbits++; /* count another repeated bit */ + ibit++; } /* bump bit index */ + bitval = 1-bitval; /* flip bit value */ + if ( wbits >= width ) { /* completed repeats */ + ibit += nrepeats*width; /*bump bit count past repeated scans*/ + if ( wbits > width ) /* out-of alignment error */ + if ( msgfp!=NULL && msglevel>=1 ) /* report error */ + fprintf(msgfp,"gftobitmap> width=%d wbits=%d\n",width,wbits); + wbits = nrepeats = 0; } /* reset repeat counts */ + } /* --- end-of-for(icount) --- */ +end_of_job: + return ( rp ); /* back to caller with image */ +} /* --- end-of-function gftobitmap() --- */ + + +/* ========================================================================== * Function: get_symdef ( symbol ) * Purpose: returns mathchardef struct for symbol * -------------------------------------------------------------------------- @@ -3024,30 +3660,46 @@ mathchardef *get_symdef ( char *symbol ) Allocations and Declarations -------------------------------------------------------------------------- */ mathchardef *symdefs = symtable; /* table of mathchardefs */ +int ligdef=0, get_ligature(); /* or we may have a ligature */ int idef = 0, /* symdefs[] index */ bestdef = (-9999); /*index of shortest matching symdef*/ int symlen = strlen(symbol), /* length of input symbol */ deflen, minlen=9999; /*length of shortest matching symdef*/ int /*alnumsym = (symlen==1 && isalnum(*symbol)),*/ /*alphanumeric sym*/ alphasym = (symlen==1 && isalpha(*symbol)); /* or alpha symbol */ +int family = fontinfo[fontnum].family; /* current font family */ static char *displaysyms[][2] = { /*xlate to Big sym for \displaystyle*/ + /* --- see table on page 536 in TLC2 --- */ {"\\int", "\\Bigint"}, {"\\oint", "\\Bigoint"}, {"\\sum", "\\Bigsum"}, {"\\prod", "\\Bigprod"}, {"\\coprod", "\\Bigcoprod"}, - {"\\cup", "\\Bigcup"}, - {"\\sqcup", "\\Bigsqcup"}, - {"\\cap", "\\Bigcap"}, - {"\\sqcap", "\\sqcap"}, /* don't have \Bigsqcap */ - {"\\odot", "\\Bigodot"}, - {"\\oplus", "\\Bigoplus"}, - {"\\otimes", "\\Bigotimes"}, - {"\\uplus", "\\Biguplus"}, - {"\\wedge", "\\Bigwedge"}, - {"\\vee", "\\Bigvee"}, + /* --- must be 'big' when related to similar binary operators --- */ + {"\\bigcup", "\\Bigcup"}, + {"\\bigsqcup", "\\Bigsqcup"}, + {"\\bigcap", "\\Bigcap"}, + /*{"\\bigsqcap", "\\sqcap"},*/ /* don't have \Bigsqcap */ + {"\\bigodot", "\\Bigodot"}, + {"\\bigoplus", "\\Bigoplus"}, + {"\\bigominus", "\\ominus"}, + {"\\bigotimes", "\\Bigotimes"}, + {"\\bigoslash", "\\oslash"}, + {"\\biguplus", "\\Biguplus"}, + {"\\bigwedge", "\\Bigwedge"}, + {"\\bigvee", "\\Bigvee"}, {NULL, NULL} }; /* ------------------------------------------------------------------------- +First check for ligature +-------------------------------------------------------------------------- */ +isligature = 0; /* init signal for no ligature */ +if ( family == CYR10 ) /*only check for cyrillic ligatures*/ + if ( (ligdef=get_ligature(subexprptr,family)) /* check for ligature */ + >= 0 ) /* found a ligature */ + { bestdef = ligdef; /* set bestdef for ligature */ + isligature = 1; /* signal we found a ligature */ + goto end_of_job; } /* so just give it to caller */ +/* ------------------------------------------------------------------------- If in \displaystyle mode, first xlate int to Bigint, etc. -------------------------------------------------------------------------- */ if ( isdisplaystyle > 1 ) /* we're in \displaystyle mode */ @@ -3070,39 +3722,104 @@ for ( idef=0; ;idef++ ) /* until trail if ( symdefs[idef].symbol == NULL ) break; /* reached end-of-table */ else /* check against caller's symbol */ if ( strncmp(symbol,symdefs[idef].symbol,symlen) == 0 ) /* found match */ - if ( symdefs[idef].handler != NULL /* mode irrelevant for directives */ - || istext==0 /* mathmode, so use first match */ - || (istext==1 && symdefs[idef].family==CMR10) /*textmode && rm text*/ - || (istext==2 && symdefs[idef].family==CMMI10) /*textmode && it text*/ - || (istext==3 && symdefs[idef].family==BBOLD10) /*textmode && bb text*/ - || (istext!=3 && !alphasym) ) /* not bb and not alpha */ + if ( (fontnum==0||family==CYR10) /* mathmode, so check every match */ + || (0 && istextmode && (!alphasym /* text mode and not alpha symbol */ + || symdefs[idef].handler!=NULL)) /* or text mode and directive */ + || (symdefs[idef].family==family /* have correct family */ + && symdefs[idef].handler==NULL) ) /* and not a handler collision */ +#if 0 + || (fontnum==1 && symdefs[idef].family==CMR10) /*textmode && rm text*/ + || (fontnum==2 && symdefs[idef].family==CMMI10) /*textmode && it text*/ + || (fontnum==3 && symdefs[idef].family==BBOLD10 /*textmode && bb text*/ + && symdefs[idef].handler==NULL) + || (fontnum==4 && symdefs[idef].family==CMMIB10 /*textmode && bf text*/ + && symdefs[idef].handler==NULL) ) +#endif if ( (deflen=strlen(symdefs[idef].symbol)) < minlen ) /*new best match*/ { bestdef = idef; /* save index of new best match */ if ( (minlen = deflen) /* and save its len for next test */ == symlen ) break; } /*perfect match, so return with it*/ if ( bestdef < 0 ) /* failed to look up symbol */ - if ( istext != 0 ) /* we're in a restricted font mode */ - { int wastext = istext; /* save current mode */ - mathchardef *symdef = NULL; /* lookup result with istext=0 */ - istext = 0; /*try to look up symbol in any font*/ - symdef = get_symdef(symbol); /* repeat lookup with istext=0 */ - istext = wastext; /* reset font mode */ - return symdef; } /* caller gets istext=0 lookup */ -if ( msgfp!=NULL && msglevel>=999 ) /* debugging output */ - { fprintf(msgfp,"get_symdefs> symbol=%s matches symtable[%d]=%s\n", - symbol,bestdef,(bestdef<0?"NotFound":symdefs[bestdef].symbol)); + if ( fontnum != 0 ) /* we're in a restricted font mode */ + { int oldfontnum = fontnum; /* save current font family */ + mathchardef *symdef = NULL; /* lookup result with fontnum=0 */ + fontnum = 0; /*try to look up symbol in any font*/ + symdef = get_symdef(symbol); /* repeat lookup with fontnum=0 */ + fontnum = oldfontnum; /* reset font family */ + return symdef; } /* caller gets fontnum=0 lookup */ +end_of_job: + if ( msgfp!=NULL && msglevel>=999 ) /* debugging output */ + { fprintf(msgfp, + "get_symdef> symbol=%s matches symtable[%d]=%s (isligature=%d)\n", + symbol,bestdef,(bestdef<0?"NotFound":symdefs[bestdef].symbol),isligature); fflush(msgfp); } -return ( (bestdef<0? NULL : &(symdefs[bestdef])) ); /*NULL or best symdef[]*/ + return ( (bestdef<0? NULL : &(symdefs[bestdef])) );/*NULL or best symdef[]*/ } /* --- end-of-function get_symdef() --- */ /* ========================================================================== + * Function: get_ligature ( expression, family ) + * Purpose: returns symtable[] index for ligature + * -------------------------------------------------------------------------- + * Arguments: expression (I) char * containing ligature + * whose corresponding mathchardef is wanted + * family (I) int containing NOVALUE for any family, + * or, e.g., CYR10 for cyrillic, etc. + * -------------------------------------------------------------------------- + * Returns: ( int ) symtable[] index defining ligature, + * or -9999 if no ligature found or for any error + * -------------------------------------------------------------------------- + * Notes: o + * ======================================================================= */ +/* --- entry point --- */ +int get_ligature ( char *expression, int family ) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +mathchardef *symdefs = symtable; /* table of mathchardefs */ +char *ligature = expression /*- 1*/, /* expression ptr */ + *symbol = NULL; /* symdefs[idef].symbol */ +int liglen = strlen(ligature); /* #chars remaining in expression */ +int iscyrfam = (family==CYR10); /* true for cyrillic families */ +int idef = 0, /* symdefs[] index */ + bestdef = (-9999), /*index of longest matching symdef*/ + maxlen=(-9999); /*length of longest matching symdef*/ +/* ------------------------------------------------------------------------- +search symdefs[] in order for first occurrence of symbol +-------------------------------------------------------------------------- */ +if ( !isstring ) { /* no ligatures in "string" mode */ + for ( idef=0; ;idef++ ) /* until trailer record found */ + if ( (symbol=symdefs[idef].symbol) == NULL ) break; /* end-of-table */ + else { /* check against caller's ligature */ + int symlen = strlen(symbol); /* #chars in symbol */ + if ( ( symlen>1 || iscyrfam ) /*ligature >1 char long or cyrillic*/ + && symlen <= liglen /* and enough remaining chars */ + && ( *symbol!='\\' || iscyrfam ) /* not escaped or cyrillic */ + && symdefs[idef].handler == NULL ) /* and not a handler */ + if ( strncmp(ligature,symbol,symlen) == 0 ) /* found match */ + if ( family < 0 /* no family specifies */ + || symdefs[idef].family == family ) /* or have correct family */ + if ( symlen > maxlen ) /* new longest ligature */ + { bestdef = idef; /* save index of new best match */ + maxlen = symlen; } /* and save its len for next test */ + } /* --- end-of-if/else(symbol==NULL) --- */ + if ( msgfp!=NULL && msglevel>=999 ) /* debugging output */ + { fprintf(msgfp,"get_ligature> ligature=%.4s matches symtable[%d]=%s\n", + ligature,bestdef,(bestdef<0?"NotFound":symdefs[bestdef].symbol)); + fflush(msgfp); } + } /* --- end-of-if(!isstring) --- */ +return ( bestdef ); /* -9999 or index of best symdef[] */ +} /* --- end-of-function get_ligature --- */ + + +/* ========================================================================== * Function: get_chardef ( symdef, size ) * Purpose: returns chardef ptr containing data for symdef at given size * -------------------------------------------------------------------------- * Arguments: symdef (I) mathchardef * corresponding to symbol * whose corresponding chardef is wanted - * size (I) int containing 0-4 for desired size + * size (I) int containing 0-5 for desired size * -------------------------------------------------------------------------- * Returns: ( chardef * ) pointer to struct defining symbol at size, * or NULL for any error @@ -3129,7 +3846,7 @@ char *symptr = NULL; /* look for 1st a initialization -------------------------------------------------------------------------- */ /* --- check symdef --- */ -if ( symdef == NULL ) return ( NULL ); /* get_symdef() probably failed */ +if ( symdef == NULL ) goto end_of_job; /* get_symdef() probably failed */ /* --- get local copy of indexes from symdef --- */ family = symdef->family; /* font family containing symbol */ charnum = symdef->charnum; /* char# of symbol within font */ @@ -3161,7 +3878,11 @@ find font family in table of fonts[] -------------------------------------------------------------------------- */ /* --- look up font family --- */ for ( ifont=0; ;ifont++ ) /* until trailer record found */ - if ( fonts[ifont].family < 0 ) return ( NULL ); /* error, no such family */ + if ( fonts[ifont].family < 0 ) { /* error, no such family */ + if ( msgfp!=NULL && msglevel>=99 ) { /* emit error */ + fprintf(msgfp,"get_chardef> failed to find font family %d\n", + family); fflush(msgfp); } + goto end_of_job; } /* quit if can't find font family*/ else if ( fonts[ifont].family == family ) break; /* found font family */ /* --- get local copy of table for this family by size --- */ fontdef = fonts[ifont].fontdef; /* font by size */ @@ -3173,7 +3894,11 @@ while ( 1 ) /* find size or closest a if ( fontdef[size] != NULL ) break; /* found available size */ else /* adjust size closer to normal */ if ( size == NORMALSIZE /* already normal so no more sizes,*/ - || sizeinc == 0 ) return ( NULL); /* or must be supersampling */ + || sizeinc == 0 ) { /* or must be supersampling */ + if ( msgfp!=NULL && msglevel>=99 ) { /* emit error */ + fprintf(msgfp,"get_chardef> failed to find font size %d\n", + size); fflush(msgfp); } + goto end_of_job; } /* quit if can't find desired size */ else /*bump size 1 closer to NORMALSIZE*/ size += sizeinc; /* see if adjusted size available */ /* --- ptr to chardef struct --- */ @@ -3188,7 +3913,14 @@ if ( family == CMEX10 ) /* cmex10 need /* ------------------------------------------------------------------------- return subraster containing chardef data for symbol in requested size -------------------------------------------------------------------------- */ -return ( gfdata ); /*ptr to chardef for symbol in size*/ +end_of_job: + if ( msgfp!=NULL && msglevel>=999 ) + { if (symdef == NULL) fprintf(msgfp,"get_chardef> input symdef==NULL\n"); + else + fprintf(msgfp,"get_chardef> requested symbol=\"%s\" size=%d %s\n", + symdef->symbol,size,(gfdata==NULL?"FAILED":"Succeeded")); + fflush(msgfp); } + return ( gfdata ); /*ptr to chardef for symbol in size*/ } /* --- end-of-function get_chardef() --- */ @@ -3197,9 +3929,9 @@ return ( gfdata ); /*ptr to chardef fo * Purpose: returns new subraster ptr containing * data for symdef at given size * -------------------------------------------------------------------------- - * Arguments: symdef (I) mathchardef * corresponding to symbol - * whose corresponding chardef is wanted - * size (I) int containing 0-4 for desired size + * Arguments: symdef (I) mathchardef * corresponding to symbol whose + * corresponding chardef subraster is wanted + * size (I) int containing 0-5 for desired size * -------------------------------------------------------------------------- * Returns: ( subraster * ) pointer to struct defining symbol at size, * or NULL for any error @@ -3215,6 +3947,8 @@ Allocations and Declarations chardef *get_chardef(), *gfdata=NULL; /* chardef struct for symdef,size */ int get_baseline(); /* baseline of gfdata */ subraster *new_subraster(), *sp=NULL; /* subraster containing gfdata */ +raster *bitmaprp=NULL, *gftobitmap(); /* convert .gf-format to bitmap */ +int delete_subraster(); /* in case gftobitmap() fails */ int aasupsamp(), /*antialias char with supersampling*/ grayscale=256; /* aasupersamp() parameters */ /* ------------------------------------------------------------------------- @@ -3225,11 +3959,23 @@ if ( (gfdata=get_chardef(symdef,size)) / if ( (sp=new_subraster(0,0,0)) /* allocate subraster "envelope" */ != NULL ) /* and check that we succeeded */ { - sp->type = CHARASTER; /* static character raster */ + raster *image = &(gfdata->image); /* ptr to chardef's bitmap or .gf */ + int format = image->format; /* 1=bitmap, else .gf */ sp->symdef = symdef; /* replace NULL with caller's arg */ sp->size = size; /*replace default with caller's size*/ sp->baseline = get_baseline(gfdata); /* get baseline of character */ - sp->image = &(gfdata->image); /* store ptr to its bitmap */ + if ( format == 1 ) /* already a bitmap */ + { sp->type = CHARASTER; /* static char raster */ + sp->image = image; } /* store ptr to its bitmap */ + else /* need to convert .gf-to-bitmap */ + if ( (bitmaprp = gftobitmap(image)) /* convert */ + != (raster *)NULL ) /* successful */ + { sp->type = IMAGERASTER; /* allocated raster will be freed */ + sp->image = bitmaprp; } /* store ptr to converted bitmap */ + else /* conversion failed */ + { delete_subraster(sp); /* free unneeded subraster */ + sp = (subraster *)NULL; /* signal error to caller */ + goto end_of_job; } /* quit */ if ( issupersampling ) /* antialias character right here */ { raster *aa = NULL; /* antialiased char raster */ @@ -3245,14 +3991,53 @@ if ( (gfdata=get_chardef(symdef,size)) / sp->type = IMAGERASTER; } /* character is an image raster */ } /* --- end-of-if(issupersampling) --- */ } /* --- end-of-if(sp!=NULL) --- */ -if ( msgfp!=NULL && msglevel>=999 ) - { fprintf(msgfp,"get_charsubraster> requested symbol=\"%s\" baseline=%d\n", - symdef->symbol, (sp==NULL?0:sp->baseline)); fflush(msgfp); } +end_of_job: + if ( msgfp!=NULL && msglevel>=999 ) + { fprintf(msgfp,"get_charsubraster> requested symbol=\"%s\" baseline=%d" + " %s %s\n", symdef->symbol, (sp==NULL?0:sp->baseline), + (sp==NULL?"FAILED":"Succeeded"), (gfdata==NULL?"(gfdata=NULL)":" ")); + fflush(msgfp); } return ( sp ); /* back to caller */ } /* --- end-of-function get_charsubraster() --- */ /* ========================================================================== + * Function: get_symsubraster ( symbol, size ) + * Purpose: returns new subraster ptr containing + * data for symbol at given size + * -------------------------------------------------------------------------- + * Arguments: symbol (I) char * corresponding to symbol + * whose corresponding subraster is wanted + * size (I) int containing 0-5 for desired size + * -------------------------------------------------------------------------- + * Returns: ( subraster * ) pointer to struct defining symbol at size, + * or NULL for any error + * -------------------------------------------------------------------------- + * Notes: o just combines get_symdef() and get_charsubraster() + * ======================================================================= */ +/* --- entry point --- */ +subraster *get_symsubraster ( char *symbol, int size ) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +subraster *sp=NULL, *get_charsubraster(); /* subraster containing gfdata */ +mathchardef *symdef=NULL, *get_symdef(); /* mathchardef lookup for symbol */ +/* ------------------------------------------------------------------------- +look up mathchardef for symbol +-------------------------------------------------------------------------- */ +if ( symbol != NULL ) /* user supplied input symbol */ + symdef = get_symdef(symbol); /*look up corresponding mathchardef*/ +/* ------------------------------------------------------------------------- +look up chardef for mathchardef and wrap a subraster structure around data +-------------------------------------------------------------------------- */ +if ( symdef != NULL ) /* lookup succeeded */ + sp = get_charsubraster(symdef,size); /* so get symbol data in subraster */ +return ( sp ); /* back to caller with sp or NULL */ +} /* --- end-of-function get_symsubraster() --- */ + + +/* ========================================================================== * Function: get_baseline ( gfdata ) * Purpose: returns baseline for a chardef struct * -------------------------------------------------------------------------- @@ -3331,6 +4116,7 @@ int defheight, bestheight=9999, /* heigh int iswidth = 0; /* true if best-fit width desired */ int isunesc = 0, /* true if leading escape removed */ issq=0, isoint=0; /* true for \sqcup,etc, \oint,etc */ +int iscurly = 0; /* true for StMary's curly symbols */ char *bigint="bigint", *bigoint="bigoint"; /* substitutes for int, oint */ /* ------------------------------------------------------------------------- determine if searching height or width, and search symdefs[] for best-fit @@ -3338,6 +4124,7 @@ determine if searching height or width, /* --- arg checks --- */ if ( symlen < 1 ) return (sp); /* no input symbol suplied */ if ( strcmp(symbol,"e") == 0 ) return(sp); /* e causes segfault??? */ +if ( strstr(symbol,"curly") != NULL ) iscurly=1; /* user wants curly delim */ /* --- ignore leading escapes for CMEX10 --- */ if ( 1 ) /* ignore leading escape */ if ( (family==CMEX10 || family==CMSYEX) ) { /* for CMEX10 or CMSYEX */ @@ -3367,7 +4154,7 @@ for ( idef=0; ;idef++ ) /* until trail if ( defsym == NULL ) break; /* reached end-of-table */ else /* check against caller's symbol */ if ( family<0 || deffam == family /* if explicitly in caller's family*/ - || (family==CMSYEX && (deffam==CMSY10||deffam==CMEX10)) ) + || (family==CMSYEX && (deffam==CMSY10||deffam==CMEX10||deffam==STMARY10)) ) { strcpy(lcsymbol,defsym); /* local copy of symdefs[] symbol */ if ( isunesc && *lcsymbol=='\\' ) /* ignored leading \ in symbol */ @@ -3379,11 +4166,12 @@ for ( idef=0; ;idef++ ) /* until trail if ((symptr=strstr(lcsymbol,unescsymbol)) != NULL) /*found caller's sym*/ if ( (isoint || strstr(lcsymbol,"oint")==NULL) /* skip unwanted "oint"*/ && (issq || strstr(lcsymbol,"sq")==NULL) ) /* skip unwanted "sq" */ - if ( (deffam == CMSY10 ? /* CMSY10 or not CMSY10 */ + if ( ( deffam == CMSY10 ? /* CMSY10 or not CMSY10 */ symptr == lcsymbol /* caller's sym is a prefix */ && deflen == symlen: /* and same length */ - symptr == lcsymbol /* caller's sym is a prefix */ - || symptr == lcsymbol+deflen-symlen) ) /* or a suffix */ + (iscurly || strstr(lcsymbol,"curly")==NULL) &&/*not unwanted curly*/ + (symptr == lcsymbol /* caller's sym is a prefix */ + || symptr == lcsymbol+deflen-symlen) ) ) /* or a suffix */ for ( size=0; size<=LARGESTSIZE; size++ ) /* check all font sizes */ if ( (gfdata=get_chardef(&(symdefs[idef]),size)) != NULL ) /*got one*/ { defheight = gfdata->image.height; /* height of this character */ @@ -3404,10 +4192,14 @@ construct subraster for best fit charact -------------------------------------------------------------------------- */ if ( bestdef >= 0 ) /* found a best fit for caller */ sp = get_charsubraster(&(symdefs[bestdef]),bestsize); /* best subraster */ -if ( sp==NULL && height-bigheight>5 ) /* try to construct delim */ +if ( (sp==NULL && height-bigheight>5) /* try to construct delim */ +|| bigdef < 0 ) /* delim not in font tables */ sp = make_delim(symbol,(iswidth?-height:height)); /* try to build delim */ if ( sp==NULL && bigdef>=0 ) /* just give biggest to caller */ sp = get_charsubraster(&(symdefs[bigdef]),bigsize); /* biggest subraster */ +if ( msgfp!=NULL && msglevel>=99 ) + fprintf(msgfp,"get_delim> symbol=%.50s, height=%d family=%d isokay=%s\n", + (symbol==NULL?"null":symbol),height,family,(sp==NULL?"fail":"success")); return ( sp ); } /* --- end-of-function get_delim() --- */ @@ -3436,18 +4228,30 @@ Allocations and Declarations -------------------------------------------------------------------------- */ subraster *sp = (subraster *)NULL, /* subraster returned to caller */ *new_subraster(); /* allocate subraster */ +subraster *get_symsubraster(), /* look up delim pieces in cmex10 */ + *symtop=NULL, *symbot=NULL, *symmid=NULL, *symbar=NULL, /* pieces */ + *topsym=NULL, *botsym=NULL, *midsym=NULL, *barsym=NULL, /* +filler */ + *rastack(), *rastcat(); /* stack pieces, concat filler */ +int isdrawparen = 0; /*1=draw paren, 0=build from pieces*/ raster *rasp = (raster *)NULL; /* sp->image */ int isokay=0, delete_subraster(); /* set true if delimiter drawn ok */ -int pixsz = 1; /* pixels are one bit each */ +int pixsz = 1, /* pixels are one bit each */ + symsize = 0; /* size arg for get_symsubraster() */ int thickness = 1; /* drawn lines are one pixel thick */ int aspectratio = 8; /* default height/width for parens */ int iswidth = 0, /*true if width specified by height*/ width = height; /* #pixels width (e.g., of ellipse)*/ -char *lp=NULL, *rp=NULL, *strchr(), /* check symbol for left or right */ - *lp2=NULL, *rp2=NULL; /* synonym for lp,rp */ +char *lp=NULL, *rp=NULL, /* check symbol for left or right */ + *lp2=NULL, *rp2=NULL, /* synonym for lp,rp */ + *lp3=NULL, *rp3=NULL, /* synonym for lp,rp */ + *lp4=NULL, *rp4=NULL; /* synonym for lp,rp */ int circle_raster(), /* ellipse for ()'s in sp->image */ rule_rsater(), /* horizontal or vertical lines */ line_raster(); /* line between two points */ +subraster *uparrow_subraster(); /* up/down arrows */ +int isprealloc = 1; /*pre-alloc subraster, except arrow*/ +int oldsmashmargin = smashmargin, /* save original smashmargin */ + wasnocatspace = isnocatspace; /* save original isnocatspace */ /* ------------------------------------------------------------------------- initialization -------------------------------------------------------------------------- */ @@ -3460,51 +4264,196 @@ if ( height < 3 ) goto end_of_job; /* to if ( iswidth ) height = (width+(aspectratio+1)/2)/aspectratio; else width = (height+(aspectratio+1)/2)/aspectratio; if ( strchr(symbol,'=') != NULL /* left or right || bracket wanted */ -|| strstr(symbol,"\\|") != NULL ) /* same || in standard tex notation*/ - width = max2(width,5); /* need space between two |'s */ +|| strstr(symbol,"\\|") != NULL /* same || in standard tex notation*/ +|| strstr(symbol,"dbl") != NULL ) /* semantic bracket with ||'s */ + width = max2(width,6); /* need space between two |'s */ if ( width < 2 ) width=2; /* set min width */ if ( strchr(symbol,'(') != NULL /* if left ( */ || strchr(symbol,')') != NULL ) /* or right ) paren wanted */ - width = (3*width)/2; /* adjust width */ + { width = (3*width)/2; /* adjust width */ + if ( !isdrawparen ) isprealloc=0; } /* don't prealloc if building */ +if ( strchr(symbol,'/') != NULL /* left / */ +|| strstr(symbol,"\\\\") != NULL /* or \\ for right \ */ +|| strstr(symbol,"backsl") != NULL ) /* or \backslash for \ */ + width = max2(height/3,5); +if ( strstr(symbol,"arrow") != NULL ) /* arrow wanted */ + { width = min2(height/3,20); /* adjust width */ + isprealloc = 0; } /* don't preallocate subraster */ +if ( strchr(symbol,'{') != NULL /* if left { */ +|| strchr(symbol,'}') != NULL ) /* or right } brace wanted */ + { isprealloc = 0; } /* don't preallocate */ /* --- allocate and initialize subraster for constructed delimiter --- */ -if ( (sp=new_subraster(width,height,pixsz)) /* allocate new subraster */ -== NULL ) goto end_of_job; /* quit if failed */ -/* --- initialize delimiter subraster parameters --- */ -sp->type = IMAGERASTER; /* image */ -sp->symdef = NULL; /* not applicable for image */ -sp->baseline = height/2 + 2; /* is a little above center good? */ -sp->size = NORMALSIZE; /* size (probably unneeded) */ -rasp = sp->image; /* pointer to image in subraster */ +if ( isprealloc ) /* pre-allocation wanted */ + { if ( (sp=new_subraster(width,height,pixsz)) /* allocate new subraster */ + == NULL ) goto end_of_job; /* quit if failed */ + /* --- initialize delimiter subraster parameters --- */ + sp->type = IMAGERASTER; /* image */ + sp->symdef = NULL; /* not applicable for image */ + sp->baseline = height/2 + 2; /* is a little above center good? */ + sp->size = NORMALSIZE; /* size (probably unneeded) */ + rasp = sp->image; } /* pointer to image in subraster */ /* ------------------------------------------------------------------------- ( ) parens -------------------------------------------------------------------------- */ if ( (lp=strchr(symbol,'(')) != NULL /* left ( paren wanted */ || (rp=strchr(symbol,')')) != NULL ) /* right ) paren wanted */ { - int mywidth = min2(width,20); /* max width for ()'s */ - circle_raster ( rasp, /* embedded raster image */ + if ( isdrawparen ) { /* draw the paren */ + int mywidth = min2(width,20); /* max width for ()'s */ + circle_raster ( rasp, /* embedded raster image */ 0, 0, /* row0,col0 are upper-left corner */ height-1, mywidth-1, /* row1,col1 are lower-right */ thickness, /* line thickness is 1 pixel */ (rp==NULL?"23":"41") ); /* "1234" quadrants to be drawn */ - isokay = 1; /* set flag */ + isokay = 1; } /* set flag */ + else { + int isleft = (lp!=NULL?1:0); /* true for left, false for right */ + char *parentop = (isleft?"\\leftparentop":"\\rightparentop"), + *parenbot = (isleft?"\\leftparenbot":"\\rightparenbot"), + *parenbar = (isleft?"\\leftparenbar":"\\rightparenbar"); + int baseht=0, barht=0, /* height of base=top+bot, bar */ + ibar=0, nbars=0; /* bar index, #bars between top&bot*/ + int largestsize = min2(2,LARGESTSIZE), /* largest size for parens */ + topfill=(isleft?0:0), botfill=(isleft?0:0), + barfill=(isleft?0:7); /* alignment fillers */ + /* --- get pieces at largest size smaller than total height --- */ + for ( symsize=largestsize; symsize>=0; symsize-- ) /*largest to smallest*/ + { + /* --- get pieces at current test size --- */ + isokay = 1; /* check for all pieces */ + if ( (symtop=get_symsubraster(parentop,symsize)) == NULL ) isokay=0; + if ( (symbot=get_symsubraster(parenbot,symsize)) == NULL ) isokay=0; + if ( (symbar=get_symsubraster(parenbar,symsize)) == NULL ) isokay=0; + /* --- check sum of pieces against total desired height --- */ + if ( isokay ) { /* all pieces retrieved */ + baseht = (symtop->image)->height + (symbot->image)->height; /*top+bot*/ + barht = (symbar->image)->height; /* bar height */ + if ( baseht < height+5 ) break; /* largest base that's not too big */ + if ( symsize < 1 ) break; /* or smallest available base */ + } /* --- end-of-if(isokay) --- */ + /* --- free test pieces that were too big --- */ + if ( symtop != NULL ) delete_subraster(symtop); /* free top */ + if ( symbot != NULL ) delete_subraster(symbot); /* free bot */ + if ( symbar != NULL ) delete_subraster(symbar); /* free bar */ + isokay = 0; /* nothing available */ + if ( symsize < 1 ) break; /* leave isokay=0 after smallest */ + } /* --- end-of-for(symsize) --- */ + /* --- construct brace from pieces --- */ + if ( isokay ) { /* we have the pieces */ + /* --- add alignment fillers --- */ + smashmargin=0; isnocatspace=99; /*turn off rastcat smashing,space*/ + topsym = (topfill>0?rastcat(new_subraster(topfill,1,1),symtop,3):symtop); + botsym = (botfill>0?rastcat(new_subraster(botfill,1,1),symbot,3):symbot); + barsym = (barfill>0?rastcat(new_subraster(barfill,1,1),symbar,3):symbar); + smashmargin = oldsmashmargin; /* reset smashmargin */ + isnocatspace = wasnocatspace; /* reset isnocatspace */ + /* --- #bars needed between top and bot --- */ + nbars = (barht<1?0:max2(0,1+(height-baseht)/barht)); /* #bars needed */ + /* --- stack pieces --- */ + sp = topsym; /* start with top piece */ + if ( nbars > 0 ) /* need nbars between top and bot */ + for ( ibar=1; ibar<=nbars; ibar++ ) sp = rastack(barsym,sp,1,0,0,2); + sp = rastack(botsym,sp,1,0,0,3); /* bottom below bars or middle */ + delete_subraster(barsym); /* barsym no longer needed */ + } /* --- end-of-if(isokay) --- */ + } /* --- end-of-if/else(isdrawparen) --- */ } /* --- end-of-if(left- or right-() paren wanted) --- */ /* ------------------------------------------------------------------------- +{ } braces +-------------------------------------------------------------------------- */ +else + if ( (lp=strchr(symbol,'{')) != NULL /* left { brace wanted */ + || (rp=strchr(symbol,'}')) != NULL ) /* right } brace wanted */ + { + int isleft = (lp!=NULL?1:0); /* true for left, false for right */ + char *bracetop = (isleft?"\\leftbracetop":"\\rightbracetop"), + *bracebot = (isleft?"\\leftbracebot":"\\rightbracebot"), + *bracemid = (isleft?"\\leftbracemid":"\\rightbracemid"), + *bracebar = (isleft?"\\leftbracebar":"\\rightbracebar"); + int baseht=0, barht=0, /* height of base=top+bot+mid, bar */ + ibar=0, nbars=0; /* bar index, #bars above,below mid*/ + int largestsize = min2(2,LARGESTSIZE), /* largest size for braces */ + topfill=(isleft?4:0), botfill=(isleft?4:0), + midfill=(isleft?0:4), barfill=(isleft?4:4); /* alignment fillers */ + /* --- get pieces at largest size smaller than total height --- */ + for ( symsize=largestsize; symsize>=0; symsize-- ) /*largest to smallest*/ + { + /* --- get pieces at current test size --- */ + isokay = 1; /* check for all pieces */ + if ( (symtop=get_symsubraster(bracetop,symsize)) == NULL ) isokay=0; + if ( (symbot=get_symsubraster(bracebot,symsize)) == NULL ) isokay=0; + if ( (symmid=get_symsubraster(bracemid,symsize)) == NULL ) isokay=0; + if ( (symbar=get_symsubraster(bracebar,symsize)) == NULL ) isokay=0; + /* --- check sum of pieces against total desired height --- */ + if ( isokay ) { /* all pieces retrieved */ + baseht = (symtop->image)->height + (symbot->image)->height + + (symmid->image)->height; /* top+bot+mid height */ + barht = (symbar->image)->height; /* bar height */ + if ( baseht < height+5 ) break; /* largest base that's not too big */ + if ( symsize < 1 ) break; /* or smallest available base */ + } /* --- end-of-if(isokay) --- */ + /* --- free test pieces that were too big --- */ + if ( symtop != NULL ) delete_subraster(symtop); /* free top */ + if ( symbot != NULL ) delete_subraster(symbot); /* free bot */ + if ( symmid != NULL ) delete_subraster(symmid); /* free mid */ + if ( symbar != NULL ) delete_subraster(symbar); /* free bar */ + isokay = 0; /* nothing available */ + if ( symsize < 1 ) break; /* leave isokay=0 after smallest */ + } /* --- end-of-for(symsize) --- */ + /* --- construct brace from pieces --- */ + if ( isokay ) { /* we have the pieces */ + /* --- add alignment fillers --- */ + smashmargin=0; isnocatspace=99; /*turn off rastcat smashing,space*/ + topsym = (topfill>0?rastcat(new_subraster(topfill,1,1),symtop,3):symtop); + botsym = (botfill>0?rastcat(new_subraster(botfill,1,1),symbot,3):symbot); + midsym = (midfill>0?rastcat(new_subraster(midfill,1,1),symmid,3):symmid); + barsym = (barfill>0?rastcat(new_subraster(barfill,1,1),symbar,3):symbar); + smashmargin = oldsmashmargin; /* reset smashmargin */ + isnocatspace = wasnocatspace; /* reset isnocatspace */ + /* --- #bars needed on each side of mid piece --- */ + nbars = (barht<1?0:max2(0,1+(height-baseht)/barht/2)); /*#bars per side*/ + /* --- stack pieces --- */ + sp = topsym; /* start with top piece */ + if ( nbars > 0 ) /* need nbars above middle */ + for ( ibar=1; ibar<=nbars; ibar++ ) sp = rastack(barsym,sp,1,0,0,2); + sp = rastack(midsym,sp,1,0,0,3); /*mid after top or bars*/ + if ( nbars > 0 ) /* need nbars below middle */ + for ( ibar=1; ibar<=nbars; ibar++ ) sp = rastack(barsym,sp,1,0,0,2); + sp = rastack(botsym,sp,1,0,0,3); /* bottom below bars or middle */ + delete_subraster(barsym); /* barsym no longer needed */ + } /* --- end-of-if(isokay) --- */ + } /* --- end-of-if(left- or right-{} brace wanted) --- */ +/* ------------------------------------------------------------------------- [ ] brackets -------------------------------------------------------------------------- */ else if ( (lp=strchr(symbol,'[')) != NULL /* left [ bracket wanted */ - || (rp=strchr(symbol,']')) != NULL ) /* right ] bracket wanted */ - { - /* --- rule_raster ( rasp, top, left, width, height, type=0 ) --- */ - int mywidth = min2(width,12); /* max width for horizontal bars */ - thickness = 1; /* set lines 1 or 2 pixels thick */ - rule_raster(rasp, 0,0, mywidth,thickness, 0); /* top horizontal bar */ - rule_raster(rasp, height-thickness,0, mywidth,thickness, 0); /* bottom */ - if ( lp != NULL ) /* left [ bracket wanted */ + || (rp=strchr(symbol,']')) != NULL /* right ] bracket wanted */ + || (lp2=strstr(symbol,"lceil")) != NULL /* left ceiling wanted */ + || (rp2=strstr(symbol,"rceil")) != NULL /* right ceiling wanted */ + || (lp3=strstr(symbol,"lfloor")) != NULL /* left floor wanted */ + || (rp3=strstr(symbol,"rfloor")) != NULL /* right floor wanted */ + || (lp4=strstr(symbol,"llbrack")) != NULL /* left semantic bracket */ + || (rp4=strstr(symbol,"rrbrack")) != NULL ) /* right semantic bracket */ + { + /* --- use rule_raster ( rasp, top, left, width, height, type=0 ) --- */ + int mywidth = min2(width,12), /* max width for horizontal bars */ + wthick = 1; /* thickness of top.bottom bars */ + thickness = (height<25?1:2); /* set lines 1 or 2 pixels thick */ + if ( lp2!=NULL || rp2!=NULL || lp3!=NULL || rp3 !=NULL ) /*ceil or floor*/ + wthick = thickness; /* same thickness for top/bot bar */ + if ( lp3==NULL && rp3==NULL ) /* set top bar if floor not wanted */ + rule_raster(rasp, 0,0, mywidth,wthick, 0); /* top horizontal bar */ + if ( lp2==NULL && rp2==NULL ) /* set bot bar if ceil not wanted */ + rule_raster(rasp, height-wthick,0, mywidth,thickness, 0); /* bottom */ + if ( lp!=NULL || lp2!=NULL || lp3!=NULL || lp4!=NULL ) /* left bracket */ rule_raster(rasp, 0,0, thickness,height, 0); /* left vertical bar */ - if ( rp != NULL ) /* right ] bracket wanted */ + if ( lp4 != NULL ) /* 2nd left vertical bar needed */ + rule_raster(rasp, 0,thickness+1, 1,height, 0); /* 2nd left vertical bar */ + if ( rp!=NULL || rp2!=NULL || rp3!=NULL || rp4!=NULL ) /* right bracket */ rule_raster(rasp, 0,mywidth-thickness, thickness,height, 0); /* right */ + if ( rp4 != NULL ) /* 2nd right vertical bar needed */ + rule_raster(rasp, 0,mywidth-thickness-2, 1,height, 0); /*2nd right vert*/ isokay = 1; /* set flag */ } /* --- end-of-if(left- or right-[] bracket wanted) --- */ /* ------------------------------------------------------------------------- @@ -3514,18 +4463,69 @@ else if ( (lp=strchr(symbol,'<')) != NULL /* left < bracket wanted */ || (rp=strchr(symbol,'>')) != NULL ) /* right > bracket wanted */ { - /* --- line_raster ( rasp, row0, col0, row1, col1, thickness ) --- */ - int mywidth = min2(width,12); /* max width for brackets */ - thickness = 1; /* set line pixel thickness */ + /* --- use line_raster( rasp, row0, col0, row1, col1, thickness ) --- */ + int mywidth = min2(width,12), /* max width for brackets */ + mythick = 1; /* all lines one pixel thick */ + thickness = (height<25?1:2); /* set line pixel thickness */ if ( lp != NULL ) /* left < bracket wanted */ - { line_raster(rasp,height/2,0,0,mywidth-1,thickness); - line_raster(rasp,height/2,0,height-1,mywidth-1,thickness); } + { line_raster(rasp,height/2,0,0,mywidth-1,mythick); + if ( thickness>1 ) + line_raster(rasp,height/2,1,0,mywidth-1,mythick); + line_raster(rasp,height/2,0,height-1,mywidth-1,mythick); + if ( thickness>1 ) + line_raster(rasp,height/2,1,height-1,mywidth-1,mythick); } if ( rp != NULL ) /* right > bracket wanted */ - { line_raster(rasp,height/2,mywidth-1,0,0,thickness); - line_raster(rasp,height/2,mywidth-1,height-1,0,thickness); } + { line_raster(rasp,height/2,mywidth-1,0,0,mythick); + if ( thickness>1 ) + line_raster(rasp,height/2,mywidth-2,0,0,mythick); + line_raster(rasp,height/2,mywidth-1,height-1,0,mythick); + if ( thickness>1 ) + line_raster(rasp,height/2,mywidth-2,height-1,0,mythick); } isokay = 1; /* set flag */ } /* --- end-of-if(left- or right-<> bracket wanted) --- */ /* ------------------------------------------------------------------------- +/ \ delimiters +-------------------------------------------------------------------------- */ +else + if ( (lp=strchr(symbol,'/')) != NULL /* left / wanted */ + || (rp=strstr(symbol,"\\\\")) != NULL /* right \ wanted */ + || (rp2=strstr(symbol,"backsl")) != NULL ) /* right \ wanted */ + { + /* --- use line_raster( rasp, row0, col0, row1, col1, thickness ) --- */ + int mywidth = width; /* max width for / \ */ + thickness = 1; /* set line pixel thickness */ + if ( lp != NULL ) /* left / wanted */ + line_raster(rasp,0,mywidth-1,height-1,0,thickness); + if ( rp!=NULL || rp2!=NULL ) /* right \ wanted */ + line_raster(rasp,0,0,height-1,mywidth-1,thickness); + isokay = 1; /* set flag */ + } /* --- end-of-if(left- or right-/\ delimiter wanted) --- */ +/* ------------------------------------------------------------------------- +arrow delimiters +-------------------------------------------------------------------------- */ +else + if ( strstr(symbol,"arrow") != NULL ) /* arrow delimiter wanted */ + { + /* --- use uparrow_subraster(width,height,pixsz,drctn,isBig) --- */ + int mywidth = width; /* max width for / \ */ + int isBig = (strstr(symbol,"Up")!=NULL /* isBig if we have an Up */ + || strstr(symbol,"Down")!=NULL); /* or a Down */ + int drctn = +1; /* init for uparrow */ + if ( strstr(symbol,"down")!=NULL /* down if we have down */ + || strstr(symbol,"Down")!=NULL ) /* or Down */ + { drctn = (-1); /* reset direction to down */ + if ( strstr(symbol,"up")!=NULL /* updown if we have up or Up */ + || strstr(symbol,"Up")!=NULL ) /* and down or Down */ + drctn = 0; } /* reset direction to updown */ + sp = uparrow_subraster(mywidth,height,pixsz,drctn,isBig); + if ( sp != NULL ) + { sp->type = IMAGERASTER; /* image */ + sp->symdef = NULL; /* not applicable for image */ + sp->baseline = height/2 + 2; /* is a little above center good? */ + sp->size = NORMALSIZE; /* size (probably unneeded) */ + isokay = 1; } /* set flag */ + } /* --- end-of-if(arrow delimiter wanted) --- */ +/* ------------------------------------------------------------------------- \- for | | brackets or \= for || || brackets -------------------------------------------------------------------------- */ else @@ -3538,13 +4538,13 @@ else int midcol = width/2; /* middle col, left of mid if even */ if ( rp != NULL /* left or right || bracket wanted */ || rp2 != NULL ) /* or || in standard tex notation */ - { thickness = 1; /* each | of || one pixel thick */ + { thickness = (height<75?1:2); /* each | of || 1 or 2 pixels thick*/ rule_raster(rasp, 0,max2(0,midcol-2), thickness,height, 0); /* left */ rule_raster(rasp, 0,min2(width,midcol+2), thickness,height, 0); } else /*nb, lp2 spuriously set if rp2 set*/ if ( lp != NULL /* left or right | bracket wanted */ || lp2 != NULL ) /* ditto for synomym */ - { thickness = 1; /* set | two pixels thick */ + { thickness = (height<75?1:2); /* set | 1 or 2 pixels thick */ rule_raster(rasp, 0,midcol, thickness,height, 0); } /*mid vertical bar*/ isokay = 1; /* set flag */ } /* --- end-of-if(left- or right-[] bracket wanted) --- */ @@ -3552,8 +4552,11 @@ else back to caller -------------------------------------------------------------------------- */ end_of_job: + if ( msgfp!=NULL && msglevel>=99 ) + fprintf(msgfp,"make_delim> symbol=%.50s, isokay=%d\n", + (symbol==NULL?"null":symbol),isokay); if ( !isokay ) /* don't have requested delimiter */ - { delete_subraster(sp); /* so free unneeded structure */ + { if (sp!=NULL) delete_subraster(sp); /* so free unneeded structure */ sp = NULL; } /* and signal error to caller */ return ( sp ); /*back to caller with delim or NULL*/ } /* --- end-of-function make_delim() --- */ @@ -3598,6 +4601,8 @@ static char *prefixes[] = /*e.g., \big "\\big", "\\Big", "\\bigg", "\\Bigg", "\\bigl", "\\Bigl", "\\biggl", "\\Biggl", "\\bigr", "\\Bigr", "\\biggr", "\\Biggr", NULL }; +static char *starred[] = /* may be followed by * */ + { "\\hspace", "\\!", NULL }; /* ------------------------------------------------------------------------- just return the next char if it's not \ -------------------------------------------------------------------------- */ @@ -3637,13 +4642,19 @@ for ( iprefix=0; prefixes[iprefix] != NU /* --- every \ must be followed by at least one char, e.g., \[ --- */ if ( esclen < 1 ) /* \ followed by non-alpha */ *ptoken++ = *expression++; /*copy non-alpha, bump ptrs*/ -else { /* normal alpha \sequence */ - /* --- respect spaces in text mode, except first space after \escape --- */ - if ( istext > 0 ) /* in \rm or \it text mode */ - if ( istext != 3 ) /* but not \mathbb */ - if ( isthischar(*expression,WHITEDELIM) ) /* delim follows \sequence */ - expression++; } /* so flush delim */ *ptoken = '\000'; /* null-terminate token */ +/* --- check for \hspace* or other starred commands --- */ +for ( iprefix=0; starred[iprefix] != NULL; iprefix++ ) /* run thru list */ + if ( strcmp(chartoken,starred[iprefix]) == 0 ) /* have an exact match */ + if ( *expression == '*' ) /* follows by a * */ + { *ptoken++ = *expression++; /* copy * and bump ptr */ + *ptoken = '\000'; /* null-terminate token */ + break; } /* stop checking */ +/* --- respect spaces in text mode, except first space after \escape --- */ +if ( esclen >= 1 ) { /*only for alpha \sequences*/ + if ( istextmode ) /* in \rm or \it text mode */ + if ( isthischar(*expression,WHITEDELIM) ) /* delim follows \sequence */ + expression++; } /* so flush delim */ /* --- back to caller --- */ end_of_job: if ( msgfp!=NULL && msglevel>=999 ) @@ -3708,16 +4719,17 @@ char *texsubexpr ( char *expression, cha Allocations and Declarations -------------------------------------------------------------------------- */ char *texchar(); /*next char (or \sequence) from expression*/ -char *leftptr, leftdelim[32] = "(\000", /* left( found in expression */ - rightdelim[32] = ")\000"; /* and matching right) */ +char *leftptr, leftdelim[256] = "(\000", /* left( found in expression */ + rightdelim[256] = ")\000"; /* and matching right) */ char *origexpression=expression, *origsubexpr=subexpr; /*original inputs*/ +char *strtexchr(), *texleft(); /* check for \left, and get it */ int gotescape = 0, /* true if leading char of expression is \ */ prevescape = 0; /* while parsing, true if preceding char \ */ int isbrace(); /* check for left,right braces */ int isanyright = 1; /* true matches any right with left, (...] */ int isleftdot = 0; /* true if left brace is a \. */ int nestlevel = 1; /* current # of nested braces */ -int subsz=0 /*, maxsubsz=8192*/; /* #chars in returned subexpr[] buffer*/ +int subsz=0 /*,maxsubsz=MAXSUBXSZ*/; /*#chars in returned subexpr buffer*/ /* ------------------------------------------------------------------------- skip leading whitespace and just return the next char if it's not { -------------------------------------------------------------------------- */ @@ -3727,18 +4739,28 @@ if ( expression == NULL ) return(NULL); skipwhite(expression); /* leading whitespace gone */ if ( *expression == '\000' ) return(NULL); /* nothing left to scan */ /* --- set maxsubsz --- */ -if ( maxsubsz < 1 ) maxsubsz = 8192; /* input 0 means unlimited */ +if ( maxsubsz < 1 ) maxsubsz = MAXSUBXSZ-2; /* input 0 means unlimited */ /* --- check for escape --- */ if ( isthischar(*expression,ESCAPE) ) /* expression is escaped */ gotescape = 1; /* so set flag accordingly */ +/* --- check for \left...\right --- */ +if ( gotescape ) /* begins with \ */ + if ( memcmp(expression+1,"left",4) ) /* and followed by left */ + if ( strchr(left,'l') != NULL ) /* caller wants \left's */ + if ( strtexchr(expression,"\\left") == expression ) /*expression=\left...*/ + { char *pright = texleft(expression,subexpr,maxsubsz, /* find ...\right*/ + (isdelim?NULL:leftdelim),rightdelim); + if ( isdelim ) strcat(subexpr,rightdelim); /* caller wants delims */ + return ( pright ); /*back to caller past \right*/ + } /* --- end-of-if(expression=="\\left") --- */ /* --- if first char isn't left{ or script, just return it to caller --- */ -if ( !isbrace(expression,left,isescape) ) /* not a left{ */ +if ( !isbrace(expression,left,isescape) ) { /* not a left{ */ if ( !isthischar(*expression,SCRIPTS) ) /* and not a script */ return ( texchar(expression,subexpr) ); /* next char to caller */ else /* --- kludge for super/subscripts to accommodate texscripts() --- */ { *subexpr++ = *expression; /* signal script */ *subexpr = '\000'; /* null-terminate subexpr */ - return ( expression ); } /* leave script in stream */ + return ( expression ); } } /* leave script in stream */ /* --- extract left and find matching right delimiter --- */ *leftdelim = *(expression+gotescape); /* the left( in expression */ if ( (gotescape && *leftdelim == '.') /* we have a left \. */ @@ -3756,9 +4778,9 @@ accumulate chars between balanced {}'s, /* --- first initialize by bumping past left{ or \{ --- */ if ( isdelim ) *subexpr++ = *expression++; /*caller wants { in subexpr*/ else expression++; /* always bump past left{ */ -if ( gotescape ) /*need to bump another char*/ +if ( gotescape ) { /*need to bump another char*/ if ( isdelim ) *subexpr++ = *expression++; /* caller wants char, too */ - else expression++; /* else just bump past it */ + else expression++; } /* else just bump past it */ /* --- set maximum size for numerical arguments --- */ if ( 0 ) /* check turned on or off? */ if ( !isescape && !isdelim ) /*looking for numerical arg*/ @@ -3771,12 +4793,12 @@ while ( 1 ) /*until balanced right} { if ( 0 && (!isescape && !isdelim) ) /*looking for numerical arg,*/ { expression = origexpression; /* so end-of-string is error*/ subexpr = origsubexpr; } /* so reset all ptrs */ - if ( isdelim ) /* generate fake right */ + if ( isdelim ) { /* generate fake right */ if ( gotescape ) /* need escaped right */ { *subexpr++ = '\\'; /* set escape char */ *subexpr++ = '.'; } /* and fake \right. */ else /* escape not wanted */ - *subexpr++ = *rightdelim; /* so fake actual right */ + *subexpr++ = *rightdelim; } /* so fake actual right */ *subexpr = '\000'; /* null-terminate subexpr */ return ( expression ); } /* back with final token */ /* --- check preceding char for escape --- */ @@ -3811,6 +4833,112 @@ while ( 1 ) /*until balanced right} /* ========================================================================== + * Function: texleft (expression,subexpr,maxsubsz,ldelim,rdelim) + * Purpose: scans expression, starting after opening \left, + * and returning ptr after matching closing \right. + * Everything between is returned in subexpr, if given. + * Likewise, if given, ldelim returns delimiter after \left + * and rdelim returns delimiter after \right. + * If ldelim is given, the returned subexpr doesn't include it. + * If rdelim is given, the returned pointer is after that delim. + * -------------------------------------------------------------------------- + * Arguments: expression (I) char * to first char of null-terminated + * string immediately following opening \left + * subexpr (O) char * to null-terminated string returning + * either everything between balanced + * \left ... \right. If leftdelim given, + * subexpr does _not_ contain that delimiter. + * maxsubsz (I) int containing max #bytes returned + * in subexpr buffer (0 means unlimited) + * ldelim (O) char * returning delimiter following + * opening \left + * rdelim (O) char * returning delimiter following + * closing \right + * -------------------------------------------------------------------------- + * Returns: ( char * ) ptr to the first char of expression + * past closing \right, or past closing + * right delimiter if rdelim!=NULL, + * or NULL for any error. + * -------------------------------------------------------------------------- + * Notes: o + * ======================================================================= */ +/* --- entry point --- */ +char *texleft ( char *expression, char *subexpr, int maxsubsz, + char *ldelim, char *rdelim ) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +char *texchar(), /* get delims after \left,\right */ + *strtexchr(), *pright=expression; /* locate matching \right */ +static char left[16]="\\left", right[16]="\\right"; /* tex delimiters */ +int sublen = 0; /* #chars between \left...\right */ +/* ------------------------------------------------------------------------- +initialization +-------------------------------------------------------------------------- */ +/* --- init output --- */ +if ( subexpr != NULL ) *subexpr = '\000'; /* init subexpr, if given */ +if ( ldelim != NULL ) *ldelim = '\000'; /* init ldelim, if given */ +if ( rdelim != NULL ) *rdelim = '\000'; /* init rdelim, if given */ +/* --- check args --- */ +if ( expression == NULL ) goto end_of_job; /* no input supplied */ +if ( *expression == '\000' ) goto end_of_job; /* nothing after \left */ +/* --- determine left delimiter --- */ +if ( ldelim != NULL ) /* caller wants left delim */ + { skipwhite(expression); /* interpret \left ( as \left( */ + expression = texchar(expression,ldelim); } /*delim from expression*/ +/* ------------------------------------------------------------------------- +locate \right balancing opening \left +-------------------------------------------------------------------------- */ +/* --- first \right following \left --- */ +if ( (pright=strtexchr(expression,right)) /* look for \right after \left */ +!= NULL ) { /* found it */ + /* --- find matching \right by pushing past any nested \left's --- */ + char *pleft = expression; /* start after first \left( */ + while ( 1 ) { /*break when matching \right found*/ + /* -- locate next nested \left if there is one --- */ + if ( (pleft=strtexchr(pleft,left)) /* find next \left */ + == NULL ) break; /*no more, so matching \right found*/ + pleft += strlen(left); /* push ptr past \left token */ + if ( pleft >= pright ) break; /* not nested if \left after \right*/ + /* --- have nested \left, so push forward to next \right --- */ + if ( (pright=strtexchr(pright+strlen(right),right)) /* find next \right */ + == NULL ) break; /* ran out of \right's */ + } /* --- end-of-while(1) --- */ + } /* --- end-of-if(pright!=NULL) --- */ +/* --- set subexpression length, push pright past \right --- */ +if ( pright != (char *)NULL ) /* found matching \right */ + { sublen = (int)(pright-expression); /* #chars between \left...\right */ + pright += strlen(right); } /* so push pright past \right */ +/* ------------------------------------------------------------------------- +get rightdelim and subexpr between \left...\right +-------------------------------------------------------------------------- */ +/* --- get delimiter following \right --- */ +if ( rdelim != NULL ) { /* caller wants right delim */ + if ( pright == (char *)NULL ) /* assume \right. at end of exprssn*/ + { strcpy(rdelim,"."); /* set default \right. */ + sublen = strlen(expression); /* use entire remaining expression */ + pright = expression + sublen; } /* and push pright to end-of-string*/ + else /* have explicit matching \right */ + { skipwhite(pright); /* interpret \right ) as \right) */ + pright = texchar(pright,rdelim); /* pull delim from expression */ + if ( *rdelim == '\000' ) strcpy(rdelim,"."); } } /* or set \right. */ +/* --- get subexpression between \left...\right --- */ +if ( sublen > 0 ) /* have subexpr */ + if ( subexpr != NULL ) { /* and caller wants it */ + if ( maxsubsz > 0 ) sublen = min2(sublen,maxsubsz-1); /* max buffer size */ + memcpy(subexpr,expression,sublen); /* stuff between \left...\right */ + subexpr[sublen] = '\000'; } /* null-terminate subexpr */ +end_of_job: + if ( msglevel>=99 && msgfp!=NULL ) + { fprintf(msgfp,"texleft> ldelim=%s, rdelim=%s, subexpr=%.128s\n", + (ldelim==NULL?"none":ldelim),(rdelim==NULL?"none":rdelim), + (subexpr==NULL?"none":subexpr)); fflush(msgfp); } + return ( pright ); +} /* --- end-of-function texleft --- */ + + +/* ========================================================================== * Function: texscripts ( expression, subscript, superscript, which ) * Purpose: scans expression, returning subscript and/or superscript * if expression is of the form _x^y or ^{x}_{y}, @@ -3846,16 +4974,16 @@ Allocations and Declarations char *texsubexpr(); /* next subexpression from expression */ int gotsub=0, gotsup=0; /* check that we don't eat, e.g., x_1_2 */ /* ------------------------------------------------------------------------- -init "scripts" and skip leading whitespace +init "scripts" -------------------------------------------------------------------------- */ -/* --- skip leading whitespace and check for end-of-string --- */ -*subscript = *superscript = '\000'; /* init in case no scripts */ -skipwhite(expression); /* leading whitespace gone */ -if ( *expression == '\000' ) return(expression); /* nothing left to scan */ +if ( subscript != NULL ) *subscript = '\000'; /*init in case no subscript*/ +if ( superscript!=NULL ) *superscript = '\000'; /*init in case no super*/ /* ------------------------------------------------------------------------- get subscript and/or superscript from expression -------------------------------------------------------------------------- */ -while ( expression != NULL ) +while ( expression != NULL ) { + skipwhite(expression); /* leading whitespace gone */ + if ( *expression == '\000' ) return(expression); /* nothing left to scan */ if ( isthischar(*expression,SUBSCRIPT) /* found _ */ && (which==1 || which>2 ) ) /* and caller wants it */ { if ( gotsub /* found 2nd subscript */ @@ -3871,6 +4999,7 @@ while ( expression != NULL ) expression = texsubexpr(expression+1,superscript,0,"{","}",0,0); } else /* neither _ nor ^ */ return ( expression ); /*return ptr past "scripts"*/ + } /* --- end-of-while(expression!=NULL) --- */ return ( expression ); } /* --- end-of-function texscripts() --- */ @@ -3913,8 +5042,9 @@ int gotescape = 0, /* true if leading c /* ------------------------------------------------------------------------- check for brace -------------------------------------------------------------------------- */ -/* --- first check for end-of-string --- */ -if ( *expression == '\000' ) return(0); /* nothing to check */ +/* --- first check for end-of-string or \= ligature --- */ +if ( *expression == '\000' /* nothing to check */ +|| isligature ) goto end_of_job; /* have a \= ligature */ /* --- check leading char for escape --- */ if ( isthischar(*expression,ESCAPE) ) /* expression is escaped */ { gotescape = 1; /* so set flag accordingly */ @@ -3930,7 +5060,11 @@ if ( gotbrace && isthischar(*expression, /* ------------------------------------------------------------------------- back to caller -------------------------------------------------------------------------- */ -if ( gotbrace && /* found a brace */ +end_of_job: + if ( msglevel>=999 && msgfp!=NULL ) + { fprintf(msgfp,"isbrace> expression=%.8s, gotbrace=%d (isligature=%d)\n", + expression,gotbrace,isligature); fflush(msgfp); } + if ( gotbrace && /* found a brace */ ( isescape==2 || /* escape irrelevant */ gotescape==isescape ) /* un/escaped as requested */ ) return ( 1 ); return ( 0 ); /* return 1,0 accordingly */ @@ -3985,7 +5119,7 @@ process preamble if present -------------------------------------------------------------------------- */ /*process_preamble:*/ if ( (dollar=strchr(expression,'$')) /* $ signals preceding preamble */ -!= NULL ) /* found embedded $ */ +!= NULL ) { /* found embedded $ */ if ( (prelen = (int)(dollar-expression)) /*#chars in expression preceding $*/ > 0 ) { /* must have preamble preceding $ */ if ( prelen < 65 ) { /* too long for a prefix */ @@ -4039,6 +5173,7 @@ if ( (dollar=strchr(expression,'$')) /* isdisplaystyle = 2; /* so set \displaystyle */ /*goto process_preamble;*/ /*check for preamble after leading $*/ } /* --- end-of-if/else(prelen>0) --- */ + } /* --- end-of-if(dollar!=NULL) --- */ /* ------------------------------------------------------------------------- back to caller -------------------------------------------------------------------------- */ @@ -4074,6 +5209,7 @@ char *expptr=expression, /* ptr within *tokptr=NULL, /*ptr to token found in expression*/ *texsubexpr(), argval[8192]; /*parse for macro args after token*/ char *strchange(); /* change leading chars of string */ +char *strwstr(); /*use strwstr() instead of strstr()*/ char *findbraces(); /*find left { and right } for \atop*/ int idelim=0, /* left- or right-index */ isymbol=0; /*symbols[],rightcomment[],etc index*/ @@ -4136,19 +5272,39 @@ static struct { char *html; char *args; #ifdef NEWCOMMANDS /* -DNEWCOMMANDS=\"filename.h\" */ #include NEWCOMMANDS #endif + /* --------------------------------------- + Cyrillic termchar mimeTeX equivalent... + --------------------------------------- */ + { "\\\'G", "embed\\","{\\acute{G}}" }, + { "\\\'g", "embed\\","{\\acute{g}}" }, + { "\\\'K", "embed\\","{\\acute{K}}" }, + { "\\\'k", "embed\\","{\\acute{k}}" }, + { "\\u U", "embed\\","{\\breve{U}}" }, + { "\\u u", "embed\\","{\\breve{u}}" }, + /*{ "\\\"E", "embed\\","{\\ddot{E}}" },*/ + /*{ "\\\"e", "embed\\","{\\ddot{e}}" },*/ + { "\\\"I", "embed\\","{\\ddot{\\=I}}" }, + { "\\\"\\i", "embed\\","{\\ddot{\\=\\i}}" }, /* ------------------------------------------ LaTeX Macro #args,default template... ------------------------------------------ */ { "\\lvec", "2n", "{#2_1,\\cdots,#2_{#1}}" }, + { "\\grave", "1", "{\\stackrel{\\Huge\\gravesym}{#1}}" }, /* \grave */ + { "\\acute", "1", "{\\stackrel{\\Huge\\acutesym}{#1}}" }, /* \acute */ + { "\\check", "1", "{\\stackrel{\\Huge\\checksym}{#1}}" }, /* \check */ + { "\\breve", "1", "{\\stackrel{\\Huge\\brevesym}{#1}}" }, /* \breve */ + { "\\buildrel","3", "{\\stackrel{#1}{#3}}" }, /* ignore #2 = \over */ { "\\overset", NULL, "\\stackrel" }, /* just an alias */ { "\\underset", "2", "\\relstack{#2}{#1}" }, /* reverse args */ /* --------------------------------------- - html termchar LaTeX equivalent... + html char termchar LaTeX equivalent... --------------------------------------- */ { """, ";", "\"" }, /* " is first, " */ { "&", ";", "&" }, { "<", ";", "<" }, { ">", ";", ">" }, + { "\", ";", "\\" }, /* backslash */ + { "&backslash",";", "\\" }, { " ", ";", "~" }, { "¡", ";", "{\\raisebox{-2}{\\rotatebox{180}{!}}}" }, { "¦", ";", "|" }, @@ -4168,30 +5324,54 @@ static struct { char *html; char *args; { "ã", ";", "{\\rm~\\tilde~a}" }, { "ÿ", ";", "{\\rm~\\ddot~y}" }, /* ÿ is last, ÿ */ /* --------------------------------------- + html tag termchar LaTeX equivalent... + --------------------------------------- */ + { "
", "embed\\i","\\\\" }, + { "
", "embed\\i","\\\\" }, + /* --------------------------------------- + garbage termchar LaTeX equivalent... + --------------------------------------- */ + { "< TEX >", "embed\\i","\000" }, + { "< / TEX >","embed\\i","\000" }, + { "
", "embed\\i","\000" }, + /* --------------------------------------- LaTeX termchar mimeTeX equivalent... --------------------------------------- */ { "\\AA", NULL, "{\\rm~A\\limits^{-1$o}}" }, { "\\aa", NULL, "{\\rm~a\\limits^{-1$o}}" }, { "\\bmod", NULL, "{\\hspace2{\\rm~mod}\\hspace2}" }, { "\\vdots", NULL, "{\\raisebox3{\\rotatebox{90}{\\ldots}}}" }, + { "\\dots", NULL, "{\\cdots}" }, { "\\cdots", NULL, "{\\raisebox3{\\ldots}}" }, { "\\ldots", NULL, "{\\fs4.\\hspace1.\\hspace1.}" }, { "\\ddots", NULL, "{\\fs4\\raisebox8.\\hspace1\\raisebox4.\\hspace1.}"}, { "\\notin", NULL, "{\\not\\in}" }, { "\\neq", NULL, "{\\not=}" }, + { "\\ne", NULL, "{\\not=}" }, { "\\hbar", NULL, "{\\compose~h{{\\fs{-1}-\\atop\\vspace3}}}" }, { "\\angle", NULL, "{\\compose{\\hspace{3}\\lt}{\\circle(10,15;-80,80)}}"}, + { "\\textcelsius", NULL, "{\\textdegree C}"}, + { "\\textdegree", NULL, "{\\Large^{^{\\tiny\\mathbf o}}}"}, { "\\cr", NULL, "\\\\" }, + { "\\iiint", NULL, "{\\int\\int\\int}\\limits" }, { "\\iint", NULL, "{\\int\\int}\\limits" }, { "\\Bigiint", NULL, "{\\Bigint\\Bigint}\\limits" }, + { "\\bigsqcap",NULL, "{\\fs{+4}\\sqcap}" }, { "!`", NULL, "{\\raisebox{-2}{\\rotatebox{180}{!}}}" }, { "?`", NULL, "{\\raisebox{-2}{\\rotatebox{180}{?}}}" }, + { "^\'", "embed","\'" }, /* avoid ^^ when re-xlating \' below */ + { "\'\'\'\'","embed","^{\\fs{-1}\\prime\\prime\\prime\\prime}" }, + { "\'\'\'", "embed","^{\\fs{-1}\\prime\\prime\\prime}" }, + { "\'\'", "embed","^{\\fs{-1}\\prime\\prime}" }, + { "\'", "embed","^{\\fs{-1}\\prime}" }, { "\\rightleftharpoons",NULL,"{\\rightharpoonup\\atop\\leftharpoondown}" }, + { "\\therefore",NULL,"{\\Huge\\raisebox{-4}{.\\atop.\\,.}}" }, { "\\LaTeX", NULL, "{\\rm~L\\raisebox{3}{\\fs{-1}A}\\TeX}" }, { "\\TeX", NULL, "{\\rm~T\\raisebox{-3}{E}X}" }, { "\\cyan", NULL, "{\\reverse\\red\\reversebg}" }, { "\\magenta",NULL, "{\\reverse\\green\\reversebg}" }, { "\\yellow",NULL, "{\\reverse\\blue\\reversebg}" }, + { "\\cancel",NULL, "\\Not" }, { "\\hhline",NULL, "\\Hline" }, { "\\Hline", NULL, "\\hline\\,\\\\\\hline" }, /* --------------------------------------------------------- @@ -4202,7 +5382,7 @@ static struct { char *html; char *args; { "cos", "1", "{\\cos{#1}}" }, { "asin", "1", "{\\sin^{-1}{#1}}" }, { "acos", "1", "{\\cos^{-1}{#1}}" }, - { "exp", "1", "{e^{#1}}" }, + { "exp", "1", "{{\\rm~e}^{#1}}" }, { "det", "1", "{\\left|{#1}\\right|}" }, /* --------------------------------------- LaTeX Constant termchar value... @@ -4239,69 +5419,63 @@ while ( (leftptr=strstr(expptr,leftcomme expptr = leftptr+1; /*resume search after this comment*/ } /* --- end-of-while(leftptr!=NULL) --- */ /* ------------------------------------------------------------------------- -convert \left( to \( and \right) to \), etc. --------------------------------------------------------------------------- */ -if ( xlateleft ) /* \left...\right xlation wanted */ - for ( idelim=0; idelim<2; idelim++ ) /* 0 for \left and 1 for \right */ - { - char *lrstr = (idelim==0?"\\left":"\\right"); /* \left on 1st pass */ - int lrlen = (idelim==0?5:6); /* strlen() of \left or \right */ - char *braces = (idelim==0?LEFTBRACES ".":RIGHTBRACES "."), /*([{*/ - **lrfrom= (idelim==0?leftfrom:rightfrom), /* long braces like \| */ - **lrto = (idelim==0?leftto:rightto), /* xlated to 1-char like = */ - *lrsym = NULL; /* lrfrom[isymbol] */ - expptr = expression; /* start search at beginning */ - while ( (tokptr=strstr(expptr,lrstr)) != NULL ) /* found \left or \right */ - { - if ( isthischar(*(tokptr+lrlen),braces) ) /* followed by a 1-char brace*/ - { strcpy(tokptr+1,tokptr+lrlen); /* so squeeze out "left" or "right"*/ - expptr = tokptr+2; } /* and resume search past brace */ - else /* may be a "long" brace like \| */ - { - expptr = tokptr+lrlen; /*init to resume search past\left\rt*/ - for(isymbol=0; (lrsym=lrfrom[isymbol]) != NULL; isymbol++) - { int symlen = strlen(lrsym); /* #chars in delim, e.g., 2 for \| */ - if ( memcmp(tokptr+lrlen,lrsym,symlen) == 0 ) /* found long delim*/ - { strcpy(tokptr+1,tokptr+lrlen+symlen-1); /* squeeze out delim */ - *(tokptr+1) = *(lrto[isymbol]); /* last char now 1-char delim*/ - expptr = tokptr+2 - lrlen; /* resume search past 1-char delim*/ - break; } /* no need to check more lrsym's */ - } /* --- end-of-for(isymbol) --- */ - } /* --- end-of-if/else(isthischar()) --- */ - } /* --- end-of-while(tokptr!=NULL) --- */ - } /* --- end-of-for(idelim) --- */ -/* ------------------------------------------------------------------------- run thru table, converting all occurrences of each macro to its expansion -------------------------------------------------------------------------- */ for(isymbol=0; (htmlsym=symbols[isymbol].html) != NULL; isymbol++) { int htmllen = strlen(htmlsym); /* length of escape, _without_ ; */ int isalgebra = isalpha((int)(*htmlsym)); /* leading char alphabetic */ + int isembedded = 0, /* true to xlate even if embedded */ + isstrwstr = 0, /* true to use strwstr() */ + wstrlen = 0; /* length of strwstr() match */ char *aleft="{([<|", *aright="})]>|"; /*left,right delims for alg syntax*/ + char embedkeywd[99] = "embed", /* keyword to signal embedded token*/ + embedterm = '\000'; /* char immediately after embed */ + int embedlen = strlen(embedkeywd); /* #chars in embedkeywd */ char *args = symbols[isymbol].args, /* number {}-args, optional []-arg */ *htmlterm = args, /*if *args nonumeric, then html term*/ *latexsym = symbols[isymbol].latex; /*latex replacement for htmlsym*/ char abuff[8192]; int iarg,nargs=0; /* macro expansion params */ + char wstrwhite[99]; /* whitespace chars for strwstr() */ if ( args != NULL ) /*we have args (or htmlterm) param*/ - if ( *args != '\000' ) /* and it's not an empty string */ + if ( *args != '\000' ) { /* and it's not an empty string */ if ( strchr("0123456789",*args) != NULL ) /* is 1st char #args=0-9 ? */ { htmlterm = NULL; /* if so, then we have no htmlterm */ *abuff = *args; abuff[1] = '\000'; /* #args char in ascii buffer */ nargs = atoi(abuff); } /* interpret #args to numeric */ + else if ( strncmp(args,embedkeywd,embedlen) == 0 )/*xlate embedded token*/ + { htmlterm = NULL; /* if so, then we have no htmlterm */ + isembedded = 1 ; /* turn on embedded flag */ + embedterm = args[embedlen]; /* char immediately after embed */ + if (strlen(args) > embedlen+1) { /* have embed,white for strwstr() */ + isstrwstr = 1; /* turn on strwtsr flag */ + strcpy(wstrwhite,args+6); } } /* and set its whitespace arg */ + } /* --- end-of-if(*args!='\000') --- */ expptr = expression; /* re-start search at beginning */ - while ( (tokptr=strstr(expptr,htmlsym)) != NULL ) /* found another sym */ - { char termchar = *(tokptr+htmllen); /* char terminating html sequence */ - int escapelen = htmllen; /* total length of escape sequence */ + while ( ( tokptr=(!isstrwstr?strstr(expptr,htmlsym): /* just use strtsr */ + strwstr(expptr,htmlsym,wstrwhite,&wstrlen)) ) /* or use our strwstr */ + != NULL ) /* found another sym */ + { int toklen = (!isstrwstr?htmllen:wstrlen); /* length of matched sym */ + char termchar = *(tokptr+toklen), /* char terminating html sequence */ + prevchar = (tokptr==expptr?' ':*(tokptr-1)); /*char preceding html*/ + int escapelen = toklen; /* total length of escape sequence */ *abuff = '\000'; /* default to empty string */ if ( latexsym != NULL ) /* table has .latex xlation */ if ( *latexsym != '\000' ) /* and it's not an empty string */ strcpy(abuff,latexsym); /* so get local copy */ if ( htmlterm != NULL ) /* sequence may have terminator */ escapelen += (isthischar(termchar,htmlterm)?1:0); /*add terminator*/ - if ( isalpha((int)termchar) ) /*we just have prefix of longer sym*/ - { expptr = tokptr+htmllen; /* just resume search after prefix */ + if ( !isembedded ) /* don't xlate embedded sequence */ + if ( isalpha((int)termchar) ) /*we just have prefix of longer sym*/ + { expptr = tokptr+toklen; /* just resume search after prefix */ continue; } /* but don't replace it */ + if ( isembedded ) /* for embedded sequence */ + if ( !isthischar(embedterm,ESCAPE) /* don't xlate escaped \token */ + && isthischar(prevchar,ESCAPE) ) /* and we have escaped \token */ + { expptr = tokptr+toklen; /*just resume search after literal*/ + continue; } /* but don't replace it */ if ( !isthischar(*htmlsym,ESCAPE) /* our symbol isn't escaped */ + && isalpha(*htmlsym) /* and our symbol starts with alpha*/ && !isthischar(*htmlsym,"&") ) /* and not an &html; special char */ if ( tokptr != expression ) /* then if we're past beginning */ if ( isthischar(*(tokptr-1),ESCAPE) /*and if inline symbol escaped*/ @@ -4326,7 +5500,7 @@ for(isymbol=0; (htmlsym=symbols[isymbol] if ( *expptr == '[' ) /* but user gave us [argval] */ expptr = texsubexpr(expptr,argval,0,"[","]",0,0); } /*so get it*/ else /* not optional, so get {argval} */ - if ( *expptr != '\000' ) /* check that some argval provided */ + if ( *expptr != '\000' ) { /* check that some argval provided */ if ( !isalgebra ) /* only { } delims for latex macro */ expptr = texsubexpr(expptr,argval,0,"{","}",0,0); /*get {argval}*/ else /*any delim for algebra syntax macro*/ @@ -4334,8 +5508,8 @@ for(isymbol=0; (htmlsym=symbols[isymbol] if ( isthischar(*argval,aleft) ) /* have delim-enclosed arg */ if ( *argval != '{' ) /* and it's not { }-enclosed */ { strchange(0,argval,"\\left"); /* insert opening \left, */ - strchange(0,argval+strlen(argval)-1,"\\right"); } /*\right*/ - } /* --- end-of-if/else(!isalgebra) --- */ + strchange(0,argval+strlen(argval)-1,"\\right"); } }/*\right*/ + } /* --- end-of-if(*expptr!='\000') --- */ /* --- replace #`iarg` in macro with argval --- */ sprintf(argsignal,"#%d",iarg); /* #1...#9 signals argument */ while ( (argsigptr=strstr(argval,argsignal)) != NULL ) /* #1...#9 */ @@ -4350,6 +5524,38 @@ for(isymbol=0; (htmlsym=symbols[isymbol] } /* --- end-of-while(tokptr!=NULL) --- */ } /* --- end-of-for(isymbol) --- */ /* ------------------------------------------------------------------------- +convert \left( to \( and \right) to \), etc. +-------------------------------------------------------------------------- */ +if ( xlateleft ) /* \left...\right xlation wanted */ + for ( idelim=0; idelim<2; idelim++ ) /* 0 for \left and 1 for \right */ + { + char *lrstr = (idelim==0?"\\left":"\\right"); /* \left on 1st pass */ + int lrlen = (idelim==0?5:6); /* strlen() of \left or \right */ + char *braces = (idelim==0?LEFTBRACES ".":RIGHTBRACES "."), /*([{*/ + **lrfrom= (idelim==0?leftfrom:rightfrom), /* long braces like \| */ + **lrto = (idelim==0?leftto:rightto), /* xlated to 1-char like = */ + *lrsym = NULL; /* lrfrom[isymbol] */ + expptr = expression; /* start search at beginning */ + while ( (tokptr=strstr(expptr,lrstr)) != NULL ) /* found \left or \right */ + { + if ( isthischar(*(tokptr+lrlen),braces) ) /* followed by a 1-char brace*/ + { strcpy(tokptr+1,tokptr+lrlen); /* so squeeze out "left" or "right"*/ + expptr = tokptr+2; } /* and resume search past brace */ + else /* may be a "long" brace like \| */ + { + expptr = tokptr+lrlen; /*init to resume search past\left\rt*/ + for(isymbol=0; (lrsym=lrfrom[isymbol]) != NULL; isymbol++) + { int symlen = strlen(lrsym); /* #chars in delim, e.g., 2 for \| */ + if ( memcmp(tokptr+lrlen,lrsym,symlen) == 0 ) /* found long delim*/ + { strcpy(tokptr+1,tokptr+lrlen+symlen-1); /* squeeze out delim */ + *(tokptr+1) = *(lrto[isymbol]); /* last char now 1-char delim*/ + expptr = tokptr+2 - lrlen; /* resume search past 1-char delim*/ + break; } /* no need to check more lrsym's */ + } /* --- end-of-for(isymbol) --- */ + } /* --- end-of-if/else(isthischar()) --- */ + } /* --- end-of-while(tokptr!=NULL) --- */ + } /* --- end-of-for(idelim) --- */ +/* ------------------------------------------------------------------------- run thru table, converting all {a+b\atop c+d} to \atop{a+b}{c+d} -------------------------------------------------------------------------- */ for(isymbol=0; (atopsym=atopcommands[isymbol]) != NULL; isymbol++) @@ -4507,12 +5713,154 @@ return ( nreps ); /* #replacements bac /* ========================================================================== - * Function: strtexchr (char *string, char *texchr ) + * Function: strwstr (char *string, char *substr, char *white, int *sublen) + * Purpose: Find first substr in string, but wherever substr contains + * a whitespace char (in white), string may contain any number + * (including 0) of whitespace chars. If white contains I or i, + * then match is case-insensitive (and I,i _not_ whitespace). + * -------------------------------------------------------------------------- + * Arguments: string (I) char * to null-terminated string in which + * first occurrence of substr will be found + * substr (I) char * to null-terminated string containing + * "template" that will be searched for + * white (I) char * to null-terminated string containing + * whitespace chars. If NULL or empty, then + * "~ \t\n\r\f\v" (WHITEMATH in mimetex.h) used. + * If white contains I or i, then match is + * case-insensitive (and I,i _not_ considered + * whitespace). + * sublen (O) address of int returning "length" of substr + * found in string (which may be longer or + * shorter than substr itself). + * -------------------------------------------------------------------------- + * Returns: ( char * ) ptr to first char of substr in string + * or NULL if not found or for any error. + * -------------------------------------------------------------------------- + * Notes: o Wherever a single whitespace char appears in substr, + * the corresponding position in string may contain any + * number (including 0) of whitespace chars, e.g., + * string="abc def" and string="abcdef" both match + * substr="c d" at offset 2 of string. + * o If substr="c d" (two spaces between c and d), + * then string must have at least one space, so now "abcdef" + * doesn't match. In general, the minimum number of spaces + * in string is the number of spaces in substr minus 1 + * (so 1 space in substr permits 0 spaces in string). + * o Embedded spaces are counted in sublen, e.g., + * string="c d" (three spaces) matches substr="c d" + * with sublen=5 returned. But string="ab c d" will + * also match substr=" c d" returning sublen=5 and + * a ptr to the "c". That is, the mandatory preceding + * space is _not_ counted as part of the match. + * But all the embedded space is counted. + * (An inconsistent bug/feature is that mandatory + * terminating space is counted.) + * o Moreover, string="c d" matches substr=" c d", i.e., + * the very beginning of a string is assumed to be preceded + * by "virtual blanks". + * ======================================================================= */ +/* --- entry point --- */ +char *strwstr ( char *string, char *substr, char *white, int *sublen ) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +char *psubstr=substr, *pstring=string,/*ptr to current char in substr,str*/ + *pfound = (char *)NULL; /*ptr to found substr back to caller*/ +char *pwhite=NULL, whitespace[256]; /* callers white whithout i,I */ +int iscase = (white==NULL?1: /* case-sensitive if i,I in white */ + strchr(white,'i')==NULL && strchr(white,'I')==NULL); +int foundlen = 0; /* length of substr found in string*/ +int nstrwhite=0, nsubwhite=0, /* #leading white chars in str,sub */ + nminwhite=0; /* #mandatory leading white in str */ +int nstrchars=0, nsubchars=0, /* #non-white chars to be matched */ + isncmp=0; /*strncmp() or strncasecmp() result*/ +/* ------------------------------------------------------------------------- +Initialization +-------------------------------------------------------------------------- */ +/* --- set up whitespace --- */ +strcpy(whitespace,WHITEMATH); /*default if no user input for white*/ +if ( white != NULL ) /*user provided ptr to white string*/ + if ( *white != '\000' ) { /*and it's not just an empty string*/ + strcpy(whitespace,white); /* so use caller's white spaces */ + while ( (pwhite=strchr(whitespace,'i')) != NULL ) /* have an embedded i */ + strcpy(pwhite,pwhite+1); /* so squeeze it out */ + while ( (pwhite=strchr(whitespace,'I')) != NULL ) /* have an embedded I */ + strcpy(pwhite,pwhite+1); /* so squeeze it out */ + if ( *whitespace == '\000' ) /* caller's white just had i,I */ + strcpy(whitespace,WHITEMATH); } /* so revert back to default */ +/* ------------------------------------------------------------------------- +Find first occurrence of substr in string +-------------------------------------------------------------------------- */ +if ( string != NULL ) /* caller passed us a string ptr */ + while ( *pstring != '\000' ) { /* break when string exhausted */ + char *pstrptr = pstring; /* (re)start at next char in string*/ + int leadingwhite = 0; /* leading whitespace */ + psubstr = substr; /* start at beginning of substr */ + foundlen = 0; /* reset length of found substr */ + if ( substr != NULL ) /* caller passed us a substr ptr */ + while ( *psubstr != '\000' ) { /*see if pstring begins with substr*/ + /* --- check for end-of-string before finding match --- */ + if ( *pstrptr == '\000' ) /* end-of-string without a match */ + goto nextstrchar; /* keep trying with next char */ + /* --- actual amount of whitespace in string and substr --- */ + nsubwhite = strspn(psubstr,whitespace); /* #leading white chars in sub */ + nstrwhite = strspn(pstrptr,whitespace); /* #leading white chars in str */ + nminwhite = max2(0,nsubwhite-1); /* #mandatory leading white in str */ + /* --- check for mandatory leading whitespace in string --- */ + if ( pstrptr != string ) /*not mandatory at start of string*/ + if ( nstrwhite < nminwhite ) /* too little leading white space */ + goto nextstrchar; /* keep trying with next char */ + /* ---hold on to #whitespace chars in string preceding substr match--- */ + if ( pstrptr == pstring ) /* whitespace at start of substr */ + leadingwhite = nstrwhite; /* save it as leadingwhite */ + /* --- check for optional whitespace --- */ + if ( psubstr != substr ) /* always okay at start of substr */ + if ( nstrwhite>0 && nsubwhite<1 ) /* too much leading white space */ + goto nextstrchar; /* keep trying with next char */ + /* --- skip any leading whitespace in substr and string --- */ + psubstr += nsubwhite; /* push past leading sub whitespace*/ + pstrptr += nstrwhite; /* push past leading str whitespace*/ + /* --- now get non-whitespace chars that we have to match --- */ + nsubchars = strcspn(psubstr,whitespace); /* #non-white chars in sub */ + nstrchars = strcspn(pstrptr,whitespace); /* #non-white chars in str */ + if ( nstrchars < nsubchars ) /* too few chars for match */ + goto nextstrchar; /* keep trying with next char */ + /* --- see if next nsubchars are a match --- */ + isncmp = (iscase? strncmp(pstrptr,psubstr,nsubchars): /*case sensitive*/ + strncasecmp(pstrptr,psubstr,nsubchars)); /*case insensitive*/ + if ( isncmp != 0 ) /* no match */ + goto nextstrchar; /* keep trying with next char */ + /* --- push past matched chars --- */ + psubstr += nsubchars; pstrptr += nsubchars; /*nsubchars were matched*/ + } /* --- end-of-while(*psubstr!='\000') --- */ + pfound = pstring + leadingwhite; /* found match starting at pstring */ + foundlen = (int)(pstrptr-pfound); /* consisting of this many chars */ + goto end_of_job; /* back to caller */ + /* ---failed to find substr, continue trying with next char in string--- */ + nextstrchar: /* continue outer loop */ + pstring++; /* bump to next char in string */ + } /* --- end-of-while(*pstring!='\000') --- */ +/* ------------------------------------------------------------------------- +Back to caller with ptr to first occurrence of substr in string +-------------------------------------------------------------------------- */ +end_of_job: + if ( msglevel>=999 && msgfp!=NULL) { /* debugging/diagnostic output */ + fprintf(msgfp,"strwstr> str=\"%.72s\" sub=\"%s\" found at offset %d\n", + string,substr,(pfound==NULL?(-1):(int)(pfound-string))); fflush(msgfp); } + if ( sublen != NULL ) /*caller wants length of found substr*/ + *sublen = foundlen; /* give it to him along with ptr */ + return ( pfound ); /*ptr to first found substr, or NULL*/ +} /* --- end-of-function strwstr() --- */ + + +/* ========================================================================== + * Function: strtexchr ( char *string, char *texchr ) * Purpose: Find first texchr in string, but texchr must be followed * by non-alpha * -------------------------------------------------------------------------- * Arguments: string (I) char * to null-terminated string in which - * firstoccurrence of delim will be found + * first occurrence of delim will be found * texchr (I) char * to null-terminated string that * will be searched for * -------------------------------------------------------------------------- @@ -4527,14 +5875,14 @@ char *strtexchr ( char *string, char *te /* ------------------------------------------------------------------------- Allocations and Declarations -------------------------------------------------------------------------- */ -char *strstr(), delim, *ptexchr=(char *)NULL; /* ptr returned to caller*/ +char delim, *ptexchr=(char *)NULL; /* ptr returned to caller*/ char *pstring = string; /* start or continue up search here*/ int texchrlen = (texchr==NULL?0:strlen(texchr)); /* #chars in texchr */ /* ------------------------------------------------------------------------- locate texchr in string -------------------------------------------------------------------------- */ if ( string != (char *)NULL /* check that we got input string */ -&& texchrlen > 0 ) /* and a texchr to search for */ +&& texchrlen > 0 ) { /* and a texchr to search for */ while ( (ptexchr=strstr(pstring,texchr)) /* look for texchr in string */ != (char *)NULL ) /* found it */ if ( (delim = ptexchr[texchrlen]) /* char immediately after texchr */ @@ -4544,7 +5892,7 @@ if ( string != (char *)NULL /* check th || 0 ) /* other tests to be determined */ pstring = ptexchr + texchrlen; /* continue search after texchr */ else /* passed all tests */ - break; /*so return ptr to texchr to caller*/ + break; } /*so return ptr to texchr to caller*/ return ( ptexchr ); /* ptr to texchar back to caller */ } /* --- end-of-function strtexchr() --- */ @@ -4641,11 +5989,12 @@ subraster *rasterize ( char *expression, /* ------------------------------------------------------------------------- Allocations and Declarations -------------------------------------------------------------------------- */ -char *preamble(), pretext[256]; /* process preamble, if present */ -char chartoken[8192], *texsubexpr(), /*get subexpression from expression*/ +char *preamble(), pretext[512]; /* process preamble, if present */ +char chartoken[MAXSUBXSZ+1], *texsubexpr(), /*get subexpression from expr*/ *subexpr = chartoken; /* token may be parenthesized expr */ int isbrace(); /* check subexpr for braces */ mathchardef *symdef, *get_symdef(); /*get mathchardef struct for symbol*/ +int ligdef, get_ligature(); /*get symtable[] index for ligature*/ int natoms=0; /* #atoms/tokens processed so far */ int type_raster(); /* display debugging output */ subraster *rasterize(), /* recurse */ @@ -4658,16 +6007,22 @@ subraster *get_charsubraster(), /* char *sp=NULL, *prevsp=NULL, /* raster for current, prev char */ *expraster = (subraster *)NULL; /* raster returned to caller */ int delete_subraster(); /* free everything before returning*/ +int family = fontinfo[fontnum].family; /* current font family */ +int isleftscript = 0, /* true if left-hand term scripted */ + wasscripted = 0, /* true if preceding token scripted*/ + wasdelimscript = 0; /* true if preceding delim scripted*/ /*int pixsz = 1;*/ /*default #bits per pixel, 1=bitmap*/ /* --- global values saved/restored at each recursive iteration --- */ -int wastext = istext, /* initial istext mode flag */ - wasstring = isstring, /* initial isstring mode flag */ +int wasstring = isstring, /* initial isstring mode flag */ wasdisplaystyle = isdisplaystyle, /*initial displaystyle mode flag*/ + oldfontnum = fontnum, /* initial font family */ oldfontsize = fontsize, /* initial fontsize */ olddisplaysize = displaysize, /* initial \displaystyle size */ oldshrinkfactor = shrinkfactor, /* initial shrinkfactor */ - oldsquashmargin = squashmargin, /* initial squashmargin */ - oldissquashdelta = issquashdelta, /* initial issquashdelta */ + oldsmashmargin = smashmargin, /* initial smashmargin */ + oldissmashdelta = issmashdelta, /* initial issmashdelta */ + oldisexplicitsmash = isexplicitsmash, /* initial isexplicitsmash */ + oldisscripted = isscripted, /* initial isscripted */ *oldworkingparam = workingparam; /* initial working parameter */ subraster *oldworkingbox = workingbox, /* initial working box */ *oldleftexpression = leftexpression; /*left half rasterized so far*/ @@ -4679,12 +6034,13 @@ initialization recurlevel++; /* wind up one more recursion level*/ leftexpression = NULL; /* no leading left half yet */ isreplaceleft = 0; /* reset replaceleft flag */ +if(1)fraccenterline = NOVALUE; /* reset \frac baseline signal */ /* shrinkfactor = shrinkfactors[max2(0,min2(size,LARGESTSIZE))];*/ /*set sf*/ shrinkfactor = shrinkfactors[max2(0,min2(size,16))]; /* have 17 sf's */ -if ( msgfp!=NULL && msglevel >= 29 ) /*display expression for debugging*/ - { fprintf(msgfp, - "rasterize> recursion level=%d, size=%d,\n\texpression=\"%s\"\n", - recurlevel,size,(expression==NULL?"null":expression)); fflush(msgfp); } +if ( msgfp!=NULL && msglevel >= 9 ) { /*display expression for debugging*/ + fprintf(msgfp, + "rasterize> recursion#%d, size=%d,\n\texpression=\"%s\"\n", + recurlevel,size,(expression==NULL?"":expression)); fflush(msgfp); } if ( expression == NULL ) goto end_of_job; /* nothing given to do */ /* ------------------------------------------------------------------------- preocess optional $-terminated preamble preceding expression @@ -4700,16 +6056,31 @@ build up raster one character (or subexp -------------------------------------------------------------------------- */ while ( 1 ) { + /* --- kludge for \= cyrillic ligature --- */ + isligature = 0; /* no ligature found yet */ + family = fontinfo[fontnum].family; /* current font family */ + if ( family == CYR10 ) /* may have cyrillic \= ligature */ + if ( (ligdef = get_ligature(expression,family)) /*check for any ligature*/ + >= 0 ) /* got some ligature */ + if ( memcmp(symtable[ligdef].symbol,"\\=",2) == 0 ) /* starts with \= */ + isligature = 1; /* signal \= ligature */ /* --- get next character/token or subexpression --- */ + subexprptr = expression; /* ptr within expression to subexpr*/ expression = texsubexpr(expression,chartoken,0,LEFTBRACES,RIGHTBRACES,1,1); subexpr = chartoken; /* "local" copy of chartoken ptr */ leftsymdef = NULL; /* no character identified yet */ sp = NULL; /* no subraster yet */ size = fontsize; /* in case reset by \tiny, etc */ + /*isleftscript = isdelimscript;*/ /*was preceding term scripted delim*/ + wasscripted = isscripted; /* true if preceding token scripted*/ + wasdelimscript = isdelimscript; /* preceding \right delim scripted */ + if(1)isscripted = 0; /* no subscripted expression yet */ + isdelimscript = 0; /* reset \right delim scripted flag*/ /* --- debugging output --- */ - if ( msgfp!=NULL && msglevel >= 999 ) /* display chartoken for debugging */ - { fprintf(msgfp,"rasterize> recursion level=%d, atom#%d = \"%s\"\n", - recurlevel,natoms+1,chartoken); fflush(msgfp); } + if ( msgfp!=NULL && msglevel >= 9 ) { /* display chartoken for debugging */ + fprintf(msgfp, + "rasterize> recursion#%d,atom#%d=\"%s\" (isligature=%d,isleftscript=%d)\n", + recurlevel,natoms+1,chartoken,isligature,isleftscript); fflush(msgfp); } if ( expression == NULL /* no more tokens */ && *subexpr == '\000' ) break; /* and this token empty */ if ( *subexpr == '\000' ) break; /* enough if just this token empty */ @@ -4720,36 +6091,60 @@ while ( 1 ) else /* --- single-character atomic token --- */ if ( !isthischar(*subexpr,SCRIPTS) ) /* scripts handled below */ { - /* --- first look up mathchardef for atomic token in table --- */ - if ( (leftsymdef=symdef=get_symdef(chartoken)) /*mathchardef for token*/ - == NULL ) /* lookup failed */ - { char literal[512] = "[?]"; /*display for unrecognized literal*/ - int wastext = istext; /* error display in default mode */ - if ( msgfp!=NULL && msglevel >= 29 ) /* display unrecognized symbol */ + /* --- first check for opening $ in \text{ if $n-m$ even} --- */ + if ( istextmode /* we're in \text mode */ + && *subexpr=='$' && subexpr[1]=='\000' ) { /* and have an opening $ */ + char *endptr=NULL, mathexpr[MAXSUBXSZ+1]; /* $expression$ in \text{ }*/ + int exprlen = 0; /* length of $expression$ */ + int textfontnum = fontnum; /* current text font number */ + /*if ( (endptr=strrchr(expression,'$')) != NULL )*/ /*ptr to closing $*/ + if ( (endptr=strchr(expression,'$')) != NULL ) /* ptr to closing $ */ + exprlen = (int)(endptr-expression); /* #chars preceding closing $ */ + else { /* no closing $ found */ + exprlen = strlen(expression); /* just assume entire expression */ + endptr = expression + (exprlen-1); } /*and push expression to '\000'*/ + exprlen = min2(exprlen,MAXSUBXSZ); /* don't overflow mathexpr[] */ + if ( exprlen > 0 ) { /* have something between $$ */ + memcpy(mathexpr,expression,exprlen); /*local copy of math expression*/ + mathexpr[exprlen] = '\000'; /* null-terminate it */ + fontnum = 0; /* set math mode */ + sp = rasterize(mathexpr,size); /* and rasterize $expression$ */ + fontnum = textfontnum; } /* set back to text mode */ + expression = endptr+1; /* push expression past closing $ */ + } /* --- end-of-if(istextmode&&*subexpr=='$') --- */ + else + /* --- otherwise, look up mathchardef for atomic token in table --- */ + if ( (leftsymdef=symdef=get_symdef(chartoken)) /*mathchardef for token*/ + == NULL ) /* lookup failed */ + { char literal[512] = "[?]"; /*display for unrecognized literal*/ + int oldfontnum = fontnum; /* error display in default mode */ + if ( msgfp!=NULL && msglevel >= 29 ) /* display unrecognized symbol*/ { fprintf(msgfp,"rasterize> get_symdef() failed for \"%s\"\n", chartoken); fflush(msgfp); } - sp = (subraster *)NULL; /* init to signal failure */ - if ( warninglevel < 1 ) continue; /* warnings not wanted */ - istext = 0; /* reset from \mathbb, etc */ - if ( isthischar(*chartoken,ESCAPE) ) /* we got unrecognized \escape */ - { /* --- so display literal {\rm~[\backslash~chartoken?]} --- */ - strcpy(literal,"{\\rm~[\\backslash~"); /* init token */ - strcat(literal,chartoken+1); /* add chars following leading \ */ - strcat(literal,"?]}"); } /* add closing brace */ - sp = rasterize(literal,size-1); /* rasterize literal token */ - istext = wastext; /* reset text mode */ - if ( sp == (subraster *)NULL ) continue; } /*flush if rasterize fails*/ - else /* --- check if we have special handler to process this token --- */ - if ( symdef->handler != NULL ) /* have a handler for this token */ - { int arg1=symdef->charnum, arg2=symdef->family, arg3=symdef->class; - if ( (sp = (subraster *) /* returned void* is subraster* */ - (*(symdef->handler))(&expression,size,prevsp,arg1,arg2,arg3))== NULL ) - continue; } /* flush token if handler failed */ - else /* --- no handler, so just get subraster for this character --- */ - if ( !isstring ) /* rasterizing */ - { if ( (sp=get_charsubraster(symdef,size)) /* get subraster */ + sp = (subraster *)NULL; /* init to signal failure */ + if ( warninglevel < 1 ) continue; /* warnings not wanted */ + fontnum = 0; /* reset from \mathbb, etc */ + if ( isthischar(*chartoken,ESCAPE) ) /* we got unrecognized \escape*/ + { /* --- so display literal {\rm~[\backslash~chartoken?]} --- */ + strcpy(literal,"{\\rm~[\\backslash~"); /* init token */ + strcat(literal,chartoken+1); /* add chars following leading \ */ + strcat(literal,"?]}"); } /* add closing brace */ + sp = rasterize(literal,size-1); /* rasterize literal token */ + fontnum = oldfontnum; /* reset font family */ + if ( sp == (subraster *)NULL ) continue; }/*flush if rasterize fails*/ + else /* --- check if we have special handler to process this token --- */ + if ( symdef->handler != NULL ) /* have a handler for this token */ + { int arg1=symdef->charnum, arg2=symdef->family, arg3=symdef->class; + if ( (sp = (subraster *) /* returned void* is subraster* */ + (*(symdef->handler))(&expression,size,prevsp,arg1,arg2,arg3))==NULL) + continue; } /* flush token if handler failed */ + else /* --- no handler, so just get subraster for this character --- */ + if ( !isstring ) /* rasterizing */ + { if ( isligature ) /* found a ligature */ + expression = subexprptr + strlen(symdef->symbol); /*push past it*/ + if ( (sp=get_charsubraster(symdef,size)) /* get subraster */ == NULL ) continue; } /* flush token if failed */ - else /* constructing ascii string */ + else /* constructing ascii string */ { char *symbol = symdef->symbol; /* symbol for ascii string */ int symlen = (symbol!=NULL?strlen(symbol):0); /*#chars in symbol*/ if ( symlen < 1 ) continue; /* no symbol for ascii string */ @@ -4760,24 +6155,36 @@ while ( 1 ) sp->baseline = 1; /* default (should be unused) */ strcpy((char *)((sp->image)->pixmap),symbol); /* copy symbol */ /*((char *)((sp->image)->pixmap))[symlen] = '\000';*/ } /*null*/ - } /* --- end-of-if/else ... if/else --- */ + } /* --- end-of-if(!isthischar(*subexpr,SCRIPTS)) --- */ /* --- handle any super/subscripts following symbol or subexpression --- */ sp = rastlimits(&expression,size,sp); + isleftscript = (wasscripted||wasdelimscript?1:0);/*preceding term scripted*/ /* --- debugging output --- */ - if ( msgfp!=NULL && msglevel >= 999 ) /* display raster for debugging */ - { fprintf(msgfp,"rasterize> recursion level=%d, atom#%d%s\n", - recurlevel,natoms+1,(sp==NULL?" = null":"...")); - if(sp!=NULL) type_raster(sp->image,msgfp); /* display raster */ - fflush(msgfp); } /* flush msgfp buffer */ + if ( msgfp!=NULL && msglevel >= 9 ) { /* display raster for debugging */ + fprintf(msgfp,"rasterize> recursion#%d,atom#%d%s\n", + recurlevel,natoms+1,(sp==NULL?" = ":"...")); + if ( msglevel >= 9 ) fprintf(msgfp, + " isleftscript=%d is/wasscripted=%d,%d is/wasdelimscript=%d,%d\n", + isleftscript,isscripted,wasscripted,isdelimscript,wasdelimscript); + if ( msglevel >= 99 ) + if(sp!=NULL) type_raster(sp->image,msgfp); /* display raster */ + fflush(msgfp); } /* flush msgfp buffer */ /* --- accumulate atom or parenthesized subexpression --- */ if ( natoms < 1 /* nothing previous to concat */ || expraster == NULL /* or previous was complete error */ || isreplaceleft ) /* or we're replacing previous */ - { expraster = subrastcpy(sp); /* so just copy static CHARASTER */ + { if ( 1 && expraster!=NULL ) /* probably replacing left */ + delete_subraster(expraster); /* so first free original left */ + expraster = subrastcpy(sp); /* copy static CHARASTER or left */ isreplaceleft = 0; } /* reset replacement flag */ else /*we've already built up atoms so...*/ - if ( sp != NULL ) /* ...if we have a new component */ - expraster = rastcat(expraster,sp,1); /* concat new one, free previous */ + if ( sp != NULL ) { /* ...if we have a new term */ + int prevsmashmargin = smashmargin; /* save current smash margin */ + if ( isleftscript ) { /* don't smash against scripts */ + isdelimscript = 0; /* reset \right delim scripted flag*/ + if ( !isexplicitsmash ) smashmargin = 0; } /* signal no smash wanted */ + expraster = rastcat(expraster,sp,1); /* concat new term, free previous */ + smashmargin = prevsmashmargin; } /* restore current smash margin */ delete_subraster(prevsp); /* free prev (if not a CHARASTER) */ prevsp = sp; /* current becomes previous */ leftexpression = expraster; /* left half rasterized so far */ @@ -4796,15 +6203,25 @@ end_of_job: if ( expraster != (subraster *)NULL ) /* i.e., if natoms>0 */ type_raster(expraster->image,msgfp); /* display completed raster */ fflush(msgfp); } /* flush msgfp buffer */ + /* --- set final raster buffer --- */ + if ( 1 && expraster != (subraster *)NULL ) /* have an expression */ + { int type = expraster->type; /* type of constructed image */ + if ( type != FRACRASTER ) /* leave \frac alone */ + expraster->type = IMAGERASTER; /* set type to constructed image */ + if ( istextmode ) /* but in text mode */ + expraster->type = blanksignal; /* set type to avoid smash */ + expraster->size = fontsize; } /* set original input font size */ /* --- restore flags/values to original saved values --- */ - istext = wastext; /* text mode reset */ isstring = wasstring; /* string mode reset */ isdisplaystyle = wasdisplaystyle; /* displaystyle mode reset */ + fontnum = oldfontnum; /* font family reset */ fontsize = oldfontsize; /* fontsize reset */ displaysize = olddisplaysize; /* \displaystyle size reset */ shrinkfactor = oldshrinkfactor; /* shrinkfactor reset */ - squashmargin = oldsquashmargin; /* squashmargin reset */ - issquashdelta = oldissquashdelta; /* issquashdelta reset */ + smashmargin = oldsmashmargin; /* smashmargin reset */ + issmashdelta = oldissmashdelta; /* issmashdelta reset */ + isexplicitsmash = oldisexplicitsmash; /* isexplicitsmash reset */ + isscripted = oldisscripted; /* isscripted reset */ workingparam = oldworkingparam; /* working parameter reset */ workingbox = oldworkingbox; /* working box reset */ leftexpression = oldleftexpression; /* leftexpression reset */ @@ -4812,9 +6229,6 @@ end_of_job: unitlength = oldunitlength; /* unitlength reset */ recurlevel--; /* unwind one recursion level */ /* --- return final subraster to caller --- */ - if ( 1 && expraster != (subraster *)NULL ) /* have an expression */ - { expraster->type = IMAGERASTER; /* set type to constructed image */ - expraster->size = fontsize; } /* set original input font size */ return ( expraster ); } /* --- end-of-function rasterize() --- */ @@ -4852,13 +6266,13 @@ int explen = strlen(expression); /* tota int isescape = 0, /* true if parens \escaped */ isrightdot = 0, /* true if right paren is \right. */ isleftdot = 0; /* true if left paren is \left. */ -char left[16], right[16]; /* parens enclosing expresion */ -char noparens[8192]; /* get subexpr without parens */ +char left[32], right[32]; /* parens enclosing expresion */ +char noparens[MAXSUBXSZ+1]; /* get subexpr without parens */ subraster *rasterize(), *sp=NULL; /* rasterize what's between ()'s */ int isheight = 1; /*true=full height, false=baseline*/ int height, /* height of rasterized noparens[] */ baseline; /* and its baseline */ -int family = CMEX10; /* family for paren chars */ +int family = /*CMSYEX*/ CMEX10; /* family for paren chars */ subraster *get_delim(), *lp=NULL, *rp=NULL; /* left and right paren chars */ subraster *rastcat(); /* concatanate subrasters */ int delete_subraster(); /*in case of error after allocation*/ @@ -4950,10 +6364,14 @@ Allocations and Declarations -------------------------------------------------------------------------- */ subraster *rastscripts(), *rastdispmath(), /*one of these will do the work*/ *rastcat(), /* may need to concat scripts */ - *scriptsp = basesp; /* and this will become the result */ + *rasterize(), /* may need to construct dummy base*/ + *scriptsp = basesp, /* and this will become the result */ + *dummybase = basesp; /* for {}_i construct a dummy base */ int isdisplay = (-1); /* set 1 for displaystyle, else 0 */ -int oldsquashmargin = squashmargin; /* save original squashmargin */ +int oldsmashmargin = smashmargin; /* save original smashmargin */ int type_raster(); /* display debugging output */ +int delete_subraster(); /* free dummybase, if necessary */ +int rastsmashcheck(); /* check if okay to smash scripts */ /* --- to check for \limits or \nolimits preceding scripts --- */ char *texchar(), *exprptr=*expression, limtoken[255]; /*check for \limits*/ int toklen=0; /* strlen(limtoken) */ @@ -4964,6 +6382,7 @@ determine whether or not to use displaym -------------------------------------------------------------------------- */ scriptlevel++; /* first, increment subscript level*/ *limtoken = '\000'; /* no token yet */ +isscripted = 0; /* signal term not (text) scripted */ if ( msgfp!=NULL && msglevel>=999 ) { fprintf(msgfp,"rastlimits> scriptlevel#%d exprptr=%.48s\n", scriptlevel,(exprptr==NULL?"null":exprptr)); fflush(msgfp); } @@ -4978,18 +6397,18 @@ if ( *limtoken != '\000' ) /* have toke if ( memcmp("\\limits",limtoken,toklen) == 0 /* may be \limits */ || memcmp("\\nolimits",limtoken,toklen) == 0 ) /* or may be \nolimits */ if ( (tokdef= get_symdef(limtoken)) /* look up token to be sure */ - != NULL ) /* found token in table */ + != NULL ) { /* found token in table */ if ( strcmp("\\limits",tokdef->symbol) == 0 ) /* found \limits */ isdisplay = 1; /* so explicitly set displaymath */ else /* wasn't \limits */ if ( strcmp("\\nolimits",tokdef->symbol) == 0 ) /* found \nolimits */ - isdisplay = 0; /* so explicitly reset displaymath */ + isdisplay = 0; } /* so explicitly reset displaymath */ /* --- see if we found \[no]limits --- */ if ( isdisplay != (-1) ) /* explicit directive found */ *expression = exprptr; /* so bump expression past it */ else /* noexplicit directive */ { isdisplay = 0; /* init displaymath flag off */ - if ( isdisplaystyle ) /* we're in displaystyle math mode */ + if ( isdisplaystyle ) { /* we're in displaystyle math mode */ if ( isdisplaystyle >= 5 ) /* and mode irrevocably forced true */ { if ( class!=OPENING && class!=CLOSING ) /*don't force ('s and )'s*/ isdisplay = 1; } /* set flag if mode forced true */ @@ -5002,23 +6421,39 @@ else /* noexplicit directive */ isdisplay = 1; } /* set flag if mode forced true */ else /* determine mode from base symbol */ if ( class == DISPOPER ) /* it's a displaystyle operator */ - isdisplay = 1; } /* so set flag */ + isdisplay = 1; } } /* so set flag */ /* ------------------------------------------------------------------------- dispatch call to create sub/superscripts -------------------------------------------------------------------------- */ if ( isdisplay ) /* scripts above/below base symbol */ scriptsp = rastdispmath(expression,size,basesp); /* everything all done */ -else /* scripts alongside base symbol */ - if ( (scriptsp=rastscripts(expression,size,basesp)) == NULL ) /*no scripts*/ +else { /* scripts alongside base symbol */ + if ( dummybase == NULL ) /* no base symbol preceding scripts*/ + dummybase = rasterize("\\rule0{10}",size); /*guess a typical base symbol*/ + issmashokay = 1; /*haven't found a no-smash char yet*/ + if((scriptsp=rastscripts(expression,size,dummybase)) == NULL) /*no scripts*/ scriptsp = basesp; /* so just return unscripted symbol*/ - else /* symbols followed by scripts */ + else { /* symbols followed by scripts */ + isscripted = 1; /*signal current term text-scripted*/ if ( basesp != NULL ) /* have base symbol */ - { squashmargin = 0; /* don't squash script */ - scriptsp = rastcat(basesp,scriptsp,2); /*concat scripts to base sym*/ - scriptsp->type = IMAGERASTER; /* flip type of composite object */ - scriptsp->size = size; } /* and set font size */ + { /*if(0)smashmargin = 0;*/ /*don't smash script (doesn't work)*/ + /*scriptsp = rastcat(basesp,scriptsp,2);*//*concat scripts to base sym*/ + /* --- smash (or just concat) script raster against base symbol --- */ + if ( !issmashokay ) /* don't smash leading - */ + if ( !isexplicitsmash ) scriptsp->type = blanksignal; /*don't smash*/ + scriptsp = rastcat(basesp,scriptsp,3); /*concat scripts to base sym*/ + if(1) scriptsp->type = IMAGERASTER; /* flip type of composite object */ + /* --- smash (or just concat) scripted term to stuff to its left --- */ + issmashokay = 1; /* okay to smash base expression */ + if ( 0 && smashcheck > 1 ) /* smashcheck=2 to check base */ + /* note -- we _don't_ have base expression available to check */ + issmashokay = rastsmashcheck(*expression); /*check if okay to smash*/ + if ( !issmashokay ) /* don't smash leading - */ + if ( !isexplicitsmash ) scriptsp->type = blanksignal; /*don't smash*/ + scriptsp->size = size; } } } /* and set font size */ end_of_job: - squashmargin = oldsquashmargin; /* reset original squashmargin */ + smashmargin = oldsmashmargin; /* reset original smashmargin */ + if ( dummybase != basesp ) delete_subraster(dummybase); /*free work area*/ if ( msgfp!=NULL && msglevel>=99 ) { fprintf(msgfp,"rastlimits> scriptlevel#%d returning %s\n", scriptlevel,(scriptsp==NULL?"null":"...")); @@ -5077,6 +6512,7 @@ int szval = min2(max2(size,0),LARGESTSIZ /*int istweak = 1;*/ /* true to tweak script positioning */ int rastput(); /*put scripts in constructed raster*/ int delete_subraster(); /* free work areas */ +int rastsmashcheck(); /* check if okay to smash scripts */ int pixsz = 1; /*default #bits per pixel, 1=bitmap*/ /* ------------------------------------------------------------------------- Obtain subscript and/or superscript expressions, and rasterize them/it @@ -5096,6 +6532,16 @@ issub = (subsp != (subraster *)NULL); / issup = (supsp != (subraster *)NULL); /* true if we have superscript */ isboth = (issub && issup); /* true if we have both */ if (!issub && !issup) goto end_of_job; /* quit if we have neither */ +/* --- check for leading no-smash chars (if enabled) --- */ +issmashokay = 0; /* default, don't smash scripts */ +if ( smashcheck > 0 ) { /* smash checking wanted */ + issmashokay = 1; /*haven't found a no-smash char yet*/ + if ( issub ) /* got a subscript */ + issmashokay = rastsmashcheck(subscript); /* check if okay to smash */ + if ( issmashokay ) /* clean sub, so check sup */ + if ( issup ) /* got a superscript */ + issmashokay = rastsmashcheck(supscript); /* check if okay to smash */ + } /* --- end-of-if(smashcheck>0) --- */ /* ------------------------------------------------------------------------- get height, width, baseline of scripts, and height, baseline of base symbol -------------------------------------------------------------------------- */ @@ -5136,14 +6582,14 @@ if ( !issub ) /* we only have a super supht+vabove-bdescend); /* sup's bot above base symbol bot */ baseline = height-1; } /*sup's baseline at bottom of raster*/ /* --- subscript only --- */ -if ( !issup ) /* we only have a subscript */ +if ( !issup ) { /* we only have a subscript */ if ( subht > sdescend ) /*sub can descend below base bot...*/ { height = subht; /* ...without extra space on top */ baseline = height-(sdescend+1); /* sub's bot below base symbol bot */ baseline = min2(baseline,max2(baseln-vbelow,0)); }/*top below base top*/ else /* sub's top will be below baseln */ { height = sdescend+1; /* sub's bot below base symbol bot */ - baseline = 0; } /* sub's baseline at top of raster */ + baseline = 0; } } /* sub's baseline at top of raster */ /* ------------------------------------------------------------------------- construct raster with superscript over subscript -------------------------------------------------------------------------- */ @@ -5224,19 +6670,19 @@ if (!issub && !issup) goto end_of_job; / stack operator and its script(s) -------------------------------------------------------------------------- */ /* --- stack superscript atop operator --- */ -if ( issup ) /* we have a superscript */ +if ( issup ) { /* we have a superscript */ if ( sp == NULL ) /* but no base expression */ sp = supsp; /* so just use superscript */ else /* have base and superscript */ if ( (sp=rastack(sp,supsp,1,vspace,1,3)) /* stack supsp atop base sp */ - == NULL ) goto end_of_job; /* and quit if failed */ + == NULL ) goto end_of_job; } /* and quit if failed */ /* --- stack operator+superscript atop subscript --- */ -if ( issub ) /* we have a subscript */ +if ( issub ) { /* we have a subscript */ if ( sp == NULL ) /* but no base expression */ sp = subsp; /* so just use subscript */ else /* have base and subscript */ if ( (sp=rastack(subsp,sp,2,vspace,1,3)) /* stack sp atop base subsp */ - == NULL ) goto end_of_job; /* and quit if failed */ + == NULL ) goto end_of_job; } /* and quit if failed */ sp->type = IMAGERASTER; /* flip type of composite object */ sp->size = size; /* and set font size */ /* ------------------------------------------------------------------------- @@ -5259,7 +6705,7 @@ end_of_job: * basesp (I) subraster * to character (or subexpression) * immediately preceding leading left{ * (unused, but passed for consistency) - * ildelim (I) int containing ldelims index of + * ildelim (I) int containing ldelims[] index of * left delimiter * arg2 (I) int unused * arg3 (I) int unused @@ -5284,15 +6730,18 @@ int family=CMSYEX, /* get_delim() fami height=0, rheight=0, /* subexpr, right delim height */ margin=(size+1), /* delim height margin over subexpr*/ opmargin=(5); /* extra margin for \int,\sum,\etc */ -char subexpr[8192]; /* chars between \left...\right */ +char /* *texleft(),*/ subexpr[MAXSUBXSZ+1];/*chars between \left...\right*/ char *texchar(), /* get delims after \left,\right */ ldelim[256]=".", rdelim[256]="."; /* delims following \left,\right */ char *strtexchr(), *pleft, *pright; /*locate \right matching our \left*/ int isleftdot=0, isrightdot=0; /* true if \left. or \right. */ +int isleftscript=0, isrightscript=0; /* true if delims are scripted */ int sublen=0; /* strlen(subexpr) */ int idelim=0; /* 1=left,2=right */ +/* int gotldelim = 0; */ /* true if ildelim given by caller */ int delete_subraster(); /* free subraster if rastleft fails*/ int wasdisplaystyle = isdisplaystyle; /* save current displaystyle */ +int istextleft=0, istextright=0; /* true for non-displaystyle delims*/ /* --- recognized delimiters --- */ static char left[16]="\\left", right[16]="\\right"; /* tex delimiters */ static char *ldelims[] = { @@ -5329,6 +6778,15 @@ static char *xto[] = /* ...to this ins "<", /* \langle to < */ ">", /* \rangle to > */ NULL } ; /* --- end-of-xto[] --- */ +/* --- non-displaystyle delimiters --- */ +static char *textdelims[] = /* these delims _aren't_ display */ + { "|", "=", + "(", ")", + "[", "]", + "<", ">", + "{", "}", + "dbl", /* \lbrackdbl and \rbrackdbl */ + NULL } ; /* --- end-of-textdelims[] --- */ /* ------------------------------------------------------------------------- initialization -------------------------------------------------------------------------- */ @@ -5336,10 +6794,15 @@ initialization if ( *(*expression) == '\000' ) goto end_of_job; /* nothing after \left */ /* --- determine left delimiter, and set default \right. delimiter --- */ if ( ildelim!=NOVALUE && ildelim>=1 ) /* called with explicit left delim */ - strcpy(ldelim,ldelims[ildelim]); /* so just get a local copy */ + { strcpy(ldelim,ldelims[ildelim]); /* so just get a local copy */ + /* gotldelim = 1; */ } /* and set flag that we got it */ else /* trapped \left without delim */ { skipwhite(*expression); /* interpret \left ( as \left( */ - *expression = texchar(*expression,ldelim); } /*pull delim from expression*/ + if ( *(*expression) == '\000' ) /* end-of-string after \left */ + goto end_of_job; /* so return NULL */ + *expression = texchar(*expression,ldelim); /*pull delim from expression*/ + if ( *expression == NULL /* probably invalid end-of-string */ + || *ldelim == '\000' ) goto end_of_job; } /* no delimiter */ strcpy(rdelim,"."); /* init default \right. delim */ /* ------------------------------------------------------------------------- locate \right balancing our opening \left @@ -5364,7 +6827,10 @@ if ( (pright=strtexchr(*expression,right push past \left(_a^b sub/superscripts, if present -------------------------------------------------------------------------- */ pleft = *expression; /*reset pleft after opening \left( */ -/*lp=*/ rastlimits(expression,size,lp); /*dummy call push expression past b*/ +if ( (lp=rastlimits(expression,size,lp)) /*dummy call push expression past b*/ +!= NULL ) /* found actual _a^b scripts, too */ + { delete_subraster(lp); /* but we don't need them */ + lp = NULL; } /* reset pointer, too */ /* ------------------------------------------------------------------------- get \right delimiter and subexpression between \left...\right, xlate delims -------------------------------------------------------------------------- */ @@ -5384,6 +6850,9 @@ else { /* have explicit matching \ri /* --- get subexpression between \left...\right --- */ if ( sublen < 1 ) goto end_of_job; /* nothing between delimiters */ subexpr[sublen] = '\000'; /* and null-terminate it */ +/* --- adjust margin for expressions containing \middle's --- */ +if ( strtexchr(subexpr,"\\middle") != NULL ) /* have enclosed \middle's */ + margin = 1; /* so don't "overwhelm" them */ /* --- check for operator delimiter --- */ for ( idelim=0; opdelims[idelim]!=NULL; idelim++ ) if ( strstr(ldelim,opdelims[idelim]) != NULL ) /* found operator */ @@ -5391,7 +6860,7 @@ for ( idelim=0; opdelims[idelim]!=NULL; if ( *ldelim == '\\' ) /* have leading escape */ strcpy(ldelim,ldelim+1); /* squeeze it out */ break; } /* no need to check rest of table */ -/* --- xlate delimiters --- */ +/* --- xlate delimiters and check for textstyle --- */ for ( idelim=1; idelim<=2; idelim++ ) { /* 1=left, 2=right */ char *lrdelim = (idelim==1? ldelim:rdelim); /* ldelim or rdelim */ int ix; char *xdelim; /* xfrom[] and xto[] index, delim */ @@ -5399,6 +6868,12 @@ for ( idelim=1; idelim<=2; idelim++ ) { if ( strcmp(lrdelim,xdelim) == 0 ) /* found delim to xlate */ { strcpy(lrdelim,xto[ix]); /* replace with corresponding xto[]*/ break; } /* no need to check further */ + for( ix=0; (xdelim=textdelims[ix]) != NULL; ix++ ) + if ( strstr(lrdelim,xdelim) != 0 ) /* found textstyle delim */ + { if ( idelim == 1 ) /* if it's the \left one */ + istextleft = 1; /* set left textstyle flag */ + else istextright = 1; /* else set right textstyle flag */ + break; } /* no need to check further */ } /* --- end-of-for(idelim) --- */ /* --- debugging --- */ if ( msgfp!=NULL && msglevel>=99 ) @@ -5419,7 +6894,7 @@ rasterize delimiters, reset baselines, a isleftdot = (strchr(ldelim,'.')!=NULL); /* true if \left. */ isrightdot = (strchr(rdelim,'.')!=NULL); /* true if \right. */ /* --- get rasters for best-fit delim characters, add sub/superscripts --- */ -isdisplaystyle = 9; /* force \displaystyle */ +isdisplaystyle = (istextleft?0:9); /* force \displaystyle */ if ( !isleftdot ) /* if not \left. */ { /* --- first get requested \left delimiter --- */ lp = get_delim(ldelim,rheight,family); /* get \left delim char */ @@ -5430,8 +6905,9 @@ if ( !isleftdot ) /* if not \left. */ if ( lheight > rheight ) /* got bigger delim than requested */ rheight = lheight-1; } /* make sure right delim matches */ /* --- then add on any sub/superscripts attached to \left( --- */ - lp = rastlimits(&pleft,size,lp); } /*\left(_a^b and push pleft past b*/ -isdisplaystyle = 9; /* force \displaystyle */ + lp = rastlimits(&pleft,size,lp); /*\left(_a^b and push pleft past b*/ + isleftscript = isscripted; } /* check if left delim scripted */ +isdisplaystyle = (istextright?0:9); /* force \displaystyle */ if ( !isrightdot ) /* and if not \right. */ { /* --- first get requested \right delimiter --- */ rp = get_delim(rdelim,rheight,family); /* get \right delim char */ @@ -5439,7 +6915,8 @@ if ( !isrightdot ) /* and if not \righ if ( rp != NULL ) /* if get_delim() succeeded */ rp->baseline = sp->baseline + ((rp->image)->height - height)/2; /* --- then add on any sub/superscripts attached to \right) --- */ - rp = rastlimits(expression,size,rp); } /*\right)_c^d, expression past d*/ + rp = rastlimits(expression,size,rp); /*\right)_c^d, expression past d*/ + isrightscript = isscripted; } /* check if right delim scripted */ isdisplaystyle = wasdisplaystyle; /* original \displystyle default */ /* --- check that we got delimiters --- */ if ( 0 ) @@ -5461,11 +6938,168 @@ if ( sp != NULL ) /* succeeded or igno sp = rastcat(sp,rp,3); /* concat sp||rp and free sp,rp */ /* --- back to caller --- */ end_of_job: + isdelimscript = isrightscript; /* signal if right delim scripted */ return ( sp ); } /* --- end-of-function rastleft() --- */ /* ========================================================================== + * Function: rastright ( expression, size, basesp, ildelim, arg2, arg3 ) + * Purpose: ...\right handler, intercepts an unexpected/unbalanced \right + * -------------------------------------------------------------------------- + * Arguments: expression (I) char ** to first char of null-terminated + * string beginning with a \right + * to be rasterized + * size (I) int containing 0-5 default font size + * basesp (I) subraster * to character (or subexpression) + * immediately preceding leading left{ + * (unused, but passed for consistency) + * ildelim (I) int containing rdelims[] index of + * right delimiter + * arg2 (I) int unused + * arg3 (I) int unused + * -------------------------------------------------------------------------- + * Returns: ( subraster * ) ptr to subraster corresponding to subexpr, + * or NULL for any parsing error + * -------------------------------------------------------------------------- + * Notes: o + * ======================================================================= */ +/* --- entry point --- */ +subraster *rastright ( char **expression, int size, subraster *basesp, + int ildelim, int arg2, int arg3 ) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +subraster /* *rasterize(),*/ *sp=NULL; /*rasterize \right subexpr's*/ + if ( sp != NULL ) /* returning entire expression */ + { + isreplaceleft = 1; /* set flag to replace left half*/ + } +return ( sp ); +} /* --- end-of-function rastright() --- */ + + +/* ========================================================================== + * Function: rastmiddle ( expression, size, basesp, arg1, arg2, arg3 ) + * Purpose: \middle handler, returns subraster corresponding to + * entire expression with \middle delimiter(s) sized to fit. + * -------------------------------------------------------------------------- + * Arguments: expression (I/O) char ** to first char of null-terminated + * string immediately following \middle to be + * rasterized, and returning ptr immediately + * to terminating null. + * size (I) int containing 0-5 default font size + * basesp (I) subraster * to character (or subexpression) + * immediately preceding \middle + * (unused, but passed for consistency) + * arg1 (I) int unused + * arg2 (I) int unused + * arg3 (I) int unused + * -------------------------------------------------------------------------- + * Returns: ( subraster * ) ptr to subraster corresponding to expression, + * or NULL for any parsing error + * (expression ptr unchanged if error occurs) + * -------------------------------------------------------------------------- + * Notes: o + * ======================================================================= */ +/* --- entry point --- */ +subraster *rastmiddle ( char **expression, int size, subraster *basesp, + int arg1, int arg2, int arg3 ) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +subraster *rasterize(), *sp=NULL, *subsp[32]; /*rasterize \middle subexpr's*/ +char *exprptr = *expression, /* local copy of ptr to expression */ + *texchar(), delim[32][132], /* delimiters following \middle's */ + *strtexchr(), /* locate \middle's */ + subexpr[MAXSUBXSZ+1], *subptr=NULL;/*subexpression between \middle's*/ +int height=0, habove=0, hbelow=0; /* height, above & below baseline */ +int idelim, ndelims=0, /* \middle count (max 32) */ + family = CMSYEX; /* delims from CMSY10 or CMEX10 */ +subraster *subrastcpy(), /* copy subraster */ + *rastcat(), /* concatanate subraster */ + *get_delim(); /* get rasterized delimiter */ +int delete_subraster(); /* free work area subsp[]'s at eoj */ +/* ------------------------------------------------------------------------- +initialization +-------------------------------------------------------------------------- */ +subsp[0] = leftexpression; /* expressn preceding 1st \middle */ +subsp[1] = NULL; /* set first null */ +/* ------------------------------------------------------------------------- +accumulate subrasters between consecutive \middle\delim...\middle\delim...'s +-------------------------------------------------------------------------- */ +while ( ndelims < 30 ) /* max of 31 \middle's */ + { + /* --- maintain max height above,below baseline --- */ + if ( subsp[ndelims] != NULL ) /*exprssn preceding current \middle*/ + { int baseline = (subsp[ndelims])->baseline; /* #rows above baseline */ + height = ((subsp[ndelims])->image)->height; /* tot #rows (height) */ + habove = max2(habove,baseline); /* max #rows above baseline */ + hbelow = max2(hbelow,height-baseline); } /* max #rows below baseline */ + /* --- get delimter after \middle --- */ + skipwhite(exprptr); /*skip space betwn \middle & \delim*/ + exprptr = texchar(exprptr,delim[ndelims]); /* \delim after \middle */ + if ( *(delim[ndelims]) == '\000' ) /* \middle at end-of-expression */ + break; /* ignore it and consider job done */ + ndelims++; /* count another \middle\delim */ + /* --- get subexpression between \delim and next \middle --- */ + subsp[ndelims] = NULL; /* no subexpresion yet */ + if ( *exprptr == '\000' ) /* end-of-expression after \delim */ + break; /* so we have all subexpressions */ + if ( (subptr = strtexchr(exprptr,"\\middle")) /* find next \middle */ + == NULL ) /* no more \middle's */ + { strncpy(subexpr,exprptr,MAXSUBXSZ); /*get entire remaining expression*/ + subexpr[MAXSUBXSZ] = '\000'; /* make sure it's null-terminated */ + exprptr += strlen(exprptr); } /* push exprptr to terminating '\0'*/ + else /* have another \middle */ + { int sublen = (int)(subptr-exprptr); /* #chars between \delim...\middle*/ + memcpy(subexpr,exprptr,min2(sublen,MAXSUBXSZ)); /* get subexpression */ + subexpr[min2(sublen,MAXSUBXSZ)] = '\000'; /* and null-terminate it */ + exprptr += (sublen+strlen("\\middle")); } /* push exprptr past \middle*/ + /* --- rasterize subexpression --- */ + subsp[ndelims] = rasterize(subexpr,size); /* rasterize subexpresion */ + } /* --- end-of-while(1) --- */ +/* ------------------------------------------------------------------------- +construct \middle\delim's and concatanate them between subexpressions +-------------------------------------------------------------------------- */ +if ( ndelims < 1 /* no delims */ +|| (height=habove+hbelow) < 1 ) /* or no subexpressions? */ + goto end_of_job; /* just flush \middle directive */ +for ( idelim=0; idelim<=ndelims; idelim++ ) + { + /* --- first add on subexpression preceding delim --- */ + if ( subsp[idelim] != NULL ) { /* have subexpr preceding delim */ + if ( sp == NULL ) /* this is first piece */ + { sp = subsp[idelim]; /* so just use it */ + if ( idelim == 0 ) sp = subrastcpy(sp); } /* or copy leftexpression */ + else sp = rastcat(sp,subsp[idelim],(idelim>0?3:1)); } /* or concat it */ + /* --- now construct delimiter --- */ + if ( *(delim[idelim]) != '\000' ) /* have delimter */ + { subraster *delimsp = get_delim(delim[idelim],height,family); + if ( delimsp != NULL ) /* rasterized delim */ + { delimsp->baseline = habove; /* set baseline */ + if ( sp == NULL ) /* this is first piece */ + sp = delimsp; /* so just use it */ + else sp = rastcat(sp,delimsp,3); } } /*or concat to existing pieces*/ + } /* --- end-of-for(idelim) --- */ +/* --- back to caller --- */ +end_of_job: + if ( 0 ) /* now handled above */ + for ( idelim=1; idelim<=ndelims; idelim++ ) /* free subsp[]'s (not 0) */ + if ( subsp[idelim] != NULL ) /* have allocated subraster */ + delete_subraster(subsp[idelim]); /* so free it */ + if ( sp != NULL ) /* returning entire expression */ + { int newht = (sp->image)->height; /* height of returned subraster */ + sp->baseline = min2(newht-1,newht/2+5); /* guess new baseline */ + isreplaceleft = 1; /* set flag to replace left half*/ + *expression += strlen(*expression); } /* and push to terminating null*/ + return ( sp ); +} /* --- end-of-function rastmiddle() --- */ + + +/* ========================================================================== * Function: rastflags ( expression, size, basesp, flag, value, arg3 ) * Purpose: sets an internal flag, e.g., for \rm, or sets an internal * value, e.g., for \unitlength=, and returns NULL @@ -5476,8 +7110,8 @@ end_of_job: * size (I) int containing base font size (not used, * just stored in subraster) * basesp (I) subraster * to character (or subexpression) - * immediately preceding space, whose baseline - * and height params are transferred to space + * immediately preceding "flags" directive + * (unused but passed for consistency) * flag (I) int containing #define'd symbol specifying * internal flag to be set * value (I) int containing new value of flag @@ -5499,7 +7133,7 @@ char *texsubexpr(), /* parse expressio int argvalue=NOVALUE, /* atoi(valuearg) */ isdelta=0, /* true if + or - precedes valuearg */ valuelen=0; /* strlen(valuearg) */ -double strtod(); /*convert ascii {valuearg} to double*/ +double dblvalue=(-99.), strtod(); /*convert ascii {valuearg} to double*/ static int displaystylelevel = (-99); /* \displaystyle set at recurlevel */ /* ------------------------------------------------------------------------- set flag or value @@ -5507,10 +7141,10 @@ set flag or value switch ( flag ) { default: break; /* unrecognized flag */ - case ISTEXT: + case ISFONTFAM: if ( isthischar((*(*expression)),WHITEMATH) ) /* \rm followed by white */ (*expression)++; /* skip leading ~ after \rm */ - istext=value; /* set text mode */ + fontnum = value; /* set font family */ break; case ISSTRING: isstring=value; break; /* set string/image mode */ case ISDISPLAYSTYLE: /* set \displaystyle mode */ @@ -5524,6 +7158,8 @@ switch ( flag ) { bgred=255-bgred; bggreen=255-bggreen; bgblue=255-bgblue; } if ( value==2 || value==NOVALUE ) isblackonwhite = !isblackonwhite; + if ( gammacorrection > 0.0001 ) /* have gamma correction */ + gammacorrection = REVERSEGAMMA; /* use reverse video gamma instead */ break; case ISSUPER: /* set supersampling/lowpass flag */ #ifndef SSFONTS /* don't have ss fonts loaded */ @@ -5534,6 +7170,7 @@ switch ( flag ) break; case ISFONTSIZE: /* set fontsize */ case ISDISPLAYSIZE: /* set displaysize */ + case ISCONTENTTYPE: /*enable/disable content-type lines*/ case ISSHRINK: /* set shrinkfactor */ case ISAAALGORITHM: /* set anti-aliasing algorithm */ case ISWEIGHT: /* set font weight */ @@ -5541,9 +7178,11 @@ switch ( flag ) case ISADJACENTWT: /* set lowpass adjacent weight */ case ISCORNERWT: /* set lowpass corner weight */ case ISCOLOR: /* set red(1),green(2),blue(3) */ - case ISSQUASH: /* set (minimum) "squash" margin */ + case ISSMASH: /* set (minimum) "smash" margin */ + case ISGAMMA: /* set gamma correction */ if ( value != NOVALUE ) /* passed a fixed value to be set */ - argvalue = value; /* set given fixed value */ + { argvalue = value; /* set given fixed int value */ + dblvalue = (double)value; } /* or maybe interpreted as double */ else /* get value from expression */ { *expression = texsubexpr(*expression,valuearg,1023,"{","}",0,0); if ( *valuearg != '\000' ) /* guard against empty string */ @@ -5552,7 +7191,12 @@ switch ( flag ) { isdelta = isthischar(*valuearg,"+-"); /* leading + or - */ if ( memcmp(valuearg,"--",2) == 0 ) /* leading -- signals...*/ { isdelta=0; strcpy(valuearg,valuearg+1); } /* ...not delta */ - argvalue = atoi(valuearg); } } /* convert to int */ + switch ( flag ) { /* convert to double or int */ + default: argvalue = atoi(valuearg); break; /* convert to int */ + case ISGAMMA: + dblvalue = strtod(valuearg,NULL); break; } /* or to double */ + } /* --- end-of-if(*valuearg!='?') --- */ + } /* --- end-of-if(value==NOVALUE) --- */ switch ( flag ) { default: break; @@ -5578,10 +7222,14 @@ switch ( flag ) fontsize = (isdelta? fontsize+argvalue : argvalue); fontsize = max2(0,min2(fontsize,largestsize)); shrinkfactor = shrinkfactors[fontsize]; - if ( isdisplaystyle == 1 ) /* displaystyle enabled but not set*/ + if ( isdisplaystyle == 1 /* displaystyle enabled but not set*/ + || (1 && isdisplaystyle==2) /* displaystyle enabled and set */ + || (0 && isdisplaystyle==0) )/*\textstyle disabled displaystyle*/ if ( displaystylelevel != recurlevel ) /*respect \displaystyle*/ - if ( !ispreambledollars ) /* respect $$...$$'s */ - isdisplaystyle = (fontsize>=displaysize? 2:1); /* forced */ + if ( !ispreambledollars ) { /* respect $$...$$'s */ + if ( fontsize >= displaysize ) + isdisplaystyle = 2; /* forced */ + else isdisplaystyle = 1; } /*displaystylelevel = (-99);*/ } /* reset \displaystyle level */ else /* embed font size in expression */ { sprintf(valuearg,"%d",fontsize); /* convert size */ @@ -5594,12 +7242,17 @@ switch ( flag ) if ( argvalue != NOVALUE ) /* got a value */ displaysize = (isdelta? displaysize+argvalue : argvalue); break; - case ISSQUASH: /* set (minimum) "squash" margin */ + case ISCONTENTTYPE: /*enable/disable content-type lines*/ + if ( argvalue != NOVALUE ) /* got a value */ + isemitcontenttype = (argvalue>0?1:0); + break; + case ISSMASH: /* set (minimum) "smash" margin */ if ( argvalue != NOVALUE ) /* got a value */ - { squashmargin = argvalue; /* set value */ + { smashmargin = argvalue; /* set value */ if ( arg3 != NOVALUE ) isdelta=arg3; /* hard-coded isdelta */ - issquashdelta = (isdelta?1:0); } /* and set delta flag */ - squashmargin = max2((isdelta?-5:0),min2(squashmargin,32)); /*sanity*/ + issmashdelta = (isdelta?1:0); } /* and set delta flag */ + smashmargin = max2((isdelta?-5:0),min2(smashmargin,32)); /*sanity*/ + isexplicitsmash = 1; /* signal explicit \smash directive*/ break; case ISSHRINK: /* set shrinkfactor */ if ( argvalue != NOVALUE ) /* got a value */ @@ -5607,9 +7260,11 @@ switch ( flag ) shrinkfactor = max2(1,min2(shrinkfactor,27)); /* sanity check */ break; case ISAAALGORITHM: /* set anti-aliasing algorithm */ - if ( argvalue != NOVALUE ) /* got a value */ - aaalgorithm = argvalue; /* set algorithm number */ - aaalgorithm = max2(0,min2(aaalgorithm,3)); /* bounds check */ + if ( argvalue != NOVALUE ) { /* got a value */ + if ( argvalue >= 0 ) { /* non-negative to set algorithm */ + aaalgorithm = argvalue; /* set algorithm number */ + aaalgorithm = max2(0,min2(aaalgorithm,4)); } /* bounds check */ + else maxfollow = abs(argvalue); } /* or maxfollow=abs(negative#) */ break; case ISWEIGHT: /* set font weight number */ value = (argvalue==NOVALUE? NOVALUE : /* don't have a value */ @@ -5638,6 +7293,10 @@ switch ( flag ) if ( argvalue != NOVALUE ) /* got a value */ cornerwt = argvalue; /* set lowpass corner weight */ break; + case ISGAMMA: /* set gamma correction */ + if ( dblvalue >= 0.0 ) /* got a value */ + gammacorrection = dblvalue; /* set gamma correction */ + break; } /* --- end-of-switch() --- */ break; case PNMPARAMS: /*set fgalias,fgonly,bgalias,bgonly*/ @@ -5680,6 +7339,8 @@ return ( NULL ); /*just set value, not * width (I) int containing #bits/pixels for space width * isfill (I) int containing true to \hfill complete * expression out to width + * (Kludge: isfill=99 signals \hspace* + * for negative space) * isheight (I) int containing true (but not NOVALUE) * to treat width arg as height * -------------------------------------------------------------------------- @@ -5696,28 +7357,64 @@ subraster *rastspace ( char **expression Allocations and Declarations -------------------------------------------------------------------------- */ subraster *new_subraster(), *spacesp=NULL; /* subraster for space */ +raster *bp=NULL, *backspace_raster(); /* for negative space */ +int delete_subraster(); /* if fail, free unneeded subraster*/ int baseht=1, baseln=0; /* height,baseline of base symbol */ int pixsz = 1; /*default #bits per pixel, 1=bitmap*/ +int isstar=0, minspace=0; /* defaults for negative hspace */ char *texsubexpr(), widtharg[256]; /* parse for optional {width} */ subraster *rasterize(), *rightsp=NULL; /*rasterize right half of expression*/ subraster *rastcat(); /* cat rightsp after \hfill */ -int blanksignal = (-991234); /*rastsquash signal right-hand blank*/ /* ------------------------------------------------------------------------- initialization -------------------------------------------------------------------------- */ +if ( isfill > 1 ) { isstar=1; isfill=0; } /* large fill signals \hspace* */ if ( isfill == NOVALUE ) isfill=0; /* novalue means false */ if ( isheight == NOVALUE ) isheight=0; /* novalue means false */ +minspace = (isstar?(-1):0); /* reset default minspace */ /* ------------------------------------------------------------------------- determine width if not given (e.g., \hspace{width}, \hfill{width}) -------------------------------------------------------------------------- */ -if ( width <= 0 ) /* width specified in expression */ - { int widthval; /* test {width} before using it */ - width = 1; /* set default width */ - *expression = texsubexpr(*expression,widtharg,255,"{","}",0,0); - widthval = /* convert {width} to integer */ - (int)((unitlength*strtod(widtharg,NULL))+0.5); - if ( widthval>=2 && widthval<=600 ) /* sanity check */ - width = widthval; } /* replace deafault width */ +if ( width == 0 ) { /* width specified in expression */ + double dwidth; int widthval; /* test {width} before using it */ + int minwidth = (isfill||isheight?1:-600); /* \hspace allows negative */ + /* --- check if optional [minspace] given for negative \hspace --- */ + if ( *(*expression) == '[' ) { /* [minspace] if leading char is [ */ + /* ---parse [minspace], bump expression past it, interpret as double--- */ + *expression = texsubexpr(*expression,widtharg,127,"[","]",0,0); + if ( *widtharg != '\000' ) /* got [minspace] */ + minspace = iround(unitlength*strtod(widtharg,NULL)); /* in pixels */ + } /* --- end-of-if(*(*expression)=='[') --- */ + width = 1; /* set default width */ + *expression = texsubexpr(*expression,widtharg,255,"{","}",0,0); + dwidth = unitlength*strtod(widtharg,NULL); /* scaled width value */ + widthval = /* convert {width} to integer */ + (int)( dwidth + (dwidth>=0.0?0.5:(-0.5)) ); + if ( widthval>=minwidth && widthval<=600 ) /* sanity check */ + width = widthval; /* replace deafault width */ + } /* --- end-of-if(width==0) --- */ +/* ------------------------------------------------------------------------- +first check for negative space +-------------------------------------------------------------------------- */ +if ( width < 0 ) { /* have negative hspace */ + if ( leftexpression != (subraster *)NULL ) /* can't backspace */ + if ( (spacesp=new_subraster(0,0,0)) /* get new subraster for backspace */ + != NULL ) { /* and if we succeed... */ + int nback=(-width), pback; /*#pixels wanted,actually backspaced*/ + if ( (bp=backspace_raster(leftexpression->image,nback,&pback,minspace,0)) + != NULL ) { /* and if backspace succeeds... */ + spacesp->image = bp; /* save backspaced image */ + /*spacesp->type = leftexpression->type;*/ /* copy original type */ + spacesp->type = blanksignal; /* need to propagate blanks */ + spacesp->size = leftexpression->size; /* copy original font size */ + spacesp->baseline = leftexpression->baseline; /* and baseline */ + blanksymspace += -(nback-pback); /* wanted more than we got */ + isreplaceleft = 1; } /*signal to replace entire expressn*/ + else { /* backspace failed */ + delete_subraster(spacesp); /* free unneeded envelope */ + spacesp = (subraster *)NULL; } } /* and signal failure */ + goto end_of_job; + } /* --- end-of-if(width<0) --- */ /* ------------------------------------------------------------------------- see if width is "absolute" or fill width -------------------------------------------------------------------------- */ @@ -5745,6 +7442,7 @@ if ( width > 0 ) /*make sure we have p != NULL ) /* and if we succeed... */ { /* --- ...re-init subraster parameters --- */ spacesp->size = size; /*propagate base font size forward*/ + if(1)spacesp->type = blanksignal; /* need to propagate blanks (???) */ spacesp->baseline = baseln; } /* ditto baseline */ /* ------------------------------------------------------------------------- concat right half if \hfill-ing @@ -5754,7 +7452,8 @@ if ( rightsp != NULL ) /* we have a ri rastcat(spacesp,rightsp,3)); /* or cat right half after space */ spacesp->type = blanksignal; /* need to propagate blanks */ *expression += strlen((*expression)); } /* push expression to its null */ -return ( spacesp ); +end_of_job: + return ( spacesp ); } /* --- end-of-function rastspace() --- */ @@ -5767,9 +7466,9 @@ return ( spacesp ); * string immediately following \\ to be * rasterized, and returning ptr immediately * to terminating null. - * size (I) int containing 0-4 default font size + * size (I) int containing 0-5 default font size * basesp (I) subraster * to character (or subexpression) - * immediately preceding \accent + * immediately preceding \\ * (unused, but passed for consistency) * arg1 (I) int unused * arg2 (I) int unused @@ -5812,7 +7511,8 @@ rasterize right half of expression and s if ( (rightsp=rasterize(*expression,size)) /* rasterize right half */ == NULL ) goto end_of_job; /* quit if failed */ /* --- stack left half above it --- */ -newlsp = rastack(rightsp,leftexpression,1,vspace,0,3); /*right under left*/ +/*newlsp = rastack(rightsp,leftexpression,1,vspace,0,3);*//*right under left*/ +newlsp = rastack(rightsp,leftexpression,1,vspace,0,1); /*right under left*/ /* --- back to caller --- */ end_of_job: if ( newlsp != NULL ) /* returning entire expression */ @@ -6156,7 +7856,7 @@ subraster *rastfrac ( char **expression, Allocations and Declarations -------------------------------------------------------------------------- */ char *texsubexpr(), /*parse expression for numer,denom*/ - numer[8192], denom[8192]; /*numer,denom parsed from expression*/ + numer[MAXSUBXSZ+1], denom[MAXSUBXSZ+1]; /* parsed numer, denom */ subraster *rasterize(), *numsp=NULL, *densp=NULL; /*rasterize numer, denom*/ subraster *rastack(), *fracsp=NULL; /* subraster for numer/denom */ subraster *new_subraster()/*, *spacesp=NULL*/; /* space for num or den */ @@ -6166,7 +7866,7 @@ int baseht=0, baseln=0; /* height,basel /*int istweak = 1;*/ /*true to tweak baseline alignment*/ int rule_raster(), /* draw horizontal line for frac */ lineheight = 1; /* thickness of fraction line */ -int vspace = 1; /*vertical space between components*/ +int vspace = (size>2?2:1); /*vertical space between components*/ int delete_subraster(); /*free work areas in case of error*/ int type_raster(); /* display debugging output */ /* ------------------------------------------------------------------------- @@ -6213,6 +7913,7 @@ width = (fracsp->image)->width; /*just /* --- initialize subraster parameters --- */ fracsp->size = size; /* propagate font size forward */ fracsp->baseline = (numheight+vspace+lineheight)+(size+2);/*default baseline*/ +fracsp->type = FRACRASTER; /* signal \frac image */ if ( basesp != (subraster *)NULL ) /* we have base symbol for frac */ { baseht = (basesp->image)->height; /* height of base symbol */ baseln = basesp->baseline; /* and its baseline */ @@ -6220,8 +7921,9 @@ if ( basesp != (subraster *)NULL ) /* we /* ------------------------------------------------------------------------- draw horizontal line between numerator and denominator -------------------------------------------------------------------------- */ +fraccenterline = numheight+vspace; /* signal that we have a \frac */ if ( isfrac ) /*line for \frac, but not for \atop*/ - rule_raster(fracsp->image,numheight+vspace,0,width,lineheight,0); + rule_raster(fracsp->image,fraccenterline,0,width,lineheight,0); /* ------------------------------------------------------------------------- return final result to caller -------------------------------------------------------------------------- */ @@ -6264,8 +7966,8 @@ subraster *rastackrel ( char **expressio /* ------------------------------------------------------------------------- Allocations and Declarations -------------------------------------------------------------------------- */ -char *texsubexpr(), /*parse expression for numer,denom*/ - upper[8192], lower[8192]; /*upper,lower parsed from expression*/ +char *texsubexpr(), /*parse expression for upper,lower*/ + upper[MAXSUBXSZ+1], lower[MAXSUBXSZ+1]; /* parsed upper, lower */ subraster *rasterize(), *upsp=NULL, *lowsp=NULL; /* rasterize upper, lower */ subraster *rastack(), *relsp=NULL; /* subraster for upper/lower */ int upsize = (base==1? size:size-1), /* font size for upper component */ @@ -6337,9 +8039,9 @@ subraster *rastmathfunc ( char **express Allocations and Declarations -------------------------------------------------------------------------- */ char *texscripts(), /* parse expression for _limits */ - func[4096], limits[8192]; /* func as {\rm func}, limits */ + func[MAXTOKNSZ+1], limits[MAXSUBXSZ+1]; /*func as {\rm func}, limits*/ char *texsubexpr(), /* parse expression for arg */ - funcarg[2048]; /* optional func arg */ + funcarg[MAXTOKNSZ+1]; /* optional func arg */ subraster *rasterize(), *funcsp=NULL, *limsp=NULL; /*rasterize func,limits*/ subraster *rastack(), *mathfuncsp=NULL; /* subraster for mathfunc/limits */ int limsize = size-1; /* font size for limits */ @@ -6443,8 +8145,8 @@ subraster *rastsqrt ( char **expression, /* ------------------------------------------------------------------------- Allocations and Declarations -------------------------------------------------------------------------- */ -char *texsubexpr(), subexpr[8192], /* parse subexpr to be sqrt-ed */ - rootarg[8192]; /* optional \sqrt[rootarg]{...} */ +char *texsubexpr(), subexpr[MAXSUBXSZ+1], /*parse subexpr to be sqrt-ed*/ + rootarg[MAXSUBXSZ+1]; /* optional \sqrt[rootarg]{...} */ subraster *rasterize(), *subsp=NULL; /* rasterize subexpr */ subraster *accent_subraster(), *sqrtsp=NULL, /* subraster with the sqrt */ *new_subraster(), *rootsp=NULL; /* optionally preceded by [rootarg]*/ @@ -6559,9 +8261,9 @@ subraster *rastaccent ( char **expressio /* ------------------------------------------------------------------------- Allocations and Declarations -------------------------------------------------------------------------- */ -char *texsubexpr(), subexpr[8192]; /* parse subexpr to be accented */ +char *texsubexpr(), subexpr[MAXSUBXSZ+1]; /*parse subexpr to be accented*/ char *texscripts(), *script=NULL, /* \under,overbrace allow scripts */ - subscript[512], supscript[512]; /* scripts parsed from expression */ + subscript[MAXTOKNSZ+1], supscript[MAXTOKNSZ+1]; /* parsed scripts */ subraster *rasterize(), *subsp=NULL, *scrsp=NULL; /*rasterize subexpr,script*/ subraster *rastack(), *accsubsp=NULL; /* stack accent, subexpr, script */ subraster *accent_subraster(), *accsp=NULL; /*raster for the accent itself*/ @@ -6588,13 +8290,15 @@ subwidth = (subsp->image)->width; /* an pixsz = (subsp->image)->pixsz; /* original pixsz remains constant */ /* --- determine desired width, height of accent --- */ accwidth = subwidth; /* same width as subexpr */ -accheight = 3; /* default for bars */ +accheight = 4; /* default for bars */ switch ( accent ) { default: break; /* default okay */ case DOTACCENT: case DDOTACCENT: - accheight = (size<3? 3:4); /* default for dots */ + accheight = (size<4? 3:4); /* default for dots */ break; - case HATACCENT: case VECACCENT: + case VECACCENT: + vspace = 1; /* set 1-pixel vertical space */ + case HATACCENT: accheight = 7; /* default */ if ( subwidth < 10 ) accheight = 5; /* unless small width */ else if ( subwidth > 25 ) accheight = 9; /* or large */ @@ -6640,7 +8344,7 @@ end_of_job: /* ========================================================================== - * Function: rastfont (expression,size,basesp,font,arg2,arg3) + * Function: rastfont (expression,size,basesp,ifontnum,arg2,arg3) * Purpose: \cal{}, \scr{}, \etc handler, returns subraster corresponding * to char(s) within {}'s rendered at size * -------------------------------------------------------------------------- @@ -6652,7 +8356,7 @@ end_of_job: * basesp (I) subraster * to character (or subexpression) * immediately preceding \accent * (unused, but passed for consistency) - * font (I) int containing 1 for \cal{}, 2 for \scr{} + * ifontnum (I) int containing 1 for \cal{}, 2 for \scr{} * arg2 (I) int unused * arg3 (I) int unused * -------------------------------------------------------------------------- @@ -6663,47 +8367,54 @@ end_of_job: * ======================================================================= */ /* --- entry point --- */ subraster *rastfont ( char **expression, int size, subraster *basesp, - int font, int arg2, int arg3 ) + int ifontnum, int arg2, int arg3 ) { /* ------------------------------------------------------------------------- Allocations and Declarations -------------------------------------------------------------------------- */ -char *texsubexpr(), fontchars[8192], /*parse chars to be rendered in font*/ - subexpr[8192]; /* turn \cal{AB} into \calA\calB */ +char *texsubexpr(), fontchars[MAXSUBXSZ+1], /* chars to render in font */ + subexpr[MAXSUBXSZ+1]; /* turn \cal{AB} into \calA\calB */ char *pfchars=fontchars, fchar='\0'; /* run thru fontchars one at a time*/ -char *name = NULL; /* fonts[font].name */ -int class = 0, /* fonts[font].class */ - istext = 0; /* set true for text type */ +char *name = NULL; /* fontinfo[ifontnum].name */ +int family = 0, /* fontinfo[ifontnum].family */ + istext = 0, /* fontinfo[ifontnum].istext */ + class = 0; /* fontinfo[ifontnum].class */ subraster *rasterize(), *fontsp=NULL, /* rasterize chars in font */ *rastflags(); /* or just set flag to switch font */ -int oldsquashmargin = squashmargin; /* turn off squash in text mode */ -int blanksignal = (-991234); /*rastsquash signal right-hand blank*/ +int oldsmashmargin = smashmargin; /* turn off smash in text mode */ +#if 0 /* --- fonts recognized by rastfont --- */ -static int nfonts = 5; /* legal font #'s are 1...nfonts */ +static int nfonts = 6; /* legal font #'s are 1...nfonts */ static struct {char *name; int class;} fonts[] = { /* --- name class 1=upper,2=alpha,3=alnum,4=lower,5=digit,9=all --- */ - { "\\badfont", 0 }, - { "\\cal", 1 }, /*(1) calligraphic, uppercase */ - { "\\scr", 1 }, /*(2) rsfs/script, uppercase */ - { "\\rm", -1 }, /*(3) \rm,\text{abc} --> {\rm~abc} */ - { "\\it", -1 }, /*(4) \it,\textit{abc}-->{\it~abc} */ - { "\\bb", -1 }, /*(5) \bb,\mathbb{abc}-->{\bb~abc} */ + { "\\math", 0 }, + { "\\mathcal", 1 }, /*(1) calligraphic, uppercase */ + { "\\mathscr", 1 }, /*(2) rsfs/script, uppercase */ + { "\\textrm", -1 }, /*(3) \rm,\text{abc} --> {\rm~abc} */ + { "\\textit", -1 }, /*(4) \it,\textit{abc}-->{\it~abc} */ + { "\\mathbb", -1 }, /*(5) \bb,\mathbb{abc}-->{\bb~abc} */ + { "\\mathbf", -1 }, /*(6) \bf,\mathbf{abc}-->{\bf~abc} */ + { "\\mathrm", -1 }, /*(7) \mathrm */ + { "\\cyr", -1 }, /*(8) \cyr */ { NULL, 0 } } ; /* --- end-of-fonts[] --- */ +#endif /* ------------------------------------------------------------------------- first get font name and class to determine type of conversion desired -------------------------------------------------------------------------- */ -if ( font<=0 || font>nfonts ) font=0; /* set error if out-of-bounds */ -name = fonts[font].name; /* font name */ -class = fonts[font].class; /* font class */ -if ( font==3 || font==4 ) /* text (respect blanks) */ - { istext = 1; /* signal text mode */ - squashmargin = 0; } /* don't squash internal blanks */ +if (ifontnum<=0 || ifontnum>nfontinfo) ifontnum=0; /*math if out-of-bounds*/ +name = fontinfo[ifontnum].name; /* font name */ +family = fontinfo[ifontnum].family; /* font family */ +istext = fontinfo[ifontnum].istext; /*true in text mode (respect space)*/ +class = fontinfo[ifontnum].class; /* font class */ +if ( istext ) /* text (respect blanks) */ + { mathsmashmargin = smashmargin; /* needed for \text{if $n-m$ even} */ + smashmargin = 0; } /* don't smash internal blanks */ /* ------------------------------------------------------------------------- now convert \font{abc} --> {\font~abc}, or convert ABC to \calA\calB\calC -------------------------------------------------------------------------- */ -if ( class < 0 ) /* not character-by-character */ +if ( 1 || class<0 ) /* not character-by-character */ { /* --- if \font not immediately followed by { then it has no arg, so just set flag @@ -6711,15 +8422,8 @@ if ( class < 0 ) /* not character-by-c if ( *(*expression) != '{' ) /* no \font arg, so just set flag */ { if ( msgfp!=NULL && msglevel>=99 ) - fprintf(msgfp,"rastfont> \\%s rastflags() for font#%d\n",name,font); - switch ( font ) /* set flag by our internal font# */ - { case 3: /* \rm, \text sets flag istext=1 */ - fontsp = rastflags(expression,size,basesp,ISTEXT,1,arg3); break; - case 4: /* \it, \text sets flag istext=2 */ - fontsp = rastflags(expression,size,basesp,ISTEXT,2,arg3); break; - case 5: /* \bb, \mathbb sets flag istext=3 */ - fontsp = rastflags(expression,size,basesp,ISTEXT,3,arg3); break; - default: break; } /* unrecognized, set no flags */ + fprintf(msgfp,"rastfont> \\%s rastflags() for font#%d\n",name,ifontnum); + fontsp = rastflags(expression,size,basesp,ISFONTFAM,ifontnum,arg3); goto end_of_job; } /* --- end-of-if(*(*expression)!='{') --- */ /* --- @@ -6752,11 +8456,12 @@ else /* character-by-character */ for ( pfchars=fontchars; (fchar= *pfchars)!='\000'; pfchars++ ) { if ( isthischar(fchar,WHITEMATH) ) /* some whitespace */ - { if ( 0 || istext ) /* and we're in a text mode */ + { if ( 0 || istext ) /* and we're in a text mode font */ strcat(subexpr,"\\;"); } /* so respect whitespace */ else /* char to be displayed in font */ { int exprlen = 0; /* #chars in subexpr before fchar */ int isinclass = 0; /* set true if fchar in font class */ + /* --- class: 1=upper, 2=alpha, 3=alnum, 4=lower, 5=digit, 9=all --- */ switch ( class ) /* check if fchar is in font class */ { default: break; /* no chars in unrecognized class */ case 1: if ( isupper((int)fchar) ) isinclass=1; break; @@ -6790,9 +8495,10 @@ if ( (fontsp = rasterize(subexpr,size)) back to caller with chars rendered in font -------------------------------------------------------------------------- */ end_of_job: - squashmargin = oldsquashmargin; /* restore squash */ - if ( istext && fontsp!=NULL ) /* raster contains text */ - fontsp->type = blanksignal; /* signal nosquash */ + smashmargin = oldsmashmargin; /* restore smash */ + mathsmashmargin = SMASHMARGIN; /* this one probably not necessary */ + if ( istext && fontsp!=NULL ) /* raster contains text mode font */ + fontsp->type = blanksignal; /* signal nosmash */ return ( fontsp ); /* chars rendered in font */ } /* --- end-of-function rastfont() --- */ @@ -6827,7 +8533,7 @@ subraster *rastbegin ( char **expression /* ------------------------------------------------------------------------- Allocations and Declarations -------------------------------------------------------------------------- */ -char *texsubexpr(), subexpr[8210], /* \begin{} environment paramaters */ +char *texsubexpr(), subexpr[MAXSUBXSZ+1], /* \begin{} environment params*/ *exprptr=NULL,*begptr=NULL,*endptr=NULL,*braceptr=NULL; /* ptrs */ char *begtoken="\\begin{", *endtoken="\\end{"; /*tokens we're looking for*/ int strreplace(); /* replace substring in string */ @@ -6840,7 +8546,7 @@ int envlen=0, sublen=0; /* #chars in en static int blevel = 0; /* \begin...\end nesting level */ static char *mdelims[] = { NULL, NULL, NULL, NULL, "()","[]","{}","||","==", /* for pbBvVmatrix */ - NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, "{.", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }; static char *environs[] = { /* types of environments we process*/ "eqnarray", /* 0 eqnarray environment */ @@ -6856,6 +8562,8 @@ static char *environs[] = { /* types of "align", /* 10 align environment */ "verbatim", /* 11 verbatim environment */ "picture", /* 12 picture environment */ + "cases", /* 13 cases environment */ + "equation", /* 14 for \begin{equation} */ NULL }; /* trailer */ /* ------------------------------------------------------------------------- determine type of environment we're beginning @@ -6917,6 +8625,12 @@ switch ( ienviron ) if ( *(subexpr+8) == '\000' ) goto end_of_job; } /* quit if no arg */ strcat(subexpr,"{"); /* opening { after (width,height) */ break; + case 13: /* cases */ + strcat(subexpr,"\\array{ll$"); /* a&b \\ c&d etc */ + break; + case 14: /* \begin{equation} */ + strcat(subexpr,"{"); /* just enclose expression in {}'s */ + break; } /* --- end-of-switch(ienviron) --- */ /* ------------------------------------------------------------------------- locate matching \end{...} @@ -7064,9 +8778,9 @@ subraster *rastarray ( char **expression /* ------------------------------------------------------------------------- Allocations and Declarations -------------------------------------------------------------------------- */ -char *texsubexpr(), subexpr[8210], *exprptr, /* parse array subexpr */ - subtok[4096], *subptr=subtok, /* & or \\ inside { }'s not a delim*/ - token[4096], *tokptr=token, /* token from subexpr to rasterize */ +char *texsubexpr(), subexpr[MAXSUBXSZ+1], *exprptr, /*parse array subexpr*/ + subtok[MAXTOKNSZ+1], *subptr=subtok, /* &,\\ inside { } not a delim*/ + token[MAXTOKNSZ+1], *tokptr=token, /* subexpr token to rasterize */ *preamble(), *preptr=token; /*process optional size,lcr preamble*/ char *coldelim="&", *rowdelim="\\"; /* need escaped rowdelim */ int maxarraysz = 64; /* max #rows, cols */ @@ -7213,9 +8927,9 @@ while ( *preptr != '\000' ) /* check p if ( prepcase != 0 ) /* only check upper,lowercase */ { int ispropagate = (*preptr=='+'?1:0); /* leading + propagates width/ht */ - if ( ispropagate ) /* set row or col propagation */ + if ( ispropagate ) { /* set row or col propagation */ if ( prepcase == 1 ) colpropagate = 1; /* propagating col values */ - else if ( prepcase == 2 ) rowpropagate = 1; /* propagating row values */ + else if ( prepcase == 2 ) rowpropagate = 1; } /*propagating row values*/ if ( !colpropagate && prepcase == 1 ) { colwidth[icol] = 0; /* reset colwidth */ fixcolsize[icol] = 0; } /* reset width flag */ @@ -7319,16 +9033,17 @@ while ( 1 ) /* scan chars till end */ tokptr=token; skipwhite(tokptr); /* skip whitespace after // */ tokptr = texchar(tokptr,hltoken); /* extract first char from token */ hltoklen = strlen(hltoken); /* length of first char */ - if ( hltoklen >= minhltoklen ) /*token must be at least \hl or \hd*/ + if ( hltoklen >= minhltoklen ) { /*token must be at least \hl or \hd*/ if ( memcmp(hlchar,hltoken,hltoklen) == 0 ) /* we have an \hline */ hline[nrows] += 1; /* bump \hline count for row */ else if ( memcmp(hdchar,hltoken,hltoklen) == 0 ) /*we have an \hdash*/ - hline[nrows] = (-1); /* set \hdash flag for row */ + hline[nrows] = (-1); } /* set \hdash flag for row */ if ( hline[nrows] != 0 ) /* \hline or \hdash prefixes token */ { skipwhite(tokptr); /* flush whitespace after \hline */ if ( *tokptr == '\000' /* end-of-expression after \hline */ || isthischar(*tokptr,coldelim) ) /* or unescaped coldelim */ - istokwhite = ishonly = 1; /* so token contains \hline only */ + { istokwhite = 1; /* so token contains \hline only */ + if ( iseox ) ishonly = 1; } /* ignore entire row at eox */ else /* token contains more than \hline */ strcpy(token,tokptr); } /* so flush \hline from token */ } /* --- end-of-if(ncols[nrows]==0) --- */ @@ -7452,6 +9167,9 @@ for ( irow=0; irow<=nrows; irow++ ) /*to for ( icol=0; icolbaseline;/*V offset (init for baseline)*/ /* --- adjust leftcol for vline to left of icol, if present ---- */ - leftcol += vlinespace(icol); /* space for vline to left of col */ + /*leftcol += vlinespace(icol);*/ /* space for vline to left of col */ /* --- reset justification (if not left-justified) --- */ if ( justify[icol] == 0 ) /* but user wants it centered */ tokencol = (cwidth-twidth+1)/2; /* so split margin left/right */ @@ -7735,7 +9453,7 @@ end_of_job: * string immediately following \line to be * rasterized, and returning ptr immediately * following last character processed. - * size (I) int containing 0-4 default font size + * size (I) int containing 0-7 default font size * basesp (I) subraster * to character (or subexpression) * immediately preceding \line * (unused, but passed for consistency) @@ -7761,10 +9479,12 @@ char *texsubexpr(),linexpr[257], *xptr=l subraster *new_subraster(), *linesp=NULL; /* subraster for line */ /*char *origexpression = *expression;*/ /*original expression after \line*/ int pixsz = 1; /* pixels are one bit each */ +int thickness = 1; /* line thickness */ double strtod(), /* convert ascii params to doubles */ xinc=0.0, yinc=0.0, /* x,y-increments for line, */ xlen=0.0, ylen=0.0; /* x,y lengths for line */ -int width=0, height=0; /* #pixels width,height of line */ +int width=0, height=0, /* #pixels width,height of line */ + rwidth=0, rheight=0; /*alloc width,height plus thickness*/ int istop=0, isright=0, /* origin at bot-left if x,yinc>=0 */ origin = 0; /* x,yinc: ++=00 +-=01 -+=10 --=11 */ int line_raster(); /* draw line in linesp->image */ @@ -7774,7 +9494,10 @@ obtain (xinc,yinc) arguments immediately /* --- parse for (xinc,yinc) arguments, and bump expression past it --- */ *expression = texsubexpr(*expression,linexpr,253,"(",")",0,0); if ( *linexpr == '\000' ) goto end_of_job; /* couldn't get (xinc,yinc) */ -/* --- now interpret xinc,yinc returned in linexpr --- */ +/* --- now interpret xinc,yinc;thickness returned in linexpr --- */ +if ( (xptr=strchr(linexpr,';')) != NULL ) /* look for ';' after xinc,yinc */ + { *xptr = '\000'; /* terminate linexpr at ; */ + thickness = (int)strtol(xptr+1,NULL,10); } /* get int thickness */ if ( (xptr=strchr(linexpr,',')) != NULL ) /* look for ',' in xinc,yinc */ *xptr = '\000'; /* found it, so replace ',' by '\0'*/ if ( *linexpr != '\000' ) /* check against missing 1st arg */ @@ -7803,8 +9526,10 @@ calculate width,height, etc, based on xl xlen = absval(xlen); /* force xlen positive */ ylen = absval(ylen); /* force ylen positive */ /* --- calculate corresponding lengths in pixels --- */ -width = max2(1,iround(unitlength*xlen)); /*scale by unitlength and round,*/ -height = max2(1,iround(unitlength*ylen)); /* and must be at least 1 pixel */ +width = max2(1,iround(unitlength*xlen)); /*scale by unitlength and round,*/ +height = max2(1,iround(unitlength*ylen)); /* and must be at least 1 pixel */ +rwidth = width + (ylen<0.001?0:max2(0,thickness-1)); +rheight = height + (xlen<0.001?0:max2(0,thickness-1)); /* --- set origin corner, x,yinc's: ++=0=(0,0) +-=1=(0,1) -+=10=(1,0) --- */ if ( xinc < 0.0 ) isright = 1; /*negative xinc, so corner is (1,?)*/ if ( yinc < 0.0 ) istop = 1; /*negative yinc, so corner is (?,1)*/ @@ -7813,18 +9538,20 @@ if ( msgfp!=NULL && msglevel>=29 ) /* de fprintf(msgfp,"rastline> width,height,origin;x,yinc=%d,%d,%d;%g,%g\n", width,height,origin,xinc,yinc); /* ------------------------------------------------------------------------- -allocate subraster and raster for complete picture +allocate subraster and raster for line -------------------------------------------------------------------------- */ -/* --- sanity check on width,height args --- */ +/* --- sanity check on width,height,thickness args --- */ if ( width < 1 || width > 600 -|| height < 1 || height > 600 ) goto end_of_job; +|| height < 1 || height > 600 +|| thickness<1||thickness>25 ) goto end_of_job; /* --- allocate and initialize subraster for constructed line --- */ -if ( (linesp=new_subraster(width,height,pixsz)) /* allocate new subraster */ +if ( (linesp=new_subraster(rwidth,rheight,pixsz)) /* alloc new subraster */ == NULL ) goto end_of_job; /* quit if failed */ /* --- initialize line subraster parameters --- */ linesp->type = IMAGERASTER; /* image */ linesp->symdef = NULL; /* not applicable for image */ -linesp->baseline = height/2 + 2; /* is a little above center good? */ +linesp->baseline = height/2 + 2 /* is a little above center good? */ + + (rheight-height)/2; /* account for line thickness too */ linesp->size = size; /* size (probably unneeded) */ /* ------------------------------------------------------------------------- draw the line @@ -7834,7 +9561,7 @@ line_raster ( linesp->image, /* embedde (isright? width-1 : 0), /* col0, from left or right */ (istop? height-1 : 0), /* row1, to top or bottom */ (isright? 0 : width-1), /* col1, to right or left */ - 1 ); /* line thickness is 1 pixel */ + thickness ); /* line thickness (usually 1 pixel)*/ /* ------------------------------------------------------------------------- return constructed line to caller -------------------------------------------------------------------------- */ @@ -7846,6 +9573,101 @@ end_of_job: /* ========================================================================== + * Function: rastrule ( expression, size, basesp, arg1, arg2, arg3 ) + * Purpose: \rule handler, returns subraster corresponding to rule + * parameters [lift]{width}{height} + * -------------------------------------------------------------------------- + * Arguments: expression (I/O) char ** to first char of null-terminated + * string immediately following \rule to be + * rasterized, and returning ptr immediately + * following last character processed. + * size (I) int containing 0-7 default font size + * basesp (I) subraster * to character (or subexpression) + * immediately preceding \rule + * (unused, but passed for consistency) + * arg1 (I) int unused + * arg2 (I) int unused + * arg3 (I) int unused + * -------------------------------------------------------------------------- + * Returns: ( subraster * ) ptr to subraster corresponding to rule + * requested, or NULL for any parsing error + * -------------------------------------------------------------------------- + * Notes: o Summary of syntax... + * \rule[lift]{width}{height} + * o if [lift] not given, then bottom of rule on baseline + * o if width=0 then you get an invisible strut 1 (one) pixel wide + * ======================================================================= */ +/* --- entry point --- */ +subraster *rastrule ( char **expression, int size, subraster *basesp, + int arg1, int arg2, int arg3 ) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +char *texsubexpr(), rulexpr[257]; /* rule[lift]{wdth}{hgt} */ +subraster *new_subraster(), *rulesp=NULL; /* subraster for rule */ +int pixsz = 1; /* pixels are one bit each */ +int lift=0, width=0, height=0; /* default rule parameters */ +double strtod(), dval; /* convert ascii params to doubles */ +int rwidth=0, rheight=0; /* alloc width, height plus lift */ +int rule_raster(); /* draw rule in rulesp->image */ +/* ------------------------------------------------------------------------- +Obtain lift,width,height +-------------------------------------------------------------------------- */ +/* --- check for optional lift arg --- */ +if ( *(*expression) == '[' ) /*check for []-enclosed optional arg*/ + { *expression = texsubexpr(*expression,rulexpr,255,"[","]",0,0); + dval = (int)(strtod(rulexpr,NULL)+0.5); /* convert [lift] to int */ + if ( dval <= 99 && dval >= (-99) ) /* sanity check */ + lift = iround(unitlength*dval); } /* scale by unitlength and round */ +/* --- parse for width --- */ +*expression = texsubexpr(*expression,rulexpr,255,"{","}",0,0); +if ( *rulexpr == '\000' ) goto end_of_job; /* quit if args missing */ +dval = (int)(strtod(rulexpr,NULL)+0.5); /* convert {width} to int */ +if ( dval <= 500 && dval >= 0 ) /* sanity check */ + width = max2(0,iround(unitlength*dval)); /* scale by unitlength and round*/ +/* --- parse for height --- */ +*expression = texsubexpr(*expression,rulexpr,255,"{","}",0,0); +if ( *rulexpr == '\000' ) goto end_of_job; /* quit if args missing */ +dval = (int)(strtod(rulexpr,NULL)+0.5); /* convert {height} to int */ +if ( dval <= 500 && dval > 0 ) /* sanity check */ + height= max2(1,iround(unitlength*dval)); /* scale by unitlength and round*/ +/* --- raster width,height in pixels --- */ +rwidth = max2(1,width); /* raster must be at least 1 pixel*/ +rheight = height + (lift>=0?lift: /* raster height plus lift */ + (-lift 600 +|| rheight < 1 || rheight > 600 ) goto end_of_job; +/* --- allocate and initialize subraster for constructed rule --- */ +if ( (rulesp=new_subraster(rwidth,rheight,pixsz)) /* alloc new subraster */ +== NULL ) goto end_of_job; /* quit if failed */ +/* --- initialize line subraster parameters --- */ +rulesp->type = IMAGERASTER; /* image */ +rulesp->symdef = NULL; /* not applicable for image */ +rulesp->baseline = rheight-1 + (lift>=0?0:lift); /*adjust baseline for lift*/ +rulesp->size = size; /* size (probably unneeded) */ +/* ------------------------------------------------------------------------- +draw the rule +-------------------------------------------------------------------------- */ +rule_raster ( rulesp->image, /* embedded raster image */ + (-lift0? 0:4 ) ); /* rule type */ +/* ------------------------------------------------------------------------- +return constructed rule to caller +-------------------------------------------------------------------------- */ +end_of_job: + return ( rulesp ); /* return rule to caller */ +} /* --- end-of-function rastrule() --- */ + + +/* ========================================================================== * Function: rastcircle ( expression, size, basesp, arg1, arg2, arg3 ) * Purpose: \circle handler, returns subraster corresponding to ellipse * parameters (xdiam[,ydiam]) @@ -8093,9 +9915,9 @@ end_of_job: * string immediately following \raisebox to be * rasterized, and returning ptr immediately * following last character processed. - * size (I) int containing 0-5 default font size + * size (I) int containing 0-7 default font size * basesp (I) subraster * to character (or subexpression) - * immediately preceding \rotatebox + * immediately preceding \raisebox * (unused, but passed for consistency) * arg1 (I) int unused * arg2 (I) int unused @@ -8115,7 +9937,7 @@ subraster *rastraise ( char **expression /* ------------------------------------------------------------------------- Allocations and Declarations -------------------------------------------------------------------------- */ -char *texsubexpr(), subexpr[8192], *liftexpr=subexpr; /* args */ +char *texsubexpr(), subexpr[MAXSUBXSZ+1], *liftexpr=subexpr; /* args */ subraster *rasterize(), *raisesp=NULL; /* rasterize subexpr to be raised */ int lift=0; /* amount to raise/lower baseline */ /* ------------------------------------------------------------------------- @@ -8155,7 +9977,7 @@ end_of_job: * string immediately following \rotatebox to be * rasterized, and returning ptr immediately * following last character processed. - * size (I) int containing 0-5 default font size + * size (I) int containing 0-7 default font size * basesp (I) subraster * to character (or subexpression) * immediately preceding \rotatebox * (unused, but passed for consistency) @@ -8177,7 +9999,7 @@ subraster *rastrotate ( char **expressio /* ------------------------------------------------------------------------- Allocations and Declarations -------------------------------------------------------------------------- */ -char *texsubexpr(), subexpr[8192], *degexpr=subexpr; /* args */ +char *texsubexpr(), subexpr[MAXSUBXSZ+1], *degexpr=subexpr; /* args */ subraster *rasterize(), *rotsp=NULL; /* subraster for rotated subexpr */ raster *rastrot(), *rotrp=NULL; /* rotate subraster->image 90 degs */ int delete_raster(); /* delete intermediate rasters */ @@ -8245,12 +10067,12 @@ if ( rotrp != NULL ) /* rotated raster { rotsp->type = IMAGERASTER; /* signal constructed image */ rotsp->image = rotrp; /* raster we just constructed */ /* --- now try to guess pleasing baseline --- */ - if ( idegrees > 2 ) /* leave unchanged if unrotated */ + if ( idegrees > 2 ) { /* leave unchanged if unrotated */ if ( strlen(subexpr) < 3 /* we rotated a short expression */ || abs(idegrees-180) < 3 ) /* or just turned it upside-down */ baseline = rotrp->height - 1; /* so set with nothing descending */ else /* rotated a long expression */ - baseline = (65*(rotrp->height-1))/100; /* roughly center long expr */ + baseline = (65*(rotrp->height-1))/100; } /* roughly center long expr */ rotsp->baseline = baseline; } /* set baseline as calculated above*/ /* --- return rotated subexpr to caller --- */ end_of_job: @@ -8259,6 +10081,85 @@ end_of_job: /* ========================================================================== + * Function: rastreflect ( expression, size, basesp, arg1, arg2, arg3 ) + * Purpose: \reflectbox[axis]{subexpression} handler, returns subraster + * containing subexpression reflected horizontally (i.e., around + * vertical axis, |_ becomes _|) if [axis] not given or axis=1, + * or reflected vertically if axis=2 given. + * -------------------------------------------------------------------------- + * Arguments: expression (I/O) char ** to first char of null-terminated + * string immediately following \reflectbox to + * be rasterized, and returning ptr immediately + * following last character processed. + * size (I) int containing 0-7 default font size + * basesp (I) subraster * to character (or subexpression) + * immediately preceding \reflectbox + * (unused, but passed for consistency) + * arg1 (I) int unused + * arg2 (I) int unused + * arg3 (I) int unused + * -------------------------------------------------------------------------- + * Returns: ( subraster * ) ptr to subraster corresponding to \reflectbox + * requested, or NULL for any parsing error + * -------------------------------------------------------------------------- + * Notes: o Summary of syntax... + * \reflectbox[axis]{subexpression} + * o + * ======================================================================= */ +/* --- entry point --- */ +subraster *rastreflect ( char **expression, int size, subraster *basesp, + int arg1, int arg2, int arg3 ) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +char *texsubexpr(), subexpr[MAXSUBXSZ+1], *axisexpr=subexpr; /* args */ +subraster *rasterize(), *refsp=NULL; /* subraster for reflected subexpr */ +raster *rastref(), *refrp=NULL; /* reflect subraster->image */ +int axis = 1; /* default horizontal reflection */ +int delete_raster(); /* delete intermediate raster */ +int baseline=0; /* baseline of rasterized image */ +/* ------------------------------------------------------------------------- +obtain [axis] argument immediately following \reflectbox command, if given +-------------------------------------------------------------------------- */ +/* --- check for optional [axis] arg --- */ +if ( *(*expression) == '[' ) /*check for []-enclosed optional arg*/ + { *expression = texsubexpr(*expression,axisexpr,255,"[","]",0,0); + axis = atoi(axisexpr); /* convert [axis] to int */ + if ( axis<1 || axis>2 ) /* check axis input */ + axis = 1; } /* back to default if illegal */ +/* ------------------------------------------------------------------------- +obtain {subexpr} argument after optional [axis], and rasterize it +-------------------------------------------------------------------------- */ +/* --- parse for {subexpr} arg, and bump expression past it --- */ +*expression = texsubexpr(*expression,subexpr,0,"{","}",0,0); +/* --- rasterize subexpression to be reflected --- */ +if ( (refsp = rasterize(subexpr,size)) /* rasterize subexpression */ +== NULL ) goto end_of_job; /* and quit if failed */ +/* --- return unmodified image if no reflection requested --- */ +if ( axis<1 || axis>2 ) goto end_of_job; /* don't bother reflecting image */ +/* --- extract params for image to be reflected --- */ +refrp = refsp->image; /* unreflected rasterized image */ +baseline = refsp->baseline; /* and baseline of that image */ +/* ------------------------------------------------------------------------- +reflect image and adjust its parameters +-------------------------------------------------------------------------- */ +/* --- reflect image --- */ +refrp = rastref(refsp->image,axis); /* reflect raster image */ +if ( refrp == NULL ) goto end_of_job; /* failed to reflect image */ +delete_raster(refsp->image); /* free original raster image */ +refsp->image = refrp; /*and replace it with reflected one*/ +/* --- adjust parameters --- */ +if ( axis == 2 ) /* for vertical reflection */ + baseline = refrp->height - 1; /* set with nothing descending */ +refsp->baseline = baseline; /* reset baseline of reflected image*/ +/* --- return reflected subexpr to caller --- */ +end_of_job: + return ( refsp ); /*back to caller with reflected expr*/ +} /* --- end-of-function rastreflect() --- */ + + +/* ========================================================================== * Function: rastfbox ( expression, size, basesp, arg1, arg2, arg3 ) * Purpose: \fbox{subexpression} handler, returns subraster * containing subexpression with frame box drawn around it @@ -8289,7 +10190,7 @@ subraster *rastfbox ( char **expression, /* ------------------------------------------------------------------------- Allocations and Declarations -------------------------------------------------------------------------- */ -char *texsubexpr(), subexpr[8192], widtharg[512]; /* args */ +char *texsubexpr(), subexpr[MAXSUBXSZ+1], widtharg[512]; /* args */ subraster *rasterize(), *framesp=NULL; /* rasterize subexpr to be framed */ raster *border_raster(), *bp=NULL; /* framed image raster */ double strtod(); /* interpret [width][height] */ @@ -8377,22 +10278,22 @@ subraster *rastinput ( char **expression /* ------------------------------------------------------------------------- Allocations and Declarations -------------------------------------------------------------------------- */ -char *texsubexpr(), tag[512]="\000", filename[1024]="\000"; /* args */ +char *texsubexpr(), tag[1024]="\000", filename[1024]="\000"; /* args */ subraster *rasterize(), *inputsp=NULL; /* rasterized input image */ int status, rastreadfile(); /* read input file */ int format=0, npts=0; /* don't reformat (numerical) input */ -char subexpr[8192], /* concatanated lines from input file */ +char subexpr[MAXFILESZ+1] = "\000", /*concatanated lines from input file*/ *mimeprep(), /* preprocess inputted data */ - *dtoa(), *reformat=NULL; /* reformat numerical input */ + *dbltoa(), *reformat=NULL; /* reformat numerical input */ /* ------------------------------------------------------------------------- obtain [tag]{filename} argument -------------------------------------------------------------------------- */ /* --- parse for optional [tag] or [fmt] arg, bump expression past it --- */ if ( *(*expression) == '[' ) /* check for []-enclosed value */ - { char argfld[2048]; /* optional argument field */ - *expression = texsubexpr(*expression,argfld,2047,"[","]",0,0); - if ( (reformat=strstr(argfld,"dtoa")) != NULL ) /* dtoa requested */ - { format = 1; /* signal dtoa() format */ + { char argfld[MAXTOKNSZ+1]; /* optional argument field */ + *expression = texsubexpr(*expression,argfld,MAXTOKNSZ,"[","]",0,0); + if ( (reformat=strstr(argfld,"dtoa")) != NULL ) /*dtoa/dbltoa requested*/ + { format = 1; /* signal dtoa()/dbltoa() format */ if ( (reformat=strchr(reformat,'=')) != NULL ) /* have dtoa= */ npts = (int)strtol(reformat+1,NULL,0); } /* so set npts */ if ( format == 0 ) /* reformat not requested */ @@ -8409,14 +10310,14 @@ if ( *filename != '\000' /* got filenam /* -------------------------------------------------------------------------- Read file and rasterize constructed subexpression -------------------------------------------------------------------------- */ -status = rastreadfile(filename,tag,subexpr); /* read file */ +status = rastreadfile(filename,0,tag,subexpr); /* read file */ if ( *subexpr == '\000' ) goto end_of_job; /* quit if problem */ /* --- rasterize input subexpression --- */ mimeprep(subexpr); /* preprocess subexpression */ -if ( format == 1 ) /* dtoa() */ +if ( format == 1 ) /* dtoa()/dbltoa() */ { double d = strtod(subexpr,NULL); /* interpret subexpr as double */ if ( d != 0.0 ) /* conversion to double successful */ - if ( (reformat=dtoa(d,npts)) != NULL ) /* reformat successful */ + if ( (reformat=dbltoa(d,npts)) != NULL ) /* reformat successful */ strcpy(subexpr,reformat); } /*replace subexpr with reformatted*/ inputsp = rasterize(subexpr,size); /* rasterize subexpression */ /* --- return input image to caller --- */ @@ -8447,8 +10348,8 @@ end_of_job: * requested, or NULL for any parsing error * -------------------------------------------------------------------------- * Notes: o Summary of syntax... - * \counter[value][logfile]{filename} - * o + * \counter[value][logfile]{filename:tag} + * o :tag is optional * ======================================================================= */ /* --- entry point --- */ subraster *rastcounter ( char **expression, int size, subraster *basesp, @@ -8458,18 +10359,22 @@ subraster *rastcounter ( char **expressi Allocations and Declarations -------------------------------------------------------------------------- */ char *texsubexpr(), filename[1024]="\000", /* counter file */ - logfile[1024]="\000", tag[512]="\000"; /* log file and tag */ + logfile[1024]="\000", tag[1024]="\000"; /*optional log file and tag*/ subraster *rasterize(), *countersp=NULL; /* rasterized counter image */ FILE /* *fp=NULL,*/ *logfp=NULL; /* counter and log file pointers */ -int rastreadfile(), rastwritefile(); /* to read and write counter file */ -char text[2048] = "1_", /* first (and only) line in counter file */ +int status=0,rastreadfile(),rastwritefile(), /*read,write counter file*/ + isstrict = 1; /* true to only write to existing files */ +char text[MAXFILESZ] = "1_", /* only line in counter file without tags */ *delim = NULL, /* delimiter in text */ - utext[32] = "1_", /* default delimiter */ + utext[128] = "1_", /* default delimiter */ *udelim = utext+1; /* underscore delimiter */ -char *timestamp(), /* timestamp for logging */ - *dtoa(); /* double to comma-separated */ +char *rasteditfilename(), /* edit log file name */ + *timestamp(), /* timestamp for logging */ + *dbltoa(); /* double to comma-separated ascii */ int counter = 1, /* atoi(text) (after _ removed, if present) */ - gotcount = 0, /* set true once counter value determined */ + value = 1, /* optional [value] argument */ + gotvalue = 0, /* set true if [value] supplied */ + isdelta = 0, /* set true if [+value] or [-value] is delta*/ ordindex = (-1); /* ordinal[] index to append ordinal suffix */ /*--- ordinal suffixes based on units digit of counter ---*/ static char *ordinal[]={"th","st","nd","rd","th","th","th","th","th","th"}; @@ -8480,28 +10385,34 @@ first obtain optional [value][logfile] a -------------------------------------------------------------------------- */ /* --- first check for optional \counter[value] --- */ if ( *(*expression) == '[' ) /* check for []-enclosed value */ - { *expression = texsubexpr(*expression,text,2047,"[","]",0,0); - if ( *text != '\000' ) /* got counter value */ - if ( strlen(text) >= 1 ) /* and it's not an empty string */ - if ( isdigit((int)(*text)) ) /* leading 0-9 digit signals value */ - { counter = (int)(strtod(text,&udelim)+0.1); /* value and delim */ - gotcount = 1; } /* signal we got counter value */ - else /* not a digit, so must be logfile */ - strcpy(logfile,text); /* so just copy it */ + { *expression = texsubexpr(*expression,text,1023,"[","]",0,0); + if ( *text != '\000' ) /* got counter value (or logfile) */ + if ( strlen(text) >= 1 ) { /* and it's not an empty string */ + if ( isthischar(*text,"+-0123456789") ) /* check for leading +-digit */ + gotvalue = 1; /* signal we got optional value */ + else /* not +-digit, so must be logfile */ + strcpy(logfile,text); } /* so just copy it */ } /* --- end-of-if(**expression=='[') --- */ /* --- next check for optional \counter[][logfile] --- */ if ( *(*expression) == '[' ) /* check for []-enclosed logfile */ { *expression = texsubexpr(*expression,filename,1023,"[","]",0,0); - if ( *text != '\000' ) /* got logfile value */ - if ( strlen(filename) >= 1 ) /* and it's not an empty string */ - if ( !(isdigit((int)(*filename))) /*leading non-digit signals logfile*/ - || gotcount ) /* or we already got counter value */ - strcpy(logfile,filename); /* so just copy it */ - else /* 0-9 digit, so must be value */ - { strcpy(text,filename); /* copy value to text line */ - counter = (int)(strtod(text,&udelim)+0.1); /* value and delim */ - gotcount = 1; } /* signal we got counter value */ + if ( *filename != '\000' ) /* got logfile (or counter value) */ + if ( strlen(filename) >= 1 ) { /* and it's not an empty string */ + if ( !(isthischar(*text,"+-0123456789")) /* not a leading +-digit */ + || gotvalue ) /* or we already got counter value */ + strcpy(logfile,filename); /* so just copy it */ + else /* leading +-digit must be value */ + { strcpy(text,filename); /* copy value to text line */ + gotvalue = 1; } } /* and signal we got optional value*/ } /* --- end-of-if(**expression=='[') --- */ +/* --- evaluate [value] if present --- */ +if ( gotvalue ) { /*leading +-digit should be in text*/ + if ( *text == '+' ) isdelta = (+1); /* signal adding */ + if ( *text == '-' ) isdelta = (-1); /* signal subtracting */ + value = (int)(strtod((isdelta==0?text:text+1),&udelim)+0.1); /*abs(value)*/ + if ( isdelta == (-1) ) value = (-value); /* set negative value if needed */ + counter = value; /* re-init counter */ + } /* --- end-of-if(gotvalue) --- */ /* ------------------------------------------------------------------------- obtain counter {filename} argument -------------------------------------------------------------------------- */ @@ -8519,22 +10430,27 @@ Read and parse file, increment and rewri if ( strlen(filename) > 1 ) /* make sure we got {filename} arg */ { /* --- read and interpret first (and only) line from counter file --- */ - if ( !gotcount ) /* if no [count] argument supplied */ - if ( rastreadfile(filename,tag,text) != 0 ) /* try reading it from file */ - { counter= 1 + (int)(strtod(text,&udelim)+0.1); /*counter val and delim*/ - gotcount = 1; } /* signal we got counter value */ + if ( !gotvalue || (isdelta!=0) ) /*if no [count] arg or if delta arg*/ + if ( (status=rastreadfile(filename,1,tag,text)) > 0 ) /*try reading file*/ + { char *vdelim = NULL; /* underscore delim from file */ + double fileval = strtod(text,&vdelim); /* value and delim from file */ + counter = (int)(fileval<0.0?fileval-0.1:fileval+0.1); /* integerized */ + counter += value; /* bump count by 1 or add/sub delta*/ + if ( !gotvalue ) udelim=vdelim; } /* default to file's current delim */ /* --- check for ordinal suffix --- */ if ( udelim != (char *)NULL ) /* have some delim after value */ if ( *udelim == '_' ) /* underscore signals ordinal */ - { ordindex = counter%10; /* least significant digit */ - if ( counter >= 10 ) /* counter is 10 or greater */ - if ( (counter/10)%10 == 1 ) /* and the last two are 10-19 */ + { int abscount = (counter>=0?counter:(-counter)); /* abs(counter) */ + ordindex = abscount%10; /* least significant digit */ + if ( abscount >= 10 ) /* counter is 10 or greater */ + if ( (abscount/10)%10 == 1 ) /* and the last two are 10-19 */ ordindex = 0; } /* use th for 11,12,13 rather than st,nd,rd */ /* --- rewrite counter file --- */ - sprintf(text,"%d",counter); /*build image of incremented counter*/ - if ( ordindex >= 0 ) strcat(text,"_"); /* tack on _ */ - if ( *tag == '\000' ) strcat(text,"\n"); /* and newline */ - rastwritefile(filename,tag,text,1); /* rewrite incremented counter */ + if ( status >= 0 ) /* file was read okay */ + { sprintf(text,"%d",counter); /*build image of incremented counter*/ + if ( ordindex >= 0 ) strcat(text,"_"); /* tack on _ */ + if ( *tag == '\000' ) strcat(text,"\n"); /* and newline */ + status = rastwritefile(filename,tag,text,isstrict); } /*rewrite counter*/ } /* --- end-of-if(strlen(filename)>1) --- */ /* -------------------------------------------------------------------------- log counter request @@ -8543,35 +10459,44 @@ if ( strlen(logfile) > 1 ) /* optional { char comment[1024] = "\000", /* embedded comment, logfile:comment*/ *commptr = strchr(logfile,':'); /* check for : signalling comment */ + int islogokay = 1; /* logfile must exist if isstrict */ if ( commptr != NULL ) /* have embedded comment */ { strcpy(comment,commptr+1); /* comment follows : */ *commptr = '\000'; } /* null-terminate actual logfile */ - if ( (logfp = fopen(logfile,"a")) /* open logfile */ - != (FILE *)NULL ) /* opened successfully */ - { - int ilog=0; /* logvars[] index */ - fprintf(logfp,"%s ",timestamp()); /* first emit timestamp */ - if (*tag=='\000') fprintf(logfp,"%s",filename); /* emit counter filename */ - else fprintf(logfp,"<%s>",tag); /* or tag if we have one */ - fprintf(logfp,"=%d",counter); /* emit counter value */ - for ( ilog=0; logvars[ilog] != NULL; ilog++ ) /* log till end-of-table */ - if ( ilog == commentvar /* replace with comment... */ - && commptr != NULL ) /* ...if available */ - fprintf(logfp," %.256s",comment); /* log embedded comment */ - else - { char *logval = getenv(logvars[ilog]); /* getenv(variable) to be logged*/ - fprintf(logfp," %.64s", /* log variable */ + strcpy(logfile,rasteditfilename(logfile)); /* edit log file name */ + if ( *logfile == '\000' ) islogokay = 0; /* given an invalid file name */ + else if ( isstrict ) { /*okay, but only write if it exists*/ + if ( (logfp=fopen(logfile,"r")) == (FILE *)NULL ) /*doesn't already exist*/ + islogokay = 0; /* so don't write log file */ + else fclose(logfp); } /* close file opened for test read */ + if ( islogokay ) /* okay to write logfile */ + if ( (logfp = fopen(logfile,"a")) /* open logfile */ + != (FILE *)NULL ) { /* opened successfully for append */ + int ilog=0; /* logvars[] index */ + fprintf(logfp,"%s ",timestamp(TZDELTA,0)); /* first emit timestamp */ + if (*tag=='\000') fprintf(logfp,"%s",filename); /* emit counter filename */ + else fprintf(logfp,"<%s>",tag); /* or tag if we have one */ + fprintf(logfp,"=%d",counter); /* emit counter value */ + if ( status < 1 ) /* read or re-write failed */ + fprintf(logfp,"(%s %d)","error status",status); /* emit error */ + for ( ilog=0; logvars[ilog] != NULL; ilog++ ) /* log till end-of-table */ + if ( ilog == commentvar /* replace with comment... */ + && commptr != NULL ) /* ...if available */ + fprintf(logfp," %.256s",comment); /* log embedded comment */ + else + { char *logval = getenv(logvars[ilog]); /*getenv(variable) to be logged*/ + fprintf(logfp," %.64s", /* log variable */ (logval!=NULL?logval:"")); } /* emit value or */ - fprintf(logfp,"\n"); /* terminating newline */ - fclose(logfp); /* close logfile */ - } /* --- end-of-if(logfp!=NULL) --- */ + fprintf(logfp,"\n"); /* terminating newline */ + fclose(logfp); /* close logfile */ + } /* --- end-of-if(islogokay&&logfp!=NULL) --- */ } /* --- end-of-if(strlen(logfile)>1) --- */ /* -------------------------------------------------------------------------- construct counter expression and rasterize it -------------------------------------------------------------------------- */ /* --- construct expression --- */ /*sprintf(text,"%d",counter);*/ /* start with counter */ -strcpy(text,dtoa(((double)counter),0)); /* comma-separated counter value */ +strcpy(text,dbltoa(((double)counter),0)); /* comma-separated counter value */ if ( ordindex >= 0 ) /* need to tack on ordinal suffix */ { strcat(text,"^{\\underline{\\rm~"); /* start with ^ and {\underline{\rm */ strcat(text,ordinal[ordindex]); /* then st,nd,rd, or th */ @@ -8585,6 +10510,135 @@ countersp = rasterize(text,size); /* ras /* ========================================================================== + * Function: rasttoday ( expression, size, basesp, arg1, arg2, arg3 ) + * Purpose: handle \today + * -------------------------------------------------------------------------- + * Arguments: expression (I/O) char ** to first char of null-terminated + * string immediately following \today, + * and returning ptr immediately + * following last character processed. + * size (I) int containing 0-5 default font size + * basesp (I) subraster * to character (or subexpression) + * immediately preceding \today + * (unused, but passed for consistency) + * arg1 (I) int unused + * arg2 (I) int unused + * arg3 (I) int unused + * -------------------------------------------------------------------------- + * Returns: ( subraster * ) subraster ptr to date stamp + * -------------------------------------------------------------------------- + * Notes: o + * ======================================================================= */ +/* --- entry point --- */ +subraster *rasttoday ( char **expression, int size, subraster *basesp, + int arg1, int arg2, int arg3 ) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +char *texsubexpr(), optarg[2050]; /* optional [+/-tzdelta,ifmt] args */ +char *timestamp(), *today=optarg; /* timestamp to be rasterized */ +subraster *rasterize(), *todaysp=NULL; /* rasterize timestamp */ +int ifmt=1, tzdelta=0; /* default timestamp() args */ +/* ------------------------------------------------------------------------- +Get optional args \today[+/-tzdelta,ifmt] +-------------------------------------------------------------------------- */ +/* --- check for optional \today[+/-tzdelta,ifmt] --- */ +if ( *(*expression) == '[' ) /* check for []-enclosed value */ + { *expression = texsubexpr(*expression,optarg,2047,"[","]",0,0); + if ( *optarg != '\000' ) /* got optional arg */ + { char *comma = strchr(optarg,','); /* comma between +/-tzdelta,ifmt */ + int iarg, nargs=(comma==NULL?1:2); /* #optional args between []'s */ + if ( comma != NULL ) *comma = '\000'; /* null-terminate first arg */ + for ( iarg=1; iarg<=nargs; iarg++ ) /* process one or both args */ + { char *arg = (iarg==1?optarg:comma+1); /* choose 1st or 2nd arg */ + if ( isthischar(*arg,"+-") ) /* leading +/- signals tzdelta */ + tzdelta = atoi(arg); /* so interpret arg as tzdelta */ + else ifmt = atoi(arg); } /* else interpret args as ifmt */ + } /* --- end-of-if(*optarg!='\0') --- */ + } /* --- end-of-if(**expression=='[') --- */ +/* ------------------------------------------------------------------------- +Get timestamp and rasterize it +-------------------------------------------------------------------------- */ +strcpy(today,"\\text{"); /* rasterize timestamp as text */ +strcat(today,timestamp(tzdelta,ifmt)); /* get timestamp */ +strcat(today,"}"); /* terminate \text{} braces */ +todaysp = rasterize(today,size); /* rasterize timestamp */ +/* --- return timestamp raster to caller --- */ +/*end_of_job:*/ + return ( todaysp ); /* return timestamp to caller */ +} /* --- end-of-function rasttoday() --- */ + + +/* ========================================================================== + * Function: rastcalendar ( expression, size, basesp, arg1, arg2, arg3 ) + * Purpose: handle \calendar + * -------------------------------------------------------------------------- + * Arguments: expression (I/O) char ** to first char of null-terminated + * string immediately following \calendar + * and returning ptr immediately + * following last character processed. + * size (I) int containing 0-5 default font size + * basesp (I) subraster * to character (or subexpression) + * immediately preceding \calendar + * (unused, but passed for consistency) + * arg1 (I) int unused + * arg2 (I) int unused + * arg3 (I) int unused + * -------------------------------------------------------------------------- + * Returns: ( subraster * ) subraster ptr to rendered one-month calendar + * -------------------------------------------------------------------------- + * Notes: o + * ======================================================================= */ +/* --- entry point --- */ +subraster *rastcalendar ( char **expression, int size, subraster *basesp, + int arg1, int arg2, int arg3 ) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +char *texsubexpr(), optarg[2050]; /* optional [year,month] args */ +char *calendar(), *calstr=NULL; /* calendar to be rasterized */ +subraster *rasterize(), *calendarsp=NULL; /* rasterize calendar string */ +int year=0,month=0,day=0, argval=0; /* default calendar() args */ +/* ------------------------------------------------------------------------- +Get optional args \today[+/-tzdelta,ifmt] +-------------------------------------------------------------------------- */ +/* --- check for optional \calendar[year,month] --- */ +if ( *(*expression) == '[' ) /* check for []-enclosed value */ + { *expression = texsubexpr(*expression,optarg,2047,"[","]",0,0); + if ( *optarg != '\000' ) /* got optional arg */ + { char *comma = strchr(optarg,','), /* comma between year,month */ + *comma2 = NULL; /* second comma before day */ + int iarg, nargs=(comma==NULL?1:2); /* #optional args between []'s */ + if ( comma != NULL ) { *comma = '\000'; /*null-terminate first arg*/ + if ( (comma2=strchr(comma+1,',')) != NULL ) /* have third arg */ + { *comma2 = '\000'; nargs++; } } /* null-term 2nd arg, bump count */ + for ( iarg=1; iarg<=nargs; iarg++ ) /* process one or both args */ + { char *arg= (iarg==1?optarg:(iarg==2?comma+1:comma2+1)); /*get arg*/ + argval = atoi(arg); /* interpret arg as integer */ + if ( iarg < 3 ) /* first two args are month,year */ + {if ( argval>1972 && argval<2100 ) year = argval; /* year value */ + else if ( argval>=1 && argval<=12 ) month = argval;} /*or month*/ + else /* only 3rd arg can be day */ + if ( argval>=1 && argval<=31 ) day = argval; } /* day value */ + } /* --- end-of-if(*optarg!='\0') --- */ + } /* --- end-of-if(**expression=='[') --- */ +/* ------------------------------------------------------------------------- +Get calendar string and rasterize it +-------------------------------------------------------------------------- */ +if ( msgfp!= NULL && msglevel>=9 ) + fprintf(msgfp,"rastcalendar> year=%d, month=%d, day=%d\n", + year,month,day); +calstr = calendar(year,month,day); /* get calendar string */ +calendarsp = rasterize(calstr,size); /* rasterize calendar string */ +/* --- return calendar raster to caller --- */ +/*end_of_job:*/ + return ( calendarsp ); /* return calendar to caller */ +} /* --- end-of-function rastcalendar() --- */ + + +/* ========================================================================== * Function: rastnoop ( expression, size, basesp, nargs, arg2, arg3 ) * Purpose: no op -- flush \escape without error * -------------------------------------------------------------------------- @@ -8594,7 +10648,7 @@ countersp = rasterize(text,size); /* ras * following last character processed. * size (I) int containing 0-5 default font size * basesp (I) subraster * to character (or subexpression) - * immediately preceding \fbox + * immediately preceding \escape * (unused, but passed for consistency) * nargs (I) int containing number of {}-args after * \escape to be flushed along with it @@ -8612,7 +10666,7 @@ subraster *rastnoop ( char **expression, /* ------------------------------------------------------------------------- Allocations and Declarations -------------------------------------------------------------------------- */ -char *texsubexpr(), subexpr[8192]; /* flush dummy args eaten by \escape*/ +char *texsubexpr(), subexpr[MAXSUBXSZ+1]; /*dummy args eaten by \escape*/ subraster *rasterize(), *noopsp=NULL; /* rasterize subexpr */ /* --- flush accompanying args if necessary --- */ if ( nargs != NOVALUE /* not unspecified */ @@ -8648,32 +10702,22 @@ FILE *rastopenfile ( char *filename, cha Allocations and Declarations -------------------------------------------------------------------------- */ FILE *fp = (FILE *)NULL /*,*fopen()*/; /*file pointer to opened filename*/ -char texfile[2048] = "\000", /* local copy of input filename */ - amode[128] = "r"; /* test open mode if arg mode=NULL */ -int ismode = 0, /* true of mode!=NULL */ - isprefix = (*pathprefix=='\000'?0:1); /* true if paths have prefix */ +char texfile[2050] = "\000", /* local, edited copy of filename */ + *rasteditfilename(), /* prepend pathprefix if necessary */ + amode[512] = "r"; /* test open mode if arg mode=NULL */ +int ismode = 0; /* true of mode!=NULL */ /* -------------------------------------------------------------------------- Check mode and open file -------------------------------------------------------------------------- */ -/* --- check filename --- */ -if ( filename != (char *)NULL ) /* caller passed filename arg */ - if ( strlen(filename) >= 1 ) /* make sure we got actual filename*/ - { char *pfilename = filename; /* ptr to 1st char of filename */ - *texfile = '\000'; /* init filename as null string */ - while ( isthischar(*pfilename," /\\") ) /* absolute paths invalid */ - pfilename++; /* so flush leading / or \ (or ' ')*/ - if ( isprefix && *pfilename!='\000' ) /* paths preceded by prefix */ - { strcat(texfile,pathprefix); /* init filename with path */ - while ( memcmp(pfilename,"../",3)==0 /* have leading ../ */ - || memcmp(pfilename,"..\\",3)==0 ) /* or ..\ with prefix */ - pfilename += 3; } /* flush leading ../ or ..\ */ - strcat(texfile,pfilename); /* local copy of given filename */ - compress(texfile,' '); } /* remove embedded blanks */ +/* --- edit filename --- */ +strncpy(texfile,rasteditfilename(filename),2047); /*edited copy of filename*/ +texfile[2047] = '\000'; /* make sure it's null terminated */ /* --- check mode --- */ if ( mode != (char *)NULL ) /* caller passed mode arg */ if ( *mode != '\000' ) /* and it's not an empty string */ { ismode = 1; /* so flip mode flag true */ - strcpy(amode,mode); /* and replace "r" with caller's */ + strncpy(amode,mode,254); /* and replace "r" with caller's */ + amode[254] = '\000'; /* make sure it's null terminated */ compress(amode,' '); } /* remove embedded blanks */ /* --- open filename or filename.tex --- */ if ( strlen(texfile) > 1 ) /* make sure we got actual filename*/ @@ -8697,13 +10741,67 @@ if ( !ismode && fp!=NULL ) /* no mode, /* ========================================================================== - * Function: rastreadfile ( filename, tag, value ) + * Function: rasteditfilename ( filename ) + * Purpose: edits filename to remove security problems, + * e.g., removes all ../'s and ..\'s. + * -------------------------------------------------------------------------- + * Arguments: filename (I) char * to null-terminated string containing + * name of file to be edited + * -------------------------------------------------------------------------- + * Returns: ( char * ) pointer to edited filename, + * or empty string "\000" if any problem + * -------------------------------------------------------------------------- + * Notes: o + * ======================================================================= */ +/* --- entry point --- */ +char *rasteditfilename ( char *filename ) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +static char editname[2050]; /*edited filename returned to caller*/ +char *strchange(); /* prepend pathprefix if necessary */ +int strreplace(), /* remove ../'s and ..\'s */ + isprefix = (*pathprefix=='\000'?0:1); /* true if paths have prefix */ +/* -------------------------------------------------------------------------- +edit filename +-------------------------------------------------------------------------- */ +/* --- first check filename arg --- */ +*editname = '\000'; /* init edited name as empty string*/ +if ( filename == (char *)NULL ) goto end_of_job; /* no filename arg */ +if ( *filename == '\000' ) goto end_of_job; /* filename is an empty string */ +/* --- init edited filename --- */ +strcpy(editname,filename); /* init edited name as input name */ +compress(editname,' '); /* remove embedded blanks */ +/* --- remove leading or embedded ....'s --- */ +while ( strreplace(editname,"....",NULL,0) > 0 ) ; /* squeeze out ....'s */ +/* --- remove leading / and \ and dots (and blanks) --- */ +if ( *editname != '\000' ) /* still have chars in filename */ + while ( isthischar(*editname," ./\\") ) /* absolute paths invalid */ + strcpy(editname,editname+1); /* so flush leading / or \ (or ' ')*/ +if ( *editname == '\000' ) goto end_of_job; /* no chars left in filename */ +/* --- remove leading or embedded ../'s and ..\'s --- */ +while ( strreplace(editname,"../",NULL,0) > 0 ) ; /* squeeze out ../'s */ +while ( strreplace(editname,"..\\",NULL,0) > 0 ) ; /* and ..\'s */ +while ( strreplace(editname,"../",NULL,0) > 0 ) ; /* and ../'s again */ +/* --- prepend path prefix (if compiled with -DPATHPREFIX) --- */ +if ( isprefix && *editname!='\000' ) /* filename is preceded by prefix */ + strchange(0,editname,pathprefix); /* so prepend prefix */ +end_of_job: + return ( editname ); /* back with edited filename */ +} /* --- end-of-function rasteditfilename() --- */ + + +/* ========================================================================== + * Function: rastreadfile ( filename, islock, tag, value ) * Purpose: Read filename, returning value as string * between ... or entire file if tag=NULL passed. * -------------------------------------------------------------------------- * Arguments: filename (I) char * to null-terminated string containing * name of file to read (preceded by path * relative to mimetex executable) + * islock (I) int containing 1 to lock file while reading + * (hopefully done by opening in "r+" mode) * tag (I) char * to null-terminated string containing * html-like tagname. File contents between * and will be returned, or @@ -8716,18 +10814,19 @@ if ( !ismode && fp!=NULL ) /* no mode, * Notes: o * ======================================================================= */ /* --- entry point --- */ -int rastreadfile ( char *filename, char *tag, char *value ) +int rastreadfile ( char *filename, int islock, char *tag, char *value ) { /* ------------------------------------------------------------------------- Allocations and Declarations -------------------------------------------------------------------------- */ FILE *fp = (FILE *)NULL, *rastopenfile(); /* pointer to opened filename */ -char texfile[2048] = "\000", /* local copy of input filename */ - text[4096]; /* line from input file */ -char *tagp, tag1[512], tag2[512]; /* left and right */ -int vallen=0, maxvallen=8000; /* #chars in value, max allowed */ -int status = 0; /* status returned, 1=okay */ +char texfile[1024] = "\000", /* local copy of input filename */ + text[MAXLINESZ+1]; /* line from input file */ +char *tagp, tag1[1024], tag2[1024]; /* left and right */ +int vallen=0, maxvallen=MAXFILESZ; /* #chars in value, max allowed */ +int status = (-1); /* status returned, 1=okay */ int tagnum = 0; /* tag we're looking for */ +/*int islock = 1;*/ /* true to lock file */ /* -------------------------------------------------------------------------- Open file -------------------------------------------------------------------------- */ @@ -8736,12 +10835,15 @@ if ( value == (char *)NULL ) goto end_of *value = '\000'; /* init buffer with empty string */ /* --- open filename or filename.tex --- */ if ( filename != (char *)NULL ) /* make sure we got filename arg */ - { strcpy(texfile,filename); /* local copy of filename */ - fp = rastopenfile(texfile,"r"); } /* try opening it */ + { strncpy(texfile,filename,1023); /* local copy of filename */ + texfile[1023] = '\000'; /* make sure it's null terminated */ + fp = rastopenfile(texfile,(islock?"r+":"r")); } /* try opening it */ /* --- check that file opened --- */ if ( fp == (FILE *)NULL ) /* failed to open file */ { sprintf(value,"{\\normalsize\\rm[file %s?]}",texfile); goto end_of_job; } /* return error message to caller */ +status = 0; /* file opened successfully */ +if ( islock ) rewind(fp); /* start at beginning of file */ /* -------------------------------------------------------------------------- construct 's -------------------------------------------------------------------------- */ @@ -8755,9 +10857,9 @@ if ( tag != (char *)NULL ) /* caller pa /* -------------------------------------------------------------------------- Read file, concatnate lines -------------------------------------------------------------------------- */ -while ( fgets(text,4090,fp) != (char *)NULL ) { /* read input till eof */ +while ( fgets(text,MAXLINESZ-1,fp) != (char *)NULL ) { /*read input till eof*/ switch ( tagnum ) { /* look for left- or right-tag */ - case 0: break; /* no tag to look for */ + case 0: status = 1; break; /* no tag to look for */ case 1: /* looking for opening left */ if ( (tagp=strstr(text,tag1)) == NULL ) break; /*haven't found it yet*/ strcpy(text,tagp+strlen(tag1)); /* shift out preceding text */ @@ -8766,6 +10868,7 @@ while ( fgets(text,4090,fp) != (char *)N if ( (tagp=strstr(text,tag2)) == NULL ) break; /*haven't found it yet*/ *tagp = '\000'; /* terminate line at tag */ tagnum = 3; /* done after this line */ + status = 1; /* successfully read tag */ break; } /* ---end-of-switch(tagnum) --- */ if ( tagnum != 1 ) { /* no tag or left tag already found*/ @@ -8812,15 +10915,16 @@ int rastwritefile( char *filename, char Allocations and Declarations -------------------------------------------------------------------------- */ FILE *fp = (FILE *)NULL, *rastopenfile(); /* pointer to opened filename */ -char texfile[2048] = "\000", /* local copy of input filename */ - filebuff[16384] = "\000", /* entire contents of file */ - tag1[512], tag2[512], /* left and right */ +char texfile[1024] = "\000", /* local copy of input filename */ + filebuff[MAXFILESZ+1] = "\000", /* entire contents of file */ + tag1[1024], tag2[1024], /* left and right */ *strchange(), /* put value between ...*/ *timestamp(); /* log modification time */ int istag=0, rastreadfile(), /* read file if tag!=NULL */ /*isstrict = (seclevel>5? 1:0),*/ /*true only writes existing files*/ isnewfile = 0, /* true if writing new file */ status = 0; /* status returned, 1=okay */ +int istimestamp = 0; /* true to update tag */ /* -------------------------------------------------------------------------- check args -------------------------------------------------------------------------- */ @@ -8830,7 +10934,8 @@ if ( filename == (char *)NULL /* quit i if ( strlen(filename) < 2 /* quit if unreasonable filename */ || *value == '\000' ) goto end_of_job; /* or empty value string supplied */ /* --- establish filename[.tex] --- */ -strcpy(texfile,filename); /* local copy of input filename */ +strncpy(texfile,filename,1023); /* local copy of input filename */ +texfile[1023] = '\000'; /* make sure it's null terminated */ if ( rastopenfile(texfile,NULL) /* unchanged or .tex appended */ == (FILE *)NULL ) /* can't open, so write new file */ { if ( isstrict ) goto end_of_job; /* fail if new files not permitted */ @@ -8850,8 +10955,8 @@ read existing file if just rewriting a s *filebuff = '\000'; /* init as empty file */ if ( !isnewfile ) /* if file already exists */ if ( istag ) /* and just rewriting one tag */ - if ( rastreadfile(texfile,NULL,filebuff) /* read entire existing file */ - == 0 ) goto end_of_job; /* signal error if failed to read */ + if ( rastreadfile(texfile,1,NULL,filebuff) /* read entire existing file */ + <= 0 ) goto end_of_job; /* signal error if failed to read */ /* -------------------------------------------------------------------------- construct new file data if needed (entire file replaced by value if no tag) -------------------------------------------------------------------------- */ @@ -8888,7 +10993,7 @@ if ( istag ) /* only replacing tag in >= 0 ) /* usually precedes */ strchange(flen,tagp1+tlen1,value); /* change ...'s to value */ else /* weirdly, precedes */ - { char fbuff[2048]; /* field buff for value */ + { char fbuff[4096]; /* field buff for value */ if ( (flen = ((int)(tagp1-tagp2))+tlen1) /* strlen(...) */ <= 0 ) goto end_of_job; /* must be internal error */ strcpy(fbuff,tag1); /* set opening */ @@ -8908,13 +11013,15 @@ if ( fputs((istag?filebuff:value),fp) /* != EOF ) status = 1; /* signal success if succeeded */ fclose ( fp ); /* close output file after writing */ /* --- modify timestamp --- */ -if ( istag ) /* log mod time in tagged file */ - if ( strstr(tag,"timestamp") == (char *)NULL ) /* but avoid recursion */ - { char fbuff[2048]; /* field buff value */ - strcpy(fbuff,tag); /* tag modified */ - strcat(fbuff," modified at "); /* spacer */ - strcat(fbuff,timestamp()); /* start with timestamp */ - rastwritefile(filename,"timestamp",fbuff,1); } +if ( status > 0 ) /*forget timestamp if write failed*/ + if ( istimestamp ) /* if we're updating timestamp */ + if ( istag ) /* only log time in tagged file */ + if ( strstr(tag,"timestamp") == (char *)NULL ) /* but avoid recursion */ + { char fbuff[2048]; /* field buff value */ + strcpy(fbuff,tag); /* tag modified */ + strcat(fbuff," modified at "); /* spacer */ + strcat(fbuff,timestamp(TZDELTA,0)); /* start with timestamp */ + status = rastwritefile(filename,"timestamp",fbuff,1); } /* --- return status to caller --- */ end_of_job: return ( status ); /* return status to caller */ @@ -8922,11 +11029,117 @@ end_of_job: /* ========================================================================== - * Function: timestamp ( ) + * Function: calendar ( year, month, day ) + * Purpose: returns null-terminated character string containing + * \begin{array}...\end{array} for the one-month calendar + * specified by year=1973...2099 and month=1...12. + * If either arg out-of-range, today's value is used. + * -------------------------------------------------------------------------- + * Arguments: year (I) int containing 1973...2099 or 0 for current + * year + * month (I) int containing 1...12 or 0 for current month + * day (I) int containing day to emphasize or 0 + * -------------------------------------------------------------------------- + * Returns: ( char * ) char ptr to null-terminated buffer + * containing \begin{array}...\end{array} + * string that will render calendar for + * requested month, or NULL for any error. + * -------------------------------------------------------------------------- + * Notes: o + * ======================================================================= */ +/* --- entry point --- */ +char *calendar( int year, int month, int day ) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +static char calbuff[4096]; /* calendar returned to caller */ +time_t time_val = (time_t)(0); /* binary value returned by time() */ +struct tm *tmstruct=(struct tm *)NULL, *localtime(); /* interpret time_val */ +int yy=0, mm=0, dd=0; /* today (emphasize today's dd) */ +int idd=1, iday=0, daynumber(); /* day-of-week for idd=1...31 */ +char aval[64]; /* ascii day or 4-digit year */ +/* --- calendar data --- */ +static char *monthnames[] = { "?", "January", "February", "March", "April", + "May", "June", "July", "August", "September", "October", + "November", "December", "?" } ; +static int modays[] = + { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0 }; +/* ------------------------------------------------------------------------- +initialization +-------------------------------------------------------------------------- */ +/* --- get current date/time --- */ +time((time_t *)(&time_val)); /* get date and time */ +tmstruct = localtime((time_t *)(&time_val)); /* interpret time_val */ +yy = 1900 + (int)(tmstruct->tm_year); /* current four-digit year */ +mm = 1 + (int)(tmstruct->tm_mon); /* current month, 1-12 */ +dd = (int)(tmstruct->tm_mday); /* current day, 1-31 */ +/* --- check args --- */ +if ( year<1973 || year>2099 ) year = yy; /* current year if out-of-bounds */ +if ( month<1 || month>12 ) month = mm; /* current month if out-of-bounds */ +if ( month==mm && year==yy && day==0 ) /* current month and default day */ + day = dd; /* emphasize current day */ +modays[2] = (year%4==0?29:28); /* Feb has 29 days in leap years */ +/* --- initialize calendar string --- */ +strcpy(calbuff,"{\\begin{gather}"); /* center `month year` above cal */ +strcat(calbuff,"\\small\\text{"); /* month set in roman */ +strcat(calbuff,monthnames[month]); /* insert month name */ +strcat(calbuff," }"); /* add a space */ +sprintf(aval,"%d",year); /* convert year to ascii */ +strcat(calbuff,aval); /* add year */ +strcat(calbuff,"\\\\"); /* end top row */ +strcat(calbuff, /* now begin calendar arrayr */ + "\\begin{array}{|c|c|c|c|c|c|c|CCCCCC} \\hline" + "\\tiny\\text{Sun} & \\tiny\\text{Mon} & \\tiny\\text{Tue} &" + "\\tiny\\text{Wed} & \\tiny\\text{Thu} & \\tiny\\text{Fri} &" + "\\tiny\\text{Sat} \\\\ \\hline " ); +/* ------------------------------------------------------------------------- +generate calendar +-------------------------------------------------------------------------- */ +for ( idd=1; idd<=modays[month]; idd++ ) /* run through days of month */ + { + /* --- get day-of-week for this day --- */ + iday = 1 + (daynumber(year,month,idd)%7); /* 1=Monday...7=Sunday */ + if ( iday == 7 ) iday = 0; /* now 0=Sunday...6=Saturday */ + /* --- may need empty cells at beginning of month --- */ + if ( idd == 1 ) /* first day of month */ + if ( iday > 0 ) /* need to skip cells */ + { strcpy(aval,"\\ &\\ &\\ &\\ &\\ &\\ &\\ &\\ &\\ &\\"); /*cells to skip*/ + aval[3*iday] = '\000'; /*skip cells preceding 1st of month*/ + strcat(calbuff,aval); } /* add skip string to buffer */ + /* --- add idd to current cell --- */ + sprintf(aval,"%d",idd); /* convert idd to ascii */ + if ( idd == day /* emphasize today's date */ + /*&& month==mm && year==yy*/ ) /* only if this month's calendar */ + { strcat(calbuff,"{\\fs{-1}\\left\\langle "); /*emphasize, 1 size smaller*/ + strcat(calbuff,aval); /* put in idd */ + strcat(calbuff,"\\right\\rangle}"); } /* finish emphasis */ + else /* not today's date */ + strcat(calbuff,aval); /* so just put in idd */ + /* --- terminate cell --- */ + if ( idd < modays[month] ) { /* not yet end-of-month */ + if ( iday < 6 ) /* still have days left in week */ + strcat(calbuff,"&"); /* new cell in same week */ + else /* reached end-of-week */ + strcat(calbuff,"\\\\ \\hline"); } /* so start new week */ + } /* --- end-of-for(idd) --- */ +strcat(calbuff,"\\\\ \\hline"); /* final underline at end-of-month */ +/* --- return calendar to caller --- */ +strcat(calbuff,"\\end{array}\\end{gather}}"); /* terminate array */ +return ( calbuff ); /* back to caller with calendar */ +} /* --- end-of-function calendar() --- */ + + +/* ========================================================================== + * Function: timestamp ( tzdelta, ifmt ) * Purpose: returns null-terminated character string containing * current date:time stamp as ccyy-mm-dd:hh:mm:ss{am,pm} * -------------------------------------------------------------------------- - * Arguments: ( none ) + * Arguments: tzdelta (I) integer, positive or negative, containing + * containing number of hours to be added or + * subtracted from system time (to accommodate + * your desired time zone). + * ifmt (I) integer containing 0 for default format * -------------------------------------------------------------------------- * Returns: ( char * ) ptr to null-terminated buffer * containing current date:time stamp @@ -8934,40 +11147,209 @@ end_of_job: * Notes: o * ======================================================================= */ /* --- entry point --- */ -char *timestamp( ) +char *timestamp( int tzdelta, int ifmt ) { /* ------------------------------------------------------------------------- Allocations and Declarations -------------------------------------------------------------------------- */ -static char timebuff[64]; /* date:time buffer back to caller */ +static char timebuff[256]; /* date:time buffer back to caller */ /*long time_val = 0L;*/ /* binary value returned by time() */ time_t time_val = (time_t)(0); /* binary value returned by time() */ struct tm *tmstruct=(struct tm *)NULL, *localtime(); /* interpret time_val */ -int year=0, hour=0,ispm=1; /* adjust year, and set am/pm hour */ +int year=0, hour=0,ispm=1, /* adjust year, and set am/pm hour */ + month=0, day=0; /* adjust day and month for delta */ +int tzadjust(); /* time zone adjustment function */ +int daynumber(); /* #days since Jan 1, 1973 */ +static char *daynames[] = { "Monday", "Tuesday", "Wednesday", + "Thursday", "Friday", "Saturday", "Sunday" } ; +static char *monthnames[] = { "?", "January", "February", "March", "April", + "May", "June", "July", "August", "September", "October", + "November", "December", "?" } ; /* ------------------------------------------------------------------------- get current date:time, adjust values, and and format stamp -------------------------------------------------------------------------- */ +/* --- first init returned timebuff in case of any error --- */ +*timebuff = '\000'; /* --- get current date:time --- */ time((time_t *)(&time_val)); /* get date and time */ tmstruct = localtime((time_t *)(&time_val)); /* interpret time_val */ -/* --- adjust year and hour as necessary --- */ -year = (int)(tmstruct->tm_year); /* local copy of year */ -hour = (int)(tmstruct->tm_hour); /* local copy of hour */ +/* --- extract fields --- */ +year = (int)(tmstruct->tm_year); /* local copy of year, 0=1900 */ +month = (int)(tmstruct->tm_mon) + 1; /* local copy of month, 1-12 */ +day = (int)(tmstruct->tm_mday); /* local copy of day, 1-31 */ +hour = (int)(tmstruct->tm_hour); /* local copy of hour, 0-23 */ +/* --- adjust year --- */ year += 1900; /* set century in year */ -if ( hour < 12 ) /* am check */ - { ispm=0; /* reset pm flag */ - if ( hour == 0 ) hour = 12; } /* set 00hrs = 12am */ -if ( hour > 12 ) hour -= 12; /* pm check sets 13hrs to 1pm, etc */ +/* --- adjust for timezone --- */ +tzadjust(tzdelta,&year,&month,&day,&hour); +/* --- check params --- */ +if ( hour<0 || hour>23 +|| day<1 || day>31 +|| month<1 || month>12 +|| year<1973 ) goto end_of_job; +/* --- adjust hour for am/pm --- */ +switch ( ifmt ) + { + default: + case 0: + if ( hour < 12 ) /* am check */ + { ispm=0; /* reset pm flag */ + if ( hour == 0 ) hour = 12; } /* set 00hrs = 12am */ + if ( hour > 12 ) hour -= 12; /* pm check sets 13hrs to 1pm, etc */ + break; + } /* --- end-of-switch(ifmt) --- */ /* --- format date:time stamp --- */ -sprintf(timebuff,"%04d-%02d-%02d:%02d:%02d:%02d%s", - year,(int)((tmstruct->tm_mon)+1),(int)(tmstruct->tm_mday), - hour,(int)(tmstruct->tm_min),(int)(tmstruct->tm_sec),((ispm)?"pm":"am")); -return ( timebuff ); /* return stamp to caller */ +switch ( ifmt ) + { + default: + case 0: /* --- 2005-03-05:11:49:59am --- */ + sprintf(timebuff,"%04d-%02d-%02d:%02d:%02d:%02d%s", year,month,day, + hour,(int)(tmstruct->tm_min),(int)(tmstruct->tm_sec),((ispm)?"pm":"am")); + break; + case 1: /* --- Saturday, March 5, 2005 --- */ + sprintf(timebuff,"%s, %s %d, %d", + daynames[daynumber(year,month,day)%7],monthnames[month],day,year); + break; + case 2: /* --- Saturday, March 5, 2005, 11:49:59am --- */ + sprintf(timebuff,"%s, %s %d, %d, %d:%02d:%02d%s", + daynames[daynumber(year,month,day)%7],monthnames[month],day,year, + hour,(int)(tmstruct->tm_min),(int)(tmstruct->tm_sec),((ispm)?"pm":"am")); + break; + case 3: /* --- 11:49:59am --- */ + sprintf(timebuff,"%d:%02d:%02d%s", + hour,(int)(tmstruct->tm_min),(int)(tmstruct->tm_sec),((ispm)?"pm":"am")); + break; + } /* --- end-of-switch(ifmt) --- */ +end_of_job: + return ( timebuff ); /* return stamp to caller */ } /* --- end-of-function timestamp() --- */ /* ========================================================================== - * Function: dtoa ( dblval, npts ) + * Function: tzadjust ( tzdelta, year, month, day, hour ) + * Purpose: Adjusts hour, and day,month,year if necessary, + * by delta increment to accommodate your time zone. + * -------------------------------------------------------------------------- + * Arguments: tzdelta (I) integer, positive or negative, containing + * containing number of hours to be added or + * subtracted from given time (to accommodate + * your desired time zone). + * year (I) addr of int containing 4-digit year + * month (I) addr of int containing month 1=Jan - 12=Dec. + * day (I) addr of int containing day 1-31 for Jan. + * hour (I) addr of int containing hour 0-23 + * Returns: ( int ) 1 for success, or 0 for error + * -------------------------------------------------------------------------- + * Notes: o + * ======================================================================= */ +/* --- entry point --- */ +int tzadjust ( int tzdelta, int *year, int *month, int *day, int *hour ) +{ +/* -------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +int yy = *year, mm = *month, dd = *day, hh = *hour; /*dereference args*/ +/* --- calendar data --- */ +static int modays[] = + { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0 }; +/* -------------------------------------------------------------------------- +check args +-------------------------------------------------------------------------- */ +if ( mm<1 || mm>12 ) return(-1); /* bad month */ +if ( dd<1 || dd>modays[mm] ) return(-1); /* bad day */ +if ( hh<0 || hh>23 ) return(-1); /* bad hour */ +if ( tzdelta>23 || tzdelta<(-23) ) return(-1); /* bad tzdelta */ +/* -------------------------------------------------------------------------- +make adjustments +-------------------------------------------------------------------------- */ +/* --- adjust hour --- */ +hh += tzdelta; /* apply caller's delta */ +/* --- adjust for feb 29 --- */ +modays[2] = (yy%4==0?29:28); /* Feb has 29 days in leap years */ +/* --- adjust day --- */ +if ( hh < 0 ) /* went to preceding day */ + { dd--; hh += 24; } +if ( hh > 23 ) /* went to next day */ + { dd++; hh -= 24; } +/* --- adjust month --- */ +if ( dd < 1 ) /* went to preceding month */ + { mm--; dd = modays[mm]; } +if ( dd > modays[mm] ) /* went to next month */ + { mm++; dd = 1; } +/* --- adjust year --- */ +if ( mm < 1 ) /* went to preceding year */ + { yy--; mm = 12; dd = modays[mm]; } +if ( mm > 12 ) /* went to next year */ + { yy++; mm = 1; dd = 1; } +/* --- back to caller --- */ +*year=yy; *month=mm; *day=dd; *hour=hh; /* reset adjusted args */ +return ( 1 ); +} /* --- end-of-function tzadjust() --- */ + + +/* ========================================================================== + * Function: daynumber ( year, month, day ) + * Purpose: Returns number of actual calendar days from Jan 1, 1973 + * to the given date (e.g., bvdaynumber(1974,1,1)=365). + * -------------------------------------------------------------------------- + * Arguments: year (I) int containing year -- may be either 1995 or + * 95, or may be either 2010 or 110 for those + * years. + * month (I) int containing month, 1=Jan thru 12=Dec. + * day (I) int containing day of month, 1-31 for Jan, etc. + * Returns: ( int ) Number of days from Jan 1, 1973 to given date, + * or -1 for error (e.g., year<1973). + * -------------------------------------------------------------------------- + * Notes: o + * ======================================================================= */ +/* --- entry point --- */ +int daynumber ( int year, int month, int day ) +{ +/* -------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +/* --- returned value (note: returned as a default "int") --- */ +int ndays; /* #days since jan 1, year0 */ +/* --- initial conditions --- */ +static int year0 = 73, /* jan 1 was a monday, 72 was a leap */ + days4yrs = 1461, /* #days in 4 yrs = 365*4 + 1 */ + days1yr = 365; +/* --- table of accumulated days per month (last index not used) --- */ +static int modays[] = + { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }; +/* --- variables for #days since day0 --- */ +int nyears, nfouryrs; /*#years, #4-yr periods since year0*/ +/* -------------------------------------------------------------------------- +Check input +-------------------------------------------------------------------------- */ +if ( month < 1 || month > 12 ) /*month used as index, so must be ok*/ + return ( -1 ); /* otherwise, forget it */ +if ( year >= 1900 ) year -= 1900; /*use two-digit years (3 after 2000)*/ +/* -------------------------------------------------------------------------- +Find #days since jan 1, 1973 +-------------------------------------------------------------------------- */ +/* --- figure #complete 4-year periods and #remaining yrs till current --- */ +nyears = year - year0; /* #years since year0 */ +if ( nyears < 0 ) return ( -1 ); /* we're not working backwards */ +nfouryrs = nyears/4; /* #complete four-year periods */ +nyears -= (4*nfouryrs); /* remainder excluding current year*/ +/* --- #days from jan 1, year0 till jan 1, this year --- */ +ndays = (days4yrs*nfouryrs) /* #days in 4-yr periods */ + + (days1yr*nyears); /* +remaining days */ +/*if ( year > 100 ) ndays--;*/ /* subtract leap year for 2000AD */ +/* --- add #days within current year --- */ +ndays += (modays[month-1] + (day-1)); +/* --- may need an extra day if current year is a leap year --- */ +if ( nyears == 3 ) /*three preceding yrs so this is 4th*/ + { if ( month > 2 ) /* past feb so need an extra day */ + /*if ( year != 100 )*/ /* unless it's 2000AD */ + ndays++; } /* so add it in */ +return ( (int)(ndays) ); /* #days back to caller */ +} /* --- end-of-function daynumber() --- */ + + +/* ========================================================================== + * Function: dbltoa ( dblval, npts ) * Purpose: Converts double to ascii, in financial format * (e.g., comma-separated and negatives enclosed in ()'s). * ------------------------------------------------------------------------- @@ -8980,14 +11362,14 @@ return ( timebuff ); /* return stamp t * Notes: o * ======================================================================= */ /* --- entry point --- */ -char *dtoa ( dblval, npts ) -double dblval; -int npts; +char *dbltoa ( double dblval, int npts ) +/* double dblval; + int npts; */ { /* ------------------------------------------------------------------------- Allocations and Declarations ------------------------------------------------------------------------- */ -static char finval[128]; /* buffer returned to caller */ +static char finval[256]; /* buffer returned to caller */ static char digittbl[32]="0123456789*"; /* table of ascii decimal digits */ char *finptr = finval; /* ptr to next char being converted*/ double floor(); /* integer which is glb(double) */ @@ -9048,7 +11430,7 @@ End-of-Job if ( isneg ) *finptr++ = ')'; /*trailing paren for negative value*/ *finptr = '\000'; /* null-terminate converted double */ return ( finval ); /* converted double back to caller */ -} /* --- end-of-function dtoa() --- */ +} /* --- end-of-function dbltoa() --- */ /* ========================================================================== @@ -9152,7 +11534,7 @@ for ( irow=0; irowheight; irow++ ) && bytemap[ipixel] > blackscale ) /* weighted avg > blackscale */ bytemap[ipixel] = grayscale-1; } /* so force it entirely black */ /*--- only anti-alias pixels whose adjacent pixels fall within bounds ---*/ - if ( !iscenter ) /* apply min/maxadjacent test */ + if ( !iscenter ) { /* apply min/maxadjacent test */ if ( isminmaxwts ) /* min/max refer to adjacent weights*/ { if ( wadjacent < minadjacent /* wts of adjacent points too low */ || wadjacent > maxadjacent ) /* or too high */ @@ -9160,7 +11542,7 @@ for ( irow=0; irowheight; irow++ ) else /* min/max refer to #adjacent points*/ { if ( nadjacent < minadjacent /* too few adjacent points black */ || nadjacent > maxadjacent ) /* or too many */ - bytemap[ipixel] = 0; } /* so leave point white */ + bytemap[ipixel] = 0; } } /* so leave point white */ } /* --- end-of-for(irow,icol) --- */ /* ------------------------------------------------------------------------- Back to caller with gray-scale anti-aliased bytemap @@ -9202,6 +11584,7 @@ int width=rp->width, height=rp->height, icol = 0, irow = 0, /* width, height indexes */ imap = (-1); /* pixel index = icol + irow*width */ int bgbitval=0, fgbitval=1; /* background, foreground bitval */ +int isfirstaa = 1; /*debugging switch signals 1st pixel*/ #if 0 int totwts=12, wts[9]={1,1,1, 1,4,1, 1,1,1}; /* pnmalias default wts */ int totwts=16, wts[9]={1,2,1, 2,4,2, 1,2,1}; /* weights */ @@ -9279,6 +11662,39 @@ for ( irow=0; irow 0 ) ngaps /= 2; /* each gap has 2 bg/fg flips */ + if ( ngapsmaxgaps ) continue; + } /* --- end-of-if(1) --- */ /* --- antialias if necessary --- */ if ( (isbgalias && isbgedge) /* alias pixel surrounding bg */ || (isfgalias && isfgedge) /* alias pixel surrounding fg */ @@ -9291,9 +11707,11 @@ for ( irow=0; irow=99 && msgfp!=NULL ) fprintf(msgfp, /* debugging */ - "aapnm> irow,icol,imap=%d,%d,%d aawtval=%.4f aabyteval=%d\n", + if ( msglevel>=99 && msgfp!=NULL ) { fprintf(msgfp, /*diagnostic output*/ + "%s> irow,icol,imap=%d,%d,%d aawtval=%.4f aabyteval=%d\n", + (isfirstaa?"aapnm algorithm":"aapnm"), irow,icol,imap, aawtval,aabyteval); + isfirstaa = 0; } } /* --- end-of-if(isedge) --- */ } /* --- end-of-for(irow,icol) --- */ /* ------------------------------------------------------------------------- @@ -9305,6 +11723,1182 @@ Back to caller with gray-scale anti-alia /* ========================================================================== + * Function: aapnmlookup ( rp, bytemap, grayscale ) + * Purpose: calculates a lowpass anti-aliased bytemap + * for rp->bitmap, with each byte 0...grayscale-1, + * based on the pnmalias.c algorithm. + * This version uses aagridnum() and aapatternnum() lookups + * to interpret 3x3 lowpass pixel grids. + * -------------------------------------------------------------------------- + * Arguments: rp (I) raster * to raster whose bitmap + * is to be anti-aliased + * bytemap (O) intbyte * to bytemap, calculated + * by applying pnm-based filter to rp->bitmap, + * and returned (as you'd expect) in 1-to-1 + * addressing correspondence with rp->bitmap + * grayscale (I) int containing number of grayscales + * to be calculated, 0...grayscale-1 + * (should typically be given as 256) + * -------------------------------------------------------------------------- + * Returns: ( int ) 1=success, 0=any error + * -------------------------------------------------------------------------- + * Notes: o Based on the pnmalias.c algorithm in the netpbm package + * on sourceforge. + * o This version uses aagridnum() and aapatternnum() lookups + * to interpret 3x3 lowpass pixel grids. + * ======================================================================= */ +/* --- entry point --- */ +int aapnmlookup (raster *rp, intbyte *bytemap, int grayscale) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +int width=rp->width, height=rp->height, /* width, height of raster */ + icol = 0, irow = 0, /* width, height indexes */ + imap = (-1); /* pixel index = icol + irow*width */ +int bgbitval=0, fgbitval=1; /* background, foreground bitval */ +int isfirstaa = 1; /*debugging switch signals 1st pixel*/ +int aacenterwt=centerwt, aaadjacentwt=adjacentwt, aacornerwt=cornerwt, + totwts = centerwt + 4*(adjacentwt+cornerwt); /*pnmalias default wts*/ +int isfgalias = fgalias, /*(1) true to antialias fg bits */ + isfgonly = fgonly, /*(0) true to only antialias fg bits*/ + isbgalias = bgalias, /*(0) true to antialias bg bits */ + isbgonly = bgonly; /*(0) true to only antialias bg bits*/ +int gridnum=(-1), aagridnum(), /* grid# for 3x3 grid at irow,icol */ + patternum=(-1), aapatternnum(); /*pattern#, 1-51, for input gridnum*/ +int aapatterns(); /* to antialias special patterns */ +/* --- + * pattern number data + * ------------------- */ +/* --- number of adjacent fg pixels set in pattern --- */ +static int nadjacents[] = { -1, /* #adjacent fg pixels for pattern */ + 0, 4, 0, 1, 4, 3, 1, 0, 1, 0, /* 1-10 */ + 2, 2, 3, 4, 3, 4, 2, 2, 1, 2, /* 11-20 */ + 1, 2, 1, 2, 0, 1, 3, 2, 3, 2, /* 21-30 */ + 3, 2, 3, 2, 4, 3, 1, 2, 2, 2, /* 31-40 */ + 2, 1, 2, 2, 3, 0, 3, 2, 2, 1, 4, /* 41-51 */ + -1 } ; /* --- end-of-nadjacents[] --- */ +/* --- number of corner fg pixels set in pattern --- */ +static int ncorners[] = { -1, /* #corner fg pixels for pattern */ + 0, 4, 1, 0, 3, 4, 1, 2, 1, 2, /* 1-10 */ + 0, 0, 3, 2, 3, 2, 4, 4, 2, 1, /* 11-20 */ + 2, 1, 2, 1, 3, 2, 0, 1, 2, 3, /* 21-30 */ + 2, 3, 2, 3, 1, 2, 4, 3, 2, 2, /* 31-40 */ + 2, 3, 2, 2, 1, 4, 1, 2, 2, 3, 0, /* 41-51 */ + -1 } ; /* --- end-of-ncorners[] --- */ +/* --- 0,1,2=pattern contains horizontal bg,fg,both edge; -1=no edge --- */ +static int horzedges[] = { -1, /* 0,1,2 = horz bg,fg,both edge */ + 0, 1, 0, 0, 1, 1, 0, 0, 0, -1, /* 1-10 */ + 0, -1, 1, 1, 1, -1, 1, -1, 2, 0, /* 11-20 */ + -1, -1, -1, 0, -1, -1, -1, -1, 2, 1, /* 21-30 */ + -1, -1, -1, 1, -1, -1, -1, -1, 2, -1, /* 31-40 */ + -1, 1, 1, -1, -1, -1, 0, 0, -1, -1, -1, /* 41-51 */ + -1 } ; /* --- end-of-horzedges[] --- */ +/* --- 0,1,2=pattern contains vertical bg,fg,both edge; -1=no edge --- */ +static int vertedges[] = { -1, /* 0,1,2 = vert bg,fg,both edge */ + 0, 1, 0, 0, 1, 1, 0, -1, -1, -1, /* 1-10 */ + 0, 0, 1, -1, -1, -1, 1, 1, -1, -1, /* 11-20 */ + -1, 0, 0, 0, -1, -1, 0, -1, -1, -1, /* 21-30 */ + -1, 1, 1, 1, -1, -1, 1, -1, -1, -1, /* 31-40 */ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 41-51 */ + -1 } ; /* --- end-of-vertedges[] --- */ +/* --- 0,1,2=pattern contains diagonal bg,fg,both edge; -1=no edge --- */ +static int diagedges[] = { -1, /* 0,1,2 = diag bg,fg,both edge */ + 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, /* 1-10 */ + 2, -1, 1, 1, 1, 1, 1, -1, 0, 2, /* 11-20 */ + 0, -1, 0, 2, 0, -1, 1, 1, 1, 1, /* 21-30 */ + 1, -1, 1, 2, 1, 1, -1, 1, 2, -1, /* 31-40 */ + 1, 0, -1, 2, 1, 0, 1, -1, 1, -1, 1, /* 41-51 */ + -1 } ; /* --- end-of-diagedges[] --- */ +/* ------------------------------------------------------------------------- +Calculate bytemap as 9-point weighted average over bitmap +-------------------------------------------------------------------------- */ +for ( irow=0; irow511 ) continue; /* gridnum out of bounds*/ + /* --- check if we're antialiasing this pixel --- */ + if ( (isbgonly && bitval==fgbitval) /* only antialias background bit */ + || (isfgonly && bitval==bgbitval) ) /* only antialias foreground bit */ + continue; /* leave default and do next bit */ + /* --- look up pattern number, 1-51, corresponding to input gridnum --- */ + patternum = aapatternnum(gridnum); /* look up pattern number */ + if ( patternum<1 || patternum>51 ) continue; /* some internal error */ + /* --- special pattern number processing --- */ + if ( (aabyteval = aapatterns(rp,irow,icol,gridnum,patternum,grayscale)) + >= 0 ) { /* special processing for pattern */ + bytemap[imap] = (intbyte)(aabyteval); /* set antialiased pixel */ + continue; } /* and continue with next pixel */ + /* --- check for diagonal edges --- */ + isbgdiag = ( diagedges[patternum]==2 || /*current pixel borders a bg edge*/ + diagedges[patternum]==0 ); + isfgdiag = ( diagedges[patternum]==2 || /*current pixel borders a fg edge*/ + diagedges[patternum]==1 ); + /* ---check top/bot left/right edges for corners (added by j.forkosh)--- */ + if ( 1 ) { /* true to perform test */ + int isbghorz=0, isfghorz=0, isbgvert=0, isfgvert=0, /* horz/vert edges */ + horzedge=horzedges[patternum], vertedge=vertedges[patternum]; + isbghorz = (horzedge==2||horzedge==0); /* top or bottom edge is all bg */ + isfghorz = (horzedge==2||horzedge==1); /* top or bottom edge is all fg */ + isbgvert = (vertedge==2||vertedge==0); /* left or right edge is all bg */ + isfgvert = (vertedge==2||vertedge==1); /* left or right edge is all fg */ + if ( (isbghorz && isbgvert && (bitval==fgbitval)) /* we're at an...*/ + || (isfghorz && isfgvert && (bitval==bgbitval)) ) /*...inside corner */ + continue; /* don't antialias */ + } /* --- end-of-if(1) --- */ +#if 0 + /* --- check #gaps for checkerboard (added by j.forkosh) --- */ + if ( 0 ) { /* true to perform test */ + int ngaps=0, mingaps=1,maxgaps=2; /* count #fg/bg flips (max=4 noop) */ + if ( nwbitval!=nnbitval ) ngaps++; /* upper-left =? upper */ + if ( nnbitval!=nebitval ) ngaps++; /* upper =? upper-right */ + if ( nebitval!=eebitval ) ngaps++; /* upper-right =? right */ + if ( eebitval!=sebitval ) ngaps++; /* right =? lower-right */ + if ( sebitval!=ssbitval ) ngaps++; /* lower-right =? lower */ + if ( ssbitval!=swbitval ) ngaps++; /* lower =? lower-left */ + if ( swbitval!=wwbitval ) ngaps++; /* lower-left =? left */ + if ( wwbitval!=nwbitval ) ngaps++; /* left =? upper-left */ + if ( ngaps > 0 ) ngaps /= 2; /* each gap has 2 bg/fg flips */ + if ( ngapsmaxgaps ) continue; + } /* --- end-of-if(1) --- */ +#endif + /* --- antialias if necessary --- */ + if ( (isbgalias && isbgdiag) /* alias pixel surrounding bg */ + || (isfgalias && isfgdiag) /* alias pixel surrounding fg */ + || (isbgdiag && isfgdiag) ) /* neighboring fg and bg pixel */ + { + int aasumval = /* sum wts[]*bitmap[] */ + aacenterwt*bitval + /* apply centerwt to center pixel */ + aaadjacentwt*nadjacents[patternum] + /* similarly for adjacents */ + aacornerwt*ncorners[patternum]; /* and corners */ + double aawtval = ((double)aasumval)/((double)totwts); /* weighted val */ + aabyteval= (int)(((double)(grayscale-1))*aawtval+0.5); /*0...grayscale-1*/ + bytemap[imap] = (intbyte)(aabyteval); /* set antialiased pixel */ + if ( msglevel>=99 && msgfp!=NULL ) { fprintf(msgfp, /*diagnostic output*/ + "%s> irow,icol,imap=%d,%d,%d aawtval=%.4f aabyteval=%d", + (isfirstaa?"aapnmlookup algorithm":"aapnm"), + irow,icol,imap, aawtval,aabyteval); + if ( msglevel < 100 ) fprintf(msgfp,"\n"); /* no more output */ + else fprintf(msgfp,", grid#,pattern#=%d,%d\n",gridnum,patternum); + isfirstaa = 0; } + } /* --- end-of-if(isedge) --- */ + } /* --- end-of-for(irow,icol) --- */ +/* ------------------------------------------------------------------------- +Back to caller with gray-scale anti-aliased bytemap +-------------------------------------------------------------------------- */ +/*end_of_job:*/ + return ( 1 ); +} /* --- end-of-function aapnmlookup() --- */ + + +/* ========================================================================== + * Function: aapatterns ( rp, irow, icol, gridnum, patternum, grayscale ) + * Purpose: For patterns requireing special processing, + * calculates anti-aliased value for pixel at irow,icol, + * whose surrounding 3x3 pixel grid is coded by gridnum + * (which must correspond to a pattern requiring special + * processing). + * -------------------------------------------------------------------------- + * Arguments: rp (I) raster * to raster whose bitmap + * is to be anti-aliased + * irow (I) int containing row, 0...height-1, + * of pixel to be antialiased + * icol (I) int containing col, 0...width-1, + * of pixel to be antialiased + * gridnum (I) int containing 0...511 corresponding to + * 3x3 pixel grid surrounding irow,icol + * patternum (I) int containing 1...51 pattern# of + * the 3x3 grid surrounding irow,icol + * grayscale (I) int containing number of grayscales + * to be calculated, 0...grayscale-1 + * (should typically be given as 256) + * -------------------------------------------------------------------------- + * Returns: ( int ) 0...grayscale-1 for success, + * -1 = error, or no special processing required + * -------------------------------------------------------------------------- + * Notes: o + * ======================================================================= */ +/* --- entry point --- */ +int aapatterns (raster *rp, int irow, int icol, + int gridnum, int patternum, int grayscale) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +int aaval = (-1); /* antialiased value returned */ +int iscenter = (gridnum&1); /* true if center pixel set/black */ +int aapatternnum(), /* if patternum not supplied */ + aapattern1124(), /* routine for patterns #11,24 */ + aapattern19(), /* special routine for pattern #19 */ + aapattern20(), /* special routine for pattern #20 */ + aapattern39(); /* special routine for pattern #39 */ +/* ------------------------------------------------------------------------- +special pattern number processing +-------------------------------------------------------------------------- */ +if ( 1 ) { + if ( patternum < 1 ) /* pattern# not supplied by caller */ + patternum = aapatternnum(gridnum); /* so look it up ourselves */ + switch ( patternum ) { + default: break; /* no special processing */ + case 11: + case 24: aaval = aapattern1124(rp,irow,icol,gridnum,grayscale); break; + case 19: aaval = aapattern19(rp,irow,icol,gridnum,grayscale); break; + case 20: aaval = aapattern20(rp,irow,icol,gridnum,grayscale); break; + case 39: aaval = aapattern39(rp,irow,icol,gridnum,grayscale); break; + /* case 24: if ( (gridnum&1) == 0 ) aaval=0; break; */ + case 29: aaval = (iscenter?grayscale-1:0); break; /* no antialiasing */ + } /* --- end-of-switch(patternum) --- */ + } /* --- end-of-if() --- */ +return ( aaval ); /* return antialiased val to caller*/ +} /* --- end-of-function aapatterns() --- */ + + +/* ========================================================================== + * Function: aapattern1124 ( rp, irow, icol, gridnum, grayscale ) + * Purpose: calculates anti-aliased value for pixel at irow,icol, + * whose surrounding 3x3 pixel grid is coded by gridnum + * (which must correspond to pattern #11 or #24). + * -------------------------------------------------------------------------- + * Arguments: rp (I) raster * to raster whose bitmap + * is to be anti-aliased + * irow (I) int containing row, 0...height-1, + * of pixel to be antialiased + * icol (I) int containing col, 0...width-1, + * of pixel to be antialiased + * gridnum (I) int containing 0...511 corresponding to + * 3x3 pixel grid surrounding irow,icol + * grayscale (I) int containing number of grayscales + * to be calculated, 0...grayscale-1 + * (should typically be given as 256) + * -------------------------------------------------------------------------- + * Returns: ( int ) 0...grayscale-1 for success, -1=any error + * -------------------------------------------------------------------------- + * Notes: o Handles the eight gridnum's + * (gridnum/2 shown to eliminate irrelevant low-order bit) + * --- --- -*- -*- + * --* = 10 *-- = 18 --* = 72 *-- = 80 (pattern$11) + * -*- -*- --- --- + * + * --- --- -** **- + * --* = 11 *-- = 22 --* = 104 *-- = 208 (pattern$24) + * -** **- --- --- + * o For black * center pixel, using grid#10 as an example, + * pixel stays --- antialiased ---* + * black if -*** if part of -** + * part of a -*- a diagonal -*- + * corner, eg, * line, eg, * + * ======================================================================= */ +/* --- entry point --- */ +int aapattern1124 (raster *rp, int irow, int icol, + int gridnum, int grayscale) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +int aaval = (-1); /* antialiased value returned */ +int iscenter = gridnum&1; /* true if pixel at irow,icol black*/ +int patternum = 24; /* init for pattern#24 default */ +pixbyte *bitmap = rp->pixmap; /* local rp->pixmap ptr */ +int width=rp->width, height=rp->height; /* width, height of raster */ +int jrow=irow, jcol=icol; /* corner or diagonal row,col */ +int vertcornval=0, horzcornval=0, /* vertical, horizontal corner bits*/ + topdiagval=0, botdiagval=0, /* upper,lower diagonal pixel bits */ + cornval=0, diagval=0; /* vert+horzcorn, top+botdiag */ +int hdirection=99, vdirection=99, /* horz,vert corner direction */ + hturn=99,vturn=99, aafollowline(); /* follow corner till turns */ +/* ------------------------------------------------------------------------- +Check corner and diagonal pixels +-------------------------------------------------------------------------- */ +if ( 0 ) goto end_of_job; /* true to turn off pattern1124 */ +switch ( gridnum/2 ) { /* check pattern#11,24 corner, diag*/ + default: goto end_of_job; /* not a pattern#11,24 gridnum */ + case 10: patternum=11; case 11: + hdirection = 2; vdirection = -1; /* directions to follow corner */ + if ( (jrow=irow+2) < height ) { /* vert corner below center pixel */ + vertcornval = getlongbit(bitmap,(icol+jrow*width)); + if ( (icol-1) >= 0 ) /* lower diag left of center */ + botdiagval = getlongbit(bitmap,((icol-1)+jrow*width)); } + if ( (jcol=icol+2) < width ) { /* horz corner right of center */ + horzcornval = getlongbit(bitmap,(jcol+irow*width)); + if ( (irow-1) >= 0 ) /* upper diag above center */ + topdiagval = getlongbit(bitmap,(jcol+(irow-1)*width)); } + break; + case 18: patternum=11; case 22: + hdirection = -2; vdirection = -1; /* directions to follow corner */ + if ( (jrow=irow+2) < height ) { /* vert corner below center pixel */ + vertcornval = getlongbit(bitmap,(icol+jrow*width)); + if ( (icol+1) < width ) /* lower diag right of center */ + botdiagval = getlongbit(bitmap,((icol+1)+jrow*width)); } + if ( (jcol=icol-2) >= 0 ) { /* horz corner left of center */ + horzcornval = getlongbit(bitmap,(jcol+irow*width)); + if ( (irow-1) >= 0 ) /* upper diag above center */ + topdiagval = getlongbit(bitmap,(jcol+(irow-1)*width)); } + break; + case 72: patternum=11; case 104: + hdirection = 2; vdirection = 1; /* directions to follow corner */ + if ( (jrow=irow-2) >= 0 ) { /* vert corner above center pixel */ + vertcornval = getlongbit(bitmap,(icol+jrow*width)); + if ( (icol-1) >= 0 ) /* upper diag left of center */ + topdiagval = getlongbit(bitmap,((icol-1)+jrow*width)); } + if ( (jcol=icol+2) < width ) { /* horz corner right of center */ + horzcornval = getlongbit(bitmap,(jcol+irow*width)); + if ( (irow+1) < height ) /* lower diag below center */ + botdiagval = getlongbit(bitmap,(jcol+(irow+1)*width)); } + break; + case 80: patternum=11; case 208: + hdirection = -2; vdirection = 1; /* directions to follow corner */ + if ( (jrow=irow-2) >= 0 ) { /* vert corner above center pixel */ + vertcornval = getlongbit(bitmap,(icol+jrow*width)); + if ( (icol+1) < width ) /* upper diag right of center */ + topdiagval = getlongbit(bitmap,((icol+1)+jrow*width)); } + if ( (jcol=icol-2) >= 0 ) { /* horz corner left of center */ + horzcornval = getlongbit(bitmap,(jcol+irow*width)); + if ( (irow+1) < height ) /* lower diag below center */ + botdiagval = getlongbit(bitmap,(jcol+(irow+1)*width)); } + break; + } /* --- end-of-switch(gridnum/2) --- */ +cornval = vertcornval+horzcornval; /* 0=no corner bits, 1, 2=both */ +diagval = topdiagval+botdiagval; /* 0=no diag bits, 1, 2=both */ +/* ------------------------------------------------------------------------- +Handle white center +-------------------------------------------------------------------------- */ +if ( 1 && !iscenter ) { aaval = (patternum==11?51:64); goto end_of_job; } +/* ------------------------------------------------------------------------- +Handle black center +-------------------------------------------------------------------------- */ +if ( diagval > 1 ) aaval = ( patternum==24? 255:191 ); +else { + hturn = aafollowline(rp,irow,icol,hdirection); + vturn = aafollowline(rp,irow,icol,vdirection); + if ( vturn*hdirection < 0 && hturn*vdirection < 0 ) + aaval = ( patternum==24? 255:191 ); + else aaval = grayscale-1; } /* actual corner */ +/* ------------------------------------------------------------------------- +Back to caller with grayscale antialiased value for pixel at irow,icol +-------------------------------------------------------------------------- */ +end_of_job: + if ( aaval >= 0 ) /* have antialiasing result */ + if ( msglevel>=99 && msgfp!=NULL ) fprintf(msgfp, /* diagnostic output */ + "aapattern1124> irow,icol,grid#/2=%d,%d,%d, top,botdiag=%d,%d, " + "vert,horzcorn=%d,%d, v,hdir=%d,%d, v,hturn=%d,%d, aaval=%d\n", + irow,icol,gridnum/2, topdiagval,botdiagval, vertcornval,horzcornval, + vdirection,hdirection, vturn,hturn, aaval); + return ( aaval ); /* back with antialiased value */ +} /* --- end-of-function aapattern1124() --- */ + + +/* ========================================================================== + * Function: aapattern19 ( rp, irow, icol, gridnum, grayscale ) + * Purpose: calculates anti-aliased value for pixel at irow,icol, + * whose surrounding 3x3 pixel grid is coded by gridnum + * (which must correspond to pattern #19). + * -------------------------------------------------------------------------- + * Arguments: rp (I) raster * to raster whose bitmap + * is to be anti-aliased + * irow (I) int containing row, 0...height-1, + * of pixel to be antialiased + * icol (I) int containing col, 0...width-1, + * of pixel to be antialiased + * gridnum (I) int containing 0...511 corresponding to + * 3x3 pixel grid surrounding irow,icol + * grayscale (I) int containing number of grayscales + * to be calculated, 0...grayscale-1 + * (should typically be given as 256) + * -------------------------------------------------------------------------- + * Returns: ( int ) 0...grayscale-1 for success, -1=any error + * -------------------------------------------------------------------------- + * Notes: o Handles the four gridnum's + * (gridnum/2 shown to eliminate irrelevant low-order bit) + * --- --* *-- *** + * --- = 7 --* = 41 *-- = 148 --- = 224 + * *** --* *-- --- + * ======================================================================= */ +/* --- entry point --- */ +int aapattern19 (raster *rp, int irow, int icol, + int gridnum, int grayscale) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +int aaval = (-1); /* antialiased value returned */ +int iscenter = gridnum&1; /* true if pixel at irow,icol black*/ +int orientation = 1, /* 1=vertical, 2=horizontal */ + jrow=irow, jcol=icol; /* middle pixel of *** line */ +int turn1=0,turn2=0, aafollowline(); /* follow *** line till it turns */ +/* ------------------------------------------------------------------------- +Initialization and calculation of antialiased value +-------------------------------------------------------------------------- */ +/* --- check input -- */ +if ( iscenter ) goto end_of_job; /* we only antialias white pixels */ +/* --- set params --- */ +switch ( gridnum/2 ) { /* check pattern#19 orientation */ + default: goto end_of_job; /* not a pattern#19 gridnum */ + case 7: orientation=2; jrow++; break; + case 41: orientation=1; jcol++; break; + case 148: orientation=1; jcol--; break; + case 224: orientation=2; jrow--; break; + } /* --- end-of-switch(gridnum/2) --- */ +/* --- get turns in both directions --- */ +if ( (turn1 = aafollowline(rp,jrow,jcol,orientation)) == 0 ) goto end_of_job; +if ( (turn2 = aafollowline(rp,jrow,jcol,-orientation)) == 0) goto end_of_job; +if ( turn1*turn2 >= 0 ) goto end_of_job; /* both turns in same direction */ +/* --- weight pixel --- */ +aaval = grayscale / ( 3 + min2(abs(turn1),abs(turn2)) ); +/* ------------------------------------------------------------------------- +Back to caller with grayscale antialiased value for pixel at irow,icol +-------------------------------------------------------------------------- */ +end_of_job: + if ( aaval >= 0 ) /* have antialiasing result */ + if ( msglevel>=99 && msgfp!=NULL ) fprintf(msgfp, /* diagnostic output */ + "aapattern19> irow,icol,grid#/2=%d,%d,%d, turn+%d,%d=%d,%d, aaval=%d\n", + irow,icol,gridnum/2, orientation,-orientation,turn1,turn2, aaval); + return ( aaval ); /* back with antialiased value */ +} /* --- end-of-function aapattern19() --- */ + + +/* ========================================================================== + * Function: aapattern20 ( rp, irow, icol, gridnum, grayscale ) + * Purpose: calculates anti-aliased value for pixel at irow,icol, + * whose surrounding 3x3 pixel grid is coded by gridnum + * (which must correspond to pattern #20). + * -------------------------------------------------------------------------- + * Arguments: rp (I) raster * to raster whose bitmap + * is to be anti-aliased + * irow (I) int containing row, 0...height-1, + * of pixel to be antialiased + * icol (I) int containing col, 0...width-1, + * of pixel to be antialiased + * gridnum (I) int containing 0...511 corresponding to + * 3x3 pixel grid surrounding irow,icol + * grayscale (I) int containing number of grayscales + * to be calculated, 0...grayscale-1 + * (should typically be given as 256) + * -------------------------------------------------------------------------- + * Returns: ( int ) 0...grayscale-1 for success, -1=any error + * -------------------------------------------------------------------------- + * Notes: o Handles the eight gridnum's + * (gridnum/2 shown to eliminate irrelevant low-order bit) + * --- --- --* -*- + * --* = 14 *-- = 19 --* = 42 --* = 73 + * **- -** -*- --* + * + * -*- -** *-- **- + * *-- = 84 *-- = 112 *-- = 146 --* = 200 + * *-- --- -*- --- + * ======================================================================= */ +/* --- entry point --- */ +int aapattern20 (raster *rp, int irow, int icol, + int gridnum, int grayscale) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +int aaval = (-1); /* antialiased value returned */ +int iscenter = gridnum&1; /* true if pixel at irow,icol black*/ +int direction = 1, /* direction to follow ** line */ + jrow1=irow, jcol1=icol, /* coords of * */ + jrow2=irow, jcol2=icol; /* coords of adjacent ** pixel */ +int turn1=0,turn2=0, aafollowline(); /* follow *,** lines till turns */ +/* ------------------------------------------------------------------------- +Initialization and calculation of antialiased value +-------------------------------------------------------------------------- */ +/* --- check input -- */ +if ( 1 ) goto end_of_job; /* don't want this one */ +if ( iscenter ) goto end_of_job; /* we only antialias white pixels */ +/* --- set params --- */ +switch ( gridnum/2 ) { /* check pattern#20 orientation */ + default: goto end_of_job; /* not a pattern#20 gridnum */ + case 14: direction=-2; jcol1++; jrow2++; break; + case 19: direction=2; jcol1--; jrow2++; break; + case 42: direction=1; jrow1++; jcol2++; break; + case 73: direction=-1; jrow1--; jcol2++; break; + case 84: direction=-1; jrow1--; jcol2--; break; + case 112: direction=2; jcol1--; jrow2--; break; + case 146: direction=1; jrow1++; jcol2--; break; + case 200: direction=-2; jcol1++; jrow2--; break; + } /* --- end-of-switch(gridnum/2) --- */ +/* --- get turns in both directions --- */ +if ( (turn1=aafollowline(rp,jrow1,jcol1,-direction)) == 0 ) goto end_of_job; +if ( (turn2=aafollowline(rp,jrow2,jcol2,direction)) == 0 ) goto end_of_job; +if ( turn1*turn2 >= 0 ) goto end_of_job; /* both turns in same direction */ +/* --- weight pixel --- */ +aaval = grayscale / ( 3 + min2(abs(turn1),abs(turn2)) ); +/* ------------------------------------------------------------------------- +Back to caller with grayscale antialiased value for pixel at irow,icol +-------------------------------------------------------------------------- */ +end_of_job: + if ( aaval >= 0 ) /* have antialiasing result */ + if ( msglevel>=99 && msgfp!=NULL ) fprintf(msgfp, /* diagnostic output */ + "aapattern20> irow,icol,grid#/2=%d,%d,%d, turn%d,%d=%d,%d, aaval=%d\n", + irow,icol,gridnum/2, -direction,direction,turn1,turn2, aaval); + return ( aaval ); /* back with antialiased value */ +} /* --- end-of-function aapattern20() --- */ + + +/* ========================================================================== + * Function: aapattern39 ( rp, irow, icol, gridnum, grayscale ) + * Purpose: calculates anti-aliased value for pixel at irow,icol, + * whose surrounding 3x3 pixel grid is coded by gridnum + * (which must correspond to pattern #39). + * -------------------------------------------------------------------------- + * Arguments: rp (I) raster * to raster whose bitmap + * is to be anti-aliased + * irow (I) int containing row, 0...height-1, + * of pixel to be antialiased + * icol (I) int containing col, 0...width-1, + * of pixel to be antialiased + * gridnum (I) int containing 0...511 corresponding to + * 3x3 pixel grid surrounding irow,icol + * grayscale (I) int containing number of grayscales + * to be calculated, 0...grayscale-1 + * (should typically be given as 256) + * -------------------------------------------------------------------------- + * Returns: ( int ) 0...grayscale-1 for success, -1=any error + * -------------------------------------------------------------------------- + * Notes: o Handles the eight gridnum's + * (gridnum/2 shown to eliminate irrelevant low-order bit) + * --- --- --* -** + * --* = 15 *-- = 23 --* = 43 --* = 105 + * *** *** -** --* + * + * **- *** *-- *** + * *-- = 212 *-- = 240 *-- = 150 --* = 232 + * *-- --- **- --- + * ======================================================================= */ +/* --- entry point --- */ +int aapattern39 (raster *rp, int irow, int icol, + int gridnum, int grayscale) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +int aaval = (-1); /* antialiased value returned */ +int iscenter = gridnum&1; /* true if pixel at irow,icol black*/ +int direction = 1, /* direction to follow ** line */ + jrow1=irow, jcol1=icol, /* coords of * */ + jrow2=irow, jcol2=icol; /* coords of adjacent ** pixel */ +int turn1=0,turn2=0, aafollowline(); /* follow *,** lines till turns */ +/* ------------------------------------------------------------------------- +Initialization and calculation of antialiased value +-------------------------------------------------------------------------- */ +/* --- check input -- */ +if ( iscenter ) goto end_of_job; /* we only antialias white pixels */ +/* --- set params --- */ +switch ( gridnum/2 ) { /* check pattern#39 orientation */ + default: goto end_of_job; /* not a pattern#39 gridnum */ + case 15: direction=-2; jcol1++; jrow2++; break; + case 23: direction=2; jcol1--; jrow2++; break; + case 43: direction=1; jrow1++; jcol2++; break; + case 105: direction=-1; jrow1--; jcol2++; break; + case 212: direction=-1; jrow1--; jcol2--; break; + case 240: direction=2; jcol1--; jrow2--; break; + case 150: direction=1; jrow1++; jcol2--; break; + case 232: direction=-2; jcol1++; jrow2--; break; + } /* --- end-of-switch(gridnum/2) --- */ +/* --- get turns directions (tunr1==1 signals inside corner) --- */ +if ( (turn1=aafollowline(rp,jrow1,jcol1,-direction)) == 1 ) + { aaval=0; goto end_of_job; } +if ( 1 ) goto end_of_job; /* stop here for now */ +if ( (turn2=aafollowline(rp,jrow2,jcol2,direction)) == 0 ) goto end_of_job; +if ( turn1*turn2 >= 0 ) goto end_of_job; /* both turns in same direction */ +/* --- weight pixel --- */ +aaval = grayscale / ( 3 + min2(abs(turn1),abs(turn2)) ); +/* ------------------------------------------------------------------------- +Back to caller with grayscale antialiased value for pixel at irow,icol +-------------------------------------------------------------------------- */ +end_of_job: + if ( aaval >= 0 ) /* have antialiasing result */ + if ( msglevel>=99 && msgfp!=NULL ) fprintf(msgfp, /* diagnostic output */ + "aapattern39> irow,icol,grid#/2=%d,%d,%d, turn%d,%d=%d,%d, aaval=%d\n", + irow,icol,gridnum/2, -direction,direction,turn1,turn2, aaval); + return ( aaval ); /* back with antialiased value */ +} /* --- end-of-function aapattern39() --- */ + + +/* ========================================================================== + * Function: aafollowline ( rp, irow, icol, direction ) + * Purpose: starting with pixel at irow,icol, moves in + * specified direction looking for a "turn" + * -------------------------------------------------------------------------- + * Arguments: rp (I) raster * to raster containing pixel image + * irow (I) int containing row, 0...height-1, + * of first pixel + * icol (I) int containing col, 0...width-1, + * of first pixel + * direction (I) int containing +1 to follow line up/north + * (decreasing irow), -1 to follow line + * down/south (increasing irow), +2 to follow + * line right/east (increasing icol), + * -2 to follow line left/west (decreasing icol) + * -------------------------------------------------------------------------- + * Returns: ( int ) #rows or #cols traversed prior to turn, + * or 0 if no turn detected (or for any error). + * Sign is + if turn direction is right/east or + * up/north, or is - if left/west or down/south. + * -------------------------------------------------------------------------- + * Notes: o Here are some examples illustrating turn detection in + * +2 (right/east) direction. Turns in other directions + * are detected similarly/symmetrically. * denotes black + * bits (usually fg), - denotes white bits (usually bg), + * and ? denotes "don't care" bit (won't affect outcome). + * Arrow --> points to start pixel denoted by irow,icol. + * + * *??? -??? turn=0 (no turn) is returned + * -->*??? or -->-??? because the start pixel isn't + * *??? -??? on an edge to begin with + * + * ---- **-- turn=0 returned because the + * -->***- or -->***- line ends abruptly without + * ---- ---- turning (even the second case) + * + * ---* ---* turn=0 returned because the + * -->***- or -->**** line forms a Y or T rather + * ---* ---* than turning + * + * ***- **** turn=+3 returned + * -->***- or -->***- (outside corner) + * ---- ---- + * + * ***** ****- turn=-4 returned + * -->***** or -->****- (inside corner) + * ----* ----* + * + * ----* ----* turn=+4 returned + * -->****- or -->***** (outside or inside corner) + * ----- ----- + * ======================================================================= */ +/* --- entry point --- */ +int aafollowline (raster *rp, int irow, int icol, int direction) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +pixbyte *bitmap = rp->pixmap; /* local rp->pixmap ptr */ +int width=rp->width, height=rp->height; /* width, height of raster */ +int drow=0, dcol=0, /* delta row,col to follow line */ + jrow=irow, jcol=icol; /* current row,col following line */ +int bitval=1, /* value of rp bit at irow,icol */ + fgval=1, bgval=0, /* "fg" is whatever bitval is */ + bitminus=0, bitplus=0; /* value of left/down, right/up bit*/ +int isline=1, isedge=0; /*isline signals one-pixel wide line*/ +int turn = 0, /* detected turn back to caller */ + maxturn = maxfollow; /* don't follow more than max pixels*/ +/* ------------------------------------------------------------------------- +Initialization +-------------------------------------------------------------------------- */ +/* --- check input --- */ +if ( irow<0 || irow>=height /* irow out-of-bounds */ +|| icol<0 || icol>=width ) goto end_of_job; /* icol out-of-bounds */ +/* --- starting bit -- see if we're following a fg (usual), or bg line --- */ +bitval = getlongbit(bitmap,(icol+irow*width)); /* starting pixel (bg or fg)*/ +fgval = bitval; bgval = (1-bitval); /* define "fg" as whatever bitval is*/ +/* --- set drow,dcol corresponding to desired direction --- */ +switch ( direction ) { /* determine drow,dcol for direction*/ + default: goto end_of_job; /* unrecognized direction arg */ + case 1: drow = (-1); break; /* follow line up/north */ + case -1: drow = 1; break; /* down/south */ + case 2: dcol = 1; break; /* right/east */ + case -2: dcol = (-1); break; } /* left/west */ +/* --- set bitminus and bitplus --- */ +if ( drow == 0 ) { /* we're following line right/left */ + if ( irow < height ) /* there's a pixel below current */ + bitminus = getlongbit(bitmap,(icol+(irow+1)*width)); /* get it */ + if ( irow > 0 ) /* there's a pixel above current */ + bitplus = getlongbit(bitmap,(icol+(irow-1)*width)); } /* get it */ +if ( dcol == 0 ) { /* we're following line up/down */ + if ( icol < width ) /* there's a pixel to the right */ + bitplus = getlongbit(bitmap,(icol+1+irow*width)); /* get it */ + if ( icol > 0 ) /* there's a pixel to the left */ + bitminus = getlongbit(bitmap,(icol-1+irow*width)); } /* get it */ +/* --- check for lack of line to follow --- */ +if ( bitval == bitplus /* starting pixel same as above */ +&& bitval == bitminus ) /* and below (or right and left) */ + goto end_of_job; /* so there's no line to follow */ +/* --- set isline and isedge (already initted for isline) --- */ +if ( bitval == bitplus ) /* starting pixel same as above */ + { isedge = (-1); isline = 0; } /* so we're at an edge below */ +if ( bitval == bitminus ) /* starting pixel same as below */ + { isedge = 1; isline = 0; } /* so we're at an edge above */ +/* ------------------------------------------------------------------------- +follow line +-------------------------------------------------------------------------- */ +while ( 1 ) { /* until turn found (or max) */ + /* --- local allocations and declarations --- */ + int dbitval=0, /* value of bit at jrow,jcol */ + dbitminus=0, dbitplus=0; /* value of left/down, right/up bit*/ + /* --- bump pixel count and indexes; check for max or end-of-raster --- */ + turn++; /* bump #pixels followed */ + jrow += drow; jcol += dcol; /* indexes of next pixel to check */ + if ( turn > maxturn /* already followed max #pixels */ + || jrow<0 || jrow>=height /* or jrow past end-of-raster */ + || jcol<0 || jcol>=width ) /* or jcol past end-of-raster */ + { turn = 0; goto end_of_job; } /* so quit without finding a turn */ + /* --- set current bit (dbitval) --- */ + dbitval = getlongbit(bitmap,(jcol+jrow*width)); /*value of jrow,jcol bit*/ + /* --- set dbitminus and dbitplus --- */ + if ( drow == 0 ) { /* we're following line right/left */ + if ( irow < height ) /* there's a pixel below current */ + dbitminus = getlongbit(bitmap,(jcol+(irow+1)*width)); /* get it */ + if ( irow > 0 ) /* there's a pixel above current */ + dbitplus = getlongbit(bitmap,(jcol+(irow-1)*width)); } /* get it */ + if ( dcol == 0 ) { /* we're following line up/down */ + if ( icol < width ) /* there's a pixel to the right */ + dbitplus = getlongbit(bitmap,(icol+1+jrow*width)); /* get it */ + if ( icol > 0 ) /* there's a pixel to the left */ + dbitminus = getlongbit(bitmap,(icol-1+jrow*width)); } /* get it */ + /* --- first check for abrupt end-of-line, or for T or Y --- */ + if ( isline != 0 ) /* abrupt end or T,Y must be a line*/ + if ( ( bgval == dbitval /* end-of-line if pixel flips to bg*/ + && bgval == dbitplus /* and bg same as above pixel */ + && bgval == dbitminus ) /* and below (or right and left) */ + || ( fgval == dbitplus /* T or Y if fg same as above pixel*/ + && fgval == dbitminus ) ) /* and below (or right and left) */ + { turn = 0; goto end_of_job; } /* so we're at a T or Y */ + /* --- check for turning line --- */ + if ( isline != 0 ) { /* turning line must be a line */ + if ( fgval == dbitminus ) /* turning down */ + { turn = -turn; goto end_of_job; } /* so return negative turn */ + else if ( fgval == dbitplus ) /* turning up */ + goto end_of_job; } /* so return positive turn */ + /* --- check for inside corner at edge --- */ + if ( isedge != 0 ) { /* inside corner must be a edge */ + if ( isedge < 0 && fgval == bitminus ) /* corner below */ + { turn = -turn; goto end_of_job; } /* so return negative turn */ + if ( isedge > 0 && fgval == bitplus ) /* corner above */ + goto end_of_job; } /* so return positive turn */ + /* --- check for abrupt end at edge --- */ + if ( isedge != 0 /* abrupt edge end must be an edge */ + && fgval == dbitval ) /* and line must not end */ + if ( (isedge < 0 && bgval == bitplus) /* abrupt end above */ + || (isedge > 0 && bgval == bitminus) ) /* or abrupt end below */ + { turn = 0; goto end_of_job; } /* so edge ended abruptly */ + /* --- check for outside corner at edge --- */ + if ( isedge != 0 /* outside corner must be a edge */ + && bgval == dbitval ) { /* and line must end */ + if ( isedge > 0 ) turn = -turn; /* outside turn down from edge above*/ + goto end_of_job; } + } /* --- end-of-while(1) --- */ +/* ------------------------------------------------------------------------- +Back to caller with #rows or #cols traversed, and direction of detected turn +-------------------------------------------------------------------------- */ +end_of_job: + if ( msglevel>=99 && msgfp!=NULL ) /* debugging/diagnostic output */ + fprintf(msgfp,"aafollowline> irow,icol,direction=%d,%d,%d, turn=%d\n", + irow,icol,direction,turn); + return ( turn ); +} /* --- end-of-function aafollowline() --- */ + + +/* ========================================================================== + * Function: aagridnum ( rp, irow, icol ) + * Purpose: calculates gridnum, 0-511 (see Notes below), + * for 3x3 grid centered at irow,icol + * -------------------------------------------------------------------------- + * Arguments: rp (I) raster * to raster containing + * bitmap image (to be anti-aliased) + * irow (I) int containing row, 0...height-1, + * at center of 3x3 grid + * icol (I) int containing col, 0...width-1, + * at center of 3x3 grid + * -------------------------------------------------------------------------- + * Returns: ( int ) 0-511 grid number, or -1=any error + * -------------------------------------------------------------------------- + * Notes: o Input gridnum is a 9-bit int, 0-511, coding a 3x3 pixel grid + * whose bit positions (and corresponding values) in gridnum are + * 876 256 128 64 + * 504 = 32 1 16 + * 321 8 4 2 + * Thus, for example (*=pixel set/black, -=pixel not set/white), + * *-- *-- -** (note that 209 is the + * -*- = 259 *-- = 302 -** = 209 inverse, set<-->unset, + * --* *** --- of 302) + * o A set pixel is considered black, an unset pixel considered + * white. + * ======================================================================= */ +/* --- entry point --- */ +int aagridnum (raster *rp, int irow, int icol) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +pixbyte *bitmap = rp->pixmap; /* local rp->pixmap ptr */ +int width=rp->width, height=rp->height, /* width, height of raster */ + imap = icol + irow*width; /* pixel index = icol + irow*width */ +int bitval=0, /* value of rp bit at irow,icol */ + nnbitval=0, nebitval=0, eebitval=0, sebitval=0, /*adjacent vals*/ + ssbitval=0, swbitval=0, wwbitval=0, nwbitval=0, /*compass pt names*/ + gridnum = (-1); /* grid# 0-511 for above 9 bits */ +/* ------------------------------------------------------------------------- +check input +-------------------------------------------------------------------------- */ +if ( irow<0 || irow>=height /* irow out-of-bounds */ +|| icol<0 || icol>=width ) goto end_of_job; /* icol out-of-bounds */ +/* ------------------------------------------------------------------------- +get the 9 bits comprising the 3x3 grid centered at irow,icol +-------------------------------------------------------------------------- */ +/* --- get center bit --- */ +bitval = getlongbit(bitmap,imap); /* value of rp input bit at imap */ +/* --- get 8 surrounding bits --- */ +if ( irow > 0 ) /* nn (north) bit available */ + nnbitval = getlongbit(bitmap,imap-width); /* nn bit value */ +if ( irow < height-1 ) /* ss (south) bit available */ + ssbitval = getlongbit(bitmap,imap+width); /* ss bit value */ +if ( icol > 0 ) /* ww (west) bit available */ + { wwbitval = getlongbit(bitmap,imap-1); /* ww bit value */ + if ( irow > 0 ) /* nw bit available */ + nwbitval = getlongbit(bitmap,imap-width-1); /* nw bit value */ + if ( irow < height-1 ) /* sw bit available */ + swbitval = getlongbit(bitmap,imap+width-1); } /* sw bit value */ +if ( icol < width-1 ) /* ee (east) bit available */ + { eebitval = getlongbit(bitmap,imap+1); /* ee bit value */ + if ( irow > 0 ) /* ne bit available */ + nebitval = getlongbit(bitmap,imap-width+1); /* ne bit value */ + if ( irow < height-1 ) /* se bit available */ + sebitval = getlongbit(bitmap,imap+width+1); } /* se bit value */ +/* --- set gridnum --- */ +gridnum = 0; /* clear all bits */ +if ( bitval ) gridnum = 1; /* set1bit(gridnum,0); */ +if ( nwbitval ) gridnum += 256; /* set1bit(gridnum,8); */ +if ( nnbitval ) gridnum += 128; /* set1bit(gridnum,7); */ +if ( nebitval ) gridnum += 64; /* set1bit(gridnum,6); */ +if ( wwbitval ) gridnum += 32; /* set1bit(gridnum,5); */ +if ( eebitval ) gridnum += 16; /* set1bit(gridnum,4); */ +if ( swbitval ) gridnum += 8; /* set1bit(gridnum,3); */ +if ( ssbitval ) gridnum += 4; /* set1bit(gridnum,2); */ +if ( sebitval ) gridnum += 2; /* set1bit(gridnum,1); */ +/* ------------------------------------------------------------------------- +Back to caller with gridnum coding 3x3 grid centered at irow,icol +-------------------------------------------------------------------------- */ +end_of_job: + return ( gridnum ); +} /* --- end-of-function aagridnum() --- */ + + +/* ========================================================================== + * Function: aapatternnum ( gridnum ) + * Purpose: Looks up the pattern number 1...51 + * corresponding to the 3x3 pixel grid coded by gridnum 0=no + * pixels set (white) to 511=all pixels set (black). + * -------------------------------------------------------------------------- + * Arguments: gridnum (I) int containing 0-511 coding a 3x3 pixel grid + * (see Notes below) + * -------------------------------------------------------------------------- + * Returns: ( int ) 1 to 51, or -1=error + * -------------------------------------------------------------------------- + * Notes: o Input gridnum is a 9-bit int, 0-511, coding a 3x3 pixel grid + * whose bit positions (and corresponding values) in gridnum are + * 876 256 128 64 + * 504 = 32 1 16 + * 321 8 4 2 + * Thus, for example (*=pixel set/black, -=pixel not set/white), + * *-- *-- -** (note that 209 is the + * -*- = 259 *-- = 302 -** = 209 inverse, set<-->unset, + * --* *** --- of 302) + * o A set pixel is considered black, an unset pixel considered + * white. + * o Ignoring whether the center pixel is set or unset, and + * taking rotation, reflection and inversion (set<-->unset) + * symmetries into account, there are 32 unique pixel patterns. + * If inversions are listed separately, there are 51 patterns. + * o Here are the 51 unique patterns, with ? always denoting the + * undetermined center pixel. At the upper-left corner of each + * pattern is the "pattern index number" assigned to it in this + * function. At the upper-right is the pattern's multiplicity, + * i.e., the number of different patterns obtained by rotations + * and reflection of the illustrated one. Inverse patters are + * illustrated immediately beneath the original (the first three + * four-pixel patterns have identical inverses). + * ------------------------------------------------------------- + * No pixels set: + * #1 1 (in this case, 1 signifies that rotation + * --- and reflection give no different grids) + * -?- + * --- + * Inverse, all eight pixels set + * #2 1 (the inverse multiplicity is always the same) + * *** + * *?* + * *** + * ------------------------------------------------------------- + * One pixel set: + * #3 4 #4 4 + * *-- -*- + * -?- -?- + * --- --- + * Inverse, seven pixels set: + * #5 4 #6 4 + * -** *-* + * *?* *?* + * *** *** + * ------------------------------------------------------------- + * Two pixels set: + * #7 8 #8 4 #9 8 10 2 11 4 12 2 + * **- *-* *-- *-- -*- -*- + * -?- -?- -?* -?- -?* -?- + * --- --- --- --* --- -*- + * Inverse, six pixels set: + * #13 8 14 4 15 8 16 2 17 4 18 2 + * --* -*- -** -** *-* *-* + * *?* *?* *?- *?* *?- *?* + * *** *** *** **- *** *-* + * ------------------------------------------------------------- + * Three pixels set: + * #19 4 20 8 21 8 22 8 23 8 24 4 25 4 26 4 27 4 28 4 + * *** **- **- **- **- **- *-* *-* -*- -*- + * -?- -?* -?- -?- -?- *?- -?- -?- -?* -?* + * --- --- --* -*- *-- --- --* -*- -*- *-- + * Inverse, five pixels set: + * #29 4 30 8 31 8 32 8 33 8 34 4 35 4 36 4 37 4 38 4 + * --- --* --* --* --* --* -*- -*- *-* *-* + * *?* *?- *?* *?* *?* -?* *?* *?* *?- *?- + * *** *** **- *-* -** *** **- *-* *-* -** + * ------------------------------------------------------------- + * Four pixels set (including inverses): + * #39 8 40 4 41 8 42 8 43 4 44 4 45 8 46 1 + * *** **- **- *** *** **- **- *-* + * -?* -?- -?* -?- -?- -?* -?* -?- + * --- -** *-- --* -*- --* -*- *-* + * + * #47 8 48 4 49 4 50 8 51 1 + * --- --- --* --* -*- + * *?* *?* *?- *?- *?* + * **- *-* **- *-* -*- + * ======================================================================= */ +/* --- entry point --- */ +int aapatternnum ( int gridnum ) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +int pattern = (-1); /*pattern#, 1-51, for input gridnum*/ +/* --- + * pattern number corresponding to input gridnum/2 code + * ( gridnum/2 strips off gridnum's low bit because it's + * the same pattern whether or not center pixel is set ) + * --- */ +static int patternnum[] = { + 1, 3, 4, 7, 3, 8, 7,19, 4, 7,11,24, 9,23,20,39, /* 0- 15 */ + 4, 9,11,20, 7,23,24,39,12,22,27,47,22,48,47,29, /* 16- 31 */ + 3, 8, 9,23,10,25,21,42, 7,19,20,39,21,42,44,34, /* 32- 47 */ + 9,26,28,41,21,50,49,30,22,43,45,33,40,32,31,13, /* 48- 63 */ + 4, 9,12,22, 9,26,22,43,11,20,27,47,28,41,45,33, /* 64- 79 */ + 11,28,27,45,20,41,47,33,27,45,51,35,45,36,35,14, /* 80- 95 */ + 7,23,22,48,21,50,40,32,24,39,47,29,49,30,31,13, /* 96-111 */ + 20,41,45,36,44,38,31,15,47,33,35,14,31,15,16, 5, /* 112-127 */ + 3,10, 9,21, 8,25,23,42, 9,21,28,49,26,50,41,30, /* 128-143 */ + 7,21,20,44,19,42,39,34,22,40,45,31,43,32,33,13, /* 144-159 */ + 8,25,26,50,25,46,50,37,23,42,41,30,50,37,38,17, /* 160-175 */ + 23,50,41,38,42,37,30,17,48,32,36,15,32,18,15, 6, /* 176-191 */ + 7,21,22,40,23,50,48,32,20,44,45,31,41,38,36,15, /* 192-207 */ + 24,49,47,31,39,30,29,13,47,31,35,16,33,15,14, 5, /* 208-223 */ + 19,42,43,32,42,37,32,18,39,34,33,13,30,17,15, 6, /* 224-239 */ + 39,30,33,15,34,17,13, 6,29,13,14, 5,13, 6, 5, 2, /* 240-255 */ + -1 } ; /* --- end-of-patternnum[] --- */ +/* ------------------------------------------------------------------------- +look up pattern number for gridnum +-------------------------------------------------------------------------- */ +/* --- first check input --- */ +if ( gridnum<0 || gridnum>511 ) goto end_of_job; /* gridnum out of bounds */ +/* --- look up pattern number, 1-51, corresponding to input gridnum --- */ +pattern = patternnum[gridnum/2]; /* /2 strips off gridnum's low bit */ +if ( pattern<1 || pattern>51 ) pattern = (-1); /* some internal error */ +end_of_job: + return ( pattern ); /* back to caller with pattern# */ +} /* --- end-of-function aapatternnum() --- */ + + +/* ========================================================================== + * Function: aalookup ( gridnum ) + * Purpose: Looks up the grayscale value 0=white to 255=black + * corresponding to the 3x3 pixel grid coded by gridnum 0=no + * pixels set (white) to 511=all pixels set (black). + * -------------------------------------------------------------------------- + * Arguments: gridnum (I) int containing 0-511 coding a 3x3 pixel grid + * -------------------------------------------------------------------------- + * Returns: ( int ) 0=white to 255=black, or -1=error + * -------------------------------------------------------------------------- + * Notes: o Input gridnum is a 9-bit int, 0-511, coding a 3x3 pixel grid + * o A set pixel is considered black, an unset pixel considered + * white. Likewise, the returned grayscale is 255 for black, + * 0 for white. You'd more typically want to use 255-grayscale + * so that 255 is white and 0 is black. + * o The returned number is the (lowpass) antialiased grayscale + * for the center pixel (gridnum bit 0) of the grid. + * ======================================================================= */ +/* --- entry point --- */ +int aalookup ( int gridnum ) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +int grayscale = (-1); /*returned grayscale, init for error*/ +int pattern = (-1), aapatternnum(); /*pattern#, 1-51, for input gridnum*/ +int iscenter = gridnum&1; /*low-order bit set for center pixel*/ +/* --- gray scales --- */ +#define WHT 0 +#define LGT 64 +#define GRY 128 +#define DRK 192 +#define BLK 255 +#if 1 +/* --- + * modified aapnm() grayscales (second try) + * --- */ +/* --- grayscale for each pattern when center pixel set/black --- */ +static int grayscale1[] = { -1, /* [0] index not used */ + BLK,BLK,BLK,BLK,242,230,BLK,BLK,BLK,160, /* 1-10 */ +/* BLK,BLK,BLK,BLK,242,230,BLK,BLK,BLK,BLK, */ /* 1-10 */ + BLK,BLK,217,230,217,230,204,BLK,BLK,166, /* 11-20 */ + BLK,BLK,BLK,BLK,BLK,BLK,178,166,204,191, /* 21-30 */ + 204,BLK,204,191,217,204,BLK,191,178,BLK, /* 31-40 */ + 178,BLK,BLK,178,191,BLK,191,BLK,178,BLK,204, /* 41-51 */ + -1 } ; /* --- end-of-grayscale1[] --- */ +/* --- grayscale for each pattern when center pixel not set/white --- */ +static int grayscale0[] = { -1, /* [0] index not used */ + WHT,WHT,WHT,WHT,WHT,WHT,WHT,WHT,WHT,WHT, /* 1-10 */ + 64,WHT,WHT,128,115,128,WHT,WHT,WHT, 64, /* 11-20 */ + /* 51,WHT,WHT,128,115,128,WHT,WHT,WHT, 64, */ /* 11-20 */ + WHT,WHT,WHT, 64,WHT,WHT, 76, 64,102, 89, /* 21-30 */ + 102,WHT,102,WHT,115,102,WHT, 89, 76,WHT, /* 31-40 */ + 76,WHT,WHT, 76, 89,WHT, 89,WHT, 76,WHT,102, /* 41-51 */ + -1 } ; /* --- end-of-grayscale0[] --- */ +#endif +#if 0 +/* --- + * modified aapnm() grayscales (first try) + * --- */ +/* --- grayscale for each pattern when center pixel set/black --- */ +static int grayscale1[] = { -1, /* [0] index not used */ + BLK,BLK,BLK,BLK,242,230,GRY,BLK,BLK,BLK, /* 1-10 */ +/* BLK,BLK,BLK,BLK,242,230,BLK,BLK,BLK,BLK, */ /* 1-10 */ + BLK,BLK,217,230,217,230,204,BLK,BLK,166, /* 11-20 */ + BLK,BLK,BLK,BLK,BLK,BLK,BLK,166,204,191, /* 21-30 */ +/* BLK,BLK,BLK,BLK,BLK,BLK,178,166,204,191, */ /* 21-30 */ + 204,BLK,204,BLK,217,204,BLK,191,GRY,BLK, /* 31-40 */ +/* 204,BLK,204,191,217,204,BLK,191,178,BLK, */ /* 31-40 */ + 178,BLK,BLK,178,191,BLK,BLK,BLK,178,BLK,204, /* 41-51 */ +/* 178,BLK,BLK,178,191,BLK,191,BLK,178,BLK,204, */ /* 41-51 */ + -1 } ; /* --- end-of-grayscale1[] --- */ +/* --- grayscale for each pattern when center pixel not set/white --- */ +static int grayscale0[] = { -1, /* [0] index not used */ + WHT,WHT,WHT,WHT,WHT,WHT,WHT,WHT,WHT,WHT, /* 1-10 */ + GRY,WHT,WHT,128,115,128,WHT,WHT,WHT,GRY, /* 11-20 */ +/* 51,WHT,WHT,128,115,128,WHT,WHT,WHT, 64, */ /* 11-20 */ + WHT,WHT,WHT,GRY,WHT,WHT, 76, 64,102, 89, /* 21-30 */ +/* WHT,WHT,WHT, 64,WHT,WHT, 76, 64,102, 89, */ /* 21-30 */ + 102,WHT,102,WHT,115,102,WHT, 89,GRY,WHT, /* 31-40 */ +/* 102,WHT,102,WHT,115,102,WHT, 89, 76,WHT, */ /* 31-40 */ + 76,WHT,WHT,GRY, 89,WHT, 89,WHT, 76,WHT,102, /* 41-51 */ +/* 76,WHT,WHT, 76, 89,WHT, 89,WHT, 76,WHT,102, */ /* 41-51 */ + -1 } ; /* --- end-of-grayscale0[] --- */ +#endif +#if 0 +/* --- + * these grayscales _exactly_ correspond to the aapnm() algorithm + * --- */ +/* --- grayscale for each pattern when center pixel set/black --- */ +static int grayscale1[] = { -1, /* [0] index not used */ + BLK,BLK,BLK,BLK,242,230,BLK,BLK,BLK,BLK, /* 1-10 */ + BLK,BLK,217,230,217,230,204,BLK,BLK,166, /* 11-20 */ + BLK,BLK,BLK,BLK,BLK,BLK,178,166,204,191, /* 21-30 */ + 204,BLK,204,191,217,204,BLK,191,178,BLK, /* 31-40 */ + 178,BLK,BLK,178,191,BLK,191,BLK,178,BLK,204, /* 41-51 */ + -1 } ; /* --- end-of-grayscale1[] --- */ +/* --- grayscale for each pattern when center pixel not set/white --- */ +static int grayscale0[] = { -1, /* [0] index not used */ + WHT,WHT,WHT,WHT,WHT,WHT,WHT,WHT,WHT,WHT, /* 1-10 */ + 51,WHT,WHT,128,115,128,WHT,WHT,WHT, 64, /* 11-20 */ + WHT,WHT,WHT, 64,WHT,WHT, 76, 64,102, 89, /* 21-30 */ + 102,WHT,102,WHT,115,102,WHT, 89, 76,WHT, /* 31-40 */ + 76,WHT,WHT, 76, 89,WHT, 89,WHT, 76,WHT,102, /* 41-51 */ + -1 } ; /* --- end-of-grayscale0[] --- */ +#endif +/* ------------------------------------------------------------------------- +look up grayscale for gridnum +-------------------------------------------------------------------------- */ +/* --- first check input --- */ +if ( gridnum<0 || gridnum>511 ) goto end_of_job; /* gridnum out of bounds */ +/* --- look up pattern number, 1-51, corresponding to input gridnum --- */ +pattern = aapatternnum(gridnum); /* look up pattern number */ +if ( pattern<1 || pattern>51 ) goto end_of_job; /* some internal error */ +if ( ispatternnumcount ) { /* counts being accumulated */ + if (iscenter) patternnumcount1[pattern] += 1; /* bump diagnostic count */ + else patternnumcount0[pattern] += 1; } +/* --- look up grayscale for this pattern --- */ +grayscale = ( iscenter? grayscale1[pattern] : grayscale0[pattern] ); +end_of_job: + return ( grayscale ); /* back to caller with grayscale */ +} /* --- end-of-function aalookup() --- */ + + +/* ========================================================================== + * Function: aalowpasslookup ( rp, bytemap, grayscale ) + * Purpose: calls aalookup() for each pixel in rp->bitmap + * to create anti-aliased bytemap + * -------------------------------------------------------------------------- + * Arguments: rp (I) raster * to raster whose bitmap + * is to be anti-aliased + * bytemap (O) intbyte * to bytemap, calculated + * by calling aalookup() for each pixel + * in rp->bitmap + * grayscale (I) int containing number of grayscales + * to be calculated, 0...grayscale-1 + * (should typically be given as 256) + * -------------------------------------------------------------------------- + * Returns: ( int ) 1=success, 0=any error + * -------------------------------------------------------------------------- + * Notes: o + * ======================================================================= */ +/* --- entry point --- */ +int aalowpasslookup (raster *rp, intbyte *bytemap, int grayscale) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +int width=rp->width, height=rp->height, /* width, height of raster */ + icol = 0, irow = 0, imap = (-1); /* width, height, bitmap indexes */ +int bgbitval=0 /*, fgbitval=1*/; /* background, foreground bitval */ +int bitval=0, /* value of rp bit at irow,icol */ + aabyteval=0; /* antialiased (or unchanged) value*/ +int gridnum=0, aagridnum(), /* grid# for 3x3 grid at irow,icol */ + aalookup(); /* table look up antialiased value*/ +/* ------------------------------------------------------------------------- +generate bytemap by table lookup for each pixel of bitmap +-------------------------------------------------------------------------- */ +for ( irow=0; irow=0 && aabyteval<=255 ) /* check for success */ + bytemap[imap] = (intbyte)(aabyteval); /* init antialiased pixel */ + } /* --- end-of-for(irow,icol) --- */ +ispatternnumcount = 0; /* accumulate counts only once */ +/* ------------------------------------------------------------------------- +Back to caller with gray-scale anti-aliased bytemap +-------------------------------------------------------------------------- */ +/*end_of_job:*/ + return ( 1 ); +} /* --- end-of-function aalowpasslookup() --- */ + + +/* ========================================================================== * Function: aasupsamp ( rp, aa, sf, grayscale ) * Purpose: calculates a supersampled anti-aliased bytemap * for rp->bitmap, with each byte 0...grayscale-1 @@ -9480,7 +13074,8 @@ end_of_job: * (usually just #rows * #cols) * colors (O) intbyte * (to be interpreted as ints) * returning a list of the discrete/different - * values in bytemap, in ascending value order + * values in bytemap, in ascending value order, + * and with gamma correction applied * colormap (O) intbyte * returning a bytemap "image", * i.e., in one-to-one pixel correspondence * with bytemap, but where the values have been @@ -9501,7 +13096,8 @@ int ncolors = 0, /* #different values igray, grayscale = 256; /* bytemap contains intbyte's */ intbyte *bytevalues = NULL; /* 1's where bytemap contains value*/ int ibyte; /* bytemap/colormap index */ -int isscale = 0; /* true to scale largest val to 255*/ +int isscale = 0, /* true to scale largest val to 255*/ + isgamma = 1; /* true to apply gamma correction */ int maxcolors = 0; /* maximum ncolors */ /* ------------------------------------------------------------------------- Accumulate colors[] from values occurring in bytemap @@ -9532,6 +13128,20 @@ if ( isscale ) /* only rescale if req { colors[igray] = min2(grayscale-1,(int)(scalefactor*colors[igray]+0.5)); if (igray>5) colors[igray] = min2(grayscale-1,colors[igray]+2*igray); } } /* --- end-of-if(isscale) --- */ +/* --- apply gamma correction --- */ +if ( isgamma /* only gamma correct if requested */ +&& gammacorrection > 0.0001 ) /* and if we have gamma correction */ + if ( ncolors > 1 ) /* and if not a "blank" raster */ + if ( colors[ncolors-1] > 0 ) /*and at least one pixel non-white*/ + { + for ( igray=1; igray= 0 ) msglevel = newmsglevel; +if ( newmsgfp != NULL ) msgfp = newmsgfp; +return ( 1 ); +} /* --- end-of-function mimetexsetmsg() --- */ #endif /* PART3 */ /* --- @@ -9849,7 +13486,7 @@ messages -------------------------------------------------------------------------- */ static char *copyright = /* copyright, gnu/gpl notice */ "+-----------------------------------------------------------------------+\n" - "|mimeTeX vers 1.61, Copyright(c) 2002-2005, John Forkosh Associates, Inc|\n" + "|mimeTeX vers 1.70, Copyright(c) 2002-2008, John Forkosh Associates, Inc|\n" "+-----------------------------------------------------------------------+\n" "| mimeTeX is free software, licensed to you under terms of the GNU/GPL, |\n" "| and comes with absolutely no warranty whatsoever. |\n" @@ -9881,7 +13518,7 @@ int main ( int argc, char *argv[] Allocations and Declarations -------------------------------------------------------------------------- */ /* --- expression to be emitted --- */ -static char exprbuffer[16385] = "f(x)=x^2"; /* expression to be processed */ +static char exprbuffer[MAXEXPRSZ+1] = "f(x)=x^2"; /* input TeX expression */ char *expression = exprbuffer; /* ptr to expression */ int size = NORMALSIZE; /* default font size */ char *query = getenv("QUERY_STRING"); /* getenv("QUERY_STRING") result */ @@ -9890,12 +13527,16 @@ int unescape_url(); /* convert %xx's t int emitcache(); /* emit cached image if it exists */ int isquery = 0, /* true if input from QUERY_STRING */ isqempty = 0, /* true if query string empty */ + isqforce = 0, /* true to force query emulation */ isqlogging = 0, /* true if logging in query mode */ isformdata = 0, /* true if input from html form */ - isdumpimage = 0; /* true to dump image on stdout */ + isinmemory = 1, /* true to generate image in memory*/ + isdumpimage = 0, /* true to dump image on stdout */ + isdumpbuffer = 0; /* true to dump to memory buffer */ /* --- rasterization --- */ -subraster *rasterize(), *sp; /* rasterize expression */ -raster *border_raster(), *bp; /* put a border around raster */ +subraster *rasterize(), *sp=NULL; /* rasterize expression */ +raster *border_raster(), *bp=NULL; /* put a border around raster */ +int delete_subraster(); /* for clean-up at end-of-job */ int type_raster(), type_bytemap(), /* screen dump function prototypes */ xbitmap_raster(); /* mime xbitmap output function */ /* --- http_referer --- */ @@ -9918,9 +13559,15 @@ int norefmaxlen = NOREFMAXLEN; /*max que void GIF_SetColor(),GIF_SetTransparent(); /* ...gifsave enntry points */ #endif char *gif_outfile = (char *)NULL, /* gif output defaults to stdout */ + gif_buffer[MAXGIFSZ] = "\000", /* or gif written in memory buffer */ cachefile[256] = "\000", /* full path and name to cache file*/ *md5str(); /* md5 has of expression */ int maxage = 7200; /* max-age is two hours */ +int valign = (-9999); /*Vertical-Align:baseline-(height-1)*/ +/* --- pbm/pgm (-g switch) --- */ +int ispbmpgm = 0; /* true to write pbm/pgm file */ +int type_pbmpgm(), ptype=0; /* entry point, graphic format */ +char *pbm_outfile = (char *)NULL; /* output file defaults to stdout */ /* --- anti-aliasing --- */ intbyte *bytemap_raster = NULL, /* anti-aliased bitmap */ colors[256]; /* grayscale vals in bytemap */ @@ -9928,6 +13575,7 @@ int aalowpass(), aapnm(), /*lowpass fil grayscale = 256; /* 0-255 grayscales in 8-bit bytes */ int ncolors=2, /* #colors (2=b&w) */ aacolormap(); /* build colormap from bytemap */ +int ipattern; /*patternnumcount[] index diagnostic*/ /* --- messages --- */ char logfile[256] = LOGFILE, /*log queries if msglevel>=LOGLEVEL*/ cachelog[256] = CACHELOG; /* cached image log in cachepath/ */ @@ -9948,21 +13596,38 @@ initialization /* --- set global variables --- */ msgfp = stdout; /* for comamnd-line mode output */ isss = issupersampling; /* set supersampling flag */ +isemitcontenttype = 1; /* true to emit mime content-type */ +iscachecontenttype = 0; /* true to cache mime content-type */ +*contenttype = '\000'; /* reset content-type:, etc. cache */ +iscaching = ISCACHING; /* true if caching images */ +if ( iscaching ) { /* images are being cached */ + strcpy(cachepath,CACHEPATH); /* relative path to cached files */ + if ( *cachepath == '%' ) { /* leading % signals cache headers */ + iscachecontenttype = 1; /* signal caching mime content-type*/ + strcpy(cachepath,cachepath+1); } } /* and squeeze out leading % char */ +gifSize = 0; /* signal that image not in memory */ +fgred=FGRED; fggreen=FGGREEN; fgblue=FGBLUE; /* default foreground colors */ +bgred=BGRED; bggreen=BGGREEN; bgblue=BGBLUE; /* default background colors */ shrinkfactor = shrinkfactors[NORMALSIZE]; /* set shrinkfactor */ +for ( ipattern=1; ipattern<=51; ipattern++ ) + patternnumcount0[ipattern] = patternnumcount1[ipattern] = 0; /* --- * check QUERY_STRING query for expression overriding command-line arg * ------------------------------------------------------------------- */ if ( query != NULL ) /* check query string from environ */ if ( strlen(query) >= 1 ) /* caller gave us a query string */ - { strncpy(expression,query,16384); /* so use it as expression */ - expression[16384] = '\000'; /* make sure it's null terminated */ + { strncpy(expression,query,MAXEXPRSZ); /* so use it as expression */ + expression[MAXEXPRSZ] = '\000'; /* make sure it's null terminated */ + if ( 0 ) /*true to remove leading whitespace*/ + while ( isspace(*expression) && *expression!='\000' ) + strcpy(expression,expression+1); /* squeeze out white space */ isquery = 1; } /* and set isquery flag */ if ( !isquery ) /* empty query string */ { char *host = getenv("HTTP_HOST"), /* additional getenv("") results */ *name = getenv("SERVER_NAME"), *addr = getenv("SERVER_ADDR"); if ( host!=NULL || name!=NULL || addr!=NULL ) /* assume http query */ { isquery = 1; /* set flag to signal query */ - strcpy(expression,"\\red\\small\\rm~missing~query~string"); } + strcpy(expression,"\\red\\small\\fbox{\\rm~no~query~string}"); } isqempty = 1; /* signal empty query string */ } /* --- end-of-if(!isquery) --- */ /* --- @@ -9997,22 +13662,30 @@ if ( !isquery /* don't have an html q if ( !isstopsignal /* haven't seen stopsignal switch */ && *argv[argnum] == '-' ) /* and have some '-' switch */ { - char flag = tolower(*(argv[argnum]+1)); /* single char following '-' */ - int arglen = strlen(argv[argnum]) - 1; /* #chars following - */ + char *field = argv[argnum] + 1; /* ptr to char(s) following - */ + char flag = tolower(*field); /* single char following '-' */ + int arglen = strlen(field); /* #chars following - */ argnum++; /* arg following flag/switch is usually its value */ nswitches++; /* another switch on command line */ - if ( isstrict && arglen!=1 ) /* only single-char switch allowed */ + if ( isstrict && /* if strict checking then... */ + !isthischar(flag,"g") && arglen!=1 ) /*must be single-char switch*/ { nbadargs++; argnum--; } /* so ignore longer -xxx switch */ else /* process single-char -x switch */ switch ( flag ) { /* see what user wants to tell us */ /* --- ignore uninterpreted flag --- */ default: nbadargs++; argnum--; break; /* --- adjustable program parameters (not checking input) --- */ + case 'b': isdumpimage++; isdumpbuffer++; argnum--; break; case 'd': isdumpimage++; argnum--; break; case 'e': isdumpimage++; gif_outfile=argv[argnum]; break; case 'f': isdumpimage++; infilearg=argnum; break; + case 'g': ispbmpgm++; + if ( arglen > 1 ) ptype = atoi(field+1); /* -g2 ==> ptype=2 */ + if ( 1 || *argv[argnum]=='-' ) argnum--; /*next arg is -switch*/ + else pbm_outfile = argv[argnum]; break; /*next arg is filename*/ case 'm': msglevel = atoi(argv[argnum]); break; - case 'o': istransparent = 0; argnum--; break; + case 'o': istransparent = (istransparent?0:1); argnum--; break; + case 'q': isqforce = 1; argnum--; break; case 's': size = atoi(argv[argnum]); break; } /* --- end-of-switch(flag) --- */ } /* --- end-of-if(*argv[argnum]=='-') --- */ @@ -10024,8 +13697,10 @@ if ( !isquery /* don't have an html q else nbadargs++; /* infile and expression invalid */ } /* --- end-of-while(argc>++argnum) --- */ if ( msglevel>=999 && msgfp!=NULL ) /* display command-line info */ - fprintf(msgfp,"argc=%d, progname=%s, #args=%d, #badargs=%d\n", - argc,progname,nargs,nbadargs); + { fprintf(msgfp,"argc=%d, progname=%s, #args=%d, #badargs=%d\n", + argc,progname,nargs,nbadargs); + fprintf(msgfp,"cachepath=\"%.50s\" pathprefix=\"%.50s\"\n", + cachepath,pathprefix); } /* --- * decide whether command-line input overrides query_string * -------------------------------------------------------- */ @@ -10040,8 +13715,8 @@ if ( !isquery /* don't have an html q && infilearg <= 0 ) /* and not given in input file */ if ( !isquery /* no conflict if no query_string */ || nswitches > 0 ) /* explicit -switch(es) also given */ - { strncpy(expression,argv[exprarg],16384); /*expression from command-line*/ - expression[16384] = '\000'; /* make sure it's null terminated */ + { strncpy(expression,argv[exprarg],MAXEXPRSZ); /*expr from command-line*/ + expression[MAXEXPRSZ] = '\000'; /* make sure it's null terminated */ isquery = 0; } /* and not from a query_string */ /* --- * or read expression from input file @@ -10050,18 +13725,38 @@ if ( !isquery /* don't have an html q { FILE *infile = fopen(argv[infilearg],"r"); /* open input file for read */ if ( infile != (FILE *)NULL ) /* opened input file successfully */ - { char instring[2049]; /* line from file */ + { char instring[MAXLINESZ+1]; /* line from file */ + int exprsz = 0; /* total #bytes read from file */ isquery = 0; /* file input, not a query_string */ *expression = '\000'; /* start expresion as empty string */ - while ( fgets(instring,2048,infile) != (char *)NULL ) /* read till eof*/ - strcat(expression,instring); /* concat line to end of expression*/ + while ( fgets(instring,MAXLINESZ,infile) != (char *)NULL ) /*till eof*/ + if ( exprsz + strlen(instring) < MAXEXPRSZ ) { /* have room for line */ + strcat(expression,instring); /* concat line to end of expression*/ + exprsz += strlen(instring); } /* update expression buffer length */ fclose ( infile ); } /*close input file after reading expression*/ } /* --- end-of-if(infilearg>0) --- */ + /* --- + * xlate +++'s to blanks only if query + * ----------------------------------- */ + if ( !isquery ) isplusblank = 0; /* don't xlate +++'s to blanks */ + /* --- + * check if emulating query (for testing) + * -------------------------------------- */ + if ( isqforce ) isquery = 1; /* emulate query string processing */ + /* --- + * check if emitting pbm/pgm graphic + * --------------------------------- */ + if ( isgoodargs && ispbmpgm > 0 ) /* have a good -g arg */ + if ( 1 && gif_outfile != NULL ) /* had an -e switch with file */ + if ( *gif_outfile != '\000' ) /* make sure string isn't empty */ + { pbm_outfile = gif_outfile; /* use -e switch file for pbm/pgm */ + gif_outfile = (char *)NULL; /* reset gif output file */ + /*isdumpimage--;*/ } /* and decrement -e count */ } /* --- end-of-if(!isquery) --- */ /* --- * check for
input * ---------------------- */ -if ( isquery ) /* must be */ +if ( isquery ) { /* must be */ if ( !memcmp(expression,"formdata",8) ) /*must be */ { char *delim=strchr(expression,'='); /* find equal following formdata */ if ( delim != (char *)NULL ) /* found unescaped equal sign */ @@ -10071,10 +13766,10 @@ if ( isquery ) /* must be input --- */ - unescape_url(expression,0); /* convert _all_ %xx's to chars */ + unescape_url(expression,0); } /* convert _all_ %xx's to chars */ /* --- * check queries for embedded prefixes signalling special processing * ----------------------------------------------------------------- */ @@ -10104,7 +13799,7 @@ if ( isquery ) /* only log query_stri if ( msglevel >= LOGLEVEL /* check if logging */ && seclevel <= 5 ) /* and if logging permitted */ if ( logfile != NULL ) /* if a logfile is given */ - if ( *logfile != '\000' ) /*and if it's not an empty string*/ + if ( *logfile != '\000' ) { /*and if it's not an empty string*/ if ( (msgfp=fopen(logfile,"a")) /* open logfile for append */ != NULL ) /* ignore logging if can't open */ { @@ -10155,7 +13850,7 @@ if ( isquery ) /* only log query_stri isqlogging = 1; /* set query logging flag */ } /* --- end-of-if(msglevel>=LOGLEVEL) --- */ else /* couldn't open logfile */ - msglevel = 0; /* can't emit messages */ + msglevel = 0; } /* can't emit messages */ /* --- * prepend prefix to submitted expression * -------------------------------------- */ @@ -10221,11 +13916,14 @@ if ( isquery ) /* not relevant if "in /* --- * check for image caching * ----------------------- */ -if ( strstr(expression,"\\counter") != NULL /* can't cache \counter{} */ -|| strstr(expression,"\\input") != NULL /* can't cache \input{} */ -|| strstr(expression,"\\nocach") != NULL /* no caching requested */ +if ( strstr(expression,"\\counter") != NULL /* can't cache \counter{} */ +|| strstr(expression,"\\input") != NULL /* can't cache \input{} */ +|| strstr(expression,"\\today") != NULL /* can't cache \today */ +|| strstr(expression,"\\calendar") != NULL /* can't cache \calendar */ +|| strstr(expression,"\\nocach") != NULL /* no caching requested */ +|| isformdata /* don't cache user form input */ ) { iscaching = 0; /* so turn caching off */ - maxage = 2; } /* and set max-age to two seconds */ + maxage = 5; } /* and set max-age to 5 seconds */ if ( isquery ) /* don't cache command-line images */ if ( iscaching ) /* image caching enabled */ { @@ -10239,11 +13937,14 @@ if ( isquery ) /* don't cache command strcat(cachefile,md5hash); /* add md5 hash of expression */ strcat(cachefile,".gif"); /* finish with .gif extension */ gif_outfile = cachefile; /* signal GIF_Create() to cache */ - /* --- (always) emit mime content-type line --- */ - fprintf( stdout, "Cache-Control: max-age=%d\n",maxage ); - fprintf( stdout, "Content-type: image/gif\n\n" ); + /* --- emit mime content-type line --- */ + if ( 0 && isemitcontenttype ) /* now done in emitcache() */ + { fprintf( stdout, "Cache-Control: max-age=%d\n",maxage ); + if ( abs(valign) < 999 ) /* have vertical align */ + fprintf( stdout, "Vertical-Align: %d\n",valign ); + fprintf( stdout, "Content-type: image/gif\n\n" ); } /* --- emit cached image if it already exists --- */ - if ( emitcache(cachefile) > 0 ) /* cached image emitted */ + if ( emitcache(cachefile,maxage,valign,0) > 0 ) /* cached image emitted */ goto end_of_job; /* so nothing else to do */ /* --- log caching request --- */ if ( msglevel >= 1 /* check if logging */ @@ -10258,7 +13959,7 @@ if ( isquery ) /* don't cache command != NULL ) /* ignore logging if can't open */ { int isreflogged = 0; /* set true if http_referer logged */ fprintf(filefp,"%s %s\n", /* timestamp, md5 file */ - timestamp(),cachefile+strlen(cachepath)); /* (path not shown) */ + timestamp(TZDELTA,0),cachefile+strlen(cachepath)); /*skip path*/ fprintf(filefp,"%s\n",expression); /* expression in filename */ if ( http_referer != NULL ) /* show referer if we have one */ if ( *http_referer != '\000' ) /* and if not an empty string*/ @@ -10286,7 +13987,8 @@ if ( !isdumpimage ) /* don't mix ascii rasterize expression and put a border around it -------------------------------------------------------------------------- */ /* --- preprocess expression, converting LaTeX constructs for mimeTeX --- */ -expression = mimeprep(expression); /* preprocess expression */ +if ( expression != NULL ) { /* have expression to rasterize */ + expression = mimeprep(expression); } /* preprocess expression */ /* --- double-check that we actually have an expression to rasterize --- */ if ( expression == NULL ) /* nothing to rasterize */ { if ( (!isquery||isqlogging) && msgfp!=NULL ) /*emit error if not a query*/ @@ -10305,6 +14007,11 @@ if ( issupersampling ) /* no border ne else /* for mime xbitmaps must have... */ bp = border_raster(sp->image,0,0,0,1); /* image width multiple of 8 bits */ sp->image = bitmap_raster = bp; /* global copy for gif,png output */ +if ( sp!=NULL && bp!=NULL ) { /* have raster */ + valign = sp->baseline - (bp->height - 1); /* #pixels for Vertical-Align: */ + if ( abs(valign) > 255 ) valign = (-9999); } /* sanity check */ +if ( ispbmpgm && ptype<2 ) /* -g switch or -g1 switch */ + type_pbmpgm(bp,ptype,pbm_outfile); /* emit b/w pbm file */ /* ------------------------------------------------------------------------- generate anti-aliased bytemap from (bordered) bitmap -------------------------------------------------------------------------- */ @@ -10334,14 +14041,52 @@ if ( isaa ) /* we want anti-aliased b * select anti-aliasing algorithm * ------------------------------ */ if ( !isss ) /* generate bytemap for lowpass */ - if ( aaalgorithm == 1 ) /* 1 for aalowpass() */ - { if ( aalowpass(bp,bytemap_raster,grayscale) /* my lowpass filter */ - == 0 ) isaa = 0; } /*failed, so turn off anti-aliasing*/ - else /* or 2 for aapnm() */ - if ( aaalgorithm == 2 ) /*2 for netpbm pnmalias.c algorithm*/ - { if ( aapnm(bp,bytemap_raster,grayscale) /* pnmalias.c filter */ - == 0 ) isaa = 0; } /*failed, so turn off anti-aliasing*/ - else isaa = 0; /* unrecognized algorithm */ + switch ( aaalgorithm ) { /* choose antialiasing algorithm */ + default: isaa = 0; break; /* unrecognized algorithm */ + case 1: /* 1 for aalowpass() */ + if ( aalowpass(bp,bytemap_raster,grayscale) /*my own lowpass filter*/ + == 0 ) isaa = 0; /*failed, so turn off anti-aliasing*/ + break; + case 2: /*2 for netpbm pnmalias.c algorithm*/ + if ( aapnm(bp,bytemap_raster,grayscale) /* pnmalias.c filter */ + == 0 ) isaa = 0; /*failed, so turn off anti-aliasing*/ + break; + case 3: /*3 for aapnm() based on aagridnum()*/ + if ( aapnmlookup(bp,bytemap_raster,grayscale) /* pnmalias.c filter */ + == 0 ) isaa = 0; /*failed, so turn off anti-aliasing*/ + break; + case 4: /* 4 for aalookup() table lookup */ + if ( aalowpasslookup(bp,bytemap_raster,grayscale) /* aalookup() */ + == 0 ) isaa = 0; /*failed, so turn off anti-aliasing*/ + break; + } /* --- end-of-switch(aaalgorithm) --- */ + /* --- + * emit aalookup() pattern# counts/percents diagnostics + * ---------------------------------------------------- */ + if ( !isquery && msgfp!=NULL && msglevel>=99 ) { /*emit patternnumcounts*/ + int pcount0=0, pcount1=0; /* init total w,b center counts */ + for ( ipattern=1; ipattern<=51; ipattern++ ) { /*each possible pattern*/ + if ( ipattern > 1 ) /* ignore all-white squares */ + pcount0 += patternnumcount0[ipattern]; /* bump total white centers */ + pcount1 += patternnumcount1[ipattern]; } /* bump total black centers */ + if ( pcount0+pcount1 > 0 ) /* have pcounts (using aalookup) */ + fprintf(msgfp, " aalookup() patterns excluding#1 white" + " (%%'s are in tenths of a percent)...\n"); + for ( ipattern=1; ipattern<=51; ipattern++ ) { /*each possible pattern*/ + int tot = patternnumcount0[ipattern] + patternnumcount1[ipattern]; + if ( tot > 0 ) /* this pattern occurs in image */ + fprintf(msgfp, + " pattern#%2d: %7d(%6.2f%%) +%7d(%6.2f%%) =%7d(%6.2f%%)\n", + ipattern, patternnumcount0[ipattern], (ipattern<=1? 999.99: + 1000.*((double)patternnumcount0[ipattern])/((double)pcount0)), + patternnumcount1[ipattern], + 1000.*((double)patternnumcount1[ipattern])/((double)pcount1), + tot, (ipattern<=1? 999.99: + 1000.*((double)tot)/((double)(pcount0+pcount1))) ); } + if ( pcount0+pcount1 > 0 ) /* true when using aalookup() */ + fprintf(msgfp, + "all patterns: %7d +%7d =%7d total pixels\n", + pcount0,pcount1,pcount0+pcount1); } /* --- * finally, generate colors and colormap * ------------------------------------- */ @@ -10351,6 +14096,11 @@ if ( isaa ) /* we want anti-aliased b { isaa = 0; /* so turn off anti-aliasing */ ncolors = 2; } /* and reset for black&white */ } /* --- end-of-if(isaa) --- */ + if ( isaa && ispbmpgm && ptype>1 ) { /* -g2 switch */ + raster pbm_raster; /*construct arg for write_pbmpgm()*/ + pbm_raster.width = bp->width; pbm_raster.height = bp->height; + pbm_raster.pixsz = 8; pbm_raster.pixmap = (pixbyte *)bytemap_raster; + type_pbmpgm(&pbm_raster,ptype,pbm_outfile); } /*write grayscale file*/ } /* --- end-of-if(isaa) --- */ } /* --- end-of-if(isaa) --- */ /* ------------------------------------------------------------------------- @@ -10392,7 +14142,7 @@ if ( (!isquery||isqlogging) || msglevel emit xbitmap or gif image, and exit -------------------------------------------------------------------------- */ if ( isquery /* called from browser (usual) */ -|| isdumpimage /* or to emit dump of image */ +|| (isdumpimage && !ispbmpgm) /* or to emit gif dump of image */ || msglevel >= 99 ) /* or for debugging */ { int igray = 0; /* grayscale index */ @@ -10400,23 +14150,55 @@ if ( isquery /* called from browser /* ------------------------------------------------------------------------ emit GIF image ------------------------------------------------------------------------- */ + /* --- don't use memory buffer if outout file given --- */ + if ( gif_outfile != NULL ) isinmemory = 0; /* reset memory buffer flag */ + /* --- construct contenttype[] buffer containing mime headers --- */ + if ( 1 ) { /* always construct buffer */ + sprintf( contenttype, "Cache-Control: max-age=%d\n", maxage ); + /*sprintf(contenttype+strlen(contenttype), + "Expires: Fri, 31 Oct 2003 23:59:59 GMT\n" );*/ + /*sprintf(contenttype+strlen(contenttype), + "Last-Modified: Wed, 15 Oct 2003 01:01:01 GMT\n");*/ + if ( abs(valign) < 999 ) /* have Vertical-Align: header info*/ + sprintf( contenttype+strlen(contenttype), + "Vertical-Align: %d\n", valign ); + sprintf( contenttype+strlen(contenttype), + "Content-type: image/gif\n\n" ); } /* --- emit mime content-type line --- */ - if ( !isdumpimage /* don't mix ascii with image dump */ - && !iscaching ) /* already emitted if caching */ - { fprintf( stdout, "Cache-Control: max-age=%d\n",maxage ); - /*fprintf( stdout, "Expires: Fri, 31 Oct 2003 23:59:59 GMT\n" );*/ - /*fprintf( stdout, "Last-Modified: Wed, 15 Oct 2003 01:01:01 GMT\n" );*/ - fprintf( stdout, "Content-type: image/gif\n\n" ); } + if ( isemitcontenttype /* content-type lines wanted */ + && !isdumpimage /* don't mix ascii with image dump */ + && !isinmemory /* done below if in memory */ + && !iscaching ) /* done by emitcache() if caching */ + { fputs(contenttype,stdout); } /* emit content-type: header buffer*/ + /* --- write output to memory buffer, possibly for testing --- */ + if ( isinmemory /* want gif written to memory */ + || isdumpbuffer ) /*or dump memory buffer for testing*/ + if ( gif_outfile == NULL ) /* and don't already have a file */ + { *gif_buffer = '\000'; /* init buffer as empty string */ + memset(gif_buffer,0,MAXGIFSZ); /* zero out buffer */ + gif_outfile = gif_buffer; /* and point outfile to buffer */ + if ( isdumpbuffer ) /* buffer dump test requested */ + isdumpbuffer = 999; } /* so signal dumping to buffer */ /* --- initialize gifsave library and colors --- */ if ( msgfp!=NULL && msglevel>=999 ) - fprintf(msgfp,"main> calling GIF_Create(*,%d,%d,%d,8)\n", - bp->width,bp->height,ncolors); + { fprintf(msgfp,"main> calling GIF_Create(*,%d,%d,%d,8)\n", + bp->width,bp->height,ncolors); fflush(msgfp); } while ( 1 ) /* init gifsave lib, and retry if caching fails */ { int status = GIF_Create(gif_outfile, bp->width,bp->height, ncolors, 8); if ( status == 0 ) break; /* continue if succeeded */ if ( iscaching == 0 ) goto end_of_job; /* quit if failed */ iscaching = 0; /* retry without cache file */ - gif_outfile = (char *)NULL; } /* emit images to stdout */ + isdumpbuffer = 0; /* reset isdumpbuffer signal */ + if ( isquery ) isinmemory = 1; /* force in-memory image generation*/ + if ( isinmemory ) { /* using memory buffer */ + gif_outfile = gif_buffer; /* emit images to memory buffer */ + *gif_outfile = '\000'; } /* empty string signals buffer */ + else { /* or */ + gif_outfile = (char *)NULL; /* emit images to stdout */ + if ( isemitcontenttype ) { /* content-type lines wanted */ + fprintf( stdout, "Cache-Control: max-age=%d\n",maxage ); + fprintf( stdout, "Content-type: image/gif\n\n" ); } } + } /* --- end-of-while(1) --- */ GIF_SetColor(0,bgred,bggreen,bgblue); /* background white if all 255 */ if ( !isaa ) /* just b&w if not anti-aliased */ { GIF_SetColor(1,fgred,fggreen,fgblue); /* foreground black if all 0 */ @@ -10441,9 +14223,26 @@ if ( isquery /* called from browser /* --- emit compressed gif image (to stdout or cache file) --- */ GIF_CompressImage(0, 0, -1, -1, GetPixel); /* emit gif */ GIF_Close(); /* close file */ - /* --- may need to emit image from cached file --- */ - if ( isquery && iscaching ) /* caching enabled */ - emitcache(cachefile); /* cached image (hopefully) emitted*/ + if ( msgfp!=NULL && msglevel>=9 ) + { fprintf(msgfp,"main> created gifSize=%d\n", gifSize); + fflush(msgfp); } + /* --- may need to emit image from cached file or from memory --- */ + if ( isquery /* have an actual query string */ + || isdumpimage /* or dumping image */ + || msglevel >= 99 ) { /* or debugging */ + int maxage2 = (isdumpimage?(-1):maxage); /* no headers if dumping image */ + if ( iscaching ) /* caching enabled */ + emitcache(cachefile,maxage2,valign,0); /*emit cached image (hopefully)*/ + else if ( isinmemory ) /* or emit image from memory buffer*/ + emitcache(gif_buffer,maxage2,valign,1); } /*emitted from memory buffer*/ + /* --- for testing, may need to write image buffer to file --- */ + if ( isdumpbuffer > 99 ) /* gif image in memory buffer */ + if ( gifSize > 0 ) /* and it's not an empty buffer */ + { FILE *dumpfp = fopen("mimetex.gif","wb"); /* dump to mimetex.gif */ + if ( dumpfp != NULL ) /* file opened successfully */ + { fwrite(gif_buffer,sizeof(unsigned char),gifSize,dumpfp); /*write*/ + fclose(dumpfp); } /* and close file */ + } /* --- end-of-if(isdumpbuffer>99) --- */ #else /* ------------------------------------------------------------------------ emit mime XBITMAP image @@ -10453,17 +14252,79 @@ if ( isquery /* called from browser } /* --- end-of-if(isquery) --- */ /* --- exit --- */ end_of_job: - if ( bytemap_raster != NULL ) free(bytemap_raster); /*free bytemap_raster*/ + if ( !isss ) /*bytemap raster in sp for supersamp*/ + if ( bytemap_raster != NULL ) free(bytemap_raster);/*free bytemap_raster*/ if (colormap_raster != NULL )free(colormap_raster); /*and colormap_raster*/ + if ( 0 && gif_buffer != NULL ) free(gif_buffer); /* free malloced buffer */ + if ( 1 && sp != NULL ) delete_subraster(sp); /* and free expression */ if ( msgfp != NULL /* have message/log file open */ && msgfp != stdout ) /* and it's not stdout */ - { fprintf(msgfp,"mimeTeX> successful end-of-job at %s\n",timestamp()); + { fprintf(msgfp,"mimeTeX> successful end-of-job at %s\n", + timestamp(TZDELTA,0)); fprintf(msgfp,"%s\n",dashes); /* so log separator line */ fclose(msgfp); } /* and close logfile */ - exit ( 0 ); + /* --- dump memory leaks in debug window if in MS VC++ debug mode --- */ + #if defined(_CRTDBG_MAP_ALLOC) + _CrtDumpMemoryLeaks(); + #endif + /* --- exit() if not running as Windows DLL (see CreateGifFromEq()) --- */ + #if !defined(_USRDLL) + exit ( 0 ); + #endif } /* --- end-of-function main() --- */ /* ========================================================================== + * Function: CreateGifFromEq ( expression, gifFileName ) + * Purpose: shortcut method to create GIF file for expression, + * with antialising and all other capabilities + * -------------------------------------------------------------------------- + * Arguments: expression (I) char *ptr to null-terminated string + * containing LaTeX expression to be rendred + * gifFileName (I) char *ptr to null-terminated string + * containing name of output gif file + * -------------------------------------------------------------------------- + * Returns: ( int ) exit value from main (0 if successful) + * -------------------------------------------------------------------------- + * Notes: o This function is the entry point when mimeTeX is built + * as a Win32 DLL rather then a standalone app or CGI + * o Contributed to mimeTeX by Shital Shah. See his homepage + * http://www.shitalshah.com + * o Shital discusses the mimeTeX Win32 DLL project at + * http://www.codeproject.com/dotnet/Eq2Img.asp + * and you can download his latest code from + * http://www.shitalshah.com/dev/eq2img_all.zip + * ======================================================================= */ +/* --- include function to expose Win32 DLL to outside world --- */ +#if defined(_USRDLL) + extern _declspec(dllexport)int _cdecl + CreateGifFromEq ( char *expression, char *gifFileName ); +#endif +/* --- entry point --- */ +int CreateGifFromEq ( char *expression, char *gifFileName ) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +int main(); /* main() akways returns an int */ +/* --- set constants --- */ +int argc = 4; /* count of args supplied to main() */ +char *argv[5] = /* command line args to run with -e option */ + { "MimeTeXWin32DLL", "-e", /* constant args */ + /*gifFileName, expression,*/ NULL, NULL, NULL }; +/* --- set argv[]'s not computable at load time --- */ +argv[2] = gifFileName; /* args are -e gifFileName */ +argv[3] = expression; /* and now -e gifFileName expression */ +/* ------------------------------------------------------------------------- +Run mimeTeX in command-line mode with -e (export) option, and then return +-------------------------------------------------------------------------- */ +return main ( argc, argv + #ifdef DUMPENVP + , NULL + #endif + ) ; +} /* --- end-of-function CreateGifFromEq() --- */ + +/* ========================================================================== * Function: isstrstr ( char *string, char *snippets, int iscase ) * Purpose: determine whether any substring of 'string' * matches any of the comma-separated list of 'snippets', @@ -10612,13 +14473,80 @@ end_of_job: * ftp://ftp.ncsa.uiuc.edu/Web/httpd/Unix/ncsa_httpd/cgi/ncsa-default.tar.Z * o Not quite "verbatim" -- I added the "isescape logic" 4-Dec-03 * so unescape_url() can be safely applied to input which may or - * may not have been url-encoded. + * may not have been url-encoded. (Note: currently, all calls + * to unescape_url() pass iescape=0, so it's not used.) + * o Added +++'s to blank xlation on 24-Sep-06 + * o Added ^M,^F,etc to blank xlation 0n 01-Oct-06 * ======================================================================= */ /* --- entry point --- */ int unescape_url(char *url, int isescape) { int x=0,y=0,prevescape=0,gotescape=0; + int xlateplus = (isplusblank==1?1:0); /* true to xlate plus to blank */ + int strreplace(); /* replace + with blank, if needed */ char x2c(); static char *hex="0123456789ABCDEFabcdef"; + /* --- + * xlate ctrl chars to blanks + * -------------------------- */ + if ( 1 ) { /* xlate ctrl chars to blanks */ + char *ctrlchars = "\n\t\v\b\r\f\a\015"; + int seglen = strspn(url,ctrlchars); /*initial segment with ctrlchars*/ + int urllen = strlen(url); /* total length of url string */ + /* --- first, entirely remove ctrlchars from beginning and end --- */ + if ( seglen > 0 ) { /*have ctrlchars at start of string*/ + strcpy(url,url+seglen); /* squeeze out initial ctrlchars */ + urllen -= seglen; } /* string is now shorter */ + while ( --urllen >= 0 ) /* now remove ctrlchars from end */ + if ( isthischar(url[urllen],ctrlchars) ) /* ctrlchar at end */ + url[urllen] = '\000'; /* re-terminate string before it */ + else break; /* or we're done */ + urllen++; /* length of url string */ + /* --- now, replace interior ctrlchars with ~ blanks --- */ + while ( (seglen=strcspn(url,ctrlchars)) < urllen ) /*found a ctrlchar*/ + url[seglen] = '~'; /* replace ctrlchar with ~ */ + } /* --- end-of-if(1) --- */ + /* --- + * xlate +'s to blanks if requested or if deemed necessary + * ------------------------------------------------------- */ + if ( isplusblank == (-1) ) { /*determine whether or not to xlate*/ + char *searchfor[] = { " ","%20", "%2B","%2b", "+++","++", + "+=+","+-+", NULL }; + int isearch = 0, /* searchfor[] index */ + nfound[11] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; /*#occurrences*/ + /* --- locate occurrences of searchfor[] strings in url --- */ + for ( isearch=0; searchfor[isearch] != NULL; isearch++ ) { + char *psearch = url; /* start search at beginning */ + nfound[isearch] = 0; /* init #occurrences count */ + while ( (psearch=strstr(psearch,searchfor[isearch])) != NULL ) { + nfound[isearch] += 1; /* count another occurrence */ + psearch += strlen(searchfor[isearch]); } /*resume search after it*/ + } /* --- end-of-for(isearch) --- */ + /* --- apply some common-sense logic --- */ + if ( nfound[0] + nfound[1] > 0 ) /* we have actual " "s or "%20"s */ + isplusblank = xlateplus = 0; /* so +++'s aren't blanks */ + if ( nfound[2] + nfound[3] > 0 ) { /* we have "%2B" for +++'s */ + if ( isplusblank != 0 ) /* and haven't disabled xlation */ + isplusblank = xlateplus = 1; /* so +++'s are blanks */ + else /* we have _both_ "%20" and "%2b" */ + xlateplus = 0; } /* tough call */ + if ( nfound[4] + nfound[5] > 0 /* we have multiple ++'s */ + || nfound[6] + nfound[7] > 0 ) /* or we have a +=+ or +-+ */ + if ( isplusblank != 0 ) /* and haven't disabled xlation */ + xlateplus = 1; /* so xlate +++'s to blanks */ + } /* --- end-of-if(isplusblank==-1) --- */ + if ( xlateplus > 0 ) { /* want +'s xlated to blanks */ + char *xlateto[] = { ""," "," "," + "," "," "," "," "," " }; + while ( xlateplus > 0 ) { /* still have +++'s to xlate */ + char plusses[99] = "++++++++++++++++++++"; /* longest +++ string */ + plusses[xlateplus] = '\000'; /* null-terminate +++'s */ + strreplace(url,plusses,xlateto[xlateplus],0); /* xlate +++'s */ + xlateplus--; /* next shorter +++ string */ + } /* --- end-of-while(xlateplus>0) --- */ + } /* --- end-of-if(xlateplus) --- */ + isplusblank = 0; /* don't iterate this xlation */ + /* --- + * xlate %nn to corresponding char + * ------------------------------- */ for(;url[y];++x,++y) { gotescape = prevescape; prevescape = (url[x]=='\\'); @@ -10669,7 +14597,7 @@ char *value = NULL; /* getenv(name) to /* ------------------------------------------------------------------------- Log each variable -------------------------------------------------------------------------- */ -fprintf(fp,"%s\n",timestamp()); /* emit timestamp before first var */ +fprintf(fp,"%s\n",timestamp(TZDELTA,0)); /*emit timestamp before first var*/ if ( message != NULL ) /* optional message supplied */ fprintf(fp," MESSAGE = %s\n",message); /* emit caller-supplied message */ if ( logvars != (logdata *)NULL ) /* have logvars */ @@ -10686,36 +14614,61 @@ return ( nlogged ); /* back to caller } /* --- end-of-function logger() --- */ /* ========================================================================== - * Function: emitcache ( cachefile ) + * Function: emitcache ( cachefile, maxage, valign, isbuffer ) * Purpose: dumps bytes from cachefile to stdout * -------------------------------------------------------------------------- * Arguments: cachefile (I) pointer to null-terminated char string - * containing full path to file to be dumped + * containing full path to file to be dumped, + * or contains buffer of bytes to be dumped + * maxage (I) int containing maxage, in seconds, for + * http header, or -1 to not emit headers + * valign (I) int containing Vertical-Align:, in pixels, + * for http header, or <= -999 to not emit + * isbuffer (I) 1 if cachefile is buffer of bytes to be + * dumped * -------------------------------------------------------------------------- * Returns: ( int ) #bytes dumped (0 signals error) * -------------------------------------------------------------------------- * Notes: o * ======================================================================= */ /* --- entry point --- */ -int emitcache ( char *cachefile ) +int emitcache ( char *cachefile, int maxage, int valign, int isbuffer ) { /* ------------------------------------------------------------------------- Allocations and Declarations -------------------------------------------------------------------------- */ -FILE *cacheptr = fopen(cachefile,"rb"), /*open cachefile for binary read*/ - *emitptr = stdout; /* emit cachefile to stdout */ -unsigned char buffer[64]; /* characters from cachefile */ -int buflen = 32, /* #bytes we try to read from cache*/ - nread = 0, /* #bytes actually read */ - nbytes = 0; /* total #bytes emitted */ +int nbytes=gifSize, readcachefile(); /* read cache file */ +FILE *emitptr = stdout; /* emit cachefile to stdout */ +unsigned char buffer[MAXGIFSZ+1]; /* bytes from cachefile */ +unsigned char *buffptr = buffer; /* ptr to buffer */ +int isvalign = (abs(valign)<999?1:0); /* true to emit Vertical-Align: */ +int iscontenttypecached = iscachecontenttype; /*true if headers cached*/ /* ------------------------------------------------------------------------- initialization -------------------------------------------------------------------------- */ /* --- check that files opened okay --- */ -if ( cacheptr == (FILE *)NULL /* failed to open cachefile */ -|| emitptr == (FILE *)NULL ) /* or failed to open emit file */ +if ( emitptr == (FILE *)NULL ) /* failed to open emit file */ goto end_of_job; /* so return 0 bytes to caller */ -/* --- set stdout to binary mode (for Windows) --- */ +/* --- read the file if necessary --- */ +if ( isbuffer ) { /* cachefile is buffer */ + buffptr = (unsigned char *)cachefile; /* so reset buffer pointer */ + iscontenttypecached = 0; } /* and iscontenttypecached flag */ +else { /* cachefile is file name */ + if ( (nbytes = readcachefile(cachefile,buffer)) /* read the file */ + < 1 ) goto end_of_job; } /* quit if file not read */ +/* --- first emit http headers if requested --- */ +if ( isemitcontenttype /* content-type lines enabled */ +&& !iscontenttypecached /* and not in cached image */ +&& maxage >= 0 ) /* caller wants http headers */ + { /* --- emit mime content-type line --- */ + fprintf( emitptr, "Cache-Control: max-age=%d\n",maxage ); + fprintf( emitptr, "Content-Length: %d\n",nbytes ); + if ( isvalign ) /* Vertical-Align: header wanted */ + fprintf( emitptr, "Vertical-Align: %d\n",valign ); + fprintf( emitptr, "Content-type: image/gif\n\n" ); } +/* ------------------------------------------------------------------------- +set stdout to binary mode (for Windows) +-------------------------------------------------------------------------- */ /* emitptr = fdopen(STDOUT_FILENO,"wb"); */ /* doesn't work portably, */ #ifdef WINDOWS /* so instead... */ #ifdef HAVE_SETMODE /* prefer (non-portable) setmode() */ @@ -10732,23 +14685,68 @@ if ( cacheptr == (FILE *)NULL /* failed /* ------------------------------------------------------------------------- emit bytes from cachefile -------------------------------------------------------------------------- */ +/* --- write bytes to stdout --- */ +if ( fwrite(buffptr,sizeof(unsigned char),nbytes,emitptr) /* write buffer */ +< nbytes ) /* failed to write all bytes */ + nbytes = 0; /* reset total count to 0 */ +end_of_job: + return ( nbytes ); /* back with #bytes emitted */ +} /* --- end-of-function emitcache() --- */ + +/* ========================================================================== + * Function: readcachefile ( cachefile, buffer ) + * Purpose: read cachefile into buffer + * -------------------------------------------------------------------------- + * Arguments: cachefile (I) pointer to null-terminated char string + * containing full path to file to be read + * buffer (O) pointer to unsigned char string + * returning contents of cachefile + * (max 64000 bytes) + * -------------------------------------------------------------------------- + * Returns: ( int ) #bytes read (0 signals error) + * -------------------------------------------------------------------------- + * Notes: o + * ======================================================================= */ +/* --- entry point --- */ +int readcachefile ( char *cachefile, unsigned char *buffer ) +{ +/* ------------------------------------------------------------------------- +Allocations and Declarations +-------------------------------------------------------------------------- */ +FILE *cacheptr = fopen(cachefile,"rb"); /*open cachefile for binary read*/ +unsigned char cachebuff[64]; /* bytes from cachefile */ +int buflen = 32, /* #bytes we try to read from file */ + nread = 0, /* #bytes actually read from file */ + maxbytes = MAXGIFSZ, /* max #bytes returned in buffer */ + nbytes = 0; /* total #bytes read */ +/* ------------------------------------------------------------------------- +initialization +-------------------------------------------------------------------------- */ +/* --- check that files opened okay --- */ +if ( cacheptr == (FILE *)NULL ) goto end_of_job; /*failed to open cachefile*/ +/* --- check that output buffer provided --- */ +if ( buffer == (unsigned char *)NULL ) goto end_of_job; /* no buffer */ +/* ------------------------------------------------------------------------- +read bytes from cachefile +-------------------------------------------------------------------------- */ while ( 1 ) { /* --- read bytes from cachefile --- */ - nread = fread(buffer,sizeof(unsigned char),buflen,cacheptr); /* read */ + nread = fread(cachebuff,sizeof(unsigned char),buflen,cacheptr); /* read */ + if ( nbytes + nread > maxbytes ) /* block too big for buffer */ + nread = maxbytes - nbytes; /* so truncate it */ if ( nread < 1 ) break; /* no bytes left in cachefile */ - /* --- write bytes to stdout --- */ - if ( fwrite(buffer,sizeof(unsigned char),nread,emitptr) /* write buffer */ - < nread) /* failed to write all bytes */ - { nbytes = 0; /* reset total count to 0 */ - goto end_of_job; } /* and signal error to caller */ + /* --- store bytes in buffer --- */ + memcpy(buffer+nbytes,cachebuff,nread); /* copy current block to buffer */ + /* --- ready to read next block --- */ nbytes += nread; /* bump total #bytes emitted */ if ( nread < buflen ) break; /* no bytes left in cachefile */ + if ( nbytes >= maxbytes ) break; /* avoid buffer overflow */ } /* --- end-of-while(1) --- */ end_of_job: if ( cacheptr != NULL ) fclose(cacheptr); /* close file if opened */ return ( nbytes ); /* back with #bytes emitted */ -} /* --- end-of-function emitcache() --- */ +} /* --- end-of-function readcachefile() --- */ /* ========================================================================== * Function: md5str ( instr )