<commit_before>/***************************************************************************/
/*                                                                         */
/*  cidgload.c                                                             */
/*                                                                         */
/*    CID-keyed Type1 Glyph Loader (body).                                 */
/*                                                                         */
/*  Copyright 1996-2001, 2002 by                                           */
/*  <NAME>, <NAME>, and <NAME>.                      */
/*                                                                         */
/*  This file is part of the FreeType project, and may only be used,       */
/*  modified, and distributed under the terms of the FreeType project      */
/*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
/*  this file you indicate that you have read the license and              */
/*  understand and accept it fully.                                        */
/*                                                                         */
/***************************************************************************/


#include <ft2build.h>
#include "cidload.h"
#include "cidgload.h"
#include FT_INTERNAL_DEBUG_H
#include FT_INTERNAL_STREAM_H
#include FT_OUTLINE_H

#include "ciderrs.h"


  /*************************************************************************/
  /*                                                                       */
  /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
  /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
  /* messages during execution.                                            */
  /*                                                                       */
#undef  FT_COMPONENT
#define FT_COMPONENT  trace_cidgload


  FT_CALLBACK_DEF( FT_Error )
  cid_load_glyph( T1_Decoder  decoder,
                  FT_UInt     glyph_index )
  {
    CID_Face      face = (CID_Face)decoder->builder.face;
    CID_FaceInfo  cid  = &face->cid;
    FT_Byte*      p;
    FT_UInt       fd_select;
    FT_Stream     stream = face->root.stream;
    FT_Error      error  = 0;
    FT_Byte*      charstring = 0;
    FT_Memory     memory = face->root.memory;
    FT_UInt       glyph_length = 0;


#ifdef FT_CONFIG_OPTION_INCREMENTAL

    /* For incremental fonts get the character data using */
    /* the callback function.                             */
    if ( face->root.incremental_interface )
    {
      FT_Data  glyph_data;


      error = face->root.incremental_interface->funcs->get_glyph_data(
                face->root.incremental_interface->object,
                glyph_index,
                &glyph_data );
      if ( error )
        goto Exit;

      p         = (FT_Byte*)glyph_data.pointer;
      fd_select = (FT_UInt)cid_get_offset( &p, (FT_Byte)cid->fd_bytes );

      glyph_data.pointer += cid->fd_bytes;
      glyph_data.length  -= cid->fd_bytes;
      glyph_length        = glyph_data.length;

      if ( glyph_length == 0 )
        goto Exit;
      if ( FT_ALLOC( charstring, glyph_length ) )
        goto Exit;
      ft_memcpy( charstring, glyph_data.pointer, glyph_length );
    }

    else

#endif

    /* For ordinary fonts read the CID font dictionary index */
    /* and charstring offset from the CIDMap.                */
    {
      FT_UInt   entry_len = cid->fd_bytes + cid->gd_bytes;
      FT_ULong  off1;


      if ( FT_STREAM_SEEK( cid->data_offset + cid->cidmap_offset +
                           glyph_index * entry_len )               ||
           FT_FRAME_ENTER( 2 * entry_len )                         )
        goto Exit;

      p            = (FT_Byte*)stream->cursor;
      fd_select    = (FT_UInt) cid_get_offset( &p, (FT_Byte)cid->fd_bytes );
      off1         = (FT_ULong)cid_get_offset( &p, (FT_Byte)cid->gd_bytes );
      p           += cid->fd_bytes;
      glyph_length = cid_get_offset( &p, (FT_Byte)cid->gd_bytes ) - off1;
      FT_FRAME_EXIT();

      if ( glyph_length == 0 )
        goto Exit;
      if ( FT_ALLOC( charstring, glyph_length ) )
        goto Exit;
      if ( FT_STREAM_READ_AT( cid->data_offset + off1,
                              charstring, glyph_length ) )
        goto Exit;
    }

    /* Now set up the subrs array and parse the charstrings. */
    {
      CID_FaceDict  dict;
      CID_Subrs     cid_subrs = face->subrs + fd_select;
      FT_Int        cs_offset;


      /* Set up subrs */
      decoder->num_subrs = cid_subrs->num_subrs;
      decoder->subrs     = cid_subrs->code;
      decoder->subrs_len = 0;

      /* Set up font matrix */
      dict                 = cid->font_dicts + fd_select;

      decoder->font_matrix = dict->font_matrix;
      decoder->font_offset = dict->font_offset;
      decoder->lenIV       = dict->private_dict.lenIV;

      /* Decode the charstring. */

      /* Adjustment for seed bytes. */
      cs_offset = ( decoder->lenIV >= 0 ? decoder->lenIV : 0 );

      /* Decrypt only if lenIV >= 0. */
      if ( decoder->lenIV >= 0 )
        cid_decrypt( charstring, glyph_length, 4330 );

      error = decoder->funcs.parse_charstrings( decoder,
                                                charstring + cs_offset,
                                                glyph_length - cs_offset  );
    }

    FT_FREE( charstring );

#ifdef FT_CONFIG_OPTION_INCREMENTAL

    /* Incremental fonts can optionally override the metrics. */
    if ( !error                                                     &&
         face->root.incremental_interface                           &&
         face->root.incremental_interface->funcs->get_glyph_metrics )
    {
      FT_Bool                 found = FALSE;
      FT_Basic_Glyph_Metrics  metrics;


      error = face->root.incremental_interface->funcs->get_glyph_metrics(
                face->root.incremental_interface->object,
                glyph_index, FALSE, &metrics, &found );
      if ( found )
      {
        decoder->builder.left_bearing.x = metrics.bearing_x;
        decoder->builder.left_bearing.y = metrics.bearing_y;
        decoder->builder.advance.x      = metrics.advance;
        decoder->builder.advance.y      = 0;
      }
    }

#endif

  Exit:
    return error;
  }<commit_msg>Corrected incremental interface implementation to suit the new system.<commit_after>/***************************************************************************/
/*                                                                         */
/*  cidgload.c                                                             */
/*                                                                         */
/*    CID-keyed Type1 Glyph Loader (body).                                 */
/*                                                                         */
/*  Copyright 1996-2001, 2002 by                                           */
/*  <NAME>, <NAME>, and <NAME>.                      */
/*                                                                         */
/*  This file is part of the FreeType project, and may only be used,       */
/*  modified, and distributed under the terms of the FreeType project      */
/*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
/*  this file you indicate that you have read the license and              */
/*  understand and accept it fully.                                        */
/*                                                                         */
/***************************************************************************/


#include <ft2build.h>
#include "cidload.h"
#include "cidgload.h"
#include FT_INTERNAL_DEBUG_H
#include FT_INTERNAL_STREAM_H
#include FT_OUTLINE_H

#include "ciderrs.h"


  /*************************************************************************/
  /*                                                                       */
  /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
  /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
  /* messages during execution.                                            */
  /*                                                                       */
#undef  FT_COMPONENT
#define FT_COMPONENT  trace_cidgload


  FT_CALLBACK_DEF( FT_Error )
  cid_load_glyph( T1_Decoder  decoder,
                  FT_UInt     glyph_index )
  {
    CID_Face      face = (CID_Face)decoder->builder.face;
    CID_FaceInfo  cid  = &face->cid;
    FT_Byte*      p;
    FT_UInt       fd_select;
    FT_Stream     stream = face->root.stream;
    FT_Error      error  = 0;
    FT_Byte*      charstring = 0;
    FT_Memory     memory = face->root.memory;
    FT_UInt       glyph_length = 0;


#ifdef FT_CONFIG_OPTION_INCREMENTAL

    /* For incremental fonts get the character data using */
    /* the callback function.                             */
    if ( face->root.internal->incremental_interface )
    {
      FT_Data  glyph_data;


      error = face->root.internal->incremental_interface->funcs->get_glyph_data(
                face->root.internal->incremental_interface->object,
                glyph_index,
                &glyph_data );
      if ( error )
        goto Exit;

      p         = (FT_Byte*)glyph_data.pointer;
      fd_select = (FT_UInt)cid_get_offset( &p, (FT_Byte)cid->fd_bytes );

      if ( glyph_data.length != 0 )
	  {
        glyph_length = glyph_data.length - cid->fd_bytes;
        FT_ALLOC( charstring, glyph_length );
	    if ( !error )
          ft_memcpy( charstring, glyph_data.pointer + cid->fd_bytes, glyph_length );
	  }

	  face->root.internal->incremental_interface->funcs->free_glyph_data(
                face->root.internal->incremental_interface->object,
                &glyph_data );

      if ( error )
        goto Exit;
    }

    else

#endif

    /* For ordinary fonts read the CID font dictionary index */
    /* and charstring offset from the CIDMap.                */
    {
      FT_UInt   entry_len = cid->fd_bytes + cid->gd_bytes;
      FT_ULong  off1;


      if ( FT_STREAM_SEEK( cid->data_offset + cid->cidmap_offset +
                           glyph_index * entry_len )               ||
           FT_FRAME_ENTER( 2 * entry_len )                         )
        goto Exit;

      p            = (FT_Byte*)stream->cursor;
      fd_select    = (FT_UInt) cid_get_offset( &p, (FT_Byte)cid->fd_bytes );
      off1         = (FT_ULong)cid_get_offset( &p, (FT_Byte)cid->gd_bytes );
      p           += cid->fd_bytes;
      glyph_length = cid_get_offset( &p, (FT_Byte)cid->gd_bytes ) - off1;
      FT_FRAME_EXIT();

      if ( glyph_length == 0 )
        goto Exit;
      if ( FT_ALLOC( charstring, glyph_length ) )
        goto Exit;
      if ( FT_STREAM_READ_AT( cid->data_offset + off1,
                              charstring, glyph_length ) )
        goto Exit;
    }

    /* Now set up the subrs array and parse the charstrings. */
    {
      CID_FaceDict  dict;
      CID_Subrs     cid_subrs = face->subrs + fd_select;
      FT_Int        cs_offset;


      /* Set up subrs */
      decoder->num_subrs = cid_subrs->num_subrs;
      decoder->subrs     = cid_subrs->code;
      decoder->subrs_len = 0;

      /* Set up font matrix */
      dict                 = cid->font_dicts + fd_select;

      decoder->font_matrix = dict->font_matrix;
      decoder->font_offset = dict->font_offset;
      decoder->lenIV       = dict->private_dict.lenIV;

      /* Decode the charstring. */

      /* Adjustment for seed bytes. */
      cs_offset = ( decoder->lenIV >= 0 ? decoder->lenIV : 0 );

      /* Decrypt only if lenIV >= 0. */
      if ( decoder->lenIV >= 0 )
        cid_decrypt( charstring, glyph_length, 4330 );

      error = decoder->funcs.parse_charstrings( decoder,
                                                charstring + cs_offset,
                                                glyph_length - cs_offset  );
    }

    FT_FREE( charstring );

#ifdef FT_CONFIG_OPTION_INCREMENTAL

    /* Incremental fonts can optionally override the metrics. */
    if ( !error                                       &&
         face->root.internal->incremental_interface   &&
         face->root.internal->incremental_interface->funcs->get_glyph_metrics )
    {
      FT_Bool                    found = FALSE;
      FT_Incremental_MetricsRec  metrics;

      error = face->root.internal->incremental_interface->funcs->get_glyph_metrics(
                face->root.internal->incremental_interface->object,
                glyph_index, FALSE, &metrics, &found );
      if ( found )
      {
        decoder->builder.left_bearing.x = metrics.bearing_x;
        decoder->builder.left_bearing.y = metrics.bearing_y;
        decoder->builder.advance.x      = metrics.advance;
        decoder->builder.advance.y      = 0;
      }
    }

#endif

  Exit:
    return error;
  }
{'fraction_non_alphanumeric': 0.12602190343976555, 'fraction_numerical': 0.005398735153478328, 'mean_word_length': 1.4250981858986347, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 137, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 67, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3054282', 'n_tokens_mistral': 3703, 'n_tokens_neox': 3422, 'n_words': 762}