Hash :
f0df85ba
        
        Author :
  
        
        Date :
2000-06-22T00:17:42
        
      
  - MAJOR INTERNAL REDESIGN:
    A lot of internal modifications have been performed lately on the
    source in order to provide the following enhancements:
      - more generic module support:
        The FT_Module type is now defined to represent a handle to a given
        module. The file <freetype/ftmodule.h> contains the FT_Module_Class
        definition, as well as the module-loading public API
        The FT_Driver type is still defined, and still represents a pointer
        to a font driver. Note that FT_Add_Driver is replaced by FT_Add_Module,
        FT_Get_Driver by FT_Get_Module, etc..
      - support for generic glyph image types:
        The FT_Renderer type is a pointer to a module used to perform various
        operations on glyph image.
        Each renderer is capable of handling images in a single format
        (e.g. ft_glyph_format_outline). Its functions are used to:
           - transform an glyph image
           - render a glyph image into a bitmap
           - return the control box (dimensions) of a given glyph image
        The scan converters "ftraster.c" and "ftgrays.c" have been moved
        to the new directory "src/renderer", and are used to provide two
        default renderer modules.
        One corresponds to the "standard" scan-converter, the other to the
        "smooth" one.
        The current renderer can be set through the new function
        FT_Set_Renderer.
        The old raster-related function FT_Set_Raster, FT_Get_Raster and
        FT_Set_Raster_Mode have now disappeared, in favor of the new:
           FT_Get_Renderer
           FT_Set_Renderer
        see the file <freetype/ftrender.h> for more details..
        These changes were necessary to properly support different scalable
        formats in the future, like bi-color glyphs, etc..
      - glyph loader object:
        A new internal object, called a 'glyph loader' has been introduced
        in the base layer. It is used by all scalable format font drivers
        to load glyphs and composites.
        This object has been created to reduce the code size of each driver,
        as each one of them basically re-implemented its functionality.
        See <freetype/internal/ftobjs.h> and the FT_GlyphLoader type for
        more information..
      - FT_GlyphSlot had new fields:
        In order to support extended features (see below), the FT_GlyphSlot
        structure has a few new fields:
           linearHoriAdvance:  this field gives the linearly scaled (i.e.
                               scaled but unhinted) advance width for the glyph,
                               expressed as a 16.16 fixed pixel value. This
                               is useful to perform WYSIWYG text.
           linearVertAdvance:  this field gives the linearly scaled advance
                               height for the glyph (relevant in vertical glyph
                               layouts only). This is useful to perform
                               WYSIWYG text.
        Note that the two above field replace the removed "metrics2" field
        in the glyph slot.
           advance:   this field is a vector that gives the transformed
                      advance for the glyph. By default, it corresponds
                      to the advance width, unless FT_LOAD_VERTICAL_LAYOUT
                      was specified when calling FT_Load_Glyph or FT_Load_Char
           bitmap_left: this field gives the distance in integer pixels from
                        the current pen position to the left-most pixel of
                        a glyph image WHEN IT IS A BITMAP. It is only valid
                        when the "format" field is set to
                        "ft_glyph_format_bitmap", for example, after calling
                        the new function FT_Render_Glyph.
           bitmap_top:  this field gives the distance in integer pixels from
                        the current pen position (located on the baseline) to
                        the top-most pixel of the glyph image WHEN IT IS A
                        BITMAP. Positive values correspond to upwards Y.
           loader:  this is a new private field for the glyph slot. Client
                    applications should not touch it..
      - support for transforms and direct rendering in FT_Load_Glyph:
        Most of the functionality found in <freetype/ftglyph.h> has been
        moved to the core library. Hence, the following:
          - a transform can be specified for a face through FT_Set_Transform.
            this transform is applied by FT_Load_Glyph to scalable glyph images
            (i.e. NOT TO BITMAPS) before the function returns, unless the
            bit flag FT_LOAD_IGNORE_TRANSFORM was set in the load flags..
          - once a glyph image has been loaded, it can be directly converted to
            a bitmap by using the new FT_Render_Glyph function. Note that this
            function takes the glyph image from the glyph slot, and converts
            it to a bitmap whose properties are returned in "face.glyph.bitmap",
            "face.glyph.bitmap_left" and "face.glyph.bitmap_top". The original
            native image might be lost after the conversion.
          - when using the new bit flag FT_LOAD_RENDER, the FT_Load_Glyph
            and FT_Load_Char functions will call FT_Render_Glyph automatically
            when needed.
      
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
/***************************************************************************/
/*                                                                         */
/*  sfobjs.c                                                               */
/*                                                                         */
/*    SFNT object management (base).                                       */
/*                                                                         */
/*  Copyright 1996-2000 by                                                 */
/*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
/*                                                                         */
/*  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 <sfobjs.h>
#include <freetype/internal/sfnt.h>
#include <freetype/internal/psnames.h>
#include <freetype/ttnameid.h>
#include <freetype/internal/tterrors.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_sfobjs
  /*************************************************************************/
  /*                                                                       */
  /* <Function>                                                            */
  /*    Get_Name                                                           */
  /*                                                                       */
  /* <Description>                                                         */
  /*    Returns a given ENGLISH name record in ASCII.                      */
  /*                                                                       */
  /* <Input>                                                               */
  /*    face   :: A handle to the source face object.                      */
  /*                                                                       */
  /*    nameid :: The name id of the name record to return.                */
  /*                                                                       */
  /* <Return>                                                              */
  /*    Character string.  NULL if no name is present.                     */
  /*                                                                       */
  static
  FT_String*  Get_Name( TT_Face    face,
                        FT_UShort  nameid )
  {
    FT_Memory    memory = face->root.memory;
    FT_UShort    n;
    TT_NameRec*  rec;
    FT_Bool      wide_chars = 1;
    rec = face->name_table.names;
    for ( n = 0; n < face->name_table.numNameRecords; n++, rec++ )
    {
      if ( rec->nameID == nameid )
      {
        /* found the name - now create an ASCII string from it */
        FT_Bool  found = 0;
        /* test for Microsoft English language */
        if ( rec->platformID == TT_PLATFORM_MICROSOFT &&
             rec->encodingID <= TT_MS_ID_UNICODE_CS   &&
             ( rec->languageID & 0x3FF ) == 0x009     )
          found = 1;
        /* test for Apple Unicode encoding */
        else if ( rec->platformID == TT_PLATFORM_APPLE_UNICODE )
          found = 1;
        /* test for Apple Roman */
        else if ( rec->platformID == TT_PLATFORM_MACINTOSH &&
                  rec->languageID == TT_MAC_ID_ROMAN       )
        {
          found      = 1;
          wide_chars = 0;
        }
        /* found a Unicode name */
        if ( found )
        {
          FT_String*  string;
          FT_UInt     len;
          if ( wide_chars )
          {
            FT_UInt   m;
            len = (FT_UInt)rec->stringLength / 2;
            if ( MEM_Alloc( string, len + 1 ) )
              return NULL;
            for ( m = 0; m < len; m ++ )
              string[m] = rec->string[2 * m + 1];
          }
          else
          {
            len = rec->stringLength;
            if ( MEM_Alloc( string, len + 1 ) )
              return NULL;
            MEM_Copy( string, rec->string, len );
          }
          string[len] = '\0';
          return string;
        }
      }
    }
    return NULL;
  }
  static
  FT_Encoding  find_encoding( int  platform_id,
                              int  encoding_id )
  {
    typedef struct  TEncoding
    {
      int          platform_id;
      int          encoding_id;
      FT_Encoding  encoding;
    } TEncoding;
    static const TEncoding  tt_encodings[] =
    {
      { TT_PLATFORM_ISO,           -1,                  ft_encoding_unicode },
      { TT_PLATFORM_APPLE_UNICODE, -1,                  ft_encoding_unicode },
      { TT_PLATFORM_MACINTOSH,     TT_MAC_ID_ROMAN,     ft_encoding_apple_roman },
      { TT_PLATFORM_MICROSOFT,     TT_MS_ID_UNICODE_CS, ft_encoding_unicode },
      { TT_PLATFORM_MICROSOFT,     TT_MS_ID_SJIS,       ft_encoding_sjis },
      { TT_PLATFORM_MICROSOFT,     TT_MS_ID_GB2312,     ft_encoding_gb2312 },
      { TT_PLATFORM_MICROSOFT,     TT_MS_ID_BIG_5,      ft_encoding_big5 },
      { TT_PLATFORM_MICROSOFT,     TT_MS_ID_WANSUNG,    ft_encoding_wansung },
      { TT_PLATFORM_MICROSOFT,     TT_MS_ID_JOHAB,      ft_encoding_johab }
    };
    const TEncoding  *cur, *limit;
    cur   = tt_encodings;
    limit = cur + sizeof ( tt_encodings ) / sizeof ( tt_encodings[0] );
    for ( ; cur < limit; cur++ )
    {
      if ( cur->platform_id == platform_id )
      {
        if ( cur->encoding_id == encoding_id ||
             cur->encoding_id == -1          )
          return cur->encoding;
      }
    }
    return ft_encoding_none;
  }
  LOCAL_FUNC
  FT_Error  SFNT_Init_Face( FT_Stream      stream,
                            TT_Face        face,
                            FT_Int         face_index,
                            FT_Int         num_params,
                            FT_Parameter*  params )
  {
    FT_Error            error;
    FT_Library          library = face->root.driver->root.library;
    SFNT_Interface*     sfnt;
    SFNT_Header         sfnt_header;
    /* for now, parameters are unused */
    UNUSED( num_params );
    UNUSED( params );
    sfnt = (SFNT_Interface*)face->sfnt;
    if ( !sfnt )
    {
      sfnt = (SFNT_Interface*)FT_Get_Module_Interface( library, "sfnt" );
      if ( !sfnt )
      {
        error = FT_Err_Invalid_File_Format;
        goto Exit;
      }
      face->sfnt       = sfnt;
      face->goto_table = sfnt->goto_table;
    }
    if ( !face->psnames )
    {
      face->psnames = (PSNames_Interface*)
                       FT_Get_Module_Interface( library, "psnames" );
    }
    /* check that we have a valid TrueType file */
    error = sfnt->load_sfnt_header( face, stream, face_index, &sfnt_header );
    if ( error )
      goto Exit;
    face->format_tag = sfnt_header.format_tag;
    face->num_tables = sfnt_header.num_tables;
    /* Load font directory */
    error = sfnt->load_directory( face, stream, &sfnt_header );
    if ( error )
      goto Exit;
    face->root.num_faces = face->ttc_header.DirCount;
    if ( face->root.num_faces < 1 )
      face->root.num_faces = 1;
  Exit:
    return error;
  }
#undef  LOAD_
#define LOAD_( x )  ( (error = sfnt->load_##x( face, stream )) != TT_Err_Ok )
  LOCAL_FUNC
  FT_Error  SFNT_Load_Face( FT_Stream      stream,
                            TT_Face        face,
                            FT_Int         face_index,
                            FT_Int         num_params,
                            FT_Parameter*  params )
  {
    FT_Error         error;
    SFNT_Interface*  sfnt = (SFNT_Interface*)face->sfnt;
    /* Load tables */
    if ( LOAD_( header )        ||
         LOAD_( max_profile )   ||
         /* load the `hhea' & `hmtx' tables at once */
         ( error = sfnt->load_metrics( face, stream, 0 ) ) != TT_Err_Ok  ||
         /* try to load the `vhea' & `vmtx' at once if present */
         ( error = sfnt->load_metrics( face, stream, 1 ) ) != TT_Err_Ok  ||
         LOAD_( charmaps )      ||
         LOAD_( names )         ||
         LOAD_( os2 )           ||
         LOAD_( psnames )       )
     goto Exit;
    /* the optional tables */
#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
    /* embedded bitmap support. */
    if ( sfnt->load_sbits && LOAD_( sbits ) )
      goto Exit;
#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
    if ( LOAD_( hdmx )          ||
         LOAD_( gasp )          ||
         LOAD_( kerning )       ||
		 LOAD_( pclt )          )
      goto Exit;
#ifdef TT_CONFIG_OPTION_EXTEND_ENGINE
    if ( ( error = TT_Extension_Create( face ) ) != TT_Err_Ok )
      goto Exit;
#endif
    face->root.family_name = Get_Name( face, TT_NAME_ID_FONT_FAMILY );
    face->root.style_name  = Get_Name( face, TT_NAME_ID_FONT_SUBFAMILY );
    /* now set up root fields */
    {
      FT_Face     root = &face->root;
      FT_Int      flags;
      TT_CharMap  charmap;
      FT_Int      n;
      FT_Memory   memory;
      memory = root->memory;
      /*********************************************************************/
      /*                                                                   */
      /* Compute face flags.                                               */
      /*                                                                   */
      flags = FT_FACE_FLAG_SCALABLE  |    /* scalable outlines */
              FT_FACE_FLAG_SFNT      |    /* SFNT file format  */
              FT_FACE_FLAG_HORIZONTAL;    /* horizontal data   */
      /* fixed width font? */
      if ( face->postscript.isFixedPitch )
        flags |= FT_FACE_FLAG_FIXED_WIDTH;
      /* vertical information? */
      if ( face->vertical_info )
        flags |= FT_FACE_FLAG_VERTICAL;
      /* kerning available ? */
      if ( face->kern_pairs )
        flags |= FT_FACE_FLAG_KERNING;
      root->face_flags = flags;
      /*********************************************************************/
      /*                                                                   */
      /* Compute style flags.                                              */
      /*                                                                   */
      flags = 0;
      if ( face->os2.version != 0xFFFF )
      {
        /* we have an OS/2 table; use the `fsSelection' field */
        if ( face->os2.fsSelection & 1 )
          flags |= FT_STYLE_FLAG_ITALIC;
        if ( face->os2.fsSelection & 32 )
          flags |= FT_STYLE_FLAG_BOLD;
      }
      else
      {
        /* this is an old Mac font, use the header field */
        if ( face->header.Mac_Style & 1 )
          flags |= FT_STYLE_FLAG_BOLD;
        if ( face->header.Mac_Style & 2 )
          flags |= FT_STYLE_FLAG_ITALIC;
      }
      face->root.style_flags = flags;
      /*********************************************************************/
      /*                                                                   */
      /* Polish the charmaps.                                              */
      /*                                                                   */
      /*   Try to set the charmap encoding according to the platform &     */
      /*   encoding ID of each charmap.                                    */
      /*                                                                   */
      charmap            = face->charmaps;
      root->num_charmaps = face->num_charmaps;
      /* allocate table of pointers */
      if ( ALLOC_ARRAY( root->charmaps, root->num_charmaps, FT_CharMap ) )
        goto Exit;
      for ( n = 0; n < root->num_charmaps; n++, charmap++ )
      {
        FT_Int  platform = charmap->cmap.platformID;
        FT_Int  encoding = charmap->cmap.platformEncodingID;
        charmap->root.face        = (FT_Face)face;
        charmap->root.platform_id = platform;
        charmap->root.encoding_id = encoding;
        charmap->root.encoding    = find_encoding( platform, encoding );
        /* now, set root->charmap with a unicode charmap */
        /* wherever available                            */
        if ( !root->charmap                                &&
             charmap->root.encoding == ft_encoding_unicode )
          root->charmap = (FT_CharMap)charmap;
        root->charmaps[n] = (FT_CharMap)charmap;
      }
#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
      if ( face->num_sbit_strikes )
      {
       face->root.num_fixed_sizes = face->num_sbit_strikes;
       if ( ALLOC_ARRAY( face->root.available_sizes,
                         face->num_sbit_strikes,
                         FT_Bitmap_Size ) )
         return error;
        for ( n = 0 ; n < face->num_sbit_strikes ; n++ )
        {
          face->root.available_sizes[n].width =
            face->sbit_strikes[n].x_ppem;
          face->root.available_sizes[n].height =
            face->sbit_strikes[n].y_ppem;
        }
      }
      else
#else /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
      {
       root->num_fixed_sizes = 0;
       root->available_sizes = 0;
      }
#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
      /*********************************************************************/
      /*                                                                   */
      /*  Set up metrics.                                                  */
      /*                                                                   */
      root->bbox.xMin    = face->header.xMin;
      root->bbox.yMin    = face->header.yMin;
      root->bbox.xMax    = face->header.xMax;
      root->bbox.yMax    = face->header.yMax;
      root->units_per_EM = face->header.Units_Per_EM;
      /* The ascender/descender/height are computed from the OS/2 table    */
      /* when found.  Otherwise, they're taken from the horizontal header. */
      if ( face->os2.version != 0xFFFF )
      {
        root->ascender  =  face->os2.sTypoAscender;
        root->descender = -face->os2.sTypoDescender;
        root->height    =  root->ascender + root->descender +
                           face->os2.sTypoLineGap;
      }
      else
      {
        root->ascender  = face->horizontal.Ascender;
        root->descender = face->horizontal.Descender;
        root->height    = root->ascender + root->descender +
                          face->horizontal.Line_Gap;
      }
      root->max_advance_width   = face->horizontal.advance_Width_Max;
      root->max_advance_height  = face->vertical_info
                                    ? face->vertical.advance_Height_Max
                                    : root->height;
      root->underline_position  = face->postscript.underlinePosition;
      root->underline_thickness = face->postscript.underlineThickness;
      /* root->max_points      - already set up */
      /* root->max_contours    - already set up */
    }
  Exit:
    return error;
  }
#undef LOAD_
  LOCAL_FUNC
  void  SFNT_Done_Face( TT_Face  face )
  {
    FT_Memory        memory = face->root.memory;
    SFNT_Interface*  sfnt   = face->sfnt;
    if ( sfnt )
    {
      /* destroy the postscript names table if it is loaded */
      if ( sfnt->free_psnames )
        sfnt->free_psnames( face );
      /* destroy the embedded bitmaps table if it is loaded */
      if ( sfnt->free_sbits )
        sfnt->free_sbits( face );
    }
    /* freeing the kerning table */
    FREE( face->kern_pairs );
    face->num_kern_pairs = 0;
    /* freeing the collection table */
    FREE( face->ttc_header.TableDirectory );
    face->ttc_header.DirCount = 0;
    /* freeing table directory */
    FREE( face->dir_tables );
    face->num_tables = 0;
    /* freeing the character mapping tables */
    if (sfnt && sfnt->load_charmaps )
    {
      FT_UShort  n;
      for ( n = 0; n < face->num_charmaps; n++ )
        sfnt->free_charmap( face, &face->charmaps[n].cmap );
    }
    FREE( face->charmaps );
    face->num_charmaps = 0;
    FREE( face->root.charmaps );
    face->root.num_charmaps = 0;
    face->root.charmap      = 0;
    /* freeing the horizontal metrics */
    FREE( face->horizontal.long_metrics );
    FREE( face->horizontal.short_metrics );
    /* freeing the vertical ones, if any */
    if ( face->vertical_info )
    {
      FREE( face->vertical.long_metrics  );
      FREE( face->vertical.short_metrics );
      face->vertical_info = 0;
    }
    /* freeing the gasp table */
    FREE( face->gasp.gaspRanges );
    face->gasp.numRanges = 0;
    /* freeing the name table */
    sfnt->free_names( face );
    /* freeing the hdmx table */
    sfnt->free_hdmx( face );
    /* freeing family and style name */
    FREE( face->root.family_name );
    FREE( face->root.style_name );
    /* freeing sbit size table */
    face->root.num_fixed_sizes = 0;
    if ( face->root.available_sizes )
      FREE( face->root.available_sizes );
    face->sfnt = 0;
  }
/* END */