tor-browser

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

cffobjs.c (35007B)


      1 /****************************************************************************
      2 *
      3 * cffobjs.c
      4 *
      5 *   OpenType objects manager (body).
      6 *
      7 * Copyright (C) 1996-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 
     20 #include <freetype/internal/ftdebug.h>
     21 #include <freetype/internal/ftcalc.h>
     22 #include <freetype/internal/ftstream.h>
     23 #include <freetype/fterrors.h>
     24 #include <freetype/ttnameid.h>
     25 #include <freetype/tttags.h>
     26 #include <freetype/internal/sfnt.h>
     27 #include <freetype/ftdriver.h>
     28 
     29 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
     30 #include <freetype/ftmm.h>
     31 #include <freetype/internal/services/svmm.h>
     32 #include <freetype/internal/services/svmetric.h>
     33 #endif
     34 
     35 #include <freetype/internal/cffotypes.h>
     36 #include "cffobjs.h"
     37 #include "cffload.h"
     38 #include "cffcmap.h"
     39 
     40 #include "cfferrs.h"
     41 
     42 #include <freetype/internal/psaux.h>
     43 #include <freetype/internal/services/svcfftl.h>
     44 
     45 #define CFF_fixedToInt( x )                          \
     46          ( (FT_Short)( ( (x) + 0x8000U ) >> 16 ) )
     47 
     48  /**************************************************************************
     49   *
     50   * The macro FT_COMPONENT is used in trace mode.  It is an implicit
     51   * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
     52   * messages during execution.
     53   */
     54 #undef  FT_COMPONENT
     55 #define FT_COMPONENT  cffobjs
     56 
     57 
     58  /**************************************************************************
     59   *
     60   *                           SIZE FUNCTIONS
     61   *
     62   */
     63 
     64 
     65  static PSH_Globals_Funcs
     66  cff_size_get_globals_funcs( CFF_Size  size )
     67  {
     68    CFF_Face          face     = (CFF_Face)size->root.face;
     69    CFF_Font          font     = (CFF_Font)face->extra.data;
     70    PSHinter_Service  pshinter = font->pshinter;
     71    FT_Module         module;
     72 
     73 
     74    module = FT_Get_Module( font->library, "pshinter" );
     75 
     76    return ( module && pshinter && pshinter->get_globals_funcs )
     77           ? pshinter->get_globals_funcs( module )
     78           : 0;
     79  }
     80 
     81 
     82  FT_LOCAL_DEF( void )
     83  cff_size_done( FT_Size  cffsize )        /* CFF_Size */
     84  {
     85    FT_Memory     memory   = cffsize->face->memory;
     86    CFF_Size      size     = (CFF_Size)cffsize;
     87    CFF_Face      face     = (CFF_Face)size->root.face;
     88    CFF_Font      font     = (CFF_Font)face->extra.data;
     89    CFF_Internal  internal = (CFF_Internal)cffsize->internal->module_data;
     90 
     91 
     92    if ( internal )
     93    {
     94      PSH_Globals_Funcs  funcs;
     95 
     96 
     97      funcs = cff_size_get_globals_funcs( size );
     98      if ( funcs )
     99      {
    100        FT_UInt  i;
    101 
    102 
    103        funcs->destroy( internal->topfont );
    104 
    105        for ( i = font->num_subfonts; i > 0; i-- )
    106          funcs->destroy( internal->subfonts[i - 1] );
    107      }
    108 
    109      FT_FREE( internal );
    110    }
    111  }
    112 
    113 
    114  /* CFF and Type 1 private dictionaries have slightly different      */
    115  /* structures; we need to synthesize a Type 1 dictionary on the fly */
    116 
    117  static void
    118  cff_make_private_dict( CFF_SubFont  subfont,
    119                         PS_Private   priv )
    120  {
    121    CFF_Private  cpriv = &subfont->private_dict;
    122    FT_UInt      n, count;
    123 
    124 
    125    FT_ZERO( priv );
    126 
    127    count = priv->num_blue_values = cpriv->num_blue_values;
    128    for ( n = 0; n < count; n++ )
    129      priv->blue_values[n] = CFF_fixedToInt( cpriv->blue_values[n] );
    130 
    131    count = priv->num_other_blues = cpriv->num_other_blues;
    132    for ( n = 0; n < count; n++ )
    133      priv->other_blues[n] = CFF_fixedToInt( cpriv->other_blues[n] );
    134 
    135    count = priv->num_family_blues = cpriv->num_family_blues;
    136    for ( n = 0; n < count; n++ )
    137      priv->family_blues[n] = CFF_fixedToInt( cpriv->family_blues[n] );
    138 
    139    count = priv->num_family_other_blues = cpriv->num_family_other_blues;
    140    for ( n = 0; n < count; n++ )
    141      priv->family_other_blues[n] =
    142        CFF_fixedToInt( cpriv->family_other_blues[n] );
    143 
    144    priv->blue_scale = cpriv->blue_scale;
    145    priv->blue_shift = (FT_Int)cpriv->blue_shift;
    146    priv->blue_fuzz  = (FT_Int)cpriv->blue_fuzz;
    147 
    148    priv->standard_width[0]  = (FT_UShort)cpriv->standard_width;
    149    priv->standard_height[0] = (FT_UShort)cpriv->standard_height;
    150 
    151    count = priv->num_snap_widths = cpriv->num_snap_widths;
    152    for ( n = 0; n < count; n++ )
    153      priv->snap_widths[n] = (FT_Short)cpriv->snap_widths[n];
    154 
    155    count = priv->num_snap_heights = cpriv->num_snap_heights;
    156    for ( n = 0; n < count; n++ )
    157      priv->snap_heights[n] = (FT_Short)cpriv->snap_heights[n];
    158 
    159    priv->force_bold     = cpriv->force_bold;
    160    priv->language_group = cpriv->language_group;
    161    priv->lenIV          = cpriv->lenIV;
    162  }
    163 
    164 
    165  FT_LOCAL_DEF( FT_Error )
    166  cff_size_init( FT_Size  cffsize )         /* CFF_Size */
    167  {
    168    CFF_Size           size  = (CFF_Size)cffsize;
    169    FT_Error           error = FT_Err_Ok;
    170    PSH_Globals_Funcs  funcs = cff_size_get_globals_funcs( size );
    171 
    172    FT_Memory     memory   = cffsize->face->memory;
    173    CFF_Internal  internal = NULL;
    174    CFF_Face      face     = (CFF_Face)cffsize->face;
    175    CFF_Font      font     = (CFF_Font)face->extra.data;
    176 
    177    PS_PrivateRec priv;
    178 
    179    FT_UInt       i;
    180 
    181    if ( !funcs )
    182      goto Exit;
    183 
    184    if ( FT_NEW( internal ) )
    185      goto Exit;
    186 
    187    cff_make_private_dict( &font->top_font, &priv );
    188    error = funcs->create( memory, &priv, &internal->topfont );
    189    if ( error )
    190      goto Exit;
    191 
    192    for ( i = font->num_subfonts; i > 0; i-- )
    193    {
    194      CFF_SubFont  sub = font->subfonts[i - 1];
    195 
    196 
    197      cff_make_private_dict( sub, &priv );
    198      error = funcs->create( memory, &priv, &internal->subfonts[i - 1] );
    199      if ( error )
    200        goto Exit;
    201    }
    202 
    203    cffsize->internal->module_data = internal;
    204 
    205    size->strike_index = 0xFFFFFFFFUL;
    206 
    207  Exit:
    208    if ( error )
    209    {
    210      if ( internal )
    211      {
    212        for ( i = font->num_subfonts; i > 0; i-- )
    213          FT_FREE( internal->subfonts[i - 1] );
    214        FT_FREE( internal->topfont );
    215      }
    216 
    217      FT_FREE( internal );
    218    }
    219 
    220    return error;
    221  }
    222 
    223 
    224 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
    225 
    226  FT_LOCAL_DEF( FT_Error )
    227  cff_size_select( FT_Size   size,
    228                   FT_ULong  strike_index )
    229  {
    230    CFF_Size           cffsize = (CFF_Size)size;
    231    PSH_Globals_Funcs  funcs;
    232 
    233 
    234    cffsize->strike_index = strike_index;
    235 
    236    FT_Select_Metrics( size->face, strike_index );
    237 
    238    funcs = cff_size_get_globals_funcs( cffsize );
    239 
    240    if ( funcs )
    241    {
    242      CFF_Face      face     = (CFF_Face)size->face;
    243      CFF_Font      font     = (CFF_Font)face->extra.data;
    244      CFF_Internal  internal = (CFF_Internal)size->internal->module_data;
    245 
    246      FT_Long  top_upm  = (FT_Long)font->top_font.font_dict.units_per_em;
    247      FT_UInt  i;
    248 
    249 
    250      funcs->set_scale( internal->topfont,
    251                        size->metrics.x_scale, size->metrics.y_scale,
    252                        0, 0 );
    253 
    254      for ( i = font->num_subfonts; i > 0; i-- )
    255      {
    256        CFF_SubFont  sub     = font->subfonts[i - 1];
    257        FT_Long      sub_upm = (FT_Long)sub->font_dict.units_per_em;
    258        FT_Pos       x_scale, y_scale;
    259 
    260 
    261        if ( top_upm != sub_upm )
    262        {
    263          x_scale = FT_MulDiv( size->metrics.x_scale, top_upm, sub_upm );
    264          y_scale = FT_MulDiv( size->metrics.y_scale, top_upm, sub_upm );
    265        }
    266        else
    267        {
    268          x_scale = size->metrics.x_scale;
    269          y_scale = size->metrics.y_scale;
    270        }
    271 
    272        funcs->set_scale( internal->subfonts[i - 1],
    273                          x_scale, y_scale, 0, 0 );
    274      }
    275    }
    276 
    277    return FT_Err_Ok;
    278  }
    279 
    280 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
    281 
    282 
    283  FT_LOCAL_DEF( FT_Error )
    284  cff_size_request( FT_Size          size,
    285                    FT_Size_Request  req )
    286  {
    287    FT_Error  error;
    288 
    289    CFF_Size           cffsize = (CFF_Size)size;
    290    PSH_Globals_Funcs  funcs;
    291 
    292 
    293 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
    294 
    295    if ( FT_HAS_FIXED_SIZES( size->face ) )
    296    {
    297      CFF_Face      cffface = (CFF_Face)size->face;
    298      SFNT_Service  sfnt    = (SFNT_Service)cffface->sfnt;
    299      FT_ULong      strike_index;
    300 
    301 
    302      if ( sfnt->set_sbit_strike( cffface, req, &strike_index ) )
    303        cffsize->strike_index = 0xFFFFFFFFUL;
    304      else
    305        return cff_size_select( size, strike_index );
    306    }
    307 
    308 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
    309 
    310    error = FT_Request_Metrics( size->face, req );
    311    if ( error )
    312      goto Exit;
    313 
    314    funcs = cff_size_get_globals_funcs( cffsize );
    315 
    316    if ( funcs )
    317    {
    318      CFF_Face      cffface  = (CFF_Face)size->face;
    319      CFF_Font      font     = (CFF_Font)cffface->extra.data;
    320      CFF_Internal  internal = (CFF_Internal)size->internal->module_data;
    321 
    322      FT_Long  top_upm  = (FT_Long)font->top_font.font_dict.units_per_em;
    323      FT_UInt  i;
    324 
    325 
    326      funcs->set_scale( internal->topfont,
    327                        size->metrics.x_scale, size->metrics.y_scale,
    328                        0, 0 );
    329 
    330      for ( i = font->num_subfonts; i > 0; i-- )
    331      {
    332        CFF_SubFont  sub     = font->subfonts[i - 1];
    333        FT_Long      sub_upm = (FT_Long)sub->font_dict.units_per_em;
    334        FT_Pos       x_scale, y_scale;
    335 
    336 
    337        if ( top_upm != sub_upm )
    338        {
    339          x_scale = FT_MulDiv( size->metrics.x_scale, top_upm, sub_upm );
    340          y_scale = FT_MulDiv( size->metrics.y_scale, top_upm, sub_upm );
    341        }
    342        else
    343        {
    344          x_scale = size->metrics.x_scale;
    345          y_scale = size->metrics.y_scale;
    346        }
    347 
    348        funcs->set_scale( internal->subfonts[i - 1],
    349                          x_scale, y_scale, 0, 0 );
    350      }
    351    }
    352 
    353  Exit:
    354    return error;
    355  }
    356 
    357 
    358  /**************************************************************************
    359   *
    360   *                           SLOT  FUNCTIONS
    361   *
    362   */
    363 
    364  FT_LOCAL_DEF( void )
    365  cff_slot_done( FT_GlyphSlot  slot )
    366  {
    367    if ( slot->internal )
    368      slot->internal->glyph_hints = NULL;
    369  }
    370 
    371 
    372  FT_LOCAL_DEF( FT_Error )
    373  cff_slot_init( FT_GlyphSlot  slot )
    374  {
    375    CFF_Face          face     = (CFF_Face)slot->face;
    376    CFF_Font          font     = (CFF_Font)face->extra.data;
    377    PSHinter_Service  pshinter = font->pshinter;
    378 
    379 
    380    if ( pshinter )
    381    {
    382      FT_Module  module;
    383 
    384 
    385      module = FT_Get_Module( slot->library, "pshinter" );
    386      if ( module )
    387      {
    388        T2_Hints_Funcs  funcs;
    389 
    390 
    391        funcs = pshinter->get_t2_funcs( module );
    392        slot->internal->glyph_hints = (void*)funcs;
    393      }
    394    }
    395 
    396    return FT_Err_Ok;
    397  }
    398 
    399 
    400  /**************************************************************************
    401   *
    402   *                          FACE  FUNCTIONS
    403   *
    404   */
    405 
    406  static FT_String*
    407  cff_strcpy( FT_Memory         memory,
    408              const FT_String*  source )
    409  {
    410    FT_Error    error;
    411    FT_String*  result;
    412 
    413 
    414    FT_MEM_STRDUP( result, source );
    415 
    416    return result;
    417  }
    418 
    419 
    420  /* Strip all subset prefixes of the form `ABCDEF+'.  Usually, there */
    421  /* is only one, but font names like `APCOOG+JFABTD+FuturaBQ-Bold'   */
    422  /* have been seen in the wild.                                      */
    423 
    424  static void
    425  remove_subset_prefix( FT_String*  name )
    426  {
    427    FT_UInt32  i = 0, idx = 0;
    428 
    429 
    430    /* six ASCII uppercase letters followed by a plus sign */
    431    while ( 'A' <= name[i] && name[i++] <= 'Z' &&
    432            'A' <= name[i] && name[i++] <= 'Z' &&
    433            'A' <= name[i] && name[i++] <= 'Z' &&
    434            'A' <= name[i] && name[i++] <= 'Z' &&
    435            'A' <= name[i] && name[i++] <= 'Z' &&
    436            'A' <= name[i] && name[i++] <= 'Z' &&
    437                              name[i++] == '+' )
    438    {
    439      idx = i;
    440    }
    441 
    442    if ( idx )
    443      FT_MEM_MOVE( name, name + idx, ft_strlen( name + idx ) + 1 );
    444  }
    445 
    446 
    447  /* Remove the style part from the family name (if present). */
    448 
    449  static void
    450  remove_style( FT_String*        family_name,
    451                const FT_String*  style_name )
    452  {
    453    FT_String*        f = family_name + ft_strlen( family_name );
    454    const FT_String*  s =  style_name + ft_strlen(  style_name );
    455 
    456 
    457    /* compare strings moving backwards */
    458    while ( s > style_name )
    459      if ( f == family_name || *--s != *--f )
    460        return;
    461 
    462    /* terminate and remove special characters */
    463    do
    464      *f = '\0';
    465    while ( f-- > family_name                                    &&
    466            ( *f == '-' || *f == ' ' || *f == '_' || *f == '+' ) );
    467  }
    468 
    469 
    470  FT_LOCAL_DEF( FT_Error )
    471  cff_face_init( FT_Stream      stream,
    472                 FT_Face        cffface,        /* CFF_Face */
    473                 FT_Int         face_index,
    474                 FT_Int         num_params,
    475                 FT_Parameter*  params )
    476  {
    477    CFF_Face            face        = (CFF_Face)cffface;
    478    FT_Error            error;
    479    SFNT_Service        sfnt;
    480    FT_Service_PsCMaps  psnames;
    481    PSHinter_Service    pshinter;
    482    PSAux_Service       psaux;
    483    FT_Service_CFFLoad  cffload;
    484    FT_Bool             pure_cff    = 1;
    485    FT_Bool             cff2        = 0;
    486    FT_Bool             sfnt_format = 0;
    487    FT_Library          library     = cffface->driver->root.library;
    488 
    489 
    490    sfnt = (SFNT_Service)FT_Get_Module_Interface( library,
    491                                                  "sfnt" );
    492    if ( !sfnt )
    493    {
    494      FT_ERROR(( "cff_face_init: cannot access `sfnt' module\n" ));
    495      error = FT_THROW( Missing_Module );
    496      goto Exit;
    497    }
    498 
    499    FT_FACE_FIND_GLOBAL_SERVICE( face, psnames, POSTSCRIPT_CMAPS );
    500 
    501    pshinter = (PSHinter_Service)FT_Get_Module_Interface( library,
    502                                                          "pshinter" );
    503 
    504    psaux = (PSAux_Service)FT_Get_Module_Interface( library,
    505                                                    "psaux" );
    506    if ( !psaux )
    507    {
    508      FT_ERROR(( "cff_face_init: cannot access `psaux' module\n" ));
    509      error = FT_THROW( Missing_Module );
    510      goto Exit;
    511    }
    512    face->psaux = psaux;
    513 
    514    FT_FACE_FIND_GLOBAL_SERVICE( face, cffload, CFF_LOAD );
    515 
    516    FT_TRACE2(( "CFF driver\n" ));
    517 
    518    /* create input stream from resource */
    519    if ( FT_STREAM_SEEK( 0 ) )
    520      goto Exit;
    521 
    522    /* check whether we have a valid OpenType file */
    523    FT_TRACE2(( "  " ));
    524    error = sfnt->init_face( stream, face, face_index, num_params, params );
    525    if ( !error )
    526    {
    527      if ( face->format_tag != TTAG_OTTO )  /* `OTTO'; OpenType/CFF font */
    528      {
    529        FT_TRACE2(( "  not an OpenType/CFF font\n" ));
    530        error = FT_THROW( Unknown_File_Format );
    531        goto Exit;
    532      }
    533 
    534      /* if we are performing a simple font format check, exit immediately */
    535      if ( face_index < 0 )
    536        return FT_Err_Ok;
    537 
    538      sfnt_format = 1;
    539 
    540      /* the font may be OpenType/CFF, SVG CEF, or sfnt/CFF; a `head' table */
    541      /* implies OpenType/CFF, otherwise just look for an optional cmap     */
    542      error = face->goto_table( face, TTAG_head, stream, 0 );
    543      if ( !error )
    544      {
    545        pure_cff = 0;
    546 
    547        /* load font directory */
    548        error = sfnt->load_face( stream, face, face_index,
    549                                 num_params, params );
    550        if ( error )
    551          goto Exit;
    552      }
    553      else
    554      {
    555        /* load the `cmap' table explicitly */
    556        error = sfnt->load_cmap( face, stream );
    557 
    558        /* this may fail because CID-keyed fonts don't have a cmap */
    559        if ( FT_ERR_NEQ( error, Table_Missing ) && FT_ERR_NEQ( error, Ok ) )
    560          goto Exit;
    561      }
    562 
    563      /* now load the CFF part of the file; */
    564      /* give priority to CFF2              */
    565      error = face->goto_table( face, TTAG_CFF2, stream, 0 );
    566      if ( !error )
    567      {
    568        cff2          = 1;
    569        face->is_cff2 = cff2;
    570      }
    571 
    572      if ( FT_ERR_EQ( error, Table_Missing ) )
    573        error = face->goto_table( face, TTAG_CFF, stream, 0 );
    574 
    575      if ( error )
    576        goto Exit;
    577    }
    578    else
    579    {
    580      /* rewind to start of file; we are going to load a pure-CFF font */
    581      if ( FT_STREAM_SEEK( 0 ) )
    582        goto Exit;
    583      error = FT_Err_Ok;
    584    }
    585 
    586    /* now load and parse the CFF table in the file */
    587    {
    588      CFF_Font         cff = NULL;
    589      CFF_FontRecDict  dict;
    590      FT_Memory        memory = cffface->memory;
    591      FT_Int32         flags;
    592      FT_UInt          i;
    593 
    594 
    595      if ( FT_NEW( cff ) )
    596        goto Exit;
    597 
    598      face->extra.data = cff;
    599      error = cff_font_load( library,
    600                             stream,
    601                             face_index,
    602                             cff,
    603                             face,
    604                             pure_cff,
    605                             cff2 );
    606      if ( error )
    607        goto Exit;
    608 
    609      /* if we are performing a simple font format check, exit immediately */
    610      /* (this is here for pure CFF)                                       */
    611      if ( face_index < 0 )
    612      {
    613        cffface->num_faces = (FT_Long)cff->num_faces;
    614        return FT_Err_Ok;
    615      }
    616 
    617      cff->pshinter = pshinter;
    618      cff->psnames  = psnames;
    619      cff->cffload  = cffload;
    620 
    621      cffface->face_index = face_index & 0xFFFF;
    622 
    623      /* Complement the root flags with some interesting information. */
    624      /* Note that this is only necessary for pure CFF and CEF fonts; */
    625      /* SFNT based fonts use the `name' table instead.               */
    626 
    627      cffface->num_glyphs = (FT_Long)cff->num_glyphs;
    628 
    629      dict = &cff->top_font.font_dict;
    630 
    631      /* we need the `psnames' module for CFF and CEF formats */
    632      /* which aren't CID-keyed                               */
    633      if ( dict->cid_registry == 0xFFFFU && !psnames )
    634      {
    635        FT_ERROR(( "cff_face_init:"
    636                   " cannot open CFF & CEF fonts\n" ));
    637        FT_ERROR(( "              "
    638                   " without the `psnames' module\n" ));
    639        error = FT_THROW( Missing_Module );
    640        goto Exit;
    641      }
    642 
    643 #ifdef FT_DEBUG_LEVEL_TRACE
    644      {
    645        FT_UInt     idx;
    646        FT_String*  s;
    647 
    648 
    649        FT_TRACE4(( "SIDs\n" ));
    650 
    651        /* dump string index, including default strings for convenience */
    652        for ( idx = 0; idx <= 390; idx++ )
    653        {
    654          s = cff_index_get_sid_string( cff, idx );
    655          if ( s )
    656            FT_TRACE4(( "  %5u %s\n", idx, s ));
    657        }
    658 
    659        /* In Multiple Master CFFs, two SIDs hold the Normalize Design  */
    660        /* Vector (NDV) and Convert Design Vector (CDV) charstrings,    */
    661        /* which may contain null bytes in the middle of the data, too. */
    662        /* We thus access `cff->strings' directly.                      */
    663        for ( idx = 1; idx < cff->num_strings; idx++ )
    664        {
    665          FT_Byte*    s1    = cff->strings[idx - 1];
    666          FT_Byte*    s2    = cff->strings[idx];
    667          FT_PtrDist  s1len = s2 - s1 - 1; /* without the final null byte */
    668          FT_PtrDist  l;
    669 
    670 
    671          FT_TRACE4(( "  %5u ", idx + 390 ));
    672          for ( l = 0; l < s1len; l++ )
    673            FT_TRACE4(( "%c", s1[l] ));
    674          FT_TRACE4(( "\n" ));
    675        }
    676 
    677        /* print last element */
    678        if ( cff->num_strings )
    679        {
    680          FT_Byte*    s1    = cff->strings[cff->num_strings - 1];
    681          FT_Byte*    s2    = cff->string_pool + cff->string_pool_size;
    682          FT_PtrDist  s1len = s2 - s1 - 1;
    683          FT_PtrDist  l;
    684 
    685 
    686          FT_TRACE4(( "  %5u ", cff->num_strings + 390 ));
    687          for ( l = 0; l < s1len; l++ )
    688            FT_TRACE4(( "%c", s1[l] ));
    689          FT_TRACE4(( "\n" ));
    690        }
    691      }
    692 #endif /* FT_DEBUG_LEVEL_TRACE */
    693 
    694 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
    695      {
    696        FT_UInt  instance_index = (FT_UInt)face_index >> 16;
    697 
    698 
    699        if ( FT_HAS_MULTIPLE_MASTERS( cffface ) )
    700        {
    701          error = FT_Set_Named_Instance( cffface, instance_index );
    702          if ( error )
    703            goto Exit;
    704        }
    705      }
    706 #endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
    707 
    708      if ( !dict->has_font_matrix )
    709        dict->units_per_em = pure_cff ? 1000 : face->root.units_per_EM;
    710 
    711      /* Normalize the font matrix so that `matrix->yy' is 1; if  */
    712      /* it is zero, we use `matrix->yx' instead.  The scaling is */
    713      /* done with `units_per_em' then (at this point, it already */
    714      /* contains the scaling factor, but without normalization   */
    715      /* of the matrix).                                          */
    716      /*                                                          */
    717      /* Note that the offsets must be expressed in integer font  */
    718      /* units.                                                   */
    719 
    720      {
    721        FT_Matrix*  matrix = &dict->font_matrix;
    722        FT_Vector*  offset = &dict->font_offset;
    723        FT_ULong*   upm    = &dict->units_per_em;
    724        FT_Fixed    temp;
    725 
    726 
    727        temp = matrix->yy ? FT_ABS( matrix->yy )
    728                          : FT_ABS( matrix->yx );
    729 
    730        if ( temp != 0x10000L )
    731        {
    732          *upm = (FT_ULong)FT_DivFix( (FT_Long)*upm, temp );
    733 
    734          matrix->xx = FT_DivFix( matrix->xx, temp );
    735          matrix->yx = FT_DivFix( matrix->yx, temp );
    736          matrix->xy = FT_DivFix( matrix->xy, temp );
    737          matrix->yy = FT_DivFix( matrix->yy, temp );
    738          offset->x  = FT_DivFix( offset->x,  temp );
    739          offset->y  = FT_DivFix( offset->y,  temp );
    740        }
    741 
    742        offset->x >>= 16;
    743        offset->y >>= 16;
    744      }
    745 
    746      for ( i = cff->num_subfonts; i > 0; i-- )
    747      {
    748        CFF_FontRecDict  sub = &cff->subfonts[i - 1]->font_dict;
    749        CFF_FontRecDict  top = &cff->top_font.font_dict;
    750 
    751        FT_Matrix*  matrix;
    752        FT_Vector*  offset;
    753        FT_ULong*   upm;
    754        FT_Fixed    temp;
    755 
    756 
    757        if ( sub->has_font_matrix )
    758        {
    759          FT_Long  scaling;
    760 
    761 
    762          /* if we have a top-level matrix, */
    763          /* concatenate the subfont matrix */
    764 
    765          if ( top->has_font_matrix )
    766          {
    767            if ( top->units_per_em > 1 && sub->units_per_em > 1 )
    768              scaling = (FT_Long)FT_MIN( top->units_per_em,
    769                                         sub->units_per_em );
    770            else
    771              scaling = 1;
    772 
    773            FT_Matrix_Multiply_Scaled( &top->font_matrix,
    774                                       &sub->font_matrix,
    775                                       scaling );
    776            FT_Vector_Transform_Scaled( &sub->font_offset,
    777                                        &top->font_matrix,
    778                                        scaling );
    779 
    780            sub->units_per_em = (FT_ULong)
    781                                  FT_MulDiv( (FT_Long)sub->units_per_em,
    782                                             (FT_Long)top->units_per_em,
    783                                             scaling );
    784          }
    785        }
    786        else
    787        {
    788          sub->font_matrix = top->font_matrix;
    789          sub->font_offset = top->font_offset;
    790 
    791          sub->units_per_em = top->units_per_em;
    792        }
    793 
    794        matrix = &sub->font_matrix;
    795        offset = &sub->font_offset;
    796        upm    = &sub->units_per_em;
    797 
    798        temp = matrix->yy ? FT_ABS( matrix->yy )
    799                          : FT_ABS( matrix->yx );
    800 
    801 
    802        if ( temp != 0x10000L )
    803        {
    804          *upm = (FT_ULong)FT_DivFix( (FT_Long)*upm, temp );
    805 
    806          matrix->xx = FT_DivFix( matrix->xx, temp );
    807          matrix->yx = FT_DivFix( matrix->yx, temp );
    808          matrix->xy = FT_DivFix( matrix->xy, temp );
    809          matrix->yy = FT_DivFix( matrix->yy, temp );
    810          offset->x  = FT_DivFix( offset->x,  temp );
    811          offset->y  = FT_DivFix( offset->y,  temp );
    812        }
    813 
    814        offset->x >>= 16;
    815        offset->y >>= 16;
    816      }
    817 
    818      if ( pure_cff )
    819      {
    820        char*  style_name = NULL;
    821 
    822 
    823        /* set up num_faces */
    824        cffface->num_faces = (FT_Long)cff->num_faces;
    825 
    826        /* compute number of glyphs */
    827        if ( dict->cid_registry != 0xFFFFU )
    828          cffface->num_glyphs = (FT_Long)( cff->charset.max_cid + 1 );
    829        else
    830          cffface->num_glyphs = (FT_Long)cff->charstrings_index.count;
    831 
    832        /* set global bbox, as well as EM size */
    833        cffface->bbox.xMin =   dict->font_bbox.xMin            >> 16;
    834        cffface->bbox.yMin =   dict->font_bbox.yMin            >> 16;
    835        /* no `U' suffix here to 0xFFFF! */
    836        cffface->bbox.xMax = ( dict->font_bbox.xMax + 0xFFFF ) >> 16;
    837        cffface->bbox.yMax = ( dict->font_bbox.yMax + 0xFFFF ) >> 16;
    838 
    839        cffface->units_per_EM = (FT_UShort)( dict->units_per_em );
    840 
    841        cffface->ascender  = (FT_Short)( cffface->bbox.yMax );
    842        cffface->descender = (FT_Short)( cffface->bbox.yMin );
    843 
    844        cffface->height = (FT_Short)( ( cffface->units_per_EM * 12 ) / 10 );
    845        if ( cffface->height < cffface->ascender - cffface->descender )
    846          cffface->height = (FT_Short)( cffface->ascender -
    847                                        cffface->descender );
    848 
    849        cffface->underline_position  =
    850          (FT_Short)( dict->underline_position >> 16 );
    851        cffface->underline_thickness =
    852          (FT_Short)( dict->underline_thickness >> 16 );
    853 
    854        /* retrieve font family & style name */
    855        if ( dict->family_name )
    856        {
    857          char*  family_name;
    858 
    859 
    860          family_name = cff_index_get_sid_string( cff, dict->family_name );
    861          if ( family_name )
    862            cffface->family_name = cff_strcpy( memory, family_name );
    863        }
    864 
    865        if ( !cffface->family_name )
    866        {
    867          cffface->family_name = cff_index_get_name(
    868                                   cff,
    869                                   (FT_UInt)( face_index & 0xFFFF ) );
    870          if ( cffface->family_name )
    871            remove_subset_prefix( cffface->family_name );
    872        }
    873 
    874        if ( cffface->family_name )
    875        {
    876          char*  full   = cff_index_get_sid_string( cff,
    877                                                    dict->full_name );
    878          char*  fullp  = full;
    879          char*  family = cffface->family_name;
    880 
    881 
    882          /* We try to extract the style name from the full name.   */
    883          /* We need to ignore spaces and dashes during the search. */
    884          if ( full && family )
    885          {
    886            while ( *fullp )
    887            {
    888              /* skip common characters at the start of both strings */
    889              if ( *fullp == *family )
    890              {
    891                family++;
    892                fullp++;
    893                continue;
    894              }
    895 
    896              /* ignore spaces and dashes in full name during comparison */
    897              if ( *fullp == ' ' || *fullp == '-' )
    898              {
    899                fullp++;
    900                continue;
    901              }
    902 
    903              /* ignore spaces and dashes in family name during comparison */
    904              if ( *family == ' ' || *family == '-' )
    905              {
    906                family++;
    907                continue;
    908              }
    909 
    910              if ( !*family && *fullp )
    911              {
    912                /* The full name begins with the same characters as the  */
    913                /* family name, with spaces and dashes removed.  In this */
    914                /* case, the remaining string in `fullp' will be used as */
    915                /* the style name.                                       */
    916                style_name = cff_strcpy( memory, fullp );
    917 
    918                /* remove the style part from the family name (if present) */
    919                if ( style_name )
    920                  remove_style( cffface->family_name, style_name );
    921              }
    922              break;
    923            }
    924          }
    925        }
    926        else
    927        {
    928          char  *cid_font_name =
    929                   cff_index_get_sid_string( cff,
    930                                             dict->cid_font_name );
    931 
    932 
    933          /* do we have a `/FontName' for a CID-keyed font? */
    934          if ( cid_font_name )
    935            cffface->family_name = cff_strcpy( memory, cid_font_name );
    936        }
    937 
    938        if ( style_name )
    939          cffface->style_name = style_name;
    940        else
    941          /* assume "Regular" style if we don't know better */
    942          cffface->style_name = cff_strcpy( memory, "Regular" );
    943 
    944        /********************************************************************
    945         *
    946         * Compute face flags.
    947         */
    948        flags = FT_FACE_FLAG_SCALABLE   | /* scalable outlines */
    949                FT_FACE_FLAG_HORIZONTAL | /* horizontal data   */
    950                FT_FACE_FLAG_HINTER;      /* has native hinter */
    951 
    952        if ( sfnt_format )
    953          flags |= FT_FACE_FLAG_SFNT;
    954 
    955        /* fixed width font? */
    956        if ( dict->is_fixed_pitch )
    957          flags |= FT_FACE_FLAG_FIXED_WIDTH;
    958 
    959  /* XXX: WE DO NOT SUPPORT KERNING METRICS IN THE GPOS TABLE FOR NOW */
    960 #if 0
    961        /* kerning available? */
    962        if ( face->kern_pairs )
    963          flags |= FT_FACE_FLAG_KERNING;
    964 #endif
    965 
    966        cffface->face_flags |= flags;
    967 
    968        /********************************************************************
    969         *
    970         * Compute style flags.
    971         */
    972        flags = 0;
    973 
    974        if ( dict->italic_angle )
    975          flags |= FT_STYLE_FLAG_ITALIC;
    976 
    977        {
    978          char  *weight = cff_index_get_sid_string( cff,
    979                                                    dict->weight );
    980 
    981 
    982          if ( weight )
    983            if ( !ft_strcmp( weight, "Bold"  ) ||
    984                 !ft_strcmp( weight, "Black" ) )
    985              flags |= FT_STYLE_FLAG_BOLD;
    986        }
    987 
    988        /* double check */
    989        if ( !(flags & FT_STYLE_FLAG_BOLD) && cffface->style_name )
    990          if ( !ft_strncmp( cffface->style_name, "Bold", 4 )  ||
    991               !ft_strncmp( cffface->style_name, "Black", 5 ) )
    992            flags |= FT_STYLE_FLAG_BOLD;
    993 
    994        cffface->style_flags = flags;
    995      }
    996 
    997      /* CID-keyed CFF or CFF2 fonts don't have glyph names -- the SFNT */
    998      /* loader has unset this flag because of the 3.0 `post' table.    */
    999      if ( dict->cid_registry == 0xFFFFU && !cff2 )
   1000        cffface->face_flags |= FT_FACE_FLAG_GLYPH_NAMES;
   1001 
   1002      if ( dict->cid_registry != 0xFFFFU && pure_cff )
   1003        cffface->face_flags |= FT_FACE_FLAG_CID_KEYED;
   1004 
   1005      /********************************************************************
   1006       *
   1007       * Compute char maps.
   1008       */
   1009 
   1010      /* Try to synthesize a Unicode charmap if there is none available */
   1011      /* already.  If an OpenType font contains a Unicode "cmap", we    */
   1012      /* will use it, whatever be in the CFF part of the file.          */
   1013      {
   1014        FT_CharMapRec  cmaprec;
   1015        FT_CharMap     cmap;
   1016        FT_Int         nn;
   1017        CFF_Encoding   encoding = &cff->encoding;
   1018 
   1019 
   1020        for ( nn = 0; nn < cffface->num_charmaps; nn++ )
   1021        {
   1022          cmap = cffface->charmaps[nn];
   1023 
   1024          /* Windows Unicode? */
   1025          if ( cmap->platform_id == TT_PLATFORM_MICROSOFT &&
   1026               cmap->encoding_id == TT_MS_ID_UNICODE_CS   )
   1027            goto Skip_Unicode;
   1028 
   1029          /* Apple Unicode platform id? */
   1030          if ( cmap->platform_id == TT_PLATFORM_APPLE_UNICODE )
   1031            goto Skip_Unicode; /* Apple Unicode */
   1032        }
   1033 
   1034        /* since CID-keyed fonts don't contain glyph names, we can't */
   1035        /* construct a cmap                                          */
   1036        if ( pure_cff && cff->top_font.font_dict.cid_registry != 0xFFFFU )
   1037          goto Exit;
   1038 
   1039        /* we didn't find a Unicode charmap -- synthesize one */
   1040        cmaprec.face        = cffface;
   1041        cmaprec.platform_id = TT_PLATFORM_MICROSOFT;
   1042        cmaprec.encoding_id = TT_MS_ID_UNICODE_CS;
   1043        cmaprec.encoding    = FT_ENCODING_UNICODE;
   1044 
   1045        nn = cffface->num_charmaps;
   1046 
   1047        error = FT_CMap_New( &cff_cmap_unicode_class_rec, NULL,
   1048                             &cmaprec, NULL );
   1049        if ( error                                      &&
   1050             FT_ERR_NEQ( error, No_Unicode_Glyph_Name ) &&
   1051             FT_ERR_NEQ( error, Unimplemented_Feature ) )
   1052          goto Exit;
   1053        error = FT_Err_Ok;
   1054 
   1055        /* if no Unicode charmap was previously selected, select this one */
   1056        if ( !cffface->charmap && nn != cffface->num_charmaps )
   1057          cffface->charmap = cffface->charmaps[nn];
   1058 
   1059      Skip_Unicode:
   1060        if ( encoding->count > 0 )
   1061        {
   1062          FT_CMap_Class  clazz;
   1063 
   1064 
   1065          cmaprec.face        = cffface;
   1066          cmaprec.platform_id = TT_PLATFORM_ADOBE;  /* Adobe platform id */
   1067 
   1068          if ( encoding->offset == 0 )
   1069          {
   1070            cmaprec.encoding_id = TT_ADOBE_ID_STANDARD;
   1071            cmaprec.encoding    = FT_ENCODING_ADOBE_STANDARD;
   1072            clazz               = &cff_cmap_encoding_class_rec;
   1073          }
   1074          else if ( encoding->offset == 1 )
   1075          {
   1076            cmaprec.encoding_id = TT_ADOBE_ID_EXPERT;
   1077            cmaprec.encoding    = FT_ENCODING_ADOBE_EXPERT;
   1078            clazz               = &cff_cmap_encoding_class_rec;
   1079          }
   1080          else
   1081          {
   1082            cmaprec.encoding_id = TT_ADOBE_ID_CUSTOM;
   1083            cmaprec.encoding    = FT_ENCODING_ADOBE_CUSTOM;
   1084            clazz               = &cff_cmap_encoding_class_rec;
   1085          }
   1086 
   1087          error = FT_CMap_New( clazz, NULL, &cmaprec, NULL );
   1088        }
   1089      }
   1090    }
   1091 
   1092  Exit:
   1093    return error;
   1094  }
   1095 
   1096 
   1097  FT_LOCAL_DEF( void )
   1098  cff_face_done( FT_Face  cffface )         /* CFF_Face */
   1099  {
   1100    CFF_Face      face = (CFF_Face)cffface;
   1101    FT_Memory     memory;
   1102    SFNT_Service  sfnt;
   1103 
   1104 
   1105    if ( !face )
   1106      return;
   1107 
   1108    memory = cffface->memory;
   1109    sfnt   = (SFNT_Service)face->sfnt;
   1110 
   1111    if ( sfnt )
   1112      sfnt->done_face( face );
   1113 
   1114    {
   1115      CFF_Font  cff = (CFF_Font)face->extra.data;
   1116 
   1117 
   1118      if ( cff )
   1119      {
   1120        cff_font_done( cff );
   1121        FT_FREE( face->extra.data );
   1122      }
   1123    }
   1124 
   1125 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
   1126    cff_done_blend( cffface );
   1127    face->blend = NULL;
   1128 #endif
   1129  }
   1130 
   1131 
   1132  FT_LOCAL_DEF( FT_Error )
   1133  cff_driver_init( FT_Module  module )        /* CFF_Driver */
   1134  {
   1135    PS_Driver  driver = (PS_Driver)module;
   1136 
   1137    FT_UInt32  seed;
   1138 
   1139 
   1140    /* set default property values, cf. `ftcffdrv.h' */
   1141    driver->hinting_engine = FT_HINTING_ADOBE;
   1142 
   1143    driver->no_stem_darkening = TRUE;
   1144 
   1145    driver->darken_params[0] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X1;
   1146    driver->darken_params[1] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y1;
   1147    driver->darken_params[2] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X2;
   1148    driver->darken_params[3] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y2;
   1149    driver->darken_params[4] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X3;
   1150    driver->darken_params[5] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y3;
   1151    driver->darken_params[6] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X4;
   1152    driver->darken_params[7] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y4;
   1153 
   1154    /* compute random seed from some memory addresses */
   1155    seed = (FT_UInt32)( (FT_Offset)(char*)&seed          ^
   1156                        (FT_Offset)(char*)&module        ^
   1157                        (FT_Offset)(char*)module->memory );
   1158    seed = seed ^ ( seed >> 10 ) ^ ( seed >> 20 );
   1159 
   1160    driver->random_seed = (FT_Int32)seed;
   1161    if ( driver->random_seed < 0 )
   1162      driver->random_seed = -driver->random_seed;
   1163    else if ( driver->random_seed == 0 )
   1164      driver->random_seed = 123456789;
   1165 
   1166    return FT_Err_Ok;
   1167  }
   1168 
   1169 
   1170  FT_LOCAL_DEF( void )
   1171  cff_driver_done( FT_Module  module )        /* CFF_Driver */
   1172  {
   1173    FT_UNUSED( module );
   1174  }
   1175 
   1176 
   1177 /* END */