📄 t1enc.c
字号:
/*-------------------------------------------------------------------------- ----- File: t1enc.c ----- Author: Rainer Menzner (Rainer.Menzner@web.de) ----- Date: 2001-10-18 ----- Description: This file is part of the t1-library. It contains functions encoding handling at runtime. ----- 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 T1ENC_C#include <stdio.h>#include <stdlib.h>#include <string.h>#if defined(_MSC_VER)# include <io.h># include <sys/types.h># include <sys/stat.h>#else# include <unistd.h>#endif#include <sys/types.h>#include <sys/stat.h>#include <ctype.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 "t1types.h"#include "t1extern.h"#include "t1enc.h"#include "t1env.h"#include "t1base.h"#include "t1finfo.h"static char defaultencodingname[]="Unspecified";/* This struct is retunred by the tokenizer. It contains indices in linebuf for the first and the last character in a token */ typedef struct { int first; int last;} CNTOKEN;/* ScanForWord(): Tokenizer for ScanEncodingFile. - and return the first and last index in linebuf of the token - skips whitespace and comments - the vector marks [ and ] are considered token-delimiters and also treated as independent tokens - the literal escape char "/" is also considered as a token delimiter but is not returned as a token. This function leaves linebuf unmodified so that in case of a failure, TryT1LibEncoding() will receive a clean buffer! */static CNTOKEN *ScanForWord( char *lb, int size){ static int i=-1; int j; int comment; static CNTOKEN currtoken={-1,-1}; /* Reset tokenizer */ if (lb==NULL) { i=-1; currtoken.first=-1; currtoken.last=-1; return NULL; } comment=0; j=-1; while (++i<size) { /* Inside word */ if (j!=-1) { /* Whitespace, comment, mark or literal ends word */ if ( (lb[i]=='%') || (lb[i]=='[') || (lb[i]==']') || (lb[i]=='/') || isspace((int)lb[i]) ) { currtoken.last=i-1; if ( (lb[i]=='[') || (lb[i]==']') || (lb[i]=='/') ) { i--; } return &currtoken; } /* End not found, try next char */ continue; } /* first, check for special tokens */ if ( ( (lb[i]=='[') || (lb[i]==']')) ) { currtoken.first=i; currtoken.last=i; return &currtoken; } /* Inside coment */ if (comment) { if (lb[i]=='\n') comment=0; continue; } if (lb[i]=='%') { comment=1; continue; } /* **Whitespace */ if (isspace((int)lb[i])) continue; /* **Something else => word */ if (j==-1) { j=i; currtoken.first=j; continue; } } /* We're at the end of the buffer. Do we have a word? */ if (j!=-1) { currtoken.last=i-1; return &currtoken; } /* We have nothing */ return NULL;}/* tokcpy(): Copy a token from linebuf and append \0 */static char *tokcpy( char *dest, const char *src, int first, int last) { /* we do not do any range or error checking in this function */ memcpy( dest, &(src[first]), last-first+1); dest[last-first+1]='\0'; return dest;}/* TryDVIPSEncoding(): Try to read an encoding file conforming to the dvips specification. The file's contents is expected in a buffer "linebuf" of size "filesize". Function returns the actual size of the charnames memory or -1. */static int TryDVIPSEncoding( char *linebuf, int filesize, char *charnames) { char token[256]; char *encname; int charname_count=0; int k=0; CNTOKEN *currtokenP; /* Initialize tokenizer */ currtokenP=ScanForWord( NULL, filesize); currtokenP=ScanForWord(linebuf,filesize); if ( (currtokenP!=NULL) && (linebuf[currtokenP->first]=='/')) { /* / indicates start of postscript string literal, so this could be a postscript .enc file */ if ((encname=(char *)malloc( (currtokenP->last - currtokenP->first + 1 + 1) * sizeof( char)))==NULL) { T1_errno=T1ERR_ALLOC_MEM; return -1; } else { /* store encoding name */ if (currtokenP->first==currtokenP->last) { /* PostScript encoding requires an identifier so this does not seem to be a valid encoding file */ free( encname); return( -1); } tokcpy( encname, linebuf, currtokenP->first+1, currtokenP->last); } /* Next, the PostScript "mark" character is expected */ if ( ((currtokenP=ScanForWord(linebuf,filesize))!=NULL) && (currtokenP->first==currtokenP->last) && (linebuf[currtokenP->first]!='[') ) { /* Since we got up to here, there is a certain probability that we have a PostScript encoding definition, but with invalid syntax. So put log message. */ sprintf( err_warn_msg_buf, "Expected encoding definition after %s, but did not find \"[\"-character", encname); T1_PrintLog( "TryDVIPSEncoding()", err_warn_msg_buf, T1LOG_WARNING); if (encname!=NULL) free( encname); return( -1); } /* now, try to read 256 literal character names. We do not check for charname count because this would bypass error checking. */ while((currtokenP=ScanForWord(linebuf,filesize))!=NULL) { /* End of vector operator? */ if ( (currtokenP->first==currtokenP->last) && /* one character? */ (linebuf[currtokenP->first]==']')) { break; } /* We drop the escape character if it is present. However, non-literal name specifications are not allowed in encoding vectors. */ if (linebuf[currtokenP->first]!='/') { sprintf( err_warn_msg_buf, "Found non-literal name (c=%c (first=%d, last=%d)) at slot %d while scanning encoding vector %s.", linebuf[currtokenP->first], currtokenP->first, currtokenP->last, charname_count, encname); T1_PrintLog( "TryDVIPSEncoding()", err_warn_msg_buf, T1LOG_WARNING); if (encname!=NULL) free( encname); return( -1); } else { /* OK, first char in token is "/". Does there follow a name? */ if ( currtokenP->first==currtokenP->last) { sprintf( err_warn_msg_buf, "Empty literal name at slot %d while scanning encoding vector %s.", charname_count, encname); T1_PrintLog( "TryDVIPSEncoding()", err_warn_msg_buf, T1LOG_WARNING); if (encname!=NULL) free( encname); return( -1); } } /* We seem to have a valid name -> copy name to *charnames-array */ tokcpy( &(charnames[k]), linebuf, currtokenP->first+1, currtokenP->last); k+=currtokenP->last-currtokenP->first+1; /* note: we have omitted "/"! */ /* Increment character counter */ charname_count++; /* set index to start of next word/line */ } /* end of loop scanning character names */ if (currtokenP==NULL) { /* In this case loop has been stopped because buffer end has been reached. Since we did not alreay read the ]-character, this is an error condition. */ sprintf( err_warn_msg_buf, "Premature end of encoding definition encountered." ); T1_PrintLog( "TryDVIPSEncoding()", err_warn_msg_buf, T1LOG_WARNING); free(encname); return( -1); } /* Since the above loop has not been finished due to a NULL-ptr, the token ] must have been encountered. Thus, read ahead and look for def: */ if ((currtokenP=ScanForWord(linebuf,filesize))==NULL) { sprintf( err_warn_msg_buf, "Premature end of encoding definition encountered."); T1_PrintLog( "TryDVIPSEncoding()", err_warn_msg_buf, T1LOG_WARNING); free(encname); return( -1); } if (strcmp(tokcpy( &(charnames[k]), linebuf, currtokenP->first, currtokenP->last), "def")!=0) { /* we store the current token in charnames only temporarily, so we do not increment k! */ sprintf( err_warn_msg_buf, "Closing token \"def\" expected but found \"%s\".", &(charnames[k])); T1_PrintLog( "TryDVIPSEncoding()", err_warn_msg_buf, T1LOG_WARNING); free(encname); return( -1); } /* Encoding definition is complete. we do not allow any further tokens except comments. */ if ((currtokenP=ScanForWord(linebuf,filesize))!=NULL) { tokcpy( token, linebuf, currtokenP->first, currtokenP->last); sprintf( err_warn_msg_buf, "Token \"%s\" after closing \"def\" in successfully scanned encoding file makes encoding definition file invalid", token); T1_PrintLog( "TryDVIPSEncoding()", err_warn_msg_buf, T1LOG_WARNING); free(encname); return( -1); } /* we allow less than 256 character names. The missing ones are filled now with .notdef */ for ( ; charname_count<256; charname_count++) { tokcpy( &(charnames[k]), ".notdef", 0, 6); k+=8; } /* Append the string for the encoding's name */ strcpy( &(charnames[k]), encname); k +=strlen(encname)+1; free(encname); return( k); } /* file does not start with / -> no dvips-encoding file */ return( -1); }/* TryT1LibEncoding(): Try to read an encoding file conforming to the t1lib specification. The file's contents is expected in a buffer "linebuf" of size "filesize". Function returns the actual size of the charnames memory or -1. */static int TryT1LibEncoding( char *linebuf, int filesize, char *charnames) { int i=0, j=0, k=0, l=0; char save_char; int charname_count=0; while(i<filesize){ j=i; /* Save index of beginning of line */ while ( (i<filesize) && (isspace((int)linebuf[i])==0) ) i++; if (i==filesize) continue; /* this will leave this loop */ save_char=linebuf[i]; linebuf[i]=0; /* replace ' ' by ASCII-0 */ if (strncmp( "Encoding=", &linebuf[j], strlen("Encoding="))==0) { /* We save the current file position to read the encoding name later */ l=j+strlen("Encoding="); /* set index to start of next line */ if (save_char=='\n') i++; else { while ( (i<filesize) && (linebuf[i]!='\n') ) i++; if (i==filesize) continue; /* this will leave this loop */ i++; } /* keyword found => now, 256 lines should follow, each specifying a character name and optionally some comment to enhance readability: */ break; } i++; } while((i<filesize) && (charname_count<256)){ j=i; /* Save index of beginning of line */ while ( (i<filesize) && (isspace((int)linebuf[i])==0)) i++; if (i==filesize) continue; /* this will leave this loop */ save_char=linebuf[i]; linebuf[i]=0; /* replace whitespace by ASCII-0 */ /* Copy Name to *char_names-array */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -