📄 t1base.c
字号:
/*-------------------------------------------------------------------------- ----- File: t1base.c ----- Author: Rainer Menzner (Rainer.Menzner@web.de) ----- Date: 2001-10-03 ----- Description: This file is part of the t1-library. It contains basic routines to initialize the data structures used by the t1-library. ----- Copyright: t1lib is copyrighted (c) Rainer Menzner, 1996-2001. As of version 0.5, t1lib is distributed under the GNU General Public Library Lincense. The conditions can be found in the files LICENSE and LGPL, which should reside in the toplevel directory of the distribution. Please note that there are parts of t1lib that are subject to other licenses: The parseAFM-package is copyrighted by Adobe Systems Inc. The type1 rasterizer is copyrighted by IBM and the X11-consortium. ----- Warranties: Of course, there's NO WARRANTY OF ANY KIND :-) ----- Credits: I want to thank IBM and the X11-consortium for making their rasterizer freely available. Also thanks to Piet Tutelaers for his ps2pk, from which I took the rasterizer sources in a format independ from X11. Thanks to all people who make free software living!--------------------------------------------------------------------------*/#define T1BASE_C#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#if defined(_MSC_VER)# include <io.h># include <sys/types.h># include <sys/stat.h>#else# include <unistd.h>#endif#include <stdlib.h>#include <math.h>#include <time.h>#include <string.h>#include <ctype.h>#include <stdarg.h>#include "../type1/ffilest.h" #include "../type1/types.h"#include "parseAFM.h" #include "../type1/objects.h"#include "../type1/spaces.h"#include "../type1/util.h"#include "../type1/fontfcn.h"#include "../type1/regions.h"#include "sysconf.h"#include "t1base.h"#include "t1types.h"#include "t1global.h"#include "t1env.h"#include "t1delete.h"static int T1_pad=0;/* This function is to be called by the user to initialize the font mechanism */void *T1_InitLib( int log){ int result; int i; char *usershome=NULL; char *logfilepath=NULL; char *envlogreq=NULL; int usrforcelog=0; /* Reset T1_errno */ T1_errno=0; /* Assign pointer to global struct and set entry padding: */ pFontBase=&FontBase; if (T1_pad) pFontBase->bitmap_pad=T1_pad; else pFontBase->bitmap_pad=T1GLYPH_PAD; pFontBase->pFontArray = NULL; pFontBase->t1lib_flags=0; /* Check for AA-caching */ if ((log & T1_AA_CACHING)){ pFontBase->t1lib_flags |= T1_AA_CACHING; } /* Check for AFM disable */ if ((log & T1_NO_AFM)) { pFontBase->t1lib_flags |= T1_NO_AFM; } /* Check environment variable ENV_LOG_STRING. By this means, a user may generate a log file even if at compile time log file creation has been suppressed. Of course, if the loglevel is reduced after initialization by the programmer, this environment variable takes no effect! */ if ((envlogreq=getenv(ENV_LOG_STRING))!=NULL) { if (strcmp( envlogreq, "logDebug")==0) T1_SetLogLevel( T1LOG_DEBUG); else if (strcmp( envlogreq, "logStatistic")==0) T1_SetLogLevel( T1LOG_STATISTIC); else if (strcmp( envlogreq, "logWarning")==0) T1_SetLogLevel( T1LOG_WARNING); else if (strcmp( envlogreq, "logError")==0) T1_SetLogLevel( T1LOG_ERROR); usrforcelog=1; } /* Open log-file: */ t1lib_log_file=NULL; if ((log & LOGFILE) || (usrforcelog!=0)) { pFontBase->t1lib_flags |= LOGFILE; /* Try first opening in current directory: */ if ((t1lib_log_file=fopen( T1_LOG_FILE, "w"))==NULL) { if ((usershome=getenv("HOME"))!=NULL) { logfilepath=(char *)malloc((strlen(usershome) + strlen(T1_LOG_FILE) + 2 ) * sizeof(char)); strcpy( logfilepath, usershome); strcat( logfilepath, DIRECTORY_SEP); strcat( logfilepath, T1_LOG_FILE); if ((t1lib_log_file=fopen( logfilepath, "w"))==NULL){ t1lib_log_file=stderr; } free( logfilepath); } else { t1lib_log_file=stderr; } } if (t1lib_log_file==stderr) { T1_PrintLog( "T1_InitLib()", "Unable to open a logfile, using stderr", T1LOG_ERROR); } } T1_PrintLog( "T1_InitLib()", "Initialization started", T1LOG_STATISTIC); /* Check for representation of data in memory: */ if ((pFontBase->endian=T1_CheckEndian())){ T1_PrintLog( "T1_InitLib()", "Using Big Endian data presentation (MSBFirst)", T1LOG_DEBUG); pFontBase->endian=1; } else{ T1_PrintLog( "T1_InitLib()", "Using Little Endian data presentation (LSBFirst)", T1LOG_DEBUG); pFontBase->endian=0; } /* Save version identifier */ sprintf( err_warn_msg_buf, "Version Identifier: %s", T1LIB_IDENT); T1_PrintLog( "T1_InitLib()", err_warn_msg_buf, T1LOG_DEBUG); /* Save how t1lib is initialized */ sprintf( err_warn_msg_buf, "Initialization flags: 0x%X", log); T1_PrintLog( "T1_InitLib()", err_warn_msg_buf, T1LOG_DEBUG); /* Save padding value in log file */ sprintf( err_warn_msg_buf, "Glyphs are padded to %d bits", pFontBase->bitmap_pad); T1_PrintLog( "T1_InitLib()", err_warn_msg_buf, T1LOG_DEBUG);#ifdef __CHAR_UNSIGNED__ T1_PrintLog( "T1_InitLib()", "System-Info: char is unsigned", T1LOG_DEBUG);#else T1_PrintLog( "T1_InitLib()", "System-Info: char is signed", T1LOG_DEBUG);#endif sprintf( err_warn_msg_buf, "System-Info: sizeof(char): %d", SIZEOF_CHAR); T1_PrintLog( "T1_InitLib()", err_warn_msg_buf, T1LOG_DEBUG); sprintf( err_warn_msg_buf, "System-Info: sizeof(short): %d", SIZEOF_SHORT); T1_PrintLog( "T1_InitLib()", err_warn_msg_buf, T1LOG_DEBUG); sprintf( err_warn_msg_buf, "System-Info: sizeof(int): %d", SIZEOF_INT); T1_PrintLog( "T1_InitLib()", err_warn_msg_buf, T1LOG_DEBUG); sprintf( err_warn_msg_buf, "System-Info: sizeof(long): %d", SIZEOF_LONG); T1_PrintLog( "T1_InitLib()", err_warn_msg_buf, T1LOG_DEBUG); sprintf( err_warn_msg_buf, "System-Info: sizeof(long long): %d", SIZEOF_LONG_LONG); T1_PrintLog( "T1_InitLib()", err_warn_msg_buf, T1LOG_DEBUG); sprintf( err_warn_msg_buf, "System-Info: sizeof(float): %d", SIZEOF_FLOAT); T1_PrintLog( "T1_InitLib()", err_warn_msg_buf, T1LOG_DEBUG); sprintf( err_warn_msg_buf, "System-Info: sizeof(double): %d", SIZEOF_DOUBLE); T1_PrintLog( "T1_InitLib()", err_warn_msg_buf, T1LOG_DEBUG); sprintf( err_warn_msg_buf, "System-Info: sizeof(long double): %d", SIZEOF_LONG_DOUBLE); T1_PrintLog( "T1_InitLib()", err_warn_msg_buf, T1LOG_DEBUG); sprintf( err_warn_msg_buf, "System-Info: sizeof(void *): %d", SIZEOF_VOID_P); T1_PrintLog( "T1_InitLib()", err_warn_msg_buf, T1LOG_DEBUG); intT1_SetupDefaultSearchPaths(); if (log & IGNORE_CONFIGFILE) { pFontBase->t1lib_flags |= IGNORE_CONFIGFILE; T1_PrintLog( "T1_InitLib()", "Skipping configuration file search!", T1LOG_STATISTIC); } else { if ((result=intT1_ScanConfigFile())==0) T1_PrintLog( "T1_InitLib()", "Warning t1lib configuration file not found!", T1LOG_WARNING); } /* Set the default encoding to the fonts' internal encoding */ pFontBase->default_enc=NULL; /* Initialize the no_fonts... values */ pFontBase->no_fonts=0; pFontBase->no_fonts_ini=pFontBase->no_fonts; pFontBase->no_fonts_limit=pFontBase->no_fonts; /* Check whether to read font database */ if ((log & IGNORE_FONTDATABASE)){ pFontBase->t1lib_flags |= IGNORE_FONTDATABASE; T1_Up=1; /* System has been initialized ! */ T1_PrintLog( "T1_InitLib()", "Initialization successfully finished (Database empty)", T1LOG_STATISTIC); return((void *) pFontBase); } result=0; /* Read fontdatabase(s) */ i=0; while (T1_FDB_ptr[i]!=NULL) { if ((result=intT1_scanFontDBase(T1_FDB_ptr[i]))==-1){ T1_PrintLog( "T1_InitLib()", "Fatal error scanning Font Database File %s", T1LOG_WARNING, T1_FDB_ptr[i]); } if (result>-1) pFontBase->no_fonts+=result; i++; } if (result == 0){ T1_PrintLog( "T1_InitLib()", "No fonts from Font Database File(s) found (T1_errno=%d)", T1LOG_ERROR, T1_errno); return(NULL); } /* Initialize the no_fonts... values */ pFontBase->no_fonts_ini=pFontBase->no_fonts; pFontBase->no_fonts_limit=pFontBase->no_fonts; T1_Up=1; /* System has been initialized ! */ T1_PrintLog( "T1_InitLib()", "Initialization successfully finished", T1LOG_STATISTIC); return((void *) pFontBase);}/* intT1_scanFontDBase(): - opens the file with the font definitions, - reads the number of fonts defined and saves this in FontBase, - allocates memory for all the filenames of the Type1 files - tests for .pfa und .pfb files and saves the name found - initializes an array that allows to acces these names by an index number, the font_ID - returns -1 on fatal error and the number of fonts located successfullly */int intT1_scanFontDBase( char *filename){ int fd; int filesize, i, j, k, m; int found=0, located=0; char *filebuffer; int nofonts=0; FONTPRIVATE* fontarrayP=NULL; if ((fd=open( filename, O_RDONLY))<3){ T1_PrintLog( "intT1_scanFontDBase()", "Font Database File %s not found!", T1LOG_WARNING, filename); T1_errno=T1ERR_FILE_OPEN_ERR; return(-1); } /* Get the file size */ filesize=lseek( fd, 0, 2); /* Reset fileposition to start */ lseek (fd, 0, 0); if ((filebuffer=(char *)malloc(filesize*sizeof(char) )) == NULL){ T1_PrintLog( "intT1_scanFontDBase()", "Couldn't allocate memory for loading font database file %s", T1LOG_ERROR, filename); T1_errno=T1ERR_ALLOC_MEM; return(-1); } i=read( fd, filebuffer, filesize); close(fd); /* Close font database file */ i=j=m=0; while (i<filesize) { if (filebuffer[i]=='\n'){ /* We are at the end of line */ if (j==0) { /* Read the first line as the number of fonts */ filebuffer[i]=0; sscanf( &filebuffer[0], "%d", &nofonts); filebuffer[i]='\n'; /* Because it gives a better feeling */ /* (Re)Allocate memory for 'no_fonts' structures: */ if ((FontBase.pFontArray=(FONTPRIVATE *) realloc( FontBase.pFontArray, (FontBase.no_fonts+nofonts)*sizeof(FONTPRIVATE))) == NULL) { T1_PrintLog( "inT1_scanFontDBase()", "Failed to allocate memory for FONTPRIVATE-area while scanning %s", T1LOG_ERROR, filename); T1_errno=T1ERR_ALLOC_MEM; return(-1); } /* setup pointer to newly allocated area and do a reset */ fontarrayP=&(FontBase.pFontArray[FontBase.no_fonts]); memset(fontarrayP, 0, nofonts*sizeof(FONTPRIVATE)); located=1; /* In order to increment m */ } else { /* We are in the second or higher line */ k=i; while (isspace((int)filebuffer[k])){ k--; } /* We are at the last printable character of a line; let's step back (over the [afm/sfm]) to the period and replace it by an ASCII 0 */ while ((filebuffer[k]!='.') && (!isspace((int)filebuffer[k])) ){ k--; } if (filebuffer[k]=='.'){ /* We have a name terminated with . */ filebuffer[k]=0; /* termination for string reading */ while (!isspace((int)filebuffer[k])){ k--; } } else { /* The filename was without . and / or the first on the line */ ; } sscanf( &(filebuffer[k+1]), "%s", &(linebuf[0])); /* We print error string before testing because after the call to test_for_t1_file() filename is substituted by an emty string if the file was not found: */ sprintf( err_warn_msg_buf, "Type 1 Font file %s.[pfa/pfb] not found (FontID=%d, SearchPath=%s)", linebuf, m-1, T1_GetFileSearchPath(T1_PFAB_PATH)); if ((test_for_t1_file( &linebuf[0]))){ T1_PrintLog( "intT1_scanFontDBase()", err_warn_msg_buf, T1LOG_WARNING); located=0; } else{ /* linebuf contains now the valid Type1 filename; let's now copy this string into the appropriate place in the FONTPRIVATE-struct: */ found++; located=1; if ((fontarrayP[m-1].pFontFileName=(char *) calloc( strlen( &linebuf[0])+1, sizeof(char))) == NULL){ T1_PrintLog( "intT1_scanFontDBase()", "Failed to allocate memory for Filename %s (FontID=%d)", T1LOG_ERROR, &linebuf[0], m-1); T1_errno=T1ERR_ALLOC_MEM; return(-1); } strcpy( fontarrayP[m-1].pFontFileName, &linebuf[0]); } } j++; /* Advance line counter */ if ((located)) m++; } if (j>nofonts) /* to ignore especially white space at end */ break; i++; /* Step further in file position */ } /* Return the memory for file reading */ free(filebuffer); return( found);}/* T1_CloseLib(): Close the library and free all associated memory */int T1_CloseLib( void){ int i, j, error=0; if (T1_Up){ for (i=pFontBase->no_fonts; i; i--){ /* Free filename only if not NULL and if the font is physical! Do it before removing the font since the physical information is no more available afterwards. If necessary, an explicitly specified AFM filename is also freed. */ if ((pFontBase->pFontArray[i-1].pFontFileName!=NULL) && (pFontBase->pFontArray[i-1].physical==1)){ free( pFontBase->pFontArray[i-1].pFontFileName); pFontBase->pFontArray[i-1].pFontFileName=NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -