⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 t1enc.c

📁 source code: Covert TXT to PDF
💻 C
📖 第 1 页 / 共 2 页
字号:
    while (linebuf[j])      charnames[k++]=linebuf[j++];    /* Append ASCII-0 */    charnames[k++]=linebuf[j++];    /* 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++;    }    /* Increment character counter */    charname_count++;  }  /* Check if exactly 256 characters have been defined, if not,     return NULL: */  if (charname_count!=256){    T1_errno=T1ERR_UNSPECIFIED;    return( -1);  }  /* Append the string for the encoding's name */  i=l;  while (isspace((int)linebuf[i])==0 && linebuf[i]!='\0'){    charnames[k++]=linebuf[i++];  }    if (i==l){    strcpy(&(charnames[k]), defaultencodingname);    k +=strlen(defaultencodingname+1);    charnames[k++]='\0';  }  else{    charnames[k++]='\0';  }  return( k);  }/* ScanEncodingFile(): Read an encoding file of an appropriate format   and prepare the read data for usage with the type1 rasterizer, i.e.   generate an array char *enc[257]. Return the pointer to the data area   or NULL in case of an error.   */static char **ScanEncodingFile( char *FileName) {    char *linebuf;  char **encoding;        /* charnames array */  char *charnames=NULL;   /* charnames memory */    int cnsize;             /* size of charnames memory, this will be			     set from the Try...() function */    FILE *enc_fp;   int filesize;  int i=0, j=0;    if ((enc_fp=fopen( FileName,"r"))==NULL){    T1_errno=T1ERR_FILE_OPEN_ERR;    return(NULL);  /* file could not be opened 		      => no encoding read */  }      /* enc_fp points now to a (hopefully) valid encoding file */  /* Get the file size */  fseek( enc_fp, 0, SEEK_END);  filesize=ftell(enc_fp);  /* Reset fileposition to start */  fseek( enc_fp, 0, SEEK_SET);    if ((linebuf=(char *)calloc( filesize,			       sizeof(char)))==NULL){    T1_errno=T1ERR_ALLOC_MEM;    return(NULL);  }    /* Allocate space for character names, assume the worst case and realloc     later. The DVIPS-parser requires one more char in order to work properly */  if ((charnames=(char *)calloc( filesize + strlen(defaultencodingname+1),				 sizeof(char)))==NULL){    free( linebuf);    T1_errno=T1ERR_ALLOC_MEM;    return(NULL);  }    fread((char *)linebuf, sizeof(char), filesize, enc_fp);  fclose(enc_fp);  /* file is read. Operate now on the buffer. */    /* try dvips encoding file */  cnsize=TryDVIPSEncoding( linebuf, filesize, charnames);  if ( cnsize>-1) {    /* a debug message to log file */    sprintf( err_warn_msg_buf,	     "Scanned file %s (%d bytes) as dvips-encoding file.",	     FileName, filesize);    T1_PrintLog( "ScanEncodingFile()", err_warn_msg_buf, T1LOG_DEBUG);  }  else {    /* try t1lib encoding file */    cnsize=TryT1LibEncoding( linebuf, filesize, charnames);    if ( cnsize>-1) {      /* write a debug message to log file */      sprintf( err_warn_msg_buf,	       "Scanned file %s (%d bytes) as t1lib-encoding file.",	       FileName, filesize);      T1_PrintLog( "ScanEncodingFile()", err_warn_msg_buf, T1LOG_DEBUG);    }    else {      /* write a warning message because loading encoding	 entirely failed */       sprintf( err_warn_msg_buf,	       "Scanning file %s (%d bytes) as encoding file failed.",	       FileName, filesize);      T1_PrintLog( "ScanEncodingFile()", err_warn_msg_buf, T1LOG_WARNING);    }  }    if ( cnsize<0) {    /* T1_errno is already set from the respective function */    if ( charnames!=NULL) {      free(charnames);     }    free(linebuf);    return( NULL);  }      /* cnsize contains the size of the charnames' memory, so let's     now realloc charnames */  charnames=(char *)realloc( charnames, cnsize*sizeof(char));  /* Now initialize the array with the start-addresses of the character     name strings */  /* We alloc 257 to save the encoding's name at the 257th entry */  if ((encoding=(char **)malloc(257*sizeof(char *)))==NULL) {    if ( charnames!=NULL) {      free(charnames);    }    free(linebuf);    T1_errno=T1ERR_ALLOC_MEM;    return(NULL);  }    while (i<257) {    encoding[i]=&charnames[j];    while (charnames[j])      j++;    j++;    i++;  }    free( linebuf);  return(encoding);}/* T1_LoadEncoding(): Load an encoding file to have a new encoding   available. If successful, the pointer to the encoding array is   returned. In case of an error, the return value is NULL.   */char **T1_LoadEncoding( char *FileName){  char **Encoding;  char *EncFileName;    if( FileName==NULL){    T1_errno=T1ERR_INVALID_PARAMETER;    return(NULL);  }    if ((EncFileName=intT1_Env_GetCompletePath( FileName, T1_ENC_ptr))==NULL){    T1_errno=T1ERR_FILE_OPEN_ERR;    return(NULL);  }  Encoding=ScanEncodingFile(EncFileName);  free(EncFileName);    return(Encoding);}  /* T1_DeleteEncoding() free a previously loaded encoding */int T1_DeleteEncoding( char **encoding){  if (encoding){    /* First free character names memory */    free( encoding[0]);    /* then, free pointer array */    free( encoding);  }  return(0);  }/* T1_ReencodeFont(): Assign a new encoding to an existent font. This is   only allowed if no size dependent data exists for the font in question.   Moreover, the font must be loaded already since must get the position   of the space-character. Function returns 0 if successful, and -1 otherwise.   */int T1_ReencodeFont( int FontID, char **Encoding){  int i, j, k, l, m;  char *charname;  PairKernData *pkd;  METRICS_ENTRY *kern_tbl;  int char1, char2;      /* First, check for valid font ID residing in memory: */  if (CheckForFontID(FontID)!=1){    T1_errno=T1ERR_INVALID_FONTID;    return(-1);  }    /* Second, check whether size-dependent data exists: */  if (pFontBase->pFontArray[FontID].pFontSizeDeps != NULL){    T1_errno=T1ERR_OP_NOT_PERMITTED;    return(-1);  }    pFontBase->pFontArray[FontID].pFontEnc=Encoding;  /* We have to update the space_position-entry in the FONTPRIVATE.      If space is not found (not encoded), set it to -1: */  pFontBase->pFontArray[FontID].space_position=-1;  i=0;  if (Encoding){ /* external encoding */    while (i<256){      if (strcmp( (char *)pFontBase->pFontArray[FontID].pFontEnc[i],		  "space")==0){	/* space found at position i: */	pFontBase->pFontArray[FontID].space_position=i;	break;      }      i++;    }  }  else{ /* reencoding to internal encoding */    while (i<256){      if (strcmp( (char *)pFontBase->pFontArray[FontID].pType1Data->fontInfoP[ENCODING].value.data.arrayP[i].data.arrayP,		  "space")==0){	/* space found at position i: */	pFontBase->pFontArray[FontID].space_position=i;	break;      }      i++;    }  }  /* Now update afm index mapping: */  if (pFontBase->pFontArray[FontID].pAFMData != NULL){    for (i=0; i<256; i++) {      charname=T1_GetCharName( FontID, i);      /* in a first loop check for ordinary characters */      for ( j=0; j<pFontBase->pFontArray[FontID].pAFMData->numOfChars; j++) {	if (strcmp( charname,		    pFontBase->pFontArray[FontID].pAFMData->cmi[j].name)==0) {	  pFontBase->pFontArray[FontID].pEncMap[i]=j+1; /* index 0 is reserved! */	  continue;	}      }      /* if nothing has been found, check for composite characters */       for ( j=0; j<pFontBase->pFontArray[FontID].pAFMData->numOfComps; j++) {	if (strcmp( charname,		    pFontBase->pFontArray[FontID].pAFMData->ccd[j].ccName)==0) {	  pFontBase->pFontArray[FontID].pEncMap[i]=-(j+1); /* index 0 is reserved! */	  /* Note: Metrics of composite characters already exist so that there is	     no need to recalculate them! */	  continue;	}      }    }    /* Update kerning table */    k=pFontBase->pFontArray[FontID].pAFMData->numOfPairs;    if (k>0){ /* i.e., there are any pairs */      /* OK, it does not suffice to alloc numOfPairs METRICS_ENTRYs, because	 a given character might be encoded at several locations and kerning	 should still work. As a worst case estimation, we allocate 256^2	 and realloc later. */       if ((pFontBase->pFontArray[FontID].pKernMap=	   (METRICS_ENTRY *)malloc( (256*256) *sizeof( METRICS_ENTRY)))==NULL){	sprintf( err_warn_msg_buf, "Error allocating memory for metrics map (FontID=%d)",		 FontID);	T1_PrintLog( "T1_LoadFont()", err_warn_msg_buf,		     T1LOG_WARNING);	T1_errno=T1ERR_ALLOC_MEM;	return(-1);      }      kern_tbl=pFontBase->pFontArray[FontID].pKernMap;      pkd=pFontBase->pFontArray[FontID].pAFMData->pkd;      j=0;      for ( i=0; i<k; i++) {	/* We do not check T1_GetEncodingIndices() against the return value	   NULL because we just loading the font in question: */	l=0;	while ((char1=(T1_GetEncodingIndices( FontID, pkd[i].name1))[l++])!=-1) {	  /* pair could be relevant in current encoding */	  m=0;	  while ((char2=(T1_GetEncodingIndices( FontID, pkd[i].name2))[m++])!=-1) {	    /* Since we get here we have a relevant pair -->	       Put char1 in higher byte and char2 in LSB: */	    kern_tbl[j].chars=(char1 << 8) | char2;	    /* We only make use of horizontal kerning */	    kern_tbl[j].hkern=pkd[i].xamt;	    j++;	  } /* while (char2) */	} /* while (char1) */      } /* for */      /* We are done, realloc memory: */      kern_tbl=(METRICS_ENTRY*) realloc( kern_tbl, j*sizeof(METRICS_ENTRY));      /* We now sort the kerning array with respect to char indices */      qsort( kern_tbl, (size_t) j, sizeof(METRICS_ENTRY),	     &cmp_METRICS_ENTRY );      /* Finally write back pointer for the case that realloc changed the	 pointer */      pFontBase->pFontArray[FontID].pKernMap=kern_tbl;      pFontBase->pFontArray[FontID].KernMapSize=j;    }    else {      pFontBase->pFontArray[FontID].pKernMap=NULL;    }  }  return(0);}/* T1_SetDefaultEncoding(): Set the default encoding vector that's   used when fonts are loaded.   */int T1_SetDefaultEncoding( char **encoding){    if (CheckForInit()){    T1_errno=T1ERR_OP_NOT_PERMITTED;    return(-1);  }    pFontBase->default_enc=encoding;  return(0);}/* T1_GetEncodingScheme(): Get the name associated with the current   encoding vector of font FontID */char *T1_GetEncodingScheme( int FontID){    static char enc_scheme[256];    /* First, check for valid font ID residing in memory: */  if (CheckForFontID(FontID)!=1){    T1_errno=T1ERR_INVALID_FONTID;    return(NULL);  }  if (pFontBase->pFontArray[FontID].pFontEnc==NULL){    if (pFontBase->pFontArray[FontID].info_flags & USES_STANDARD_ENCODING){      strcpy( enc_scheme, "StandardEncoding");    }    else {      strcpy( enc_scheme, "FontSpecific");    }  }  else    strcpy( enc_scheme, pFontBase->pFontArray[FontID].pFontEnc[256]);    return(enc_scheme);  }/* A function for comparing METRICS_ENTRY structs */static int cmp_METRICS_ENTRY( const void *entry1, const void *entry2){  if (((METRICS_ENTRY *)entry1)->chars <      ((METRICS_ENTRY *)entry2)->chars)    return(-1);  if (((METRICS_ENTRY *)entry1)->chars >      ((METRICS_ENTRY *)entry2)->chars)    return(1);  return(0); /* This should not happen */}    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -