Back to index

tetex-bin  3.0
Classes | Defines | Typedefs | Functions | Variables
encoding.c File Reference
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "basics.h"

Go to the source code of this file.

Classes

struct  Node

Defines

#define LINEBUF   1024
#define POOLSIZE   10000

Typedefs

typedef char * encoding [256]

Functions

char * my_string (char *)
void remove_string ()
void addcc (char *name, int charcode)
int getcc (char *name)
void initcc ()
static char * value_after (char *s, char *t)
int decimal (char *s)
char nextsymbol ()
char * nextpsname ()
void getenc (char **fontname, char **encname, encoding ev, int width[256])

Variables

char * encfile
char * afmfile
FILEenc
char line [LINEBUF]
char * pline = NULL
struct Node node [256]
static int freenode = 0
static short root = -1
static char stringpool [POOLSIZE]
static int poolsize
static int lastpoolsize

Class Documentation

struct Node

Definition at line 250 of file encoding.c.

Class Members
short cc
short left
char * name
short right

Define Documentation

#define LINEBUF   1024

Definition at line 69 of file encoding.c.

#define POOLSIZE   10000

Definition at line 324 of file encoding.c.


Typedef Documentation

typedef char* encoding[256]

Definition at line 125 of file encoding.c.


Function Documentation

void addcc ( char *  name,
int  charcode 
)

Definition at line 259 of file encoding.c.

{  short r, p, q;

   q = root;
   while (q != -1) {
      p = q; r = strcmp(name, node[p].name);
      if (r == 0) {
        if (node[p].cc[1] == -1) {
           node[p].cc[1] = charcode;
           return;
        }
        else 
           fatal("%s: more than two codes for %s (%d,%d,%d)\n", 
              encfile, name, node[p].cc[0], node[p].cc[1], charcode);
      }
      else if (r < 0) q=node[p].left;
      else q=node[p].right;
   }
   if (freenode == 256) 
      fatal("%s: too many PostScript names\n", encfile);
      /* not found hence insert */
   node[freenode].name = name;
   node[freenode].cc[0] = charcode;
   node[freenode].cc[1] = -1;
   node[freenode].left = node[freenode].right = -1;
   if (root == -1)
      root = freenode;
   else {
      if (r < 0) node[p].left=freenode;
      else node[p].right=freenode;
   }
   freenode++;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int decimal ( char *  s)

Definition at line 53 of file encoding.c.

{  int sign = 1, d = 0;

   while (isspace(*s)) s++;
   if (*s == '-') { sign = -1; s++; }
   while (isdigit(*s)) { d = d * 10 + (*s - '0'); s++; }
   return sign*d;
}

Here is the caller graph for this function:

int getcc ( char *  name)

Definition at line 293 of file encoding.c.

{  short r, p, q, cc;

   q = root;
   while (q != -1) {
      p = q; r = strcmp(name, node[p].name);
      if (r == 0) {
        cc = node[p].cc[0];
        node[p].cc[0] = node[p].cc[1];
        node[p].cc[1] = -1;
        return cc; 
      }
      else if (r < 0) q = node[p].left;
      else q = node[p].right;
   }
   return -1;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void getenc ( char **  fontname,
char **  encname,
encoding  ev,
int  width[256] 
)

Definition at line 127 of file encoding.c.

{  int i, len, SCMseen, Ccnt, wx, cc;
   FILE *afm;
   char *name, ns;
   
   for (i=0; i < 256; i++) { ev[i] = NULL; width[i] = -1000; }
   if (encfile) {
      enc = fopen(encfile, "r");
      if (enc == NULL) fatal("Can't open %s\n", encfile);
      if (nextsymbol() != '/') fatal("missing '/encoding' in %s\n", encfile);
      *encname = nextpsname();
      if (nextsymbol() != '[') fatal("missing '[' in %s\n", encfile);
      i = 0;
      while (i < 256 && (ns = nextsymbol()) == '/') {
         name = my_string(pline);
         if (strcmp(name, ".notdef") == 0) { 
            i++; remove_string();
            continue; 
         }
         addcc(name, i++);
      }
      if (i != 256) fatal("missing %d symbols in %s\n", 256-i, encfile);
      if (nextsymbol() != ']') fatal("missing ']' in %s\n", encfile);
      fclose(enc);
   }
   afm = fopen(afmfile, "r");
   if (afm == NULL) fatal("Can't open %s\n", afmfile);
   /*
    * Scan header of AFM file and take the FontName and EncodingScheme
    * (used for identification purposes in the PK postamble)
    * Stop after reading `StartCharMetrics'.
    */
   SCMseen = 0; Ccnt = 0;
   while (fgets(line, LINEBUF-1, afm)) {
      if (strncmp(line, "FontName", 8) == 0) { 
        pline = value_after(line, "FontName"); 
        len = strlen(pline);
        if (*(pline+len-1) == '\n') {
           *(pline+len-1) = '\0'; len--;
        }
        *fontname = malloc(len + 1);
        if (*fontname == NULL) fatal("Out of memory\n");
         strcpy(*fontname, pline);
      }
      else if (encname == NULL && strncmp(line, "EncodingScheme", 8) == 0) { 
        pline = value_after(line, "EncodingScheme"); 
        len = strlen(pline);
        if (*(pline+len-1) == '\n') {
           *(pline+len-1) = '\0'; len--;
        }
        *encname = malloc(len + 1);
        if (*encname == NULL) fatal("Out of memory\n");
        strcpy(*encname, pline);
      }
      else if (strncmp(line, "StartCharMetrics", 16) == 0) {
         SCMseen = 1; break;
      }
   }
   if (SCMseen == 0) fatal("%s: no metrics info\n", afmfile);
   while (fgets(line, LINEBUF-1, afm)) {
      if (strncmp(line, "EndCharMetrics", 14) == 0) break;
      if (strncmp(line, "C ", 2) != 0) 
         fatal("%s: unexpected line\n", afmfile);

      /* Gracefully terminate when the AFM file is not valid.  Every line */
      /* in the AFM file should specify a "C"haracter, a "WX"idth, and    */
      /* a "N"ame. (ndw)
      */
      pline = value_after(line, "C"); 
      if (pline == NULL)
         fatal("\nBad char metric in %s (no char):\n\t %s\n", afmfile, line);
      cc = decimal(pline);
      pline = value_after(pline, "WX"); 
      if (pline == NULL)
         fatal("\nBad char metric in %s (no width):\n\t %s\n", afmfile, line);
      wx = decimal(pline);
      pline = value_after(pline, "N"); 
      if (pline == NULL)
         fatal("\nBad char metric in %s (no name):\n\t %s\n", afmfile, line);
      name = my_string(pline);
      
      if (encfile) {
         if ((i = getcc(name)) == -1) {
            remove_string();
            continue;
        }
         do { /* allow more occurences per name */
            ev[i] = name; width[i] = wx;
            i = getcc(name);
         }
         while (i >= 0);
      }
      else if (cc >= 0 && cc <= 255) { /* ndw: 10/13/92 */
        ev[cc] = name; width[cc] = wx;
      }
      else if (cc > 255)
        msg("Char code %d ignored (out of range).\n", cc);
      Ccnt++;
   }
   if (Ccnt == 0) fatal("%s: no characters selected\n", afmfile);   
}

Here is the call graph for this function:

Here is the caller graph for this function:

void initcc ( )
char * my_string ( char *  s)

Definition at line 329 of file encoding.c.

{  int length; char *str;

   while (isspace(*s)) s++;
   str = s; length = 0;
   while (isalnum(*s) || *s == '.' || *s == '_') { length++; s++; }
   if (length == 0) return NULL;
   else {
      if (length + 1 > POOLSIZE - poolsize)
         fatal("Out of memory %s\n", s);
      strncpy(stringpool+poolsize, str, length);
      str = stringpool + poolsize;
      s = stringpool + poolsize + length;
      *s = '\0';
      lastpoolsize = poolsize;
      poolsize += length+1;
      return str; 
   }
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* nextpsname ( void  )

Definition at line 90 of file encoding.c.

{  int i = 0;
   char the_nextname[MAXSTRLEN], *name;

   for (;;) {
      if (pline == NULL) {
         if (fgets(line, LINEBUF-1, enc) == NULL)
            fatal("unexpected EOF while reading %s\n", encfile);
         pline = line;
      }
      if (*pline == '%' || *pline == '\n' || *pline == '\0') 
      {  pline = NULL; 
         continue; 
      }
      if (isspace(*pline)) {
         pline++;
        continue;
      }
      if (!isalpha(*pline)) fatal("invalid name in %s\n", encfile);
      /* pt981009: added '_' Staszek Wawrykiewicz <staw@gust.org.pl> */
      while (isalnum(*pline) || *pline == '-' || *pline == '_') {
       if (i > MAXSTRLEN-2)
         fatal("name too long in %s (%s)\n", line, encfile);
       the_nextname[i++] = *pline++;
      }
      the_nextname[i] = '\0';
      name = malloc(i+1);
      if (name == NULL) fatal("Out of memory\n");
      strcpy(name, the_nextname);
      break;
   }
   /* pt981009: removed pline++ Staszek Wawrykiewicz <staw@gust.org.pl> */
   return name;
}

Here is the call graph for this function:

Here is the caller graph for this function:

char nextsymbol ( void  )

Definition at line 72 of file encoding.c.

{
   for (;;) {
      if (pline == NULL) {
         if (fgets(line, LINEBUF-1, enc) == NULL)
            fatal("unexpected EOF while reading %s\n", encfile);
         pline = line;
      }
      if (*pline == '[' || *pline == ']' || *pline == '/') break;
      if (*pline == '%' || *pline == '\n' || *pline == '\0') 
      {  pline = NULL; 
         continue; 
      }
      pline++;
   }
   pline++; return *(pline-1);
}

Here is the caller graph for this function:

Definition at line 350 of file encoding.c.

Here is the caller graph for this function:

static char* value_after ( char *  s,
char *  t 
) [static]

Definition at line 39 of file encoding.c.

{  int l;
   l = strlen(t);
   for (;;) {
      while (*s != *t && *s != '\n') s++;
      if (*s == '\n') return NULL;
      if (strncmp(s, t, l) == 0) break;
      s++;
   }
   s += l;
   while (isspace(*s)) s++;
   return s;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

char * afmfile

Definition at line 210 of file ps2pk.c.

Definition at line 67 of file encoding.c.

char* encfile

Definition at line 210 of file ps2pk.c.

int freenode = 0 [static]

Definition at line 256 of file encoding.c.

int lastpoolsize [static]

Definition at line 326 of file encoding.c.

char line[LINEBUF]

Definition at line 70 of file encoding.c.

struct Node node[256]
char * pline = NULL

Definition at line 70 of file encoding.c.

int poolsize [static]

Definition at line 326 of file encoding.c.

short root = -1 [static]

Definition at line 257 of file encoding.c.

char stringpool[POOLSIZE] [static]

Definition at line 325 of file encoding.c.