tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

cffcmap.c (7312B)


      1 /****************************************************************************
      2 *
      3 * cffcmap.c
      4 *
      5 *   CFF character mapping table (cmap) support (body).
      6 *
      7 * Copyright (C) 2002-2025 by
      8 * David Turner, Robert Wilhelm, and Werner Lemberg.
      9 *
     10 * This file is part of the FreeType project, and may only be used,
     11 * modified, and distributed under the terms of the FreeType project
     12 * license, LICENSE.TXT.  By continuing to use, modify, or distribute
     13 * this file you indicate that you have read the license and
     14 * understand and accept it fully.
     15 *
     16 */
     17 
     18 
     19 #include <freetype/internal/ftdebug.h>
     20 #include "cffcmap.h"
     21 #include "cffload.h"
     22 
     23 #include "cfferrs.h"
     24 
     25 
     26  /*************************************************************************/
     27  /*************************************************************************/
     28  /*****                                                               *****/
     29  /*****           CFF STANDARD (AND EXPERT) ENCODING CMAPS            *****/
     30  /*****                                                               *****/
     31  /*************************************************************************/
     32  /*************************************************************************/
     33 
     34  FT_CALLBACK_DEF( FT_Error )
     35  cff_cmap_encoding_init( FT_CMap     cmap,
     36                          FT_Pointer  pointer )
     37  {
     38    CFF_CMapStd   cffcmap  = (CFF_CMapStd)cmap;
     39    TT_Face       face     = (TT_Face)FT_CMAP_FACE( cmap );
     40    CFF_Font      cff      = (CFF_Font)face->extra.data;
     41    CFF_Encoding  encoding = &cff->encoding;
     42 
     43    FT_UNUSED( pointer );
     44 
     45 
     46    cffcmap->gids = encoding->codes;
     47 
     48    return 0;
     49  }
     50 
     51 
     52  FT_CALLBACK_DEF( void )
     53  cff_cmap_encoding_done( FT_CMap  cmap )
     54  {
     55    CFF_CMapStd  cffcmap = (CFF_CMapStd)cmap;
     56 
     57 
     58    cffcmap->gids = NULL;
     59  }
     60 
     61 
     62  FT_CALLBACK_DEF( FT_UInt )
     63  cff_cmap_encoding_char_index( FT_CMap    cmap,
     64                                FT_UInt32  char_code )
     65  {
     66    CFF_CMapStd  cffcmap = (CFF_CMapStd)cmap;
     67    FT_UInt      result  = 0;
     68 
     69 
     70    if ( char_code < 256 )
     71      result = cffcmap->gids[char_code];
     72 
     73    return result;
     74  }
     75 
     76 
     77  FT_CALLBACK_DEF( FT_UInt )
     78  cff_cmap_encoding_char_next( FT_CMap     cmap,
     79                               FT_UInt32  *pchar_code )
     80  {
     81    CFF_CMapStd  cffcmap   = (CFF_CMapStd)cmap;
     82    FT_UInt      result    = 0;
     83    FT_UInt32    char_code = *pchar_code;
     84 
     85 
     86    while ( char_code < 255 )
     87    {
     88      result = cffcmap->gids[++char_code];
     89      if ( result )
     90      {
     91        *pchar_code = char_code;
     92        break;
     93      }
     94    }
     95 
     96    return result;
     97  }
     98 
     99 
    100  FT_DEFINE_CMAP_CLASS(
    101    cff_cmap_encoding_class_rec,
    102 
    103    sizeof ( CFF_CMapStdRec ),
    104 
    105    (FT_CMap_InitFunc)     cff_cmap_encoding_init,        /* init       */
    106    (FT_CMap_DoneFunc)     cff_cmap_encoding_done,        /* done       */
    107    (FT_CMap_CharIndexFunc)cff_cmap_encoding_char_index,  /* char_index */
    108    (FT_CMap_CharNextFunc) cff_cmap_encoding_char_next,   /* char_next  */
    109 
    110    (FT_CMap_CharVarIndexFunc)    NULL,  /* char_var_index   */
    111    (FT_CMap_CharVarIsDefaultFunc)NULL,  /* char_var_default */
    112    (FT_CMap_VariantListFunc)     NULL,  /* variant_list     */
    113    (FT_CMap_CharVariantListFunc) NULL,  /* charvariant_list */
    114    (FT_CMap_VariantCharListFunc) NULL   /* variantchar_list */
    115  )
    116 
    117 
    118  /*************************************************************************/
    119  /*************************************************************************/
    120  /*****                                                               *****/
    121  /*****              CFF SYNTHETIC UNICODE ENCODING CMAP              *****/
    122  /*****                                                               *****/
    123  /*************************************************************************/
    124  /*************************************************************************/
    125 
    126  FT_CALLBACK_DEF( const char* )
    127  cff_sid_to_glyph_name( void*    face_,  /* TT_Face */
    128                         FT_UInt  idx )
    129  {
    130    TT_Face      face    = (TT_Face)face_;
    131    CFF_Font     cff     = (CFF_Font)face->extra.data;
    132    CFF_Charset  charset = &cff->charset;
    133    FT_UInt      sid     = charset->sids[idx];
    134 
    135 
    136    return cff_index_get_sid_string( cff, sid );
    137  }
    138 
    139 
    140  FT_CALLBACK_DEF( FT_Error )
    141  cff_cmap_unicode_init( FT_CMap      cmap,     /* PS_Unicodes */
    142                         FT_Pointer   pointer )
    143  {
    144    PS_Unicodes         unicodes = (PS_Unicodes)cmap;
    145    TT_Face             face     = (TT_Face)FT_CMAP_FACE( cmap );
    146    FT_Memory           memory   = FT_FACE_MEMORY( face );
    147    CFF_Font            cff      = (CFF_Font)face->extra.data;
    148    CFF_Charset         charset  = &cff->charset;
    149    FT_Service_PsCMaps  psnames  = (FT_Service_PsCMaps)cff->psnames;
    150 
    151    FT_UNUSED( pointer );
    152 
    153 
    154    /* can't build Unicode map for CID-keyed font */
    155    /* because we don't know glyph names.         */
    156    if ( !charset->sids )
    157      return FT_THROW( No_Unicode_Glyph_Name );
    158 
    159    if ( !psnames->unicodes_init )
    160      return FT_THROW( Unimplemented_Feature );
    161 
    162    return psnames->unicodes_init( memory,
    163                                   unicodes,
    164                                   cff->num_glyphs,
    165                                   &cff_sid_to_glyph_name,
    166                                   (PS_FreeGlyphNameFunc)NULL,
    167                                   (FT_Pointer)face );
    168  }
    169 
    170 
    171  FT_CALLBACK_DEF( void )
    172  cff_cmap_unicode_done( FT_CMap  cmap )    /* PS_Unicodes */
    173  {
    174    PS_Unicodes  unicodes = (PS_Unicodes)cmap;
    175    FT_Face      face     = FT_CMAP_FACE( cmap );
    176    FT_Memory    memory   = FT_FACE_MEMORY( face );
    177 
    178 
    179    FT_FREE( unicodes->maps );
    180    unicodes->num_maps = 0;
    181  }
    182 
    183 
    184  FT_CALLBACK_DEF( FT_UInt )
    185  cff_cmap_unicode_char_index( FT_CMap    cmap,       /* PS_Unicodes */
    186                               FT_UInt32  char_code )
    187  {
    188    PS_Unicodes         unicodes = (PS_Unicodes)cmap;
    189    TT_Face             face     = (TT_Face)FT_CMAP_FACE( cmap );
    190    CFF_Font            cff      = (CFF_Font)face->extra.data;
    191    FT_Service_PsCMaps  psnames  = (FT_Service_PsCMaps)cff->psnames;
    192 
    193 
    194    return psnames->unicodes_char_index( unicodes, char_code );
    195  }
    196 
    197 
    198  FT_CALLBACK_DEF( FT_UInt )
    199  cff_cmap_unicode_char_next( FT_CMap     cmap,        /* PS_Unicodes */
    200                              FT_UInt32  *pchar_code )
    201  {
    202    PS_Unicodes         unicodes = (PS_Unicodes)cmap;
    203    TT_Face             face     = (TT_Face)FT_CMAP_FACE( cmap );
    204    CFF_Font            cff      = (CFF_Font)face->extra.data;
    205    FT_Service_PsCMaps  psnames  = (FT_Service_PsCMaps)cff->psnames;
    206 
    207 
    208    return psnames->unicodes_char_next( unicodes, pchar_code );
    209  }
    210 
    211 
    212  FT_DEFINE_CMAP_CLASS(
    213    cff_cmap_unicode_class_rec,
    214 
    215    sizeof ( PS_UnicodesRec ),
    216 
    217    (FT_CMap_InitFunc)     cff_cmap_unicode_init,        /* init       */
    218    (FT_CMap_DoneFunc)     cff_cmap_unicode_done,        /* done       */
    219    (FT_CMap_CharIndexFunc)cff_cmap_unicode_char_index,  /* char_index */
    220    (FT_CMap_CharNextFunc) cff_cmap_unicode_char_next,   /* char_next  */
    221 
    222    (FT_CMap_CharVarIndexFunc)    NULL,  /* char_var_index   */
    223    (FT_CMap_CharVarIsDefaultFunc)NULL,  /* char_var_default */
    224    (FT_CMap_VariantListFunc)     NULL,  /* variant_list     */
    225    (FT_CMap_CharVariantListFunc) NULL,  /* charvariant_list */
    226    (FT_CMap_VariantCharListFunc) NULL   /* variantchar_list */
    227  )
    228 
    229 
    230 /* END */