Back to index

texmacs  1.0.7.15
Functions
truetype.h File Reference
#include "pdffont.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int pdf_font_open_truetype (pdf_font *font)
int pdf_font_load_truetype (pdf_font *font)

Function Documentation

Definition at line 878 of file truetype.c.

{
  pdf_obj   *descriptor  = pdf_font_get_descriptor(font);
  char      *ident       = pdf_font_get_ident(font);
  int        encoding_id = pdf_font_get_encoding(font);
  char      *usedchars   = pdf_font_get_usedchars(font);
#ifdef  ENABLE_NOEMBED
  int        embedding   = pdf_font_get_flag(font, PDF_FONT_FLAG_NOEMBED) ? 0 : 1;
#endif /* ENABLE_NOEMBED */
  int        index       = pdf_font_get_index(font);
  char     **enc_vec;
  pdf_obj   *fontfile;
  FILE      *fp;
  sfnt      *sfont;
  int        i, error = 0;

  if (!pdf_font_is_in_use(font))
    return  0;

  verbose = pdf_font_get_verbose();

  fp = DPXFOPEN(ident, DPX_RES_TYPE_TTFONT);
  if (!fp) {
    fp = DPXFOPEN(ident, DPX_RES_TYPE_DFONT);
    if (!fp) ERROR("Unable to open TrueType/dfont font file: %s", ident); /* Should find *truetype* here */
    sfont = dfont_open(fp, index);
  } else {
    sfont = sfnt_open(fp);
  }

  if (!sfont) {
    ERROR("Unable to open TrueType/dfont file: %s", ident);
    DPXFCLOSE(fp);
    return  -1;
  } else if (sfont->type != SFNT_TYPE_TRUETYPE &&
             sfont->type != SFNT_TYPE_TTC &&
             sfont->type != SFNT_TYPE_DFONT) { 
    ERROR("Font \"%s\" not a TrueType/dfont font?", ident);
    sfnt_close(sfont);
    DPXFCLOSE(fp);
    return  -1;
  }

  if (sfont->type == SFNT_TYPE_TTC) {
    unsigned long offset;
    offset = ttc_read_offset(sfont, index);
    if (offset == 0) ERROR("Invalid TTC index in %s.", ident);
    error = sfnt_read_table_directory(sfont, ttc_read_offset(sfont, offset));
  } else {
    error = sfnt_read_table_directory(sfont, sfont->offset);
  }

  if (error) {
    ERROR("Reading SFND table dir failed for font-file=\"%s\"... Not a TrueType font?", ident);
    sfnt_close(sfont);
    DPXFCLOSE(fp);
    return  -1;
  }

  /*
   * Create new TrueType cmap table with MacRoman encoding.
   */
  if (encoding_id < 0)
    error = do_builtin_encoding(font, usedchars, sfont);
  else {
    enc_vec  = pdf_encoding_get_encoding(encoding_id);
    error = do_custom_encoding(font, enc_vec, usedchars, sfont);
  }
  if (error) {
    ERROR("Error occured while creating font subfont for \"%s\"", ident);
    sfnt_close(sfont);
    DPXFCLOSE(fp);
    return  -1;
  }

#if  ENABLE_NOEMBED
  if (!embedding) {
    sfnt_close(sfont);
    DPXFCLOSE(fp);
    return  0;
  }
#endif /* ENABLE_NOEMBED */

  /*
   * TODO: post table?
   */

  for (i = 0; required_table[i].name != NULL; i++) {
    if (sfnt_require_table(sfont,
                           required_table[i].name,
                           required_table[i].must_exist) < 0) {
      ERROR("Required TrueType table \"%s\" does not exist in font: %s",
            required_table[i].name, ident);
      sfnt_close(sfont);
      DPXFCLOSE(fp);
      return  -1;
    }
  }

  /*
   * FontFile2
   */
  fontfile = sfnt_create_FontFile_stream(sfont);
  if (!fontfile)
    ERROR("Could not created FontFile stream for \"%s\".", ident);

  sfnt_close(sfont);
  DPXFCLOSE(fp);

  if (verbose > 1)
    MESG("[%ld bytes]", pdf_stream_length(fontfile));

  pdf_add_dict(descriptor,
               pdf_new_name("FontFile2"), pdf_ref_obj(fontfile)); /* XXX */
  pdf_release_obj(fontfile);

  return  0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 64 of file truetype.c.

{
  char     *ident;
  int       index, encoding_id;
  pdf_obj  *fontdict, *descriptor;
  sfnt     *sfont;
  int       embedding = 1; /* Must be embedded. */
  FILE     *fp;
  int       length, error = 0;

  ASSERT( font );

  ident = pdf_font_get_ident(font);
  index = pdf_font_get_index(font);

  ASSERT( ident );

  fp = DPXFOPEN(ident, DPX_RES_TYPE_TTFONT);
  if (!fp) {
    fp = DPXFOPEN(ident, DPX_RES_TYPE_DFONT);
    if (!fp) return  -1;
    sfont = dfont_open(fp, index);
  } else {
    sfont = sfnt_open(fp);
  }

  if (!sfont) {
    WARN("Could not open TrueType font: %s", ident);
    DPXFCLOSE(fp);
    return  -1;
  }

  if (sfont->type == SFNT_TYPE_TTC) {
    unsigned long offset;
    offset = ttc_read_offset(sfont, index);
    if (offset == 0) ERROR("Invalid TTC index in %s.", ident);
    error = sfnt_read_table_directory(sfont, offset);
  } else {
    error = sfnt_read_table_directory(sfont, sfont->offset);
  }

  if (error) {
    sfnt_close(sfont);
    DPXFCLOSE(fp);
    return  -1; /* Silently */
  }

  /* Reading fontdict before checking fonttype conflicts with PKFONT
   * because pdf_font_get_resource() always makes a dictionary.
   */
  encoding_id = pdf_font_get_encoding(font);
  fontdict    = pdf_font_get_resource(font);
  descriptor  = pdf_font_get_descriptor(font);
#ifdef  ENABLE_NOEMBED
  embedding   = pdf_font_get_flag(font, PDF_FONT_FLAG_NOEMBED) ? 0 : 1;
#endif /* ENABLE_NOEMBED */

  ASSERT( fontdict && descriptor );

  {
    pdf_obj  *tmp;
    tmp  = tt_get_fontdesc(sfont, &embedding, -1, 1);
    if (!tmp) {
      ERROR("Could not obtain neccesary font info.");
      sfnt_close(sfont);
      DPXFCLOSE(fp);
      return  -1;
    }
    ASSERT(pdf_obj_typeof(tmp) == PDF_DICT);

    pdf_merge_dict(descriptor, tmp);
    pdf_release_obj(tmp);
  }

  if (!embedding) {
    if (encoding_id >= 0 &&
        !pdf_encoding_is_predefined(encoding_id)) {
      ERROR("Custom encoding not allowed for non-embedded TrueType font.");
      sfnt_close(sfont);
      return -1;
    } else {
      /* There are basically no guarantee for font substitution
       * can work with "symblic" fonts. At least all glyphs
       * contained in the font must be identified; glyphs covers
       * by this instance of font should contain glyphs only from
       * Adobe Standard Latin Set. We allow non-embedded font
       * only to predefined encodings for this reason. Note that
       * "builtin" encoding means "MacRoman" here.
       */
      pdf_obj  *tmp;
      long      flags;

#ifndef  ENABLE_NOEMBED
      ERROR("Font file=\"%s\" can't be embedded due to liscence restrictions.", ident);
#endif /* ENABLE_NOEMBED */
      pdf_font_set_flags(font, PDF_FONT_FLAG_NOEMBED);
      tmp = pdf_lookup_dict(descriptor, "Flags");
      if (tmp && pdf_obj_typeof(tmp) == PDF_NUMBER) {
        flags  = (long) pdf_number_value(tmp);
        flags &= (1 << 2); /* clear Symbolic */
        flags |= (1 << 5); /* set Nonsymbolic */
        pdf_add_dict(descriptor, pdf_new_name("Flags"), pdf_new_number(flags));
      }
    }
  }

  {
    char  fontname[256];
    int   n;

    memset(fontname, 0, 256);
    length = tt_get_ps_fontname(sfont, fontname, 255);
    if (length < 1) {
      length = MIN(strlen(ident), 255);
      strncpy(fontname, ident, length);
    }
    fontname[length] = '\0';
    for (n = 0; n < length; n++) {
      if (fontname[n] == 0) {
        memmove(fontname + n, fontname + n + 1, length - n - 1);
      }
    }
    if (strlen(fontname) == 0)
      ERROR("Can't find valid fontname for \"%s\".", ident);
    pdf_font_set_fontname(font, fontname);
  }

  sfnt_close(sfont);
  DPXFCLOSE(fp);

  pdf_add_dict(fontdict,
               pdf_new_name("Type"),    pdf_new_name("Font"));
  pdf_add_dict(fontdict,
               pdf_new_name("Subtype"), pdf_new_name("TrueType"));

  return  0;
}

Here is the call graph for this function:

Here is the caller graph for this function: