Back to index

tetex-bin  3.0
t1delete.c
Go to the documentation of this file.
00001 /*--------------------------------------------------------------------------
00002   ----- File:        t1delete.c 
00003   ----- Author:      Rainer Menzner (Rainer.Menzner@web.de)
00004   ----- Date:        2002-12-02
00005   ----- Description: This file is part of the t1-library. It contains
00006                      functions for giving free previously allocated
00007                    memory areas and similar things.
00008   ----- Copyright:   t1lib is copyrighted (c) Rainer Menzner, 1996-2002. 
00009                      As of version 0.5, t1lib is distributed under the
00010                    GNU General Public Library Lincense. The
00011                    conditions can be found in the files LICENSE and
00012                    LGPL, which should reside in the toplevel
00013                    directory of the distribution.  Please note that 
00014                    there are parts of t1lib that are subject to
00015                    other licenses:
00016                    The parseAFM-package is copyrighted by Adobe Systems
00017                    Inc.
00018                    The type1 rasterizer is copyrighted by IBM and the
00019                    X11-consortium.
00020   ----- Warranties:  Of course, there's NO WARRANTY OF ANY KIND :-)
00021   ----- Credits:     I want to thank IBM and the X11-consortium for making
00022                      their rasterizer freely available.
00023                    Also thanks to Piet Tutelaers for his ps2pk, from
00024                    which I took the rasterizer sources in a format
00025                    independent from X11.
00026                      Thanks to all people who make free software living!
00027 --------------------------------------------------------------------------*/
00028   
00029 #define T1DELETE_C
00030 
00031 
00032 #include <stdio.h>
00033 #include <sys/types.h>
00034 #include <sys/stat.h>
00035 #include <fcntl.h>
00036 #if defined(_MSC_VER)
00037 # include <io.h>
00038 # include <sys/types.h>
00039 # include <sys/stat.h>
00040 #else
00041 # include <unistd.h>
00042 #endif
00043 #include <stdlib.h>
00044 #include <math.h>
00045 
00046 
00047 #include "../type1/types.h"
00048 #include "parseAFM.h" 
00049 #include "../type1/objects.h" 
00050 #include "../type1/spaces.h"  
00051 #include "../type1/util.h" 
00052 #include "../type1/fontfcn.h"
00053 
00054 #include "t1types.h"
00055 #include "t1extern.h"
00056 #include "t1delete.h"
00057 #include "t1load.h"
00058 #include "t1finfo.h"
00059 #include "t1base.h"
00060 
00061 
00062 
00063 /* T1_DeleteSize(): Gives back all the memory allocated for size to the
00064    system. If size is somewhere in the middle of a linked list of sizes,
00065    it further takes care that the remaining list is linked in a proper
00066    way. Function returns 0 if successful and otherwise -1*/
00067 int T1_DeleteSize( int FontID, float size)
00068 {
00069   int i, j;
00070   FONTSIZEDEPS *ptr, *next_ptr, *prev_ptr;
00071   int jobs=0;
00072   int antialias;
00073   int level[4]={0,T1_AA_NONE,T1_AA_LOW,T1_AA_HIGH};
00074   
00075 
00076   for ( j=0; j<4; j++){
00077     antialias=level[j];
00078     /* Check if size exists; if not, return 1 */
00079     if ((ptr=T1int_QueryFontSize( FontID, size, antialias))!=NULL){
00080       /* We have to remove a size-> */
00081       jobs++;
00082       /* Get pointers to structure which is before/after  the structure
00083         to be deleted        in the linked list and properly relink
00084         structures */
00085       next_ptr=((FONTSIZEDEPS *)ptr)->pNextFontSizeDeps;
00086       prev_ptr=((FONTSIZEDEPS *)ptr)->pPrevFontSizeDeps;
00087 
00088       if ((prev_ptr==NULL)&&(next_ptr==NULL)){
00089        /* There's only one single size, no relink is necessary
00090           => reset the initial pointer to indicate that no size
00091           dependent data is available */
00092        pFontBase->pFontArray[FontID].pFontSizeDeps=NULL;
00093       }
00094       else{
00095        if (prev_ptr!=NULL)
00096          /* We are at the first size of the linked list and
00097             there are still some sizes left after removing the
00098             current */
00099          prev_ptr->pNextFontSizeDeps=next_ptr;
00100        else
00101          pFontBase->pFontArray[FontID].pFontSizeDeps=next_ptr;
00102        if (next_ptr!=NULL)
00103          /* We are at the end of an list of at least two sizes: */
00104          next_ptr->pPrevFontSizeDeps=prev_ptr;
00105       }
00106       
00107       /* Now, that the list is properly linked, free the memory used by size: */
00108       /* Free the bitmaps memory: */
00109       for (i=0; i<256; i++)
00110        if (ptr->pFontCache[i].bits)
00111          free(ptr->pFontCache[i].bits);
00112 
00113       /* Free memory for glyphs: */
00114       free(ptr->pFontCache);
00115       /* Free the structure itself: */
00116       free(ptr);
00117       /* Print log: */
00118       sprintf( err_warn_msg_buf, "Size %f deleted for FontID %d (antialias=%d)",
00119               size, FontID, antialias);
00120       T1_PrintLog( "T1_DeleteSize()", err_warn_msg_buf, T1LOG_STATISTIC);
00121     }
00122   }
00123 
00124   /* Return the appropriate value */
00125   if (jobs==0)
00126     return(-1);
00127   else  
00128     return(0);
00129   
00130 }
00131 
00132 
00133 /* T1_DeleteAllSizes(): Gives back all the memory allocated for all sizes
00134    to the system. Function returns the number of removed sizes or -1 if an
00135    error ocurred. */
00136 int T1_DeleteAllSizes( int FontID)
00137 {
00138   int sizecount;
00139   float currsize;
00140   
00141   FONTSIZEDEPS *ptr;
00142   
00143   if (T1_CheckForFontID(FontID)!=1)
00144     return(-1);
00145   
00146   /* Start deleting at the end of the linked list: */ 
00147   sizecount=0;
00148   if ((ptr=T1int_GetLastFontSize( FontID))==NULL){
00149     /* There has not been any size dependent data: */
00150     return(0);
00151   }
00152 
00153   while (((ptr=T1int_GetLastFontSize(FontID)) != NULL)){
00154     currsize=ptr->size;
00155     T1_DeleteSize( FontID, currsize);
00156     sizecount++;
00157     
00158   }
00159 
00160   return(sizecount);
00161 }
00162 
00163 
00164 /* T1_FreeGlyph(): Gives the memory used by a glyph back to the system. */
00165 int T1_FreeGlyph( GLYPH *glyph)
00166 {
00167   if (glyph!=NULL) {
00168     if (glyph->bits!=NULL) {
00169       free(glyph->bits);
00170     }
00171     free(glyph);
00172   }
00173   return(0);
00174 }
00175 
00176 
00177 /* T1_FreeCompCharData(): Return emory used by a composite character
00178    data information structure to the system */
00179 int T1_FreeCompCharData( T1_COMP_CHAR_INFO *cci)
00180 {
00181 
00182   if (cci!=NULL) {
00183     if (cci->pieces!=NULL) {
00184       free( cci->pieces);
00185     }
00186     free( cci);
00187   }
00188   return( 0);
00189 }
00190 
00191 
00192 /* T1_DeleteFont(): Gives all memory used by a font back to the system.
00193    If the font is successfully deinstalled 0 is returned. A positive value
00194    indicates an error. */
00195 int T1_DeleteFont( int FontID)
00196 {
00197 
00198   int result;
00199 
00200   
00201   if (T1_CheckForFontID(FontID)==-1){  /* Invalid ID */
00202     T1_errno=T1ERR_INVALID_FONTID;
00203     return(-1);   
00204   }
00205   
00206   if (T1_CheckForFontID(FontID)==0)   /* Font is not loaded */
00207     return(0);   
00208 
00209   /* Memory freeing must be done hierachical, start with size dependent
00210      data: */
00211   result=T1_DeleteAllSizes(FontID);
00212 
00213   /* Next we delete the AFM-mapping tables */
00214   if (pFontBase->pFontArray[FontID].pEncMap!=NULL)
00215     free( pFontBase->pFontArray[FontID].pEncMap);
00216   if (pFontBase->pFontArray[FontID].pKernMap!=NULL)
00217     free( pFontBase->pFontArray[FontID].pKernMap);
00218 
00219   /* We do not touch the file name because this is only to be done by
00220      T1_CloseLib(): */
00221   
00222   /* The Type-1 area and tyhe AFM-area may only be free'ed, if the
00223      font is a "physical" font and if its reference counter is 1, or
00224      if it is a logical font. Otherwise, other logical font use this
00225      physical one and it may not be free'ed.  In this case, return the
00226      number of logical fonts which refer to this physical font. */
00227   if ((pFontBase->pFontArray[FontID].physical==1)&&
00228       (pFontBase->pFontArray[FontID].refcount==1)){
00229     /* Now handle the type 1 data: */
00230     if (pFontBase->pFontArray[FontID].pType1Data!=NULL){
00231       /* First: VM, which includes CharStrings, Private, .... */
00232       free(pFontBase->pFontArray[FontID].vm_base); 
00233       /* .. then the struct itself: */
00234       free(pFontBase->pFontArray[FontID].pType1Data);
00235       pFontBase->pFontArray[FontID].pType1Data=NULL;
00236     }
00237     
00238     /* afm-data is yet there -> */
00239     if (pFontBase->pFontArray[FontID].pAFMData!=NULL){
00240       result=FreeAFMData(pFontBase->pFontArray[FontID].pAFMData);
00241       pFontBase->pFontArray[FontID].pAFMData=NULL;
00242     }
00243   }
00244   else{
00245     if (pFontBase->pFontArray[FontID].physical==1){
00246       /* font is physical and is referred to by other fonts ->
00247         Do nothing further and return number of references: */
00248 
00249       return(pFontBase->pFontArray[FontID].refcount - 1);
00250     }
00251   }
00252   
00253   /* If we get here and the font is logical, we have to
00254      decrement the refcount of the referred physical font */
00255   if (pFontBase->pFontArray[FontID].physical==0){
00256     pFontBase->pFontArray[pFontBase->pFontArray[FontID].refcount].refcount--;
00257   }
00258   
00259   
00260   /* Set remaining area explicitly to 0 (all but pFontFileName and
00261      pAfmFileName!) */
00262   pFontBase->pFontArray[FontID].pAFMData=NULL;
00263   pFontBase->pFontArray[FontID].pType1Data=NULL;
00264   pFontBase->pFontArray[FontID].pEncMap=NULL;
00265   pFontBase->pFontArray[FontID].pKernMap=NULL;
00266   pFontBase->pFontArray[FontID].pFontEnc=NULL;
00267   pFontBase->pFontArray[FontID].pFontSizeDeps=NULL;
00268   pFontBase->pFontArray[FontID].vm_base=NULL;
00269   pFontBase->pFontArray[FontID].FontMatrix[0]=0.0;
00270   pFontBase->pFontArray[FontID].FontMatrix[1]=0.0;
00271   pFontBase->pFontArray[FontID].FontMatrix[2]=0.0;
00272   pFontBase->pFontArray[FontID].FontMatrix[3]=0.0;
00273   pFontBase->pFontArray[FontID].FontTransform[0]=0.0;
00274   pFontBase->pFontArray[FontID].FontTransform[1]=0.0;
00275   pFontBase->pFontArray[FontID].FontTransform[2]=0.0;
00276   pFontBase->pFontArray[FontID].FontTransform[3]=0.0;
00277   pFontBase->pFontArray[FontID].slant=0.0;
00278   pFontBase->pFontArray[FontID].extend=0.0;
00279   pFontBase->pFontArray[FontID].UndrLnPos=0.0;
00280   pFontBase->pFontArray[FontID].UndrLnThick=0.0;
00281   pFontBase->pFontArray[FontID].OvrLnPos=0.0;
00282   pFontBase->pFontArray[FontID].OvrLnThick=0.0;
00283   pFontBase->pFontArray[FontID].OvrStrkPos=0.0;
00284   pFontBase->pFontArray[FontID].OvrStrkThick=0.0;
00285   pFontBase->pFontArray[FontID].physical=0;
00286   pFontBase->pFontArray[FontID].refcount=0; 
00287   pFontBase->pFontArray[FontID].space_position=0; 
00288   pFontBase->pFontArray[FontID].info_flags=0; 
00289 
00290   return(0);
00291   
00292 }
00293 
00294 
00295 
00296 /* FreeAFMData(): Give all memory used by afm-Information back to the
00297    system. */
00298 int FreeAFMData( FontInfo *pAFMData)
00299 {
00300   if (pAFMData != NULL){
00301     if (pAFMData->gfi != NULL){
00302       free(pAFMData->gfi->afmVersion); pAFMData->gfi->afmVersion = NULL;
00303       free(pAFMData->gfi->fontName); pAFMData->gfi->fontName = NULL;
00304       free(pAFMData->gfi->fullName); pAFMData->gfi->fullName = NULL;
00305       free(pAFMData->gfi->familyName); pAFMData->gfi->familyName = NULL;
00306       free(pAFMData->gfi->weight); pAFMData->gfi->weight = NULL;
00307       free(pAFMData->gfi->version); pAFMData->gfi->version = NULL;
00308       free(pAFMData->gfi->notice); pAFMData->gfi->notice = NULL;
00309       free(pAFMData->gfi->encodingScheme); pAFMData->gfi->encodingScheme = NULL;
00310       free(pAFMData->gfi); pAFMData->gfi = NULL;
00311     }
00312 
00313     if (pAFMData->cwi != NULL){
00314       free(pAFMData->cwi); pAFMData->cwi = NULL; 
00315     }
00316     
00317     if (pAFMData->cmi != NULL){ 
00318       int i = 0;
00319       CharMetricInfo *temp = pAFMData->cmi;
00320       Ligature *node = temp->ligs;
00321       for (i = 0; i < pAFMData->numOfChars; ++i){
00322        for (node = temp->ligs; node != NULL; node = node->next){
00323          free(node->succ); node->succ = NULL;
00324          free(node->lig); node->lig = NULL;
00325        }
00326        free(temp->name); temp->name = NULL;
00327        temp++;
00328       }
00329       free(pAFMData->cmi); pAFMData->cmi = NULL;
00330     }
00331 
00332     if (pAFMData->tkd != NULL){
00333       free(pAFMData->tkd); pAFMData->tkd = NULL;
00334     }
00335       
00336     if (pAFMData->pkd != NULL){
00337       int i = 0;
00338       for (i = 0; i < pAFMData->numOfPairs; ++i){
00339        free(pAFMData->pkd[i].name1); pAFMData->pkd[i].name1 = NULL;
00340        free(pAFMData->pkd[i].name2); pAFMData->pkd[i].name2 = NULL;
00341       }
00342       free(pAFMData->pkd); pAFMData->pkd = NULL;
00343     }
00344 
00345     if (pAFMData->ccd != NULL){
00346       int i = 0, j = 0;
00347       CompCharData *ccd = pAFMData->ccd;
00348       for (i = 0; i < pAFMData->numOfComps; ++i){
00349        for (j = 0; j < ccd[i].numOfPieces; ++j){
00350          free(ccd[i].pieces[j].pccName); 
00351          ccd[i].pieces[j].pccName = NULL;
00352        }
00353        free(ccd[i].ccName); ccd[i].ccName = NULL;
00354       }
00355       free(pAFMData->ccd); pAFMData->ccd = NULL;
00356     }
00357     free(pAFMData);
00358   }
00359   return(0);
00360   
00361 } 
00362