📄 mimetex.c
字号:
/* -------------------------------------------------------------------------
allocate raster and embed it in subraster, and return to caller
-------------------------------------------------------------------------- */
/* --- allocate raster struct if desired --- */
if ( width>0 && height>0 && pixsz>0 ) /* caller wants raster */
{ if ( (rp=new_raster(width,height,pixsz)) /* allocate embedded raster */
!= NULL ) /* if allocate succeeded */
sp->image = rp; /* embed raster in subraster */
else /* or if allocate failed */
{ delete_subraster(sp); /* free non-unneeded subraster */
sp = NULL; } } /* signal error */
/* --- back to caller with new subraster or NULL --- */
end_of_job:
if ( msgfp!=NULL && msglevel>=9999 )
{ fprintf(msgfp,"new_subraster(%d,%d,%d)> returning (%s)\n",
width,height,pixsz,(sp==NULL?"null ptr":"success")); fflush(msgfp); }
return ( sp );
} /* --- end-of-function new_subraster() --- */
/* ==========================================================================
* Function: new_chardef ( )
* Purpose: Allocates and initializes a chardef struct,
* but _not_ the embedded raster struct.
* --------------------------------------------------------------------------
* Arguments: none
* --------------------------------------------------------------------------
* Returns: ( chardef * ) ptr to allocated and initialized
* chardef struct, or NULL for any error.
* --------------------------------------------------------------------------
* Notes:
* ======================================================================= */
/* --- entry point --- */
chardef *new_chardef ( )
{
/* -------------------------------------------------------------------------
Allocations and Declarations
-------------------------------------------------------------------------- */
chardef *cp = (chardef *)NULL; /* chardef ptr returned to caller */
/* -------------------------------------------------------------------------
allocate and initialize chardef struct
-------------------------------------------------------------------------- */
cp = (chardef *)malloc(sizeof(chardef)); /* malloc chardef struct */
if ( cp == (chardef *)NULL ) /* malloc failed */
goto end_of_job; /* return error to caller */
cp->charnum = cp->location = 0; /* init character description */
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 */
/* -------------------------------------------------------------------------
Back to caller with address of chardef struct, or NULL ptr for any error.
-------------------------------------------------------------------------- */
end_of_job:
return ( cp );
} /* --- end-of-function new_chardef() --- */
/* ==========================================================================
* Function: delete_raster ( rp )
* Purpose: Destructor for raster.
* Frees memory for raster bitmap and struct.
* --------------------------------------------------------------------------
* Arguments: rp (I) ptr to raster struct to be deleted.
* --------------------------------------------------------------------------
* Returns: ( int ) 1 if completed successfully,
* or 0 otherwise (for any error).
* --------------------------------------------------------------------------
* Notes:
* ======================================================================= */
/* --- entry point --- */
int delete_raster ( raster *rp )
{
/* -------------------------------------------------------------------------
free raster bitmap and struct
-------------------------------------------------------------------------- */
if ( rp != (raster *)NULL ) /* can't free null ptr */
{
if ( rp->pixmap != (pixbyte *)NULL ) /* can't free null ptr */
free((void *)rp->pixmap); /* free pixmap within raster */
free((void *)rp); /* lastly, free raster struct */
} /* --- end-of-if(rp!=NULL) --- */
return ( 1 ); /* back to caller, 1=okay 0=failed */
} /* --- end-of-function delete_raster() --- */
/* ==========================================================================
* Function: delete_subraster ( sp )
* Purpose: Deallocates a subraster (and embedded raster)
* --------------------------------------------------------------------------
* Arguments: sp (I) ptr to subraster struct to be deleted.
* --------------------------------------------------------------------------
* Returns: ( int ) 1 if completed successfully,
* or 0 otherwise (for any error).
* --------------------------------------------------------------------------
* Notes:
* ======================================================================= */
/* --- entry point --- */
int delete_subraster ( subraster *sp )
{
/* -------------------------------------------------------------------------
free subraster struct
-------------------------------------------------------------------------- */
int delete_raster(); /* to delete embedded raster */
if ( sp != (subraster *)NULL ) /* can't free null ptr */
{
if ( sp->type != CHARASTER ) /* not static character data */
if ( sp->image != NULL ) /*raster allocated within subraster*/
delete_raster(sp->image); /* so free embedded raster */
free((void *)sp); /* and free subraster struct itself*/
} /* --- end-of-if(sp!=NULL) --- */
return ( 1 ); /* back to caller, 1=okay 0=failed */
} /* --- end-of-function delete_subraster() --- */
/* ==========================================================================
* Function: delete_chardef ( cp )
* Purpose: Deallocates a chardef (and bitmap of embedded raster)
* --------------------------------------------------------------------------
* Arguments: cp (I) ptr to chardef struct to be deleted.
* --------------------------------------------------------------------------
* Returns: ( int ) 1 if completed successfully,
* or 0 otherwise (for any error).
* --------------------------------------------------------------------------
* Notes:
* ======================================================================= */
/* --- entry point --- */
int delete_chardef ( chardef *cp )
{
/* -------------------------------------------------------------------------
free chardef struct
-------------------------------------------------------------------------- */
if ( cp != (chardef *)NULL ) /* can't free null ptr */
{
if ( cp->image.pixmap != NULL ) /* pixmap allocated within raster */
free((void *)cp->image.pixmap); /* so free embedded pixmap */
free((void *)cp); /* and free chardef struct itself */
} /* --- end-of-if(cp!=NULL) --- */
/* -------------------------------------------------------------------------
Back to caller with 1=okay, 0=failed.
-------------------------------------------------------------------------- */
return ( 1 );
} /* --- end-of-function delete_chardef() --- */
/* ==========================================================================
* Function: rastcpy ( rp )
* Purpose: makes duplicate copy of rp
* --------------------------------------------------------------------------
* Arguments: rp (I) ptr to raster struct to be copied
* --------------------------------------------------------------------------
* Returns: ( raster * ) ptr to new copy rp,
* or NULL for any error.
* --------------------------------------------------------------------------
* Notes: o
* ======================================================================= */
/* --- entry point --- */
raster *rastcpy ( raster *rp )
{
/* -------------------------------------------------------------------------
Allocations and Declarations
-------------------------------------------------------------------------- */
raster *new_raster(), *newrp=NULL; /*copied raster returned to caller*/
int height= (rp==NULL?0:rp->height), /* original and copied height */
width = (rp==NULL?0:rp->width), /* original and copied width */
pixsz = (rp==NULL?0:rp->pixsz), /* #bits per pixel */
nbytes= (rp==NULL?0:(pixmapsz(rp))); /* #bytes in rp's pixmap */
/* -------------------------------------------------------------------------
allocate rotated raster and fill it
-------------------------------------------------------------------------- */
/* --- allocate copied raster with same width,height, and copy bitmap --- */
if ( rp != NULL ) /* nothing to copy if ptr null */
if ( (newrp = new_raster(width,height,pixsz)) /*same width,height in copy*/
!= NULL ) /* check that allocate succeeded */
memcpy(newrp->pixmap,rp->pixmap,nbytes); /* fill copied raster pixmap */
return ( newrp ); /* return copied raster to caller */
} /* --- end-of-function rastcpy() --- */
/* ==========================================================================
* Function: subrastcpy ( sp )
* Purpose: makes duplicate copy of sp
* --------------------------------------------------------------------------
* Arguments: sp (I) ptr to subraster struct to be copied
* --------------------------------------------------------------------------
* Returns: ( subraster * ) ptr to new copy sp,
* or NULL for any error.
* --------------------------------------------------------------------------
* Notes: o
* ======================================================================= */
/* --- entry point --- */
subraster *subrastcpy ( subraster *sp )
{
/* -------------------------------------------------------------------------
Allocations and Declarations
-------------------------------------------------------------------------- */
subraster *new_subraster(), *newsp=NULL; /* allocate new subraster */
raster *rastcpy(), *newrp=NULL; /* and new raster image within it */
int delete_subraster(); /* dealloc newsp if rastcpy() fails*/
/* -------------------------------------------------------------------------
make copy, and return it to caller
-------------------------------------------------------------------------- */
if ( sp == NULL ) goto end_of_job; /* nothing to copy */
/* --- allocate new subraster "envelope" for copy --- */
if ( (newsp=new_subraster(0,0,0)) /* allocate subraster "envelope" */
== NULL ) goto end_of_job; /* and quit if we fail to allocate */
/* --- transparently copy original envelope to new one --- */
memcpy((void *)newsp,(void *)sp,sizeof(subraster)); /* copy envelope */
/* --- make a copy of the rasterized image itself, if there is one --- */
if ( sp->image != NULL ) /* there's an image embedded in sp */
if ( (newrp = rastcpy(sp->image)) /* so copy rasterized image in sp */
== NULL ) /* failed to copy successfully */
{ delete_subraster(newsp); /* won't need newsp any more */
newsp = NULL; /* because we're returning error */
goto end_of_job; } /* back to caller with error signal*/
/* --- set new params in new envelope --- */
newsp->image = newrp; /* new raster image we just copied */
switch ( sp->type ) /* set new raster image type */
{ case STRINGRASTER: case CHARASTER: newsp->type = STRINGRASTER; break;
case ASCIISTRING: newsp->type = ASCIISTRING; break;
case IMAGERASTER: default: newsp->type = IMAGERASTER; break; }
/* --- return copy of sp to caller --- */
end_of_job:
return ( newsp ); /* copy back to caller */
} /* --- end-of-function subrastcpy() --- */
/* ==========================================================================
* Function: rastrot ( rp )
* Purpose: rotates rp image 90 degrees right/clockwise
* --------------------------------------------------------------------------
* Arguments: rp (I) ptr to raster struct to be rotated
* --------------------------------------------------------------------------
* Returns: ( raster * ) ptr to new raster rotated ralative to rp,
* or NULL for any error.
* --------------------------------------------------------------------------
* Notes: o An underbrace is } rotated 90 degrees clockwise,
* a hat is <, etc.
* ======================================================================= */
/* --- entry point --- */
raster *rastrot ( raster *rp )
{
/* -------------------------------------------------------------------------
Allocations and Declarations
-------------------------------------------------------------------------- */
raster *new_raster(), *rotated=NULL; /*rotated raster returned to caller*/
int height = rp->height, irow, /* original height, row index */
width = rp->width, icol, /* original width, column index */
pixsz = rp->pixsz; /* #bits per pixel */
/* -------------------------------------------------------------------------
allocate rotated raster and fill it
-------------------------------------------------------------------------- */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -