Home | History | Annotate | Download | only in sfnt
      1 /***************************************************************************/
      2 /*                                                                         */
      3 /*  ttpost.c                                                               */
      4 /*                                                                         */
      5 /*    PostScript name table processing for TrueType and OpenType fonts     */
      6 /*    (body).                                                              */
      7 /*                                                                         */
      8 /*  Copyright 1996-2017 by                                                 */
      9 /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
     10 /*                                                                         */
     11 /*  This file is part of the FreeType project, and may only be used,       */
     12 /*  modified, and distributed under the terms of the FreeType project      */
     13 /*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
     14 /*  this file you indicate that you have read the license and              */
     15 /*  understand and accept it fully.                                        */
     16 /*                                                                         */
     17 /***************************************************************************/
     18 
     19   /*************************************************************************/
     20   /*                                                                       */
     21   /* The post table is not completely loaded by the core engine.  This     */
     22   /* file loads the missing PS glyph names and implements an API to access */
     23   /* them.                                                                 */
     24   /*                                                                       */
     25   /*************************************************************************/
     26 
     27 
     28 #include <ft2build.h>
     29 #include FT_INTERNAL_DEBUG_H
     30 #include FT_INTERNAL_STREAM_H
     31 #include FT_TRUETYPE_TAGS_H
     32 #include "ttpost.h"
     33 
     34 #include "sferrors.h"
     35 
     36 
     37   /*************************************************************************/
     38   /*                                                                       */
     39   /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
     40   /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
     41   /* messages during execution.                                            */
     42   /*                                                                       */
     43 #undef  FT_COMPONENT
     44 #define FT_COMPONENT  trace_ttpost
     45 
     46 
     47   /* If this configuration macro is defined, we rely on the `PSNames' */
     48   /* module to grab the glyph names.                                  */
     49 
     50 #ifdef FT_CONFIG_OPTION_POSTSCRIPT_NAMES
     51 
     52 
     53 #include FT_SERVICE_POSTSCRIPT_CMAPS_H
     54 
     55 #define MAC_NAME( x )  (FT_String*)psnames->macintosh_name( (FT_UInt)(x) )
     56 
     57 
     58 #else /* FT_CONFIG_OPTION_POSTSCRIPT_NAMES */
     59 
     60 
     61    /* Otherwise, we ignore the `PSNames' module, and provide our own  */
     62    /* table of Mac names.  Thus, it is possible to build a version of */
     63    /* FreeType without the Type 1 driver & PSNames module.            */
     64 
     65 #define MAC_NAME( x )  (FT_String*)tt_post_default_names[x]
     66 
     67   /* the 258 default Mac PS glyph names; see file `tools/glnames.py' */
     68 
     69   static const FT_String* const  tt_post_default_names[258] =
     70   {
     71     /*   0 */
     72     ".notdef", ".null", "nonmarkingreturn", "space", "exclam",
     73     "quotedbl", "numbersign", "dollar", "percent", "ampersand",
     74     /*  10 */
     75     "quotesingle", "parenleft", "parenright", "asterisk", "plus",
     76     "comma", "hyphen", "period", "slash", "zero",
     77     /*  20 */
     78     "one", "two", "three", "four", "five",
     79     "six", "seven", "eight", "nine", "colon",
     80     /*  30 */
     81     "semicolon", "less", "equal", "greater", "question",
     82     "at", "A", "B", "C", "D",
     83     /*  40 */
     84     "E", "F", "G", "H", "I",
     85     "J", "K", "L", "M", "N",
     86     /*  50 */
     87     "O", "P", "Q", "R", "S",
     88     "T", "U", "V", "W", "X",
     89     /*  60 */
     90     "Y", "Z", "bracketleft", "backslash", "bracketright",
     91     "asciicircum", "underscore", "grave", "a", "b",
     92     /*  70 */
     93     "c", "d", "e", "f", "g",
     94     "h", "i", "j", "k", "l",
     95     /*  80 */
     96     "m", "n", "o", "p", "q",
     97     "r", "s", "t", "u", "v",
     98     /*  90 */
     99     "w", "x", "y", "z", "braceleft",
    100     "bar", "braceright", "asciitilde", "Adieresis", "Aring",
    101     /* 100 */
    102     "Ccedilla", "Eacute", "Ntilde", "Odieresis", "Udieresis",
    103     "aacute", "agrave", "acircumflex", "adieresis", "atilde",
    104     /* 110 */
    105     "aring", "ccedilla", "eacute", "egrave", "ecircumflex",
    106     "edieresis", "iacute", "igrave", "icircumflex", "idieresis",
    107     /* 120 */
    108     "ntilde", "oacute", "ograve", "ocircumflex", "odieresis",
    109     "otilde", "uacute", "ugrave", "ucircumflex", "udieresis",
    110     /* 130 */
    111     "dagger", "degree", "cent", "sterling", "section",
    112     "bullet", "paragraph", "germandbls", "registered", "copyright",
    113     /* 140 */
    114     "trademark", "acute", "dieresis", "notequal", "AE",
    115     "Oslash", "infinity", "plusminus", "lessequal", "greaterequal",
    116     /* 150 */
    117     "yen", "mu", "partialdiff", "summation", "product",
    118     "pi", "integral", "ordfeminine", "ordmasculine", "Omega",
    119     /* 160 */
    120     "ae", "oslash", "questiondown", "exclamdown", "logicalnot",
    121     "radical", "florin", "approxequal", "Delta", "guillemotleft",
    122     /* 170 */
    123     "guillemotright", "ellipsis", "nonbreakingspace", "Agrave", "Atilde",
    124     "Otilde", "OE", "oe", "endash", "emdash",
    125     /* 180 */
    126     "quotedblleft", "quotedblright", "quoteleft", "quoteright", "divide",
    127     "lozenge", "ydieresis", "Ydieresis", "fraction", "currency",
    128     /* 190 */
    129     "guilsinglleft", "guilsinglright", "fi", "fl", "daggerdbl",
    130     "periodcentered", "quotesinglbase", "quotedblbase", "perthousand", "Acircumflex",
    131     /* 200 */
    132     "Ecircumflex", "Aacute", "Edieresis", "Egrave", "Iacute",
    133     "Icircumflex", "Idieresis", "Igrave", "Oacute", "Ocircumflex",
    134     /* 210 */
    135     "apple", "Ograve", "Uacute", "Ucircumflex", "Ugrave",
    136     "dotlessi", "circumflex", "tilde", "macron", "breve",
    137     /* 220 */
    138     "dotaccent", "ring", "cedilla", "hungarumlaut", "ogonek",
    139     "caron", "Lslash", "lslash", "Scaron", "scaron",
    140     /* 230 */
    141     "Zcaron", "zcaron", "brokenbar", "Eth", "eth",
    142     "Yacute", "yacute", "Thorn", "thorn", "minus",
    143     /* 240 */
    144     "multiply", "onesuperior", "twosuperior", "threesuperior", "onehalf",
    145     "onequarter", "threequarters", "franc", "Gbreve", "gbreve",
    146     /* 250 */
    147     "Idotaccent", "Scedilla", "scedilla", "Cacute", "cacute",
    148     "Ccaron", "ccaron", "dcroat",
    149   };
    150 
    151 
    152 #endif /* FT_CONFIG_OPTION_POSTSCRIPT_NAMES */
    153 
    154 
    155   static FT_Error
    156   load_format_20( TT_Face    face,
    157                   FT_Stream  stream,
    158                   FT_ULong   post_limit )
    159   {
    160     FT_Memory   memory = stream->memory;
    161     FT_Error    error;
    162 
    163     FT_Int      num_glyphs;
    164     FT_UShort   num_names;
    165 
    166     FT_UShort*  glyph_indices = NULL;
    167     FT_Char**   name_strings  = NULL;
    168 
    169 
    170     if ( FT_READ_USHORT( num_glyphs ) )
    171       goto Exit;
    172 
    173     /* UNDOCUMENTED!  The number of glyphs in this table can be smaller */
    174     /* than the value in the maxp table (cf. cyberbit.ttf).             */
    175 
    176     /* There already exist fonts which have more than 32768 glyph names */
    177     /* in this table, so the test for this threshold has been dropped.  */
    178 
    179     if ( num_glyphs > face->max_profile.numGlyphs )
    180     {
    181       error = FT_THROW( Invalid_File_Format );
    182       goto Exit;
    183     }
    184 
    185     /* load the indices */
    186     {
    187       FT_Int  n;
    188 
    189 
    190       if ( FT_NEW_ARRAY ( glyph_indices, num_glyphs ) ||
    191            FT_FRAME_ENTER( num_glyphs * 2L )          )
    192         goto Fail;
    193 
    194       for ( n = 0; n < num_glyphs; n++ )
    195         glyph_indices[n] = FT_GET_USHORT();
    196 
    197       FT_FRAME_EXIT();
    198     }
    199 
    200     /* compute number of names stored in table */
    201     {
    202       FT_Int  n;
    203 
    204 
    205       num_names = 0;
    206 
    207       for ( n = 0; n < num_glyphs; n++ )
    208       {
    209         FT_Int  idx;
    210 
    211 
    212         idx = glyph_indices[n];
    213         if ( idx >= 258 )
    214         {
    215           idx -= 257;
    216           if ( idx > num_names )
    217             num_names = (FT_UShort)idx;
    218         }
    219       }
    220     }
    221 
    222     /* now load the name strings */
    223     {
    224       FT_UShort  n;
    225 
    226 
    227       if ( FT_NEW_ARRAY( name_strings, num_names ) )
    228         goto Fail;
    229 
    230       for ( n = 0; n < num_names; n++ )
    231       {
    232         FT_UInt  len;
    233 
    234 
    235         if ( FT_STREAM_POS() >= post_limit )
    236           break;
    237         else
    238         {
    239           FT_TRACE6(( "load_format_20: %d byte left in post table\n",
    240                       post_limit - FT_STREAM_POS() ));
    241 
    242           if ( FT_READ_BYTE( len ) )
    243             goto Fail1;
    244         }
    245 
    246         if ( len > post_limit                   ||
    247              FT_STREAM_POS() > post_limit - len )
    248         {
    249           FT_Int  d = (FT_Int)post_limit - (FT_Int)FT_STREAM_POS();
    250 
    251 
    252           FT_ERROR(( "load_format_20:"
    253                      " exceeding string length (%d),"
    254                      " truncating at end of post table (%d byte left)\n",
    255                      len, d ));
    256           len = (FT_UInt)FT_MAX( 0, d );
    257         }
    258 
    259         if ( FT_NEW_ARRAY( name_strings[n], len + 1 ) ||
    260              FT_STREAM_READ( name_strings[n], len   ) )
    261           goto Fail1;
    262 
    263         name_strings[n][len] = '\0';
    264       }
    265 
    266       if ( n < num_names )
    267       {
    268         FT_ERROR(( "load_format_20:"
    269                    " all entries in post table are already parsed,"
    270                    " using NULL names for gid %d - %d\n",
    271                     n, num_names - 1 ));
    272         for ( ; n < num_names; n++ )
    273           if ( FT_NEW_ARRAY( name_strings[n], 1 ) )
    274             goto Fail1;
    275           else
    276             name_strings[n][0] = '\0';
    277       }
    278     }
    279 
    280     /* all right, set table fields and exit successfully */
    281     {
    282       TT_Post_20  table = &face->postscript_names.names.format_20;
    283 
    284 
    285       table->num_glyphs    = (FT_UShort)num_glyphs;
    286       table->num_names     = (FT_UShort)num_names;
    287       table->glyph_indices = glyph_indices;
    288       table->glyph_names   = name_strings;
    289     }
    290     return FT_Err_Ok;
    291 
    292   Fail1:
    293     {
    294       FT_UShort  n;
    295 
    296 
    297       for ( n = 0; n < num_names; n++ )
    298         FT_FREE( name_strings[n] );
    299     }
    300 
    301   Fail:
    302     FT_FREE( name_strings );
    303     FT_FREE( glyph_indices );
    304 
    305   Exit:
    306     return error;
    307   }
    308 
    309 
    310   static FT_Error
    311   load_format_25( TT_Face    face,
    312                   FT_Stream  stream,
    313                   FT_ULong   post_limit )
    314   {
    315     FT_Memory  memory = stream->memory;
    316     FT_Error   error;
    317 
    318     FT_Int     num_glyphs;
    319     FT_Char*   offset_table = NULL;
    320 
    321     FT_UNUSED( post_limit );
    322 
    323 
    324     /* UNDOCUMENTED!  This value appears only in the Apple TT specs. */
    325     if ( FT_READ_USHORT( num_glyphs ) )
    326       goto Exit;
    327 
    328     /* check the number of glyphs */
    329     if ( num_glyphs > face->max_profile.numGlyphs ||
    330          num_glyphs > 258                         ||
    331          num_glyphs < 1                           )
    332     {
    333       error = FT_THROW( Invalid_File_Format );
    334       goto Exit;
    335     }
    336 
    337     if ( FT_NEW_ARRAY( offset_table, num_glyphs )   ||
    338          FT_STREAM_READ( offset_table, num_glyphs ) )
    339       goto Fail;
    340 
    341     /* now check the offset table */
    342     {
    343       FT_Int  n;
    344 
    345 
    346       for ( n = 0; n < num_glyphs; n++ )
    347       {
    348         FT_Long  idx = (FT_Long)n + offset_table[n];
    349 
    350 
    351         if ( idx < 0 || idx > num_glyphs )
    352         {
    353           error = FT_THROW( Invalid_File_Format );
    354           goto Fail;
    355         }
    356       }
    357     }
    358 
    359     /* OK, set table fields and exit successfully */
    360     {
    361       TT_Post_25  table = &face->postscript_names.names.format_25;
    362 
    363 
    364       table->num_glyphs = (FT_UShort)num_glyphs;
    365       table->offsets    = offset_table;
    366     }
    367 
    368     return FT_Err_Ok;
    369 
    370   Fail:
    371     FT_FREE( offset_table );
    372 
    373   Exit:
    374     return error;
    375   }
    376 
    377 
    378   static FT_Error
    379   load_post_names( TT_Face  face )
    380   {
    381     FT_Stream  stream;
    382     FT_Error   error;
    383     FT_Fixed   format;
    384     FT_ULong   post_len;
    385     FT_ULong   post_limit;
    386 
    387 
    388     /* get a stream for the face's resource */
    389     stream = face->root.stream;
    390 
    391     /* seek to the beginning of the PS names table */
    392     error = face->goto_table( face, TTAG_post, stream, &post_len );
    393     if ( error )
    394       goto Exit;
    395 
    396     post_limit = FT_STREAM_POS() + post_len;
    397 
    398     format = face->postscript.FormatType;
    399 
    400     /* go to beginning of subtable */
    401     if ( FT_STREAM_SKIP( 32 ) )
    402       goto Exit;
    403 
    404     /* now read postscript table */
    405     if ( format == 0x00020000L )
    406       error = load_format_20( face, stream, post_limit );
    407     else if ( format == 0x00028000L )
    408       error = load_format_25( face, stream, post_limit );
    409     else
    410       error = FT_THROW( Invalid_File_Format );
    411 
    412     face->postscript_names.loaded = 1;
    413 
    414   Exit:
    415     return error;
    416   }
    417 
    418 
    419   FT_LOCAL_DEF( void )
    420   tt_face_free_ps_names( TT_Face  face )
    421   {
    422     FT_Memory      memory = face->root.memory;
    423     TT_Post_Names  names  = &face->postscript_names;
    424     FT_Fixed       format;
    425 
    426 
    427     if ( names->loaded )
    428     {
    429       format = face->postscript.FormatType;
    430 
    431       if ( format == 0x00020000L )
    432       {
    433         TT_Post_20  table = &names->names.format_20;
    434         FT_UShort   n;
    435 
    436 
    437         FT_FREE( table->glyph_indices );
    438         table->num_glyphs = 0;
    439 
    440         for ( n = 0; n < table->num_names; n++ )
    441           FT_FREE( table->glyph_names[n] );
    442 
    443         FT_FREE( table->glyph_names );
    444         table->num_names = 0;
    445       }
    446       else if ( format == 0x00028000L )
    447       {
    448         TT_Post_25  table = &names->names.format_25;
    449 
    450 
    451         FT_FREE( table->offsets );
    452         table->num_glyphs = 0;
    453       }
    454     }
    455     names->loaded = 0;
    456   }
    457 
    458 
    459   /*************************************************************************/
    460   /*                                                                       */
    461   /* <Function>                                                            */
    462   /*    tt_face_get_ps_name                                                */
    463   /*                                                                       */
    464   /* <Description>                                                         */
    465   /*    Get the PostScript glyph name of a glyph.                          */
    466   /*                                                                       */
    467   /* <Input>                                                               */
    468   /*    face   :: A handle to the parent face.                             */
    469   /*                                                                       */
    470   /*    idx    :: The glyph index.                                         */
    471   /*                                                                       */
    472   /* <InOut>                                                               */
    473   /*    PSname :: The address of a string pointer.  Will be NULL in case   */
    474   /*              of error, otherwise it is a pointer to the glyph name.   */
    475   /*                                                                       */
    476   /*              You must not modify the returned string!                 */
    477   /*                                                                       */
    478   /* <Output>                                                              */
    479   /*    FreeType error code.  0 means success.                             */
    480   /*                                                                       */
    481   FT_LOCAL_DEF( FT_Error )
    482   tt_face_get_ps_name( TT_Face      face,
    483                        FT_UInt      idx,
    484                        FT_String**  PSname )
    485   {
    486     FT_Error       error;
    487     TT_Post_Names  names;
    488     FT_Fixed       format;
    489 
    490 #ifdef FT_CONFIG_OPTION_POSTSCRIPT_NAMES
    491     FT_Service_PsCMaps  psnames;
    492 #endif
    493 
    494 
    495     if ( !face )
    496       return FT_THROW( Invalid_Face_Handle );
    497 
    498     if ( idx >= (FT_UInt)face->max_profile.numGlyphs )
    499       return FT_THROW( Invalid_Glyph_Index );
    500 
    501 #ifdef FT_CONFIG_OPTION_POSTSCRIPT_NAMES
    502     psnames = (FT_Service_PsCMaps)face->psnames;
    503     if ( !psnames )
    504       return FT_THROW( Unimplemented_Feature );
    505 #endif
    506 
    507     names = &face->postscript_names;
    508 
    509     /* `.notdef' by default */
    510     *PSname = MAC_NAME( 0 );
    511 
    512     format = face->postscript.FormatType;
    513 
    514     if ( format == 0x00010000L )
    515     {
    516       if ( idx < 258 )                    /* paranoid checking */
    517         *PSname = MAC_NAME( idx );
    518     }
    519     else if ( format == 0x00020000L )
    520     {
    521       TT_Post_20  table = &names->names.format_20;
    522 
    523 
    524       if ( !names->loaded )
    525       {
    526         error = load_post_names( face );
    527         if ( error )
    528           goto End;
    529       }
    530 
    531       if ( idx < (FT_UInt)table->num_glyphs )
    532       {
    533         FT_UShort  name_index = table->glyph_indices[idx];
    534 
    535 
    536         if ( name_index < 258 )
    537           *PSname = MAC_NAME( name_index );
    538         else
    539           *PSname = (FT_String*)table->glyph_names[name_index - 258];
    540       }
    541     }
    542     else if ( format == 0x00028000L )
    543     {
    544       TT_Post_25  table = &names->names.format_25;
    545 
    546 
    547       if ( !names->loaded )
    548       {
    549         error = load_post_names( face );
    550         if ( error )
    551           goto End;
    552       }
    553 
    554       if ( idx < (FT_UInt)table->num_glyphs )    /* paranoid checking */
    555         *PSname = MAC_NAME( (FT_Int)idx + table->offsets[idx] );
    556     }
    557 
    558     /* nothing to do for format == 0x00030000L */
    559 
    560   End:
    561     return FT_Err_Ok;
    562   }
    563 
    564 
    565 /* END */
    566