📄 t1subset.c
字号:
/* if .notdef is not in the encoding mask, we have to reserve room for the additional charstring .notdef. Note that still nocharstrings is an upper bound estimation, which is reached in cases where no characters are encoded more than one time. */ if (notdefencoded==0) nocharstrings++; i+=sprintf( &(filebuf[i]), "2 index /CharStrings %d dict dup begin\n", nocharstrings); break; } } /* the portion until Charstrings-dict is now read in */ /* We now have to write the CharStrings. Each charstring must be written once, even if the respective character appears more than once in the encoding. So we set up table to remember which charstrings already have been written. Note: The indices returned by locateCharString() range from 1 to n, so that we have to decrement the index when filling the csdone array! */ if (( csdone=(char *)calloc( pFontBase->pFontArray[FontID].pType1Data->CharStringsP[0].key.len, sizeof(char)))==NULL) { T1_errno=T1ERR_ALLOC_MEM; free( filebuf); T1Close( ifp); return( NULL); } /* The .notdef character is in force! */ if ((currstring_no=locateCharString( FontID, ".notdef"))==0) { T1_errno=T1ERR_UNSPECIFIED; free( csdone); T1Close( ifp); return( NULL); } i+=sprintf( &(filebuf[i]), "/.notdef %d %s ", charstringL, rdstring); memcpy(&(filebuf[i]), charstringP, charstringL); i+=charstringL; i+=sprintf( &(filebuf[i]), " %s\n", ndstring); csdone[currstring_no-1]=1; /* Now, step through the specifier matrix and write only the necessary charstrings. */ for ( j=0; j<256; j++) { if (mask[j]!=0) { charnameP=T1_GetCharName( FontID, j); if ((currstring_no=locateCharString( FontID, charnameP))==0) { /* This is mysterious, but causes no harm because .notdef will be substituted */ sprintf( err_warn_msg_buf, "Could not locate CS ""%s"" for index %d", charnameP, j); T1_PrintLog( "T1_SubsetFont()", err_warn_msg_buf, T1LOG_WARNING); continue; } /* Process charstring only if it has not already been done */ if (csdone[currstring_no-1]==0) { k=i; i+=sprintf( &(filebuf[i]), "/%s %d %s ", charnameP, charstringL, rdstring); memcpy(&(filebuf[i]), charstringP, charstringL); i+=charstringL; i+=sprintf( &(filebuf[i]), " %s\n", ndstring); csdone[currstring_no-1]=1; sprintf( err_warn_msg_buf, "Processing of CS ""%s"" for index %d successful (len=%d bytes, line=%d bytes)", charnameP, j, charstringL, i-k); T1_PrintLog( "T1_SubsetFont()", err_warn_msg_buf, T1LOG_DEBUG); } else { sprintf( err_warn_msg_buf, "Skipped multiple processing of CS ""%s"" (index %d)", charnameP, j); T1_PrintLog( "T1_SubsetFont()", err_warn_msg_buf, T1LOG_DEBUG); } } } if (csdone!=NULL) free( csdone); /* All charstrings are written. Some PostScript code follows */ i+=sprintf( &(filebuf[i]), "end\nend\nreadonly put\nnoaccess put\ndup /FontName get exch definefont pop\nmark currentfile closefile\n"); sprintf( err_warn_msg_buf, "Charstrings and Font definition finished (%d data bytes, from which %d bytes will be encrypted)", i, i-dindex); T1_PrintLog( "T1_SubsetFont()", err_warn_msg_buf, T1LOG_DEBUG); /* we compute the size of the encrypted area. */ if (encrypt==1) { encryptsize=i-dindex; encryptsize+=4; /* the four random bytes */ encryptsize*=2; /* since we use ASCII-hex output */ encryptsize+=(int)ceil((double)encryptsize/linewidth); /* we need a few newline characters to adjust the output format */ } else if (encrypt==2) { /* binary encryption. The whole data is contiguous and only at the end of the data a newline is added. */ encryptsize=i-dindex+1; encryptsize+=4; /* the four random bytes */ } else { /*The case of no encryption does not produce valid fonts, it is for debugging purposes only */ encryptsize=i-dindex+1; } /* we add the size of the 512 zeros and the cleartomark */ trailersize+=ZEROS; trailersize+=(int)ceil((double)ZEROS/linewidth); trailersize+=12; /* cleartomark */ /* Search for remaining PostScript code in the last 1024 bytes. This should suffice for any font. */ tr_len=T1GetTrailer( linebuf, 1025, ifp); T1Close(ifp); /* we do not need the file any longer */ /* And we add the size of the trailer. A trailer only consisting of a newline is ignored because the newline has already been included in the cleartomark-string. */ if ( !((tr_len==1) && ((linebuf[0]=='\n') || (linebuf[0]=='\r')) ) ) { trailersize+=tr_len; } if ((encryptbuf=(char *)calloc( encryptsize, 1))==NULL) { T1_errno=T1ERR_ALLOC_MEM; free( filebuf); return( NULL); } /* Allocate one byte in orer to be able to use sprintf() (which appends an ASCII-\0).*/ if ((trailerbuf=(char *)calloc( trailersize+1, 1))==NULL) { T1_errno=T1ERR_ALLOC_MEM; free( filebuf); free( encryptbuf); return( NULL); } k=0; colcount=0; /* perform eexec-encryption */ eexec_r=55665; if (encrypt==0) { for (j=dindex; j<i; j++) { encryptbuf[k++]=filebuf[j]; } } else if ( encrypt==1) { /* consume four random bytes. We take the numbers '0', '1', '2' and '3'. The first cipher then is 217-dec (0xD9-hex), i.e., it is no white space character. The resulting font can thus be converted to pfb-format without the risk violating the Adobe spec.*/ for (j=0; j<4; j++) { cipher=EexecEncrypt( (unsigned char) j); encryptbuf[k++]=highHexByte[cipher]; if (++colcount==linewidth) { colcount=0; encryptbuf[k++]='\n'; } encryptbuf[k++]=lowHexByte[cipher]; if (++colcount==linewidth) { colcount=0; encryptbuf[k++]='\n'; } } for (j=dindex; j<i; j++) { cipher=EexecEncrypt( (unsigned char) filebuf[j]); encryptbuf[k++]=highHexByte[cipher]; if (++colcount==linewidth) { colcount=0; encryptbuf[k++]='\n'; } encryptbuf[k++]=lowHexByte[cipher]; if (++colcount==linewidth) { colcount=0; encryptbuf[k++]='\n'; } } /* If necessary, append newline char */ if (encryptbuf[k-1]!='\n') { encryptbuf[k++]='\n'; } } /* if encrypt==1 */ else { /* four random bytes ... */ for (j=0; j<4; j++) { encryptbuf[k++]=EexecEncrypt( (unsigned char) j); } for (j=dindex; j<i; j++) { /* and encrypted charstrings */ encryptbuf[k++]=EexecEncrypt( (unsigned char) filebuf[j]); } } /* encrypt == 2 */ /* Encryption finished */ /* If necessary, append newline char */ if (encryptbuf[k-1]!='\n') { encryptbuf[k++]='\n'; } colcount=0; /* Append zeroes and cleartomark */ for (j=0; j<ZEROS; j++) { trailerbuf[l++]='0'; if (++colcount==linewidth) { colcount=0; trailerbuf[l++]='\n'; } } /* If necessary, append newline char */ if (trailerbuf[l-1]!='\n') { trailerbuf[l++]='\n'; } l+=sprintf( &(trailerbuf[l]), "cleartomark\n"); if ( !((tr_len==1) && ((linebuf[0]=='\n') || (linebuf[0]=='\r')) ) ) { sprintf( err_warn_msg_buf, "Including additional PostScript trailer (%d bytes)", tr_len); T1_PrintLog( "T1_SubsetFont()", err_warn_msg_buf, T1LOG_DEBUG); l+=sprintf( &(trailerbuf[l]), linebuf); /* contains the PostScript trailer */ } /* compute size of output file */ outsize=0; if (encrypt==2) { /* binary encryption, take care about pfb-blocks */ /* each segment requires 6 bytes for the header */ filesegs=(int)(ceil((double)dindex/maxblocksize)); encryptsegs=(int)(ceil((double)k/maxblocksize)); trailersegs=(int)(ceil((double)l/maxblocksize)); /* trainling ASCII-part: */ outsize +=dindex; /* binary encrypted data */ outsize +=k; /* leading ASCII-part: */ outsize +=l; /* segment headers */ outsize +=6*(filesegs+encryptsegs+trailersegs); outsize +=2; /* the EOF-marker segment */ } else { /* ASCII-hex encryption or no encryption: no segmentation */ outsize=dindex+k+l; } if ((outbuf=(char *)calloc( outsize, 1))==NULL) { T1_errno=T1ERR_ALLOC_MEM; free( filebuf); free( encryptbuf); free( trailerbuf); return( NULL); } /* The full bufsize is given as the unencrypted portion plus size of encryptbuf (which also inccorporates an unencrypted trailer). In addition, the segment headers have to be taken into account for pfb-format. */ i=0; if (encrypt==2) { /* we have to add segment headers */ T1_PrintLog( "T1_SubsetFont()", "Setting up segmented binary buffer (pfb-format)", T1LOG_DEBUG); /* Clear text header */ m=0; for ( j=0; j<filesegs; j++) { if (j==filesegs-1) n=dindex % maxblocksize; else n=maxblocksize; i +=make_pfb_segment_header( &(outbuf[i]), SEGMENT_ASCII, n); memcpy( &(outbuf[i]), &(filebuf[m]), n); i +=n; m +=n; } sprintf( err_warn_msg_buf, " Readable header (%d bytes: %d data bytes in %d ASCII-segments of maximum size %lu bytes)", i, dindex, filesegs, maxblocksize); T1_PrintLog( "T1_SubsetFont()", err_warn_msg_buf, T1LOG_DEBUG); /* Binary data */ m=0; o=i; for ( j=0; j<encryptsegs; j++) { if (j==encryptsegs-1) n=k % maxblocksize; else n=maxblocksize; i +=make_pfb_segment_header( &(outbuf[i]), SEGMENT_BINARY, n); memcpy( &(outbuf[i]), &(encryptbuf[m]), n); i +=n; m +=n; } sprintf( err_warn_msg_buf, " Binary data (%d bytes: %d data bytes in %d binary segments of maximum size %lu bytes)", i-o, k, encryptsegs, maxblocksize); T1_PrintLog( "T1_SubsetFont()", err_warn_msg_buf, T1LOG_DEBUG); /* Readable ASCII-trailer */ m=0; o=i; for ( j=0; j<trailersegs; j++) { if (j==trailersegs-1) n=l % maxblocksize; else n=maxblocksize; i +=make_pfb_segment_header( &(outbuf[i]), SEGMENT_ASCII, n); memcpy( &(outbuf[i]), &(trailerbuf[m]), n); i +=n; m +=n; } sprintf( err_warn_msg_buf, " Readable trailer (%d bytes: %d data bytes in %d ASCII-segments of maximum size %lu bytes)", i-o, l, trailersegs, maxblocksize); T1_PrintLog( "T1_SubsetFont()", err_warn_msg_buf, T1LOG_DEBUG); i +=make_pfb_segment_header( &(outbuf[i]), SEGMENT_EOF, 0); sprintf( err_warn_msg_buf, " EOF-segment marker (2 bytes)"); T1_PrintLog( "T1_SubsetFont()", err_warn_msg_buf, T1LOG_DEBUG); *bufsize=i; } /* end of pfb-setup */ else { /* we continuously copy the data */ if (encrypt==1) sprintf( err_warn_msg_buf, "Dumping output buffer (%d bytes: %d readable ASCII, %d ASCII-hex in lines of width %d, %d readable ASCII)", dindex+k+l, dindex, k, linewidth, l); else sprintf( err_warn_msg_buf, "Dumping output buffer (%d bytes: %d readable ASCII, %d non-encrypted binary, %d readable ASCII)", dindex+k+l, dindex, k, l); T1_PrintLog( "T1_SubsetFont()", err_warn_msg_buf, T1LOG_DEBUG); memcpy( outbuf, filebuf, dindex); memcpy( &(outbuf[dindex]), encryptbuf, k); memcpy( &(outbuf[dindex+k]), trailerbuf, l); *bufsize=dindex+k+l; } sprintf( err_warn_msg_buf, "Output buffer is %ld bytes, original file size is %lu bytes", *bufsize, filesize); T1_PrintLog( "T1_SubsetFont()", err_warn_msg_buf, T1LOG_DEBUG); if (encryptbuf!=NULL) free(encryptbuf); if (trailerbuf!=NULL) free(trailerbuf); if (filebuf!=NULL) free( filebuf); return( outbuf); }/* This function returns the encrypted charstring of character charname of font FontID. The length is saved at len */char *T1_GetCharString( int FontID, char *charname, int *len){ static char *charstring=NULL; if (CheckForFontID(FontID)!=1) { T1_errno=T1ERR_INVALID_FONTID; return(NULL); } if ( (charname==NULL) || (len==0) ) { T1_errno=T1ERR_INVALID_PARAMETER; return(NULL); } if ((locateCharString( FontID, charname))==0) { *len=0; T1_errno=T1ERR_UNSPECIFIED; return( NULL); } if (charstring!=NULL) { free( charstring); charstring=NULL; } if ((charstring=(char *)malloc( charstringL))==NULL) { *len=0; T1_errno=T1ERR_ALLOC_MEM; return( NULL); } memcpy( charstring, charstringP, charstringL); *len=charstringL; return( charstring); }/* For those who really need it, this function returns the value of lenIV from the Private dictionary. It specifies how many random leading bytes are used in Charstring encryption in the current font. */int T1_GetlenIV( int FontID){ if (CheckForFontID(FontID)!=1) { T1_errno=T1ERR_INVALID_FONTID; return( -2); } return( pFontBase->pFontArray[FontID].pType1Data->Private[LENIV].value.data.integer);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -