📄 mimetex.c
字号:
/****************************************************************************
*
* Copyright(c) 2002-2006, John Forkosh Associates, Inc. All rights reserved.
* --------------------------------------------------------------------------
* 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.
* 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.
* By using mimeTeX, you warrant that you have read, understood and
* 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
* --------------------------------------------------------------------------
*
* Purpose: o MimeTeX, licensed under the gpl, lets you easily embed
* LaTeX math in your html pages. It parses a LaTeX math
* expression and immediately emits the corresponding gif
* image, rather than the usual TeX dvi. And mimeTeX is an
* 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 <img> tag in your document
* wherever you want to see the corresponding LaTeX expression.
* For example,
* <img src="../cgi-bin/mimetex.cgi?\int_{-\infty}^xe^{-t^2}dt"
* alt="" border=0 align=middle>
* immediately generates the corresponding gif image on-the-fly,
* displaying the rendered expression wherever you put that
* <img> 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.
* There's also no inherent need to repeatedly write the
* cumbersome <img> tag illustrated above. You can write
* your own custom tags, or write a wrapper script around
* mimeTeX to simplify the necessary notation.
*
* Functions: ===================== Raster Functions ======================
* PART2 --- raster constructor functions ---
* new_raster(width,height,pixsz) allocation (and constructor)
* new_subraster(width,height,pixsz)allocation (and constructor)
* new_chardef() allocate chardef struct
* delete_raster(rp) deallocate raster (rp = raster ptr)
* delete_subraster(sp) deallocate subraster (sp=subraster ptr)
* delete_chardef(cp) deallocate chardef (cp = chardef ptr)
* --- primitive (sub)raster functions ---
* rastcpy(rp) allocate new copy of rp
* subrastcpy(sp) allocate new copy of sp
* rastrot(rp) new raster rotated right 90 degrees to rp
* 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
* rastsmash(sp1,sp2,xmin,ymin) calc #smash pixels sp1||sp2
* --- raster "drawing" functions ---
* accent_subraster(accent,width,height) draw \hat\vec\etc
* arrow_subraster(width,height,drctn,isBig) left/right arrow
* uparrow_subraster(width,height,drctn,isBig) up/down arrow
* rule_raster(rp,top,left,width,height,type) draw rule in rp
* line_raster(rp,row0,col0,row1,col1,thickness) draw line in rp
* line_recurse(rp,row0,col0,row1,col1,thickness) recurse line
* circle_raster(rp,row0,col0,row1,col1,thickness,quads) ellipse
* 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
* --- 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_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
* make_delim(symbol,height) construct delim exactly height size
* ================= 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
* preamble(expression,size,subexpr) parse preamble
* 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
* strtexchr(string,texchr) find texchr in string
* findbraces(expression,command) find opening { or closing }
* PART3 =========== Rasterize an Expression (recursively) ===========
* --- here's the primary entry point for all of mimeTeX ---
* rasterize(expression,size) parse and rasterize expression
* --- explicitly called handlers that rasterize... ---
* rastparen(subexpr,size,basesp) parenthesized subexpr
* rastlimits(expression,size,basesp) dispatch super/sub call
* rastscripts(expression,size,basesp) super/subscripted exprssn
* 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) \\
* rastarrow(expression,size,basesp,width,height,drctn) \longarr
* rastuparrow(expression,size,basesp,width,height,drctn)up/down
* rastoverlay(expression,size,basesp,overlay,arg2,arg3) \not
* rastfrac(expression,size,basesp,isfrac,arg2,arg3) \frac \atop
* rastackrel(expression,size,basesp,base,arg2,arg3) \stackrel
* rastmathfunc(expression,size,basesp,base,arg2,arg3) \lim,\etc
* rastsqrt(expression,size,basesp,arg1,arg2,arg3) \sqrt
* rastaccent(expression,size,basesp,accent,isabove,isscript)
* rastfont(expression,size,basesp,font,arg2,arg3) \cal{},\scr{}
* rastbegin(expression,size,basesp,arg1,arg2,arg3) \begin{}
* rastarray(expression,size,basesp,arg1,arg2,arg3) \array
* rastpicture(expression,size,basesp,arg1,arg2,arg3) \picture
* rastline(expression,size,basesp,arg1,arg2,arg3) \line
* 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
* 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
* rasteditfilename(filename) edit filename (for security)
* rastreadfile(filename,islock,tag,value) read <tag>...</tag>
* rastwritefile(filename,tag,value,isstrict)write<tag>...</tag>
* 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
* 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
* 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,maxage,isbuffer) emit cachefile to stdout
* 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)
*
* --------------------------------------------------------------------------
* Notes o See bottom of file for main() driver (and "friends"),
* and compile as
* cc -DAA mimetex.c gifsave.c -lm -o mimetex.cgi
* to produce an executable that emits gif images with
* anti-aliasing (see Notes below). You may also compile
* cc -DGIF mimetex.c gifsave.c -lm -o mimetex.cgi
* to produce an executable that emits gif images without
* anti-aliasing. Alternatively, compile mimeTeX as
* cc -DXBITMAP mimetex.c -lm -o mimetex.cgi
* to produce an executable that just emits mime xbitmaps.
* In either case you'll need mimetex.h and texfonts.h,
* and with -DAA or -DGIF you'll also need gifsave.c
* o For gif images, the gifsave.c library by Sverre H. Huseby
* <http://shh.thathost.com> 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 .
* o Optional compile-line -D defined symbols are documented
* in mimetex.html#options . They include...
* -DAA
* Turns on gif anti-aliasing with default values
* (CENTERWT=32, ADJACENTWT=3, CORNERWT=1)
* for the following anti-aliasing parameters...
* -DCENTERWT=n
* -DADJACENTWT=j
* -DCORNERWT=k
* MimeTeX currently provides a lowpass filtering
* 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
* to neighboring pixels. The defaults weights are
* CENTERWT=32, ADJACENTWT=3 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.
* Experimentation is recommended to determine
* what value works best for you.
* -DCACHEPATH=\"path/\"
* This option saves each rendered image to a file
* in directory path/ which mimeTeX reads rather than
* re-rendering the same image every time it's given
* the same LaTeX expression. Sometimes mimeTeX disables
* caching, e.g., expressions containing \input{ } are
* re-rendered since the contents of the inputted file
* may have changed. If compiled without -DCACHEPATH
* mimeTeX always re-renders expressions. This usually
* isn't too cpu intensive, but if you have unusually
* high hit rates then image caching may be helpful.
* The path/ is relative to mimetex.cgi, and must
* be writable by it. Files created under path/ are
* named filename.gif, where filename is the 32-character
* MD5 hash of the LaTeX expression.
* -DDISPLAYSIZE=n
* By default, operator limits like \int_a^b are rendered
* \textstyle at font sizes \normalsize and smaller,
* and rendered \displaystyle at font sizes \large and
* larger. This default corresponds to -DDISPLAYSIZE=3,
* which you can adjust; e.g., -DDISPLAYSIZE=0 always
* defaults to \displaystyle, and 99 (or any large number)
* always defaults to \textstyle. Note that explicit
* \textstyle, \displaystyle, \limits or \nolimits
* directives in an expression always override
* the DISPLAYSIZE default.
* -NORMALSIZE=n
* MimeTeX currently has six font sizes numbered 0-5,
* and always starts in NORMALSIZE whose default value
* is 2. Specify -DNORMALSIZE=3 on the compile line if
* you prefer mimeTeX to start in default size 3, etc.
* -DREFERER=\"domain\" -or-
* -DREFERER=\"domain1,domain2,etc\"
* Blocks mimeTeX requests from unauthorized domains that
* may be using your server's mimetex.cgi without permission.
* If REFERER is defined, mimeTeX checks for the environment
* variable HTTP_REFERER and, if it exists, performs a
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -