Back to index

texmacs  1.0.7.15
Classes | Defines | Typedefs | Functions
sfnt.h File Reference
#include "mfileio.h"
#include "numbers.h"
#include "pdfobj.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  sfnt_table
struct  sfnt_table_directory
struct  sfnt

Defines

#define SFNT_TABLE_REQUIRED   (1 << 0)
#define SFNT_TYPE_TRUETYPE   (1 << 0)
#define SFNT_TYPE_OPENTYPE   (1 << 1)
#define SFNT_TYPE_POSTSCRIPT   (1 << 2)
#define SFNT_TYPE_TTC   (1 << 4)
#define SFNT_TYPE_DFONT   (1 << 8)
#define fixed(a)
#define sfnt_get_byte(s)   ((BYTE) get_unsigned_byte((s)->stream))
#define sfnt_get_char(s)   ((CHAR) get_signed_byte ((s)->stream))
#define sfnt_get_ushort(s)   ((USHORT) get_unsigned_pair((s)->stream))
#define sfnt_get_short(s)   ((SHORT) get_signed_pair ((s)->stream))
#define sfnt_get_ulong(s)   ((ULONG) get_unsigned_quad((s)->stream))
#define sfnt_get_long(s)   ((LONG) get_signed_quad ((s)->stream))
#define sfnt_seek_set(s, o)   seek_absolute((s)->stream, (o))
#define sfnt_read(b, l, s)   fread((b), 1, (l), (s)->stream)
#define sfnt_put_ushort(s, v)   put_big_endian((s), v, 2);
#define sfnt_put_short(s, v)   put_big_endian((s), v, 2);
#define sfnt_put_ulong(s, v)   put_big_endian((s), v, 4);
#define sfnt_put_long(s, v)   put_big_endian((s), v, 4);

Typedefs

typedef unsigned char BYTE
typedef signed char CHAR
typedef unsigned short USHORT
typedef signed short SHORT
typedef unsigned long ULONG
typedef signed long LONG
typedef unsigned long Fixed
typedef short FWord
typedef unsigned short uFWord
typedef short F2Dot14

Functions

int put_big_endian (void *s, LONG q, int n)
sfntsfnt_open (FILE *fp)
void sfnt_close (sfnt *sfont)
sfntdfont_open (FILE *fp, int index)
int sfnt_read_table_directory (sfnt *sfont, ULONG offset)
ULONG sfnt_find_table_len (sfnt *sfont, const char *tag)
ULONG sfnt_find_table_pos (sfnt *sfont, const char *tag)
ULONG sfnt_locate_table (sfnt *sfont, const char *tag)
void sfnt_set_table (sfnt *sfont, const char *tag, void *data, ULONG length)
int sfnt_require_table (sfnt *sfont, const char *tag, int must_exist)
pdf_objsfnt_create_FontFile_stream (sfnt *sfont)

Class Documentation

struct sfnt_table

Definition at line 43 of file sfnt.h.

Class Members
ULONG check_sum
char * data
ULONG length
ULONG offset
char tag
struct sfnt_table_directory

Definition at line 55 of file sfnt.h.

Collaboration diagram for sfnt_table_directory:
Class Members
USHORT entry_selector
char * flags
USHORT num_kept_tables
USHORT num_tables
USHORT range_shift
USHORT search_range
struct sfnt_table * tables
ULONG version
struct sfnt

Definition at line 74 of file sfnt.h.

Collaboration diagram for sfnt:
Class Members
struct sfnt_table_directory * directory
ULONG offset
FILE * stream
int type

Define Documentation

#define fixed (   a)
Value:
((double)((a)%0x10000L)/(double)(0x10000L) + \
 (a)/0x10000L - (((a)/0x10000L > 0x7fffL) ? 0x10000L : 0))

Definition at line 83 of file sfnt.h.

#define sfnt_get_byte (   s)    ((BYTE) get_unsigned_byte((s)->stream))

Definition at line 87 of file sfnt.h.

#define sfnt_get_char (   s)    ((CHAR) get_signed_byte ((s)->stream))

Definition at line 88 of file sfnt.h.

#define sfnt_get_long (   s)    ((LONG) get_signed_quad ((s)->stream))

Definition at line 92 of file sfnt.h.

#define sfnt_get_short (   s)    ((SHORT) get_signed_pair ((s)->stream))

Definition at line 90 of file sfnt.h.

#define sfnt_get_ulong (   s)    ((ULONG) get_unsigned_quad((s)->stream))

Definition at line 91 of file sfnt.h.

#define sfnt_get_ushort (   s)    ((USHORT) get_unsigned_pair((s)->stream))

Definition at line 89 of file sfnt.h.

#define sfnt_put_long (   s,
 
)    put_big_endian((s), v, 4);

Definition at line 102 of file sfnt.h.

#define sfnt_put_short (   s,
 
)    put_big_endian((s), v, 2);

Definition at line 100 of file sfnt.h.

#define sfnt_put_ulong (   s,
 
)    put_big_endian((s), v, 4);

Definition at line 101 of file sfnt.h.

#define sfnt_put_ushort (   s,
 
)    put_big_endian((s), v, 2);

Definition at line 99 of file sfnt.h.

#define sfnt_read (   b,
  l,
 
)    fread((b), 1, (l), (s)->stream)

Definition at line 95 of file sfnt.h.

#define sfnt_seek_set (   s,
 
)    seek_absolute((s)->stream, (o))

Definition at line 94 of file sfnt.h.

#define SFNT_TABLE_REQUIRED   (1 << 0)

Definition at line 53 of file sfnt.h.

#define SFNT_TYPE_DFONT   (1 << 8)

Definition at line 72 of file sfnt.h.

#define SFNT_TYPE_OPENTYPE   (1 << 1)

Definition at line 69 of file sfnt.h.

#define SFNT_TYPE_POSTSCRIPT   (1 << 2)

Definition at line 70 of file sfnt.h.

#define SFNT_TYPE_TRUETYPE   (1 << 0)

Definition at line 68 of file sfnt.h.

#define SFNT_TYPE_TTC   (1 << 4)

Definition at line 71 of file sfnt.h.


Typedef Documentation

typedef unsigned char BYTE

Definition at line 31 of file sfnt.h.

typedef signed char CHAR

Definition at line 32 of file sfnt.h.

typedef short F2Dot14

Definition at line 40 of file sfnt.h.

typedef unsigned long Fixed

Definition at line 37 of file sfnt.h.

typedef short FWord

Definition at line 38 of file sfnt.h.

typedef signed long LONG

Definition at line 36 of file sfnt.h.

typedef signed short SHORT

Definition at line 34 of file sfnt.h.

typedef unsigned short uFWord

Definition at line 39 of file sfnt.h.

typedef unsigned long ULONG

Definition at line 35 of file sfnt.h.

typedef unsigned short USHORT

Definition at line 33 of file sfnt.h.


Function Documentation

sfnt* dfont_open ( FILE *  fp,
int  index 
)

Definition at line 88 of file sfnt.c.

{
  sfnt  *sfont;
  ULONG  rdata_pos, map_pos, tags_pos, types_pos, res_pos, tag;
  USHORT tags_num, types_num, i;

  ASSERT(fp);

  rewind(fp);

  sfont = NEW(1, sfnt);

  sfont->stream = fp;

  rdata_pos = sfnt_get_ulong(sfont);
  map_pos   = sfnt_get_ulong(sfont);
  sfnt_seek_set(sfont, map_pos + 0x18);
  tags_pos = map_pos + sfnt_get_ushort(sfont);
  sfnt_seek_set(sfont, tags_pos);
  tags_num = sfnt_get_ushort(sfont);

  for (i = 0; i <= tags_num; i++) {
    tag = sfnt_get_ulong(sfont); /* tag name */
    types_num = sfnt_get_ushort(sfont); /* typefaces number */
    types_pos = tags_pos + sfnt_get_ushort(sfont); /* typefaces position */
    if (tag == 0x73666e74UL) /* "sfnt" */
      break;
  }

  if (i > tags_num) {
    RELEASE(sfont);
    return NULL;
  }

  sfnt_seek_set(sfont, types_pos);
  if (index > types_num) {
    ERROR("Invalid index %d for dfont.", index);
  }

  for (i = 0; i <= types_num; i++) {
    (void) sfnt_get_ushort(sfont); /* resource id */
    (void) sfnt_get_ushort(sfont); /* resource name position from name_list */
    res_pos = sfnt_get_ulong(sfont);   /* resource flag (byte) + resource offset */
    sfnt_get_ulong(sfont);  /* mbz */
    if (i == index) break;
  }

  rewind(sfont->stream);

  sfont->type = SFNT_TYPE_DFONT;
  sfont->directory = NULL;
  sfont->offset = (res_pos & 0x00ffffffUL) + rdata_pos + 4;

  return sfont;
}

Here is the caller graph for this function:

int put_big_endian ( void *  s,
LONG  q,
int  n 
)

Definition at line 179 of file sfnt.c.

{
  int   i;
  char *p;

  p = (char *) s;
  for (i = n - 1; i >= 0; i--) {
    p[i] = (char) (q & 0xff);
    q >>= 8;
  }

  return n;
}
void sfnt_close ( sfnt sfont)

Definition at line 166 of file sfnt.c.

{

  if (sfont) {
    if (sfont->directory)
      release_directory(sfont->directory);
    RELEASE(sfont);
  }

  return;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 435 of file sfnt.c.

{
  pdf_obj *stream;
  pdf_obj *stream_dict;
  struct sfnt_table_directory *td;
  long     offset, nb_read, length;
  int      i, sr;
  char    *p;

  ASSERT(sfont && sfont->directory);

  stream = pdf_new_stream(STREAM_COMPRESS);

  td  = sfont->directory;

  /* Header */
  p  = (char *) wbuf;
  p += sfnt_put_ulong (p, td->version);
  p += sfnt_put_ushort(p, td->num_kept_tables);
  sr = max2floor(td->num_kept_tables) * 16;
  p += sfnt_put_ushort(p, sr);
  p += sfnt_put_ushort(p, log2floor(td->num_kept_tables));
  p += sfnt_put_ushort(p, td->num_kept_tables * 16 - sr);

  pdf_add_stream(stream, wbuf, 12);

  /*
   * Compute start of actual tables (after headers).
   */
  offset = 12 + 16 * td->num_kept_tables;
  for (i = 0; i < td->num_tables; i++) {
    /* This table must exist in FontFile */
    if (td->flags[i] & SFNT_TABLE_REQUIRED) {
      if ((offset % 4) != 0) {
       offset += 4 - (offset % 4);
      }

      p = (char *) wbuf;
      memcpy(p, td->tables[i].tag, 4);
      p += 4;
      p += sfnt_put_ulong(p, td->tables[i].check_sum);
      p += sfnt_put_ulong(p, offset);
      p += sfnt_put_ulong(p, td->tables[i].length);
      pdf_add_stream(stream, wbuf, 16);

      offset += td->tables[i].length;
    }
  }

  offset = 12 + 16 * td->num_kept_tables;
  for (i = 0; i < td->num_tables; i++) {
    if (td->flags[i] & SFNT_TABLE_REQUIRED) {
      if ((offset % 4) != 0) {
       length  = 4 - (offset % 4);
       pdf_add_stream(stream, padbytes, length);
       offset += length;
      }
      if (!td->tables[i].data) {
       if (!sfont->stream) {
         pdf_release_obj(stream);
         ERROR("Font file not opened or already closed...");
         return NULL;
       }

       length = td->tables[i].length;
       sfnt_seek_set(sfont, td->tables[i].offset); 
       while (length > 0) {
         nb_read = sfnt_read(wbuf, MIN(length, 1024), sfont);
         if (nb_read < 0) {
           pdf_release_obj(stream);
           ERROR("Reading file failed...");
           return NULL;
         } else if (nb_read > 0) {
           pdf_add_stream(stream, wbuf, nb_read);
         }
         length -= nb_read;
       }
      } else {
       pdf_add_stream(stream,
                     td->tables[i].data, td->tables[i].length);
       RELEASE(td->tables[i].data);
       td->tables[i].data = NULL;
      }
      /* Set offset for next table */
      offset += td->tables[i].length;
    }
  }

  stream_dict = pdf_stream_dict(stream);
  pdf_add_dict(stream_dict,
              pdf_new_name("Length1"),
              pdf_new_number(offset));

  return stream;
}

Here is the call graph for this function:

Here is the caller graph for this function:

ULONG sfnt_find_table_len ( sfnt sfont,
const char *  tag 
)

Definition at line 302 of file sfnt.c.

{
  ULONG  length;
  struct sfnt_table_directory *td;
  int    idx;

  ASSERT(sfont && tag);

  td  = sfont->directory;
  idx = find_table_index(td, tag);
  if (idx < 0)
    length = 0;
  else {
    length = td->tables[idx].length;
  }

  return length;
}

Here is the call graph for this function:

Here is the caller graph for this function:

ULONG sfnt_find_table_pos ( sfnt sfont,
const char *  tag 
)

Definition at line 322 of file sfnt.c.

{
  ULONG  offset;
  struct sfnt_table_directory *td;
  int    idx;

  ASSERT(sfont && tag);

  td  = sfont->directory;
  idx = find_table_index(td, tag);
  if (idx < 0)
    offset = 0;
  else {
    offset = td->tables[idx].offset;
  }

  return offset;
}

Here is the call graph for this function:

Here is the caller graph for this function:

ULONG sfnt_locate_table ( sfnt sfont,
const char *  tag 
)

Definition at line 342 of file sfnt.c.

{
  ULONG offset;

  ASSERT(sfont && tag);

  offset = sfnt_find_table_pos(sfont, tag);
  if (offset == 0)
    ERROR("sfnt: table not found...");

  sfnt_seek_set(sfont, offset);

  return offset;
}

Here is the call graph for this function:

Here is the caller graph for this function:

sfnt* sfnt_open ( FILE *  fp)

Definition at line 41 of file sfnt.c.

{
  sfnt  *sfont;
  ULONG  type;

  ASSERT(fp);

  rewind(fp);

  sfont = NEW(1, sfnt);

  sfont->stream = fp;

/*
 * type:
 *  `true' (0x74727565): TrueType (Mac)
 *  `typ1' (0x74797031) (Mac): PostScript font housed in a sfnt wrapper
 *  0x00010000: TrueType (Win)/OpenType
 *  `OTTO': PostScript CFF font with OpenType wrapper
 *  `ttcf': TrueType Collection
*/
#define SFNT_TRUETYPE   0x00010000UL
#define SFNT_OPENTYPE   0x00010000UL
#define SFNT_POSTSCRIPT 0x4f54544fUL
#define SFNT_TTC        0x74746366UL

  type = sfnt_get_ulong(sfont);

  if (type == SFNT_TRUETYPE) {
    sfont->type = SFNT_TYPE_TRUETYPE;
  } else if (type == SFNT_OPENTYPE) {
    sfont->type = SFNT_TYPE_OPENTYPE;
  } else if (type == SFNT_POSTSCRIPT) { 
    sfont->type = SFNT_TYPE_POSTSCRIPT;
  } else if (type == SFNT_TTC) {
    sfont->type = SFNT_TYPE_TTC;
  }

  rewind(sfont->stream);

  sfont->directory = NULL;
  sfont->offset = 0UL;

  return sfont;
}

Here is the caller graph for this function:

int sfnt_read_table_directory ( sfnt sfont,
ULONG  offset 
)

Definition at line 358 of file sfnt.c.

{
  struct sfnt_table_directory *td;
  unsigned long i, u_tag;

  ASSERT(sfont && sfont->stream);

  sfnt_seek_set(sfont, offset);

  if (sfont->directory)
    release_directory(sfont->directory);    

  sfont->directory = td = NEW (1, struct sfnt_table_directory);

  td->version      = sfnt_get_ulong(sfont);
  td->num_tables   = sfnt_get_ushort(sfont);
  td->search_range = sfnt_get_ushort(sfont);
  td->entry_selector = sfnt_get_ushort(sfont);
  td->range_shift    = sfnt_get_ushort(sfont);

  td->flags  = NEW(td->num_tables, char);
  td->tables = NEW(td->num_tables, struct sfnt_table);

  for (i = 0; i < td->num_tables; i++) {
    u_tag = sfnt_get_ulong(sfont);

    convert_tag(td->tables[i].tag, u_tag);
    td->tables[i].check_sum = sfnt_get_ulong(sfont);
    td->tables[i].offset    = sfnt_get_ulong(sfont) + sfont->offset;
    td->tables[i].length    = sfnt_get_ulong(sfont);
    td->tables[i].data      = NULL;
//fprintf(stderr, "[%4s:%x]", td->tables[i].tag, td->tables[i].offset);

    td->flags[i] = 0;
  }
  td->num_kept_tables = 0;

  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int sfnt_require_table ( sfnt sfont,
const char *  tag,
int  must_exist 
)

Definition at line 399 of file sfnt.c.

{
  struct sfnt_table_directory *td;
  int    idx;

  ASSERT(sfont && sfont->directory);

  td  = sfont->directory;
  idx = find_table_index(td, tag);
  if (idx < 0) {
    if (must_exist)
      return -1;
  } else {
    td->flags[idx] |= SFNT_TABLE_REQUIRED;
    td->num_kept_tables++;
  }

  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void sfnt_set_table ( sfnt sfont,
const char *  tag,
void *  data,
ULONG  length 
)

Definition at line 276 of file sfnt.c.

{
  struct sfnt_table_directory *td;
  int    idx;

  ASSERT(sfont);

  td  = sfont->directory;
  idx = find_table_index(td, tag);

  if (idx < 0) {
    idx = td->num_tables;
    td->num_tables++;
    td->tables = RENEW(td->tables, td->num_tables, struct sfnt_table);
    memcpy(td->tables[idx].tag, tag, 4);
  }

  td->tables[idx].check_sum = sfnt_calc_checksum(data, length);
  td->tables[idx].offset    = 0L;
  td->tables[idx].length    = length;
  td->tables[idx].data      = data;

  return;
}

Here is the call graph for this function:

Here is the caller graph for this function: