- Remove svn:needs-lock, svn:eol-type, and svn:eol-tyle properties.
[reactos.git] / reactos / lib / 3rdparty / freetype / src / sfnt / ttsbit0.c
index 593a428..f8adc64 100644 (file)
-/***************************************************************************/\r
-/*                                                                         */\r
-/*  ttsbit0.c                                                              */\r
-/*                                                                         */\r
-/*    TrueType and OpenType embedded bitmap support (body).                */\r
-/*    This is a heap-optimized version.                                    */\r
-/*                                                                         */\r
-/*  Copyright 2005, 2006, 2007 by                                          */\r
-/*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */\r
-/*                                                                         */\r
-/*  This file is part of the FreeType project, and may only be used,       */\r
-/*  modified, and distributed under the terms of the FreeType project      */\r
-/*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */\r
-/*  this file you indicate that you have read the license and              */\r
-/*  understand and accept it fully.                                        */\r
-/*                                                                         */\r
-/***************************************************************************/\r
-\r
-\r
-/* This file is included by ttsbit.c */\r
-\r
-\r
-#include <ft2build.h>\r
-#include FT_INTERNAL_DEBUG_H\r
-#include FT_INTERNAL_STREAM_H\r
-#include FT_TRUETYPE_TAGS_H\r
-#include "ttsbit.h"\r
-\r
-#include "sferrors.h"\r
-\r
-\r
-  /*************************************************************************/\r
-  /*                                                                       */\r
-  /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */\r
-  /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */\r
-  /* messages during execution.                                            */\r
-  /*                                                                       */\r
-#undef  FT_COMPONENT\r
-#define FT_COMPONENT  trace_ttsbit\r
-\r
-\r
-    static const FT_Frame_Field  tt_sbit_line_metrics_fields[] =\r
-    {\r
-#undef  FT_STRUCTURE\r
-#define FT_STRUCTURE  TT_SBit_LineMetricsRec\r
-\r
-      /* no FT_FRAME_START */\r
-        FT_FRAME_CHAR( ascender ),\r
-        FT_FRAME_CHAR( descender ),\r
-        FT_FRAME_BYTE( max_width ),\r
-\r
-        FT_FRAME_CHAR( caret_slope_numerator ),\r
-        FT_FRAME_CHAR( caret_slope_denominator ),\r
-        FT_FRAME_CHAR( caret_offset ),\r
-\r
-        FT_FRAME_CHAR( min_origin_SB ),\r
-        FT_FRAME_CHAR( min_advance_SB ),\r
-        FT_FRAME_CHAR( max_before_BL ),\r
-        FT_FRAME_CHAR( min_after_BL ),\r
-        FT_FRAME_CHAR( pads[0] ),\r
-        FT_FRAME_CHAR( pads[1] ),\r
-      FT_FRAME_END\r
-    };\r
-\r
-    static const FT_Frame_Field  tt_strike_start_fields[] =\r
-    {\r
-#undef  FT_STRUCTURE\r
-#define FT_STRUCTURE  TT_SBit_StrikeRec\r
-\r
-      /* no FT_FRAME_START */\r
-        FT_FRAME_ULONG( ranges_offset ),\r
-        FT_FRAME_SKIP_LONG,\r
-        FT_FRAME_ULONG( num_ranges ),\r
-        FT_FRAME_ULONG( color_ref ),\r
-      FT_FRAME_END\r
-    };\r
-\r
-    static const FT_Frame_Field  tt_strike_end_fields[] =\r
-    {\r
-      /* no FT_FRAME_START */\r
-        FT_FRAME_USHORT( start_glyph ),\r
-        FT_FRAME_USHORT( end_glyph ),\r
-        FT_FRAME_BYTE  ( x_ppem ),\r
-        FT_FRAME_BYTE  ( y_ppem ),\r
-        FT_FRAME_BYTE  ( bit_depth ),\r
-        FT_FRAME_CHAR  ( flags ),\r
-      FT_FRAME_END\r
-    };\r
-\r
-\r
-  FT_LOCAL_DEF( FT_Error )\r
-  tt_face_load_eblc( TT_Face    face,\r
-                     FT_Stream  stream )\r
-  {\r
-    FT_Error   error  = SFNT_Err_Ok;\r
-    FT_Fixed   version;\r
-    FT_ULong   num_strikes, table_size;\r
-    FT_Byte*   p;\r
-    FT_Byte*   p_limit;\r
-    FT_UInt    count;\r
-\r
-\r
-    face->sbit_num_strikes = 0;\r
-\r
-    /* this table is optional */\r
-    error = face->goto_table( face, TTAG_EBLC, stream, &table_size );\r
-    if ( error )\r
-      error = face->goto_table( face, TTAG_bloc, stream, &table_size );\r
-    if ( error )\r
-      goto Exit;\r
-\r
-    if ( table_size < 8 )\r
-    {\r
-      FT_ERROR(( "%s: table too short!\n", "tt_face_load_sbit_strikes" ));\r
-      error = SFNT_Err_Invalid_File_Format;\r
-      goto Exit;\r
-    }\r
-\r
-    if ( FT_FRAME_EXTRACT( table_size, face->sbit_table ) )\r
-      goto Exit;\r
-\r
-    face->sbit_table_size = table_size;\r
-\r
-    p       = face->sbit_table;\r
-    p_limit = p + table_size;\r
-\r
-    version     = FT_NEXT_ULONG( p );\r
-    num_strikes = FT_NEXT_ULONG( p );\r
-\r
-    if ( version != 0x00020000UL || num_strikes >= 0x10000UL )\r
-    {\r
-      FT_ERROR(( "%s: invalid table version!\n",\r
-                 "tt_face_load_sbit_strikes" ));\r
-      error = SFNT_Err_Invalid_File_Format;\r
-      goto Fail;\r
-    }\r
-\r
-    /*\r
-     *  Count the number of strikes available in the table.  We are a bit\r
-     *  paranoid there and don't trust the data.\r
-     */\r
-    count = (FT_UInt)num_strikes;\r
-    if ( 8 + 48UL * count > table_size )\r
-      count = (FT_UInt)( ( p_limit - p ) / 48 );\r
-\r
-    face->sbit_num_strikes = count;\r
-\r
-    FT_TRACE3(( "sbit_num_strikes: %u\n", count ));\r
-  Exit:\r
-    return error;\r
-\r
-  Fail:\r
-    FT_FRAME_RELEASE( face->sbit_table );\r
-    face->sbit_table_size = 0;\r
-    goto Exit;\r
-  }\r
-\r
-\r
-  FT_LOCAL_DEF( void )\r
-  tt_face_free_eblc( TT_Face  face )\r
-  {\r
-    FT_Stream  stream = face->root.stream;\r
-\r
-\r
-    FT_FRAME_RELEASE( face->sbit_table );\r
-    face->sbit_table_size  = 0;\r
-    face->sbit_num_strikes = 0;\r
-  }\r
-\r
-\r
-  FT_LOCAL_DEF( FT_Error )\r
-  tt_face_set_sbit_strike( TT_Face          face,\r
-                           FT_Size_Request  req,\r
-                           FT_ULong*        astrike_index )\r
-  {\r
-    return FT_Match_Size( (FT_Face)face, req, 0, astrike_index );\r
-  }\r
-\r
-\r
-  FT_LOCAL_DEF( FT_Error )\r
-  tt_face_load_strike_metrics( TT_Face           face,\r
-                               FT_ULong          strike_index,\r
-                               FT_Size_Metrics*  metrics )\r
-  {\r
-    FT_Byte*         strike;\r
-\r
-\r
-    if ( strike_index >= (FT_ULong)face->sbit_num_strikes )\r
-      return SFNT_Err_Invalid_Argument;\r
-\r
-    strike = face->sbit_table + 8 + strike_index * 48;\r
-\r
-    metrics->x_ppem = (FT_UShort)strike[44];\r
-    metrics->y_ppem = (FT_UShort)strike[45];\r
-\r
-    metrics->ascender  = (FT_Char)strike[16] << 6;  /* hori.ascender  */\r
-    metrics->descender = (FT_Char)strike[17] << 6;  /* hori.descender */\r
-    metrics->height    = metrics->ascender - metrics->descender;\r
-\r
-    /* XXX: Is this correct? */\r
-    metrics->max_advance = ( (FT_Char)strike[22] + /* min_origin_SB  */\r
-                                      strike[18] + /* max_width      */\r
-                             (FT_Char)strike[23]   /* min_advance_SB */\r
-                                                 ) << 6;\r
-\r
-    return SFNT_Err_Ok;\r
-  }\r
-\r
-\r
-  typedef struct\r
-  {\r
-    TT_Face          face;\r
-    FT_Stream        stream;\r
-    FT_Bitmap*       bitmap;\r
-    TT_SBit_Metrics  metrics;\r
-    FT_Bool          metrics_loaded;\r
-    FT_Bool          bitmap_allocated;\r
-    FT_Byte          bit_depth;\r
-\r
-    FT_ULong         ebdt_start;\r
-    FT_ULong         ebdt_size;\r
-\r
-    FT_ULong         strike_index_array;\r
-    FT_ULong         strike_index_count;\r
-    FT_Byte*         eblc_base;\r
-    FT_Byte*         eblc_limit;\r
-\r
-  } TT_SBitDecoderRec, *TT_SBitDecoder;\r
-\r
-\r
-  static FT_Error\r
-  tt_sbit_decoder_init( TT_SBitDecoder       decoder,\r
-                        TT_Face              face,\r
-                        FT_ULong             strike_index,\r
-                        TT_SBit_MetricsRec*  metrics )\r
-  {\r
-    FT_Error   error;\r
-    FT_Stream  stream = face->root.stream;\r
-    FT_ULong   ebdt_size;\r
-\r
-\r
-    error = face->goto_table( face, TTAG_EBDT, stream, &ebdt_size );\r
-    if ( error )\r
-      error = face->goto_table( face, TTAG_bdat, stream, &ebdt_size );\r
-    if ( error )\r
-      goto Exit;\r
-\r
-    decoder->face    = face;\r
-    decoder->stream  = stream;\r
-    decoder->bitmap  = &face->root.glyph->bitmap;\r
-    decoder->metrics = metrics;\r
-\r
-    decoder->metrics_loaded   = 0;\r
-    decoder->bitmap_allocated = 0;\r
-\r
-    decoder->ebdt_start = FT_STREAM_POS();\r
-    decoder->ebdt_size  = ebdt_size;\r
-\r
-    decoder->eblc_base  = face->sbit_table;\r
-    decoder->eblc_limit = face->sbit_table + face->sbit_table_size;\r
-\r
-    /* now find the strike corresponding to the index */\r
-    {\r
-      FT_Byte*  p;\r
-\r
-\r
-      if ( 8 + 48 * strike_index + 3 * 4 + 34 + 1 > face->sbit_table_size )\r
-      {\r
-        error = SFNT_Err_Invalid_File_Format;\r
-        goto Exit;\r
-      }\r
-\r
-      p = decoder->eblc_base + 8 + 48 * strike_index;\r
-\r
-      decoder->strike_index_array = FT_NEXT_ULONG( p );\r
-      p                          += 4;\r
-      decoder->strike_index_count = FT_NEXT_ULONG( p );\r
-      p                          += 34;\r
-      decoder->bit_depth          = *p;\r
-\r
-      if ( decoder->strike_index_array > face->sbit_table_size             ||\r
-           decoder->strike_index_array + 8 * decoder->strike_index_count >\r
-             face->sbit_table_size                                         )\r
-        error = SFNT_Err_Invalid_File_Format;\r
-    }\r
-\r
-  Exit:\r
-    return error;\r
-  }\r
-\r
-\r
-  static void\r
-  tt_sbit_decoder_done( TT_SBitDecoder  decoder )\r
-  {\r
-    FT_UNUSED( decoder );\r
-  }\r
-\r
-\r
-  static FT_Error\r
-  tt_sbit_decoder_alloc_bitmap( TT_SBitDecoder  decoder )\r
-  {\r
-    FT_Error    error = SFNT_Err_Ok;\r
-    FT_UInt     width, height;\r
-    FT_Bitmap*  map = decoder->bitmap;\r
-    FT_Long     size;\r
-\r
-\r
-    if ( !decoder->metrics_loaded )\r
-    {\r
-      error = SFNT_Err_Invalid_Argument;\r
-      goto Exit;\r
-    }\r
-\r
-    width  = decoder->metrics->width;\r
-    height = decoder->metrics->height;\r
-\r
-    map->width = (int)width;\r
-    map->rows  = (int)height;\r
-\r
-    switch ( decoder->bit_depth )\r
-    {\r
-    case 1:\r
-      map->pixel_mode = FT_PIXEL_MODE_MONO;\r
-      map->pitch      = ( map->width + 7 ) >> 3;\r
-      break;\r
-\r
-    case 2:\r
-      map->pixel_mode = FT_PIXEL_MODE_GRAY2;\r
-      map->pitch      = ( map->width + 3 ) >> 2;\r
-      break;\r
-\r
-    case 4:\r
-      map->pixel_mode = FT_PIXEL_MODE_GRAY4;\r
-      map->pitch      = ( map->width + 1 ) >> 1;\r
-      break;\r
-\r
-    case 8:\r
-      map->pixel_mode = FT_PIXEL_MODE_GRAY;\r
-      map->pitch      = map->width;\r
-      break;\r
-\r
-    default:\r
-      error = SFNT_Err_Invalid_File_Format;\r
-      goto Exit;\r
-    }\r
-\r
-    size = map->rows * map->pitch;\r
-\r
-    /* check that there is no empty image */\r
-    if ( size == 0 )\r
-      goto Exit;     /* exit successfully! */\r
-\r
-    error = ft_glyphslot_alloc_bitmap( decoder->face->root.glyph, size );\r
-    if ( error )\r
-      goto Exit;\r
-\r
-    decoder->bitmap_allocated = 1;\r
-\r
-  Exit:\r
-    return error;\r
-  }\r
-\r
-\r
-  static FT_Error\r
-  tt_sbit_decoder_load_metrics( TT_SBitDecoder  decoder,\r
-                                FT_Byte*       *pp,\r
-                                FT_Byte*        limit,\r
-                                FT_Bool         big )\r
-  {\r
-    FT_Byte*         p       = *pp;\r
-    TT_SBit_Metrics  metrics = decoder->metrics;\r
-\r
-\r
-    if ( p + 5 > limit )\r
-      goto Fail;\r
-\r
-    if ( !decoder->metrics_loaded )\r
-    {\r
-      metrics->height       = p[0];\r
-      metrics->width        = p[1];\r
-      metrics->horiBearingX = (FT_Char)p[2];\r
-      metrics->horiBearingY = (FT_Char)p[3];\r
-      metrics->horiAdvance  = p[4];\r
-    }\r
-\r
-    p += 5;\r
-    if ( big )\r
-    {\r
-      if ( p + 3 > limit )\r
-        goto Fail;\r
-\r
-      if ( !decoder->metrics_loaded )\r
-      {\r
-        metrics->vertBearingX = (FT_Char)p[0];\r
-        metrics->vertBearingY = (FT_Char)p[1];\r
-        metrics->vertAdvance  = p[2];\r
-      }\r
-\r
-      p += 3;\r
-    }\r
-\r
-    decoder->metrics_loaded = 1;\r
-    *pp = p;\r
-    return 0;\r
-\r
-  Fail:\r
-    return SFNT_Err_Invalid_Argument;\r
-  }\r
-\r
-\r
-  /* forward declaration */\r
-  static FT_Error\r
-  tt_sbit_decoder_load_image( TT_SBitDecoder  decoder,\r
-                              FT_UInt         glyph_index,\r
-                              FT_Int          x_pos,\r
-                              FT_Int          y_pos );\r
-\r
-  typedef FT_Error  (*TT_SBitDecoder_LoadFunc)( TT_SBitDecoder  decoder,\r
-                                                FT_Byte*        p,\r
-                                                FT_Byte*        plimit,\r
-                                                FT_Int          x_pos,\r
-                                                FT_Int          y_pos );\r
-\r
-\r
-  static FT_Error\r
-  tt_sbit_decoder_load_byte_aligned( TT_SBitDecoder  decoder,\r
-                                     FT_Byte*        p,\r
-                                     FT_Byte*        limit,\r
-                                     FT_Int          x_pos,\r
-                                     FT_Int          y_pos )\r
-  {\r
-    FT_Error    error = SFNT_Err_Ok;\r
-    FT_Byte*    line;\r
-    FT_Int      bit_height, bit_width, pitch, width, height, h;\r
-    FT_Bitmap*  bitmap;\r
-\r
-\r
-    if ( !decoder->bitmap_allocated )\r
-    {\r
-      error = tt_sbit_decoder_alloc_bitmap( decoder );\r
-      if ( error )\r
-        goto Exit;\r
-    }\r
-\r
-    /* check that we can write the glyph into the bitmap */\r
-    bitmap     = decoder->bitmap;\r
-    bit_width  = bitmap->width;\r
-    bit_height = bitmap->rows;\r
-    pitch      = bitmap->pitch;\r
-    line       = bitmap->buffer;\r
-\r
-    width  = decoder->metrics->width;\r
-    height = decoder->metrics->height;\r
-\r
-    if ( x_pos < 0 || x_pos + width > bit_width   ||\r
-         y_pos < 0 || y_pos + height > bit_height )\r
-    {\r
-      error = SFNT_Err_Invalid_File_Format;\r
-      goto Exit;\r
-    }\r
-\r
-    if ( p + ( ( width + 7 ) >> 3 ) * height > limit )\r
-    {\r
-      error = SFNT_Err_Invalid_File_Format;\r
-      goto Exit;\r
-    }\r
-\r
-    /* now do the blit */\r
-    line  += y_pos * pitch + ( x_pos >> 3 );\r
-    x_pos &= 7;\r
-\r
-    if ( x_pos == 0 )  /* the easy one */\r
-    {\r
-      for ( h = height; h > 0; h--, line += pitch )\r
-      {\r
-        FT_Byte*  write = line;\r
-        FT_Int    w;\r
-\r
-\r
-        for ( w = width; w >= 8; w -= 8 )\r
-        {\r
-          write[0] = (FT_Byte)( write[0] | *p++ );\r
-          write   += 1;\r
-        }\r
-\r
-        if ( w > 0 )\r
-          write[0] = (FT_Byte)( write[0] | ( *p++ & ( 0xFF00U >> w ) ) );\r
-      }\r
-    }\r
-    else  /* x_pos > 0 */\r
-    {\r
-      for ( h = height; h > 0; h--, line += pitch )\r
-      {\r
-        FT_Byte*  write = line;\r
-        FT_Int    w;\r
-        FT_UInt   wval = 0;\r
-\r
-\r
-        for ( w = width; w >= 8; w -= 8 )\r
-        {\r
-          wval      = (FT_UInt)( wval | *p++ );\r
-          write[0]  = (FT_Byte)( write[0] | ( wval >> x_pos ) );\r
-          write    += 1;\r
-          wval    <<= 8;\r
-        }\r
-\r
-        if ( w > 0 )\r
-          wval = (FT_UInt)( wval | ( *p++ & ( 0xFF00U >> w ) ) );\r
-\r
-        /* all bits read and there are ( x_pos + w ) bits to be written */\r
-\r
-        write[0] = (FT_Byte)( write[0] | ( wval >> x_pos ) );\r
-\r
-        if ( x_pos + w > 8 )\r
-        {\r
-          write++;\r
-          wval   <<= 8;\r
-          write[0] = (FT_Byte)( write[0] | ( wval >> x_pos ) );\r
-        }\r
-      }\r
-    }\r
-\r
-  Exit:\r
-    return error;\r
-  }\r
-\r
-\r
-  static FT_Error\r
-  tt_sbit_decoder_load_bit_aligned( TT_SBitDecoder  decoder,\r
-                                    FT_Byte*        p,\r
-                                    FT_Byte*        limit,\r
-                                    FT_Int          x_pos,\r
-                                    FT_Int          y_pos )\r
-  {\r
-    FT_Error    error = SFNT_Err_Ok;\r
-    FT_Byte*    line;\r
-    FT_Int      bit_height, bit_width, pitch, width, height, h, nbits;\r
-    FT_Bitmap*  bitmap;\r
-    FT_UShort   rval;\r
-\r
-\r
-    if ( !decoder->bitmap_allocated )\r
-    {\r
-      error = tt_sbit_decoder_alloc_bitmap( decoder );\r
-      if ( error )\r
-        goto Exit;\r
-    }\r
-\r
-    /* check that we can write the glyph into the bitmap */\r
-    bitmap     = decoder->bitmap;\r
-    bit_width  = bitmap->width;\r
-    bit_height = bitmap->rows;\r
-    pitch      = bitmap->pitch;\r
-    line       = bitmap->buffer;\r
-\r
-    width  = decoder->metrics->width;\r
-    height = decoder->metrics->height;\r
-\r
-    if ( x_pos < 0 || x_pos + width  > bit_width  ||\r
-         y_pos < 0 || y_pos + height > bit_height )\r
-    {\r
-      error = SFNT_Err_Invalid_File_Format;\r
-      goto Exit;\r
-    }\r
-\r
-    if ( p + ( ( width * height + 7 ) >> 3 ) > limit )\r
-    {\r
-      error = SFNT_Err_Invalid_File_Format;\r
-      goto Exit;\r
-    }\r
-\r
-    /* now do the blit */\r
-    line  += y_pos * pitch + ( x_pos >> 3 );\r
-    x_pos &= 7;\r
-\r
-    /* the higher byte of `rval' is used as a buffer */\r
-    rval  = 0;\r
-    nbits = 0;\r
-\r
-    for ( h = height; h > 0; h--, line += pitch )\r
-    {\r
-      FT_Byte*  write = line;\r
-      FT_Int    w = width;\r
-\r
-\r
-      if ( x_pos )\r
-      {\r
-        w = ( width < 8 - x_pos ) ? width : 8 - x_pos;\r
-\r
-        if ( nbits < w )\r
-        {\r
-          rval  |= *p++;\r
-          nbits += 8 - w;\r
-        }\r
-        else\r
-        {\r
-          rval  >>= 8;\r
-          nbits  -= w;\r
-        }\r
-\r
-        *write++ |= ( ( rval >> nbits ) & 0xFF ) & ~( 0xFF << w );\r
-        rval    <<= 8;\r
-\r
-        w = width - w;\r
-      }\r
-\r
-      for ( ; w >= 8; w -= 8 )\r
-      {\r
-        rval     |= *p++;\r
-        *write++ |= ( rval >> nbits ) & 0xFF;\r
-\r
-        rval <<= 8;\r
-      }\r
-\r
-      if ( w > 0 )\r
-      {\r
-        if ( nbits < w )\r
-        {\r
-          rval   |= *p++;\r
-          *write |= ( ( rval >> nbits ) & 0xFF ) & ( 0xFF00U >> w );\r
-          nbits  += 8 - w;\r
-\r
-          rval <<= 8;\r
-        }\r
-        else\r
-        {\r
-          *write |= ( ( rval >> nbits ) & 0xFF ) & ( 0xFF00U >> w );\r
-          nbits  -= w;\r
-        }\r
-      }\r
-    }\r
-\r
-  Exit:\r
-    return error;\r
-  }\r
-\r
-\r
-  static FT_Error\r
-  tt_sbit_decoder_load_compound( TT_SBitDecoder  decoder,\r
-                                 FT_Byte*        p,\r
-                                 FT_Byte*        limit,\r
-                                 FT_Int          x_pos,\r
-                                 FT_Int          y_pos )\r
-  {\r
-    FT_Error  error = SFNT_Err_Ok;\r
-    FT_UInt   num_components, nn;\r
-\r
-\r
-    if ( p + 2 > limit )\r
-      goto Fail;\r
-\r
-    num_components = FT_NEXT_USHORT( p );\r
-    if ( p + 4 * num_components > limit )\r
-      goto Fail;\r
-\r
-    for ( nn = 0; nn < num_components; nn++ )\r
-    {\r
-      FT_UInt  gindex = FT_NEXT_USHORT( p );\r
-      FT_Byte  dx     = FT_NEXT_BYTE( p );\r
-      FT_Byte  dy     = FT_NEXT_BYTE( p );\r
-\r
-\r
-      /* NB: a recursive call */\r
-      error = tt_sbit_decoder_load_image( decoder, gindex,\r
-                                          x_pos + dx, y_pos + dy );\r
-      if ( error )\r
-        break;\r
-    }\r
-\r
-  Exit:\r
-    return error;\r
-\r
-  Fail:\r
-    error = SFNT_Err_Invalid_File_Format;\r
-    goto Exit;\r
-  }\r
-\r
-\r
-  static FT_Error\r
-  tt_sbit_decoder_load_bitmap( TT_SBitDecoder  decoder,\r
-                               FT_UInt         glyph_format,\r
-                               FT_ULong        glyph_start,\r
-                               FT_ULong        glyph_size,\r
-                               FT_Int          x_pos,\r
-                               FT_Int          y_pos )\r
-  {\r
-    FT_Error   error;\r
-    FT_Stream  stream = decoder->stream;\r
-    FT_Byte*   p;\r
-    FT_Byte*   p_limit;\r
-    FT_Byte*   data;\r
-\r
-\r
-    /* seek into the EBDT table now */\r
-    if ( glyph_start + glyph_size > decoder->ebdt_size )\r
-    {\r
-      error = SFNT_Err_Invalid_Argument;\r
-      goto Exit;\r
-    }\r
-\r
-    if ( FT_STREAM_SEEK( decoder->ebdt_start + glyph_start ) ||\r
-         FT_FRAME_EXTRACT( glyph_size, data )                )\r
-      goto Exit;\r
-\r
-    p       = data;\r
-    p_limit = p + glyph_size;\r
-\r
-    /* read the data, depending on the glyph format */\r
-    switch ( glyph_format )\r
-    {\r
-    case 1:\r
-    case 2:\r
-    case 8:\r
-      error = tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 0 );\r
-      break;\r
-\r
-    case 6:\r
-    case 7:\r
-    case 9:\r
-      error = tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 1 );\r
-      break;\r
-\r
-    default:\r
-      error = SFNT_Err_Ok;\r
-    }\r
-\r
-    if ( error )\r
-      goto Fail;\r
-\r
-    {\r
-      TT_SBitDecoder_LoadFunc  loader;\r
-\r
-\r
-      switch ( glyph_format )\r
-      {\r
-      case 1:\r
-      case 6:\r
-        loader = tt_sbit_decoder_load_byte_aligned;\r
-        break;\r
-\r
-      case 2:\r
-      case 5:\r
-      case 7:\r
-        loader = tt_sbit_decoder_load_bit_aligned;\r
-        break;\r
-\r
-      case 8:\r
-        if ( p + 1 > p_limit )\r
-          goto Fail;\r
-\r
-        p += 1;  /* skip padding */\r
-        /* fall-through */\r
-\r
-      case 9:\r
-        loader = tt_sbit_decoder_load_compound;\r
-        break;\r
-\r
-      default:\r
-        goto Fail;\r
-      }\r
-\r
-      error = loader( decoder, p, p_limit, x_pos, y_pos );\r
-    }\r
-\r
-  Fail:\r
-    FT_FRAME_RELEASE( data );\r
-\r
-  Exit:\r
-    return error;\r
-  }\r
-\r
-\r
-  static FT_Error\r
-  tt_sbit_decoder_load_image( TT_SBitDecoder  decoder,\r
-                              FT_UInt         glyph_index,\r
-                              FT_Int          x_pos,\r
-                              FT_Int          y_pos )\r
-  {\r
-    /*\r
-     *  First, we find the correct strike range that applies to this\r
-     *  glyph index.\r
-     */\r
-\r
-    FT_Byte*  p          = decoder->eblc_base + decoder->strike_index_array;\r
-    FT_Byte*  p_limit    = decoder->eblc_limit;\r
-    FT_ULong  num_ranges = decoder->strike_index_count;\r
-    FT_UInt   start, end, index_format, image_format;\r
-    FT_ULong  image_start = 0, image_end = 0, image_offset;\r
-\r
-\r
-    for ( ; num_ranges > 0; num_ranges-- )\r
-    {\r
-      start = FT_NEXT_USHORT( p );\r
-      end   = FT_NEXT_USHORT( p );\r
-\r
-      if ( glyph_index >= start && glyph_index <= end )\r
-        goto FoundRange;\r
-\r
-      p += 4;  /* ignore index offset */\r
-    }\r
-    goto NoBitmap;\r
-\r
-  FoundRange:\r
-    image_offset = FT_NEXT_ULONG( p );\r
-\r
-    /* overflow check */\r
-    if ( decoder->eblc_base + decoder->strike_index_array + image_offset <\r
-           decoder->eblc_base )\r
-      goto Failure;\r
-\r
-    p = decoder->eblc_base + decoder->strike_index_array + image_offset;\r
-    if ( p + 8 > p_limit )\r
-      goto NoBitmap;\r
-\r
-    /* now find the glyph's location and extend within the ebdt table */\r
-    index_format = FT_NEXT_USHORT( p );\r
-    image_format = FT_NEXT_USHORT( p );\r
-    image_offset = FT_NEXT_ULONG ( p );\r
-\r
-    switch ( index_format )\r
-    {\r
-    case 1: /* 4-byte offsets relative to `image_offset' */\r
-      {\r
-        p += 4 * ( glyph_index - start );\r
-        if ( p + 8 > p_limit )\r
-          goto NoBitmap;\r
-\r
-        image_start = FT_NEXT_ULONG( p );\r
-        image_end   = FT_NEXT_ULONG( p );\r
-\r
-        if ( image_start == image_end )  /* missing glyph */\r
-          goto NoBitmap;\r
-      }\r
-      break;\r
-\r
-    case 2: /* big metrics, constant image size */\r
-      {\r
-        FT_ULong  image_size;\r
-\r
-\r
-        if ( p + 12 > p_limit )\r
-          goto NoBitmap;\r
-\r
-        image_size = FT_NEXT_ULONG( p );\r
-\r
-        if ( tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 1 ) )\r
-          goto NoBitmap;\r
-\r
-        image_start = image_size * ( glyph_index - start );\r
-        image_end   = image_start + image_size;\r
-      }\r
-      break;\r
-\r
-    case 3: /* 2-byte offsets relative to 'image_offset' */\r
-      {\r
-        p += 2 * ( glyph_index - start );\r
-        if ( p + 4 > p_limit )\r
-          goto NoBitmap;\r
-\r
-        image_start = FT_NEXT_USHORT( p );\r
-        image_end   = FT_NEXT_USHORT( p );\r
-\r
-        if ( image_start == image_end )  /* missing glyph */\r
-          goto NoBitmap;\r
-      }\r
-      break;\r
-\r
-    case 4: /* sparse glyph array with (glyph,offset) pairs */\r
-      {\r
-        FT_ULong  mm, num_glyphs;\r
-\r
-\r
-        if ( p + 4 > p_limit )\r
-          goto NoBitmap;\r
-\r
-        num_glyphs = FT_NEXT_ULONG( p );\r
-\r
-        /* overflow check */\r
-        if ( p + ( num_glyphs + 1 ) * 4 < p )\r
-          goto Failure;\r
-\r
-        if ( p + ( num_glyphs + 1 ) * 4 > p_limit )\r
-          goto NoBitmap;\r
-\r
-        for ( mm = 0; mm < num_glyphs; mm++ )\r
-        {\r
-          FT_UInt  gindex = FT_NEXT_USHORT( p );\r
-\r
-\r
-          if ( gindex == glyph_index )\r
-          {\r
-            image_start = FT_NEXT_USHORT( p );\r
-            p          += 2;\r
-            image_end   = FT_PEEK_USHORT( p );\r
-            break;\r
-          }\r
-          p += 2;\r
-        }\r
-\r
-        if ( mm >= num_glyphs )\r
-          goto NoBitmap;\r
-      }\r
-      break;\r
-\r
-    case 5: /* constant metrics with sparse glyph codes */\r
-      {\r
-        FT_ULong  image_size, mm, num_glyphs;\r
-\r
-\r
-        if ( p + 16 > p_limit )\r
-          goto NoBitmap;\r
-\r
-        image_size = FT_NEXT_ULONG( p );\r
-\r
-        if ( tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 1 ) )\r
-          goto NoBitmap;\r
-\r
-        num_glyphs = FT_NEXT_ULONG( p );\r
-\r
-        /* overflow check */\r
-        if ( p + 2 * num_glyphs < p )\r
-          goto Failure;\r
-\r
-        if ( p + 2 * num_glyphs > p_limit )\r
-          goto NoBitmap;\r
-\r
-        for ( mm = 0; mm < num_glyphs; mm++ )\r
-        {\r
-          FT_UInt  gindex = FT_NEXT_USHORT( p );\r
-\r
-\r
-          if ( gindex == glyph_index )\r
-            break;\r
-        }\r
-\r
-        if ( mm >= num_glyphs )\r
-          goto NoBitmap;\r
-\r
-        image_start = image_size * mm;\r
-        image_end   = image_start + image_size;\r
-      }\r
-      break;\r
-\r
-    default:\r
-      goto NoBitmap;\r
-    }\r
-\r
-    if ( image_start > image_end )\r
-      goto NoBitmap;\r
-\r
-    image_end  -= image_start;\r
-    image_start = image_offset + image_start;\r
-\r
-    return tt_sbit_decoder_load_bitmap( decoder,\r
-                                        image_format,\r
-                                        image_start,\r
-                                        image_end,\r
-                                        x_pos,\r
-                                        y_pos );\r
-\r
-  Failure:\r
-    return SFNT_Err_Invalid_Table;\r
-\r
-  NoBitmap:\r
-    return SFNT_Err_Invalid_Argument;\r
-  }\r
-\r
-\r
-  FT_LOCAL( FT_Error )\r
-  tt_face_load_sbit_image( TT_Face              face,\r
-                           FT_ULong             strike_index,\r
-                           FT_UInt              glyph_index,\r
-                           FT_UInt              load_flags,\r
-                           FT_Stream            stream,\r
-                           FT_Bitmap           *map,\r
-                           TT_SBit_MetricsRec  *metrics )\r
-  {\r
-    TT_SBitDecoderRec  decoder[1];\r
-    FT_Error           error;\r
-\r
-    FT_UNUSED( load_flags );\r
-    FT_UNUSED( stream );\r
-    FT_UNUSED( map );\r
-\r
-\r
-    error = tt_sbit_decoder_init( decoder, face, strike_index, metrics );\r
-    if ( !error )\r
-    {\r
-      error = tt_sbit_decoder_load_image( decoder, glyph_index, 0, 0 );\r
-      tt_sbit_decoder_done( decoder );\r
-    }\r
-\r
-    return error;\r
-  }\r
-\r
-/* EOF */\r
+/***************************************************************************/
+/*                                                                         */
+/*  ttsbit0.c                                                              */
+/*                                                                         */
+/*    TrueType and OpenType embedded bitmap support (body).                */
+/*    This is a heap-optimized version.                                    */
+/*                                                                         */
+/*  Copyright 2005, 2006, 2007 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.                                        */
+/*                                                                         */
+/***************************************************************************/
+
+
+/* This file is included by ttsbit.c */
+
+
+#include <ft2build.h>
+#include FT_INTERNAL_DEBUG_H
+#include FT_INTERNAL_STREAM_H
+#include FT_TRUETYPE_TAGS_H
+#include "ttsbit.h"
+
+#include "sferrors.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_ttsbit
+
+
+    static const FT_Frame_Field  tt_sbit_line_metrics_fields[] =
+    {
+#undef  FT_STRUCTURE
+#define FT_STRUCTURE  TT_SBit_LineMetricsRec
+
+      /* no FT_FRAME_START */
+        FT_FRAME_CHAR( ascender ),
+        FT_FRAME_CHAR( descender ),
+        FT_FRAME_BYTE( max_width ),
+
+        FT_FRAME_CHAR( caret_slope_numerator ),
+        FT_FRAME_CHAR( caret_slope_denominator ),
+        FT_FRAME_CHAR( caret_offset ),
+
+        FT_FRAME_CHAR( min_origin_SB ),
+        FT_FRAME_CHAR( min_advance_SB ),
+        FT_FRAME_CHAR( max_before_BL ),
+        FT_FRAME_CHAR( min_after_BL ),
+        FT_FRAME_CHAR( pads[0] ),
+        FT_FRAME_CHAR( pads[1] ),
+      FT_FRAME_END
+    };
+
+    static const FT_Frame_Field  tt_strike_start_fields[] =
+    {
+#undef  FT_STRUCTURE
+#define FT_STRUCTURE  TT_SBit_StrikeRec
+
+      /* no FT_FRAME_START */
+        FT_FRAME_ULONG( ranges_offset ),
+        FT_FRAME_SKIP_LONG,
+        FT_FRAME_ULONG( num_ranges ),
+        FT_FRAME_ULONG( color_ref ),
+      FT_FRAME_END
+    };
+
+    static const FT_Frame_Field  tt_strike_end_fields[] =
+    {
+      /* no FT_FRAME_START */
+        FT_FRAME_USHORT( start_glyph ),
+        FT_FRAME_USHORT( end_glyph ),
+        FT_FRAME_BYTE  ( x_ppem ),
+        FT_FRAME_BYTE  ( y_ppem ),
+        FT_FRAME_BYTE  ( bit_depth ),
+        FT_FRAME_CHAR  ( flags ),
+      FT_FRAME_END
+    };
+
+
+  FT_LOCAL_DEF( FT_Error )
+  tt_face_load_eblc( TT_Face    face,
+                     FT_Stream  stream )
+  {
+    FT_Error   error  = SFNT_Err_Ok;
+    FT_Fixed   version;
+    FT_ULong   num_strikes, table_size;
+    FT_Byte*   p;
+    FT_Byte*   p_limit;
+    FT_UInt    count;
+
+
+    face->sbit_num_strikes = 0;
+
+    /* this table is optional */
+    error = face->goto_table( face, TTAG_EBLC, stream, &table_size );
+    if ( error )
+      error = face->goto_table( face, TTAG_bloc, stream, &table_size );
+    if ( error )
+      goto Exit;
+
+    if ( table_size < 8 )
+    {
+      FT_ERROR(( "%s: table too short!\n", "tt_face_load_sbit_strikes" ));
+      error = SFNT_Err_Invalid_File_Format;
+      goto Exit;
+    }
+
+    if ( FT_FRAME_EXTRACT( table_size, face->sbit_table ) )
+      goto Exit;
+
+    face->sbit_table_size = table_size;
+
+    p       = face->sbit_table;
+    p_limit = p + table_size;
+
+    version     = FT_NEXT_ULONG( p );
+    num_strikes = FT_NEXT_ULONG( p );
+
+    if ( version != 0x00020000UL || num_strikes >= 0x10000UL )
+    {
+      FT_ERROR(( "%s: invalid table version!\n",
+                 "tt_face_load_sbit_strikes" ));
+      error = SFNT_Err_Invalid_File_Format;
+      goto Fail;
+    }
+
+    /*
+     *  Count the number of strikes available in the table.  We are a bit
+     *  paranoid there and don't trust the data.
+     */
+    count = (FT_UInt)num_strikes;
+    if ( 8 + 48UL * count > table_size )
+      count = (FT_UInt)( ( p_limit - p ) / 48 );
+
+    face->sbit_num_strikes = count;
+
+    FT_TRACE3(( "sbit_num_strikes: %u\n", count ));
+  Exit:
+    return error;
+
+  Fail:
+    FT_FRAME_RELEASE( face->sbit_table );
+    face->sbit_table_size = 0;
+    goto Exit;
+  }
+
+
+  FT_LOCAL_DEF( void )
+  tt_face_free_eblc( TT_Face  face )
+  {
+    FT_Stream  stream = face->root.stream;
+
+
+    FT_FRAME_RELEASE( face->sbit_table );
+    face->sbit_table_size  = 0;
+    face->sbit_num_strikes = 0;
+  }
+
+
+  FT_LOCAL_DEF( FT_Error )
+  tt_face_set_sbit_strike( TT_Face          face,
+                           FT_Size_Request  req,
+                           FT_ULong*        astrike_index )
+  {
+    return FT_Match_Size( (FT_Face)face, req, 0, astrike_index );
+  }
+
+
+  FT_LOCAL_DEF( FT_Error )
+  tt_face_load_strike_metrics( TT_Face           face,
+                               FT_ULong          strike_index,
+                               FT_Size_Metrics*  metrics )
+  {
+    FT_Byte*         strike;
+
+
+    if ( strike_index >= (FT_ULong)face->sbit_num_strikes )
+      return SFNT_Err_Invalid_Argument;
+
+    strike = face->sbit_table + 8 + strike_index * 48;
+
+    metrics->x_ppem = (FT_UShort)strike[44];
+    metrics->y_ppem = (FT_UShort)strike[45];
+
+    metrics->ascender  = (FT_Char)strike[16] << 6;  /* hori.ascender  */
+    metrics->descender = (FT_Char)strike[17] << 6;  /* hori.descender */
+    metrics->height    = metrics->ascender - metrics->descender;
+
+    /* XXX: Is this correct? */
+    metrics->max_advance = ( (FT_Char)strike[22] + /* min_origin_SB  */
+                                      strike[18] + /* max_width      */
+                             (FT_Char)strike[23]   /* min_advance_SB */
+                                                 ) << 6;
+
+    return SFNT_Err_Ok;
+  }
+
+
+  typedef struct
+  {
+    TT_Face          face;
+    FT_Stream        stream;
+    FT_Bitmap*       bitmap;
+    TT_SBit_Metrics  metrics;
+    FT_Bool          metrics_loaded;
+    FT_Bool          bitmap_allocated;
+    FT_Byte          bit_depth;
+
+    FT_ULong         ebdt_start;
+    FT_ULong         ebdt_size;
+
+    FT_ULong         strike_index_array;
+    FT_ULong         strike_index_count;
+    FT_Byte*         eblc_base;
+    FT_Byte*         eblc_limit;
+
+  } TT_SBitDecoderRec, *TT_SBitDecoder;
+
+
+  static FT_Error
+  tt_sbit_decoder_init( TT_SBitDecoder       decoder,
+                        TT_Face              face,
+                        FT_ULong             strike_index,
+                        TT_SBit_MetricsRec*  metrics )
+  {
+    FT_Error   error;
+    FT_Stream  stream = face->root.stream;
+    FT_ULong   ebdt_size;
+
+
+    error = face->goto_table( face, TTAG_EBDT, stream, &ebdt_size );
+    if ( error )
+      error = face->goto_table( face, TTAG_bdat, stream, &ebdt_size );
+    if ( error )
+      goto Exit;
+
+    decoder->face    = face;
+    decoder->stream  = stream;
+    decoder->bitmap  = &face->root.glyph->bitmap;
+    decoder->metrics = metrics;
+
+    decoder->metrics_loaded   = 0;
+    decoder->bitmap_allocated = 0;
+
+    decoder->ebdt_start = FT_STREAM_POS();
+    decoder->ebdt_size  = ebdt_size;
+
+    decoder->eblc_base  = face->sbit_table;
+    decoder->eblc_limit = face->sbit_table + face->sbit_table_size;
+
+    /* now find the strike corresponding to the index */
+    {
+      FT_Byte*  p;
+
+
+      if ( 8 + 48 * strike_index + 3 * 4 + 34 + 1 > face->sbit_table_size )
+      {
+        error = SFNT_Err_Invalid_File_Format;
+        goto Exit;
+      }
+
+      p = decoder->eblc_base + 8 + 48 * strike_index;
+
+      decoder->strike_index_array = FT_NEXT_ULONG( p );
+      p                          += 4;
+      decoder->strike_index_count = FT_NEXT_ULONG( p );
+      p                          += 34;
+      decoder->bit_depth          = *p;
+
+      if ( decoder->strike_index_array > face->sbit_table_size             ||
+           decoder->strike_index_array + 8 * decoder->strike_index_count >
+             face->sbit_table_size                                         )
+        error = SFNT_Err_Invalid_File_Format;
+    }
+
+  Exit:
+    return error;
+  }
+
+
+  static void
+  tt_sbit_decoder_done( TT_SBitDecoder  decoder )
+  {
+    FT_UNUSED( decoder );
+  }
+
+
+  static FT_Error
+  tt_sbit_decoder_alloc_bitmap( TT_SBitDecoder  decoder )
+  {
+    FT_Error    error = SFNT_Err_Ok;
+    FT_UInt     width, height;
+    FT_Bitmap*  map = decoder->bitmap;
+    FT_Long     size;
+
+
+    if ( !decoder->metrics_loaded )
+    {
+      error = SFNT_Err_Invalid_Argument;
+      goto Exit;
+    }
+
+    width  = decoder->metrics->width;
+    height = decoder->metrics->height;
+
+    map->width = (int)width;
+    map->rows  = (int)height;
+
+    switch ( decoder->bit_depth )
+    {
+    case 1:
+      map->pixel_mode = FT_PIXEL_MODE_MONO;
+      map->pitch      = ( map->width + 7 ) >> 3;
+      break;
+
+    case 2:
+      map->pixel_mode = FT_PIXEL_MODE_GRAY2;
+      map->pitch      = ( map->width + 3 ) >> 2;
+      break;
+
+    case 4:
+      map->pixel_mode = FT_PIXEL_MODE_GRAY4;
+      map->pitch      = ( map->width + 1 ) >> 1;
+      break;
+
+    case 8:
+      map->pixel_mode = FT_PIXEL_MODE_GRAY;
+      map->pitch      = map->width;
+      break;
+
+    default:
+      error = SFNT_Err_Invalid_File_Format;
+      goto Exit;
+    }
+
+    size = map->rows * map->pitch;
+
+    /* check that there is no empty image */
+    if ( size == 0 )
+      goto Exit;     /* exit successfully! */
+
+    error = ft_glyphslot_alloc_bitmap( decoder->face->root.glyph, size );
+    if ( error )
+      goto Exit;
+
+    decoder->bitmap_allocated = 1;
+
+  Exit:
+    return error;
+  }
+
+
+  static FT_Error
+  tt_sbit_decoder_load_metrics( TT_SBitDecoder  decoder,
+                                FT_Byte*       *pp,
+                                FT_Byte*        limit,
+                                FT_Bool         big )
+  {
+    FT_Byte*         p       = *pp;
+    TT_SBit_Metrics  metrics = decoder->metrics;
+
+
+    if ( p + 5 > limit )
+      goto Fail;
+
+    if ( !decoder->metrics_loaded )
+    {
+      metrics->height       = p[0];
+      metrics->width        = p[1];
+      metrics->horiBearingX = (FT_Char)p[2];
+      metrics->horiBearingY = (FT_Char)p[3];
+      metrics->horiAdvance  = p[4];
+    }
+
+    p += 5;
+    if ( big )
+    {
+      if ( p + 3 > limit )
+        goto Fail;
+
+      if ( !decoder->metrics_loaded )
+      {
+        metrics->vertBearingX = (FT_Char)p[0];
+        metrics->vertBearingY = (FT_Char)p[1];
+        metrics->vertAdvance  = p[2];
+      }
+
+      p += 3;
+    }
+
+    decoder->metrics_loaded = 1;
+    *pp = p;
+    return 0;
+
+  Fail:
+    return SFNT_Err_Invalid_Argument;
+  }
+
+
+  /* forward declaration */
+  static FT_Error
+  tt_sbit_decoder_load_image( TT_SBitDecoder  decoder,
+                              FT_UInt         glyph_index,
+                              FT_Int          x_pos,
+                              FT_Int          y_pos );
+
+  typedef FT_Error  (*TT_SBitDecoder_LoadFunc)( TT_SBitDecoder  decoder,
+                                                FT_Byte*        p,
+                                                FT_Byte*        plimit,
+                                                FT_Int          x_pos,
+                                                FT_Int          y_pos );
+
+
+  static FT_Error
+  tt_sbit_decoder_load_byte_aligned( TT_SBitDecoder  decoder,
+                                     FT_Byte*        p,
+                                     FT_Byte*        limit,
+                                     FT_Int          x_pos,
+                                     FT_Int          y_pos )
+  {
+    FT_Error    error = SFNT_Err_Ok;
+    FT_Byte*    line;
+    FT_Int      bit_height, bit_width, pitch, width, height, h;
+    FT_Bitmap*  bitmap;
+
+
+    if ( !decoder->bitmap_allocated )
+    {
+      error = tt_sbit_decoder_alloc_bitmap( decoder );
+      if ( error )
+        goto Exit;
+    }
+
+    /* check that we can write the glyph into the bitmap */
+    bitmap     = decoder->bitmap;
+    bit_width  = bitmap->width;
+    bit_height = bitmap->rows;
+    pitch      = bitmap->pitch;
+    line       = bitmap->buffer;
+
+    width  = decoder->metrics->width;
+    height = decoder->metrics->height;
+
+    if ( x_pos < 0 || x_pos + width > bit_width   ||
+         y_pos < 0 || y_pos + height > bit_height )
+    {
+      error = SFNT_Err_Invalid_File_Format;
+      goto Exit;
+    }
+
+    if ( p + ( ( width + 7 ) >> 3 ) * height > limit )
+    {
+      error = SFNT_Err_Invalid_File_Format;
+      goto Exit;
+    }
+
+    /* now do the blit */
+    line  += y_pos * pitch + ( x_pos >> 3 );
+    x_pos &= 7;
+
+    if ( x_pos == 0 )  /* the easy one */
+    {
+      for ( h = height; h > 0; h--, line += pitch )
+      {
+        FT_Byte*  write = line;
+        FT_Int    w;
+
+
+        for ( w = width; w >= 8; w -= 8 )
+        {
+          write[0] = (FT_Byte)( write[0] | *p++ );
+          write   += 1;
+        }
+
+        if ( w > 0 )
+          write[0] = (FT_Byte)( write[0] | ( *p++ & ( 0xFF00U >> w ) ) );
+      }
+    }
+    else  /* x_pos > 0 */
+    {
+      for ( h = height; h > 0; h--, line += pitch )
+      {
+        FT_Byte*  write = line;
+        FT_Int    w;
+        FT_UInt   wval = 0;
+
+
+        for ( w = width; w >= 8; w -= 8 )
+        {
+          wval      = (FT_UInt)( wval | *p++ );
+          write[0]  = (FT_Byte)( write[0] | ( wval >> x_pos ) );
+          write    += 1;
+          wval    <<= 8;
+        }
+
+        if ( w > 0 )
+          wval = (FT_UInt)( wval | ( *p++ & ( 0xFF00U >> w ) ) );
+
+        /* all bits read and there are ( x_pos + w ) bits to be written */
+
+        write[0] = (FT_Byte)( write[0] | ( wval >> x_pos ) );
+
+        if ( x_pos + w > 8 )
+        {
+          write++;
+          wval   <<= 8;
+          write[0] = (FT_Byte)( write[0] | ( wval >> x_pos ) );
+        }
+      }
+    }
+
+  Exit:
+    return error;
+  }
+
+
+  static FT_Error
+  tt_sbit_decoder_load_bit_aligned( TT_SBitDecoder  decoder,
+                                    FT_Byte*        p,
+                                    FT_Byte*        limit,
+                                    FT_Int          x_pos,
+                                    FT_Int          y_pos )
+  {
+    FT_Error    error = SFNT_Err_Ok;
+    FT_Byte*    line;
+    FT_Int      bit_height, bit_width, pitch, width, height, h, nbits;
+    FT_Bitmap*  bitmap;
+    FT_UShort   rval;
+
+
+    if ( !decoder->bitmap_allocated )
+    {
+      error = tt_sbit_decoder_alloc_bitmap( decoder );
+      if ( error )
+        goto Exit;
+    }
+
+    /* check that we can write the glyph into the bitmap */
+    bitmap     = decoder->bitmap;
+    bit_width  = bitmap->width;
+    bit_height = bitmap->rows;
+    pitch      = bitmap->pitch;
+    line       = bitmap->buffer;
+
+    width  = decoder->metrics->width;
+    height = decoder->metrics->height;
+
+    if ( x_pos < 0 || x_pos + width  > bit_width  ||
+         y_pos < 0 || y_pos + height > bit_height )
+    {
+      error = SFNT_Err_Invalid_File_Format;
+      goto Exit;
+    }
+
+    if ( p + ( ( width * height + 7 ) >> 3 ) > limit )
+    {
+      error = SFNT_Err_Invalid_File_Format;
+      goto Exit;
+    }
+
+    /* now do the blit */
+    line  += y_pos * pitch + ( x_pos >> 3 );
+    x_pos &= 7;
+
+    /* the higher byte of `rval' is used as a buffer */
+    rval  = 0;
+    nbits = 0;
+
+    for ( h = height; h > 0; h--, line += pitch )
+    {
+      FT_Byte*  write = line;
+      FT_Int    w = width;
+
+
+      if ( x_pos )
+      {
+        w = ( width < 8 - x_pos ) ? width : 8 - x_pos;
+
+        if ( nbits < w )
+        {
+          rval  |= *p++;
+          nbits += 8 - w;
+        }
+        else
+        {
+          rval  >>= 8;
+          nbits  -= w;
+        }
+
+        *write++ |= ( ( rval >> nbits ) & 0xFF ) & ~( 0xFF << w );
+        rval    <<= 8;
+
+        w = width - w;
+      }
+
+      for ( ; w >= 8; w -= 8 )
+      {
+        rval     |= *p++;
+        *write++ |= ( rval >> nbits ) & 0xFF;
+
+        rval <<= 8;
+      }
+
+      if ( w > 0 )
+      {
+        if ( nbits < w )
+        {
+          rval   |= *p++;
+          *write |= ( ( rval >> nbits ) & 0xFF ) & ( 0xFF00U >> w );
+          nbits  += 8 - w;
+
+          rval <<= 8;
+        }
+        else
+        {
+          *write |= ( ( rval >> nbits ) & 0xFF ) & ( 0xFF00U >> w );
+          nbits  -= w;
+        }
+      }
+    }
+
+  Exit:
+    return error;
+  }
+
+
+  static FT_Error
+  tt_sbit_decoder_load_compound( TT_SBitDecoder  decoder,
+                                 FT_Byte*        p,
+                                 FT_Byte*        limit,
+                                 FT_Int          x_pos,
+                                 FT_Int          y_pos )
+  {
+    FT_Error  error = SFNT_Err_Ok;
+    FT_UInt   num_components, nn;
+
+
+    if ( p + 2 > limit )
+      goto Fail;
+
+    num_components = FT_NEXT_USHORT( p );
+    if ( p + 4 * num_components > limit )
+      goto Fail;
+
+    for ( nn = 0; nn < num_components; nn++ )
+    {
+      FT_UInt  gindex = FT_NEXT_USHORT( p );
+      FT_Byte  dx     = FT_NEXT_BYTE( p );
+      FT_Byte  dy     = FT_NEXT_BYTE( p );
+
+
+      /* NB: a recursive call */
+      error = tt_sbit_decoder_load_image( decoder, gindex,
+                                          x_pos + dx, y_pos + dy );
+      if ( error )
+        break;
+    }
+
+  Exit:
+    return error;
+
+  Fail:
+    error = SFNT_Err_Invalid_File_Format;
+    goto Exit;
+  }
+
+
+  static FT_Error
+  tt_sbit_decoder_load_bitmap( TT_SBitDecoder  decoder,
+                               FT_UInt         glyph_format,
+                               FT_ULong        glyph_start,
+                               FT_ULong        glyph_size,
+                               FT_Int          x_pos,
+                               FT_Int          y_pos )
+  {
+    FT_Error   error;
+    FT_Stream  stream = decoder->stream;
+    FT_Byte*   p;
+    FT_Byte*   p_limit;
+    FT_Byte*   data;
+
+
+    /* seek into the EBDT table now */
+    if ( glyph_start + glyph_size > decoder->ebdt_size )
+    {
+      error = SFNT_Err_Invalid_Argument;
+      goto Exit;
+    }
+
+    if ( FT_STREAM_SEEK( decoder->ebdt_start + glyph_start ) ||
+         FT_FRAME_EXTRACT( glyph_size, data )                )
+      goto Exit;
+
+    p       = data;
+    p_limit = p + glyph_size;
+
+    /* read the data, depending on the glyph format */
+    switch ( glyph_format )
+    {
+    case 1:
+    case 2:
+    case 8:
+      error = tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 0 );
+      break;
+
+    case 6:
+    case 7:
+    case 9:
+      error = tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 1 );
+      break;
+
+    default:
+      error = SFNT_Err_Ok;
+    }
+
+    if ( error )
+      goto Fail;
+
+    {
+      TT_SBitDecoder_LoadFunc  loader;
+
+
+      switch ( glyph_format )
+      {
+      case 1:
+      case 6:
+        loader = tt_sbit_decoder_load_byte_aligned;
+        break;
+
+      case 2:
+      case 5:
+      case 7:
+        loader = tt_sbit_decoder_load_bit_aligned;
+        break;
+
+      case 8:
+        if ( p + 1 > p_limit )
+          goto Fail;
+
+        p += 1;  /* skip padding */
+        /* fall-through */
+
+      case 9:
+        loader = tt_sbit_decoder_load_compound;
+        break;
+
+      default:
+        goto Fail;
+      }
+
+      error = loader( decoder, p, p_limit, x_pos, y_pos );
+    }
+
+  Fail:
+    FT_FRAME_RELEASE( data );
+
+  Exit:
+    return error;
+  }
+
+
+  static FT_Error
+  tt_sbit_decoder_load_image( TT_SBitDecoder  decoder,
+                              FT_UInt         glyph_index,
+                              FT_Int          x_pos,
+                              FT_Int          y_pos )
+  {
+    /*
+     *  First, we find the correct strike range that applies to this
+     *  glyph index.
+     */
+
+    FT_Byte*  p          = decoder->eblc_base + decoder->strike_index_array;
+    FT_Byte*  p_limit    = decoder->eblc_limit;
+    FT_ULong  num_ranges = decoder->strike_index_count;
+    FT_UInt   start, end, index_format, image_format;
+    FT_ULong  image_start = 0, image_end = 0, image_offset;
+
+
+    for ( ; num_ranges > 0; num_ranges-- )
+    {
+      start = FT_NEXT_USHORT( p );
+      end   = FT_NEXT_USHORT( p );
+
+      if ( glyph_index >= start && glyph_index <= end )
+        goto FoundRange;
+
+      p += 4;  /* ignore index offset */
+    }
+    goto NoBitmap;
+
+  FoundRange:
+    image_offset = FT_NEXT_ULONG( p );
+
+    /* overflow check */
+    if ( decoder->eblc_base + decoder->strike_index_array + image_offset <
+           decoder->eblc_base )
+      goto Failure;
+
+    p = decoder->eblc_base + decoder->strike_index_array + image_offset;
+    if ( p + 8 > p_limit )
+      goto NoBitmap;
+
+    /* now find the glyph's location and extend within the ebdt table */
+    index_format = FT_NEXT_USHORT( p );
+    image_format = FT_NEXT_USHORT( p );
+    image_offset = FT_NEXT_ULONG ( p );
+
+    switch ( index_format )
+    {
+    case 1: /* 4-byte offsets relative to `image_offset' */
+      {
+        p += 4 * ( glyph_index - start );
+        if ( p + 8 > p_limit )
+          goto NoBitmap;
+
+        image_start = FT_NEXT_ULONG( p );
+        image_end   = FT_NEXT_ULONG( p );
+
+        if ( image_start == image_end )  /* missing glyph */
+          goto NoBitmap;
+      }
+      break;
+
+    case 2: /* big metrics, constant image size */
+      {
+        FT_ULong  image_size;
+
+
+        if ( p + 12 > p_limit )
+          goto NoBitmap;
+
+        image_size = FT_NEXT_ULONG( p );
+
+        if ( tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 1 ) )
+          goto NoBitmap;
+
+        image_start = image_size * ( glyph_index - start );
+        image_end   = image_start + image_size;
+      }
+      break;
+
+    case 3: /* 2-byte offsets relative to 'image_offset' */
+      {
+        p += 2 * ( glyph_index - start );
+        if ( p + 4 > p_limit )
+          goto NoBitmap;
+
+        image_start = FT_NEXT_USHORT( p );
+        image_end   = FT_NEXT_USHORT( p );
+
+        if ( image_start == image_end )  /* missing glyph */
+          goto NoBitmap;
+      }
+      break;
+
+    case 4: /* sparse glyph array with (glyph,offset) pairs */
+      {
+        FT_ULong  mm, num_glyphs;
+
+
+        if ( p + 4 > p_limit )
+          goto NoBitmap;
+
+        num_glyphs = FT_NEXT_ULONG( p );
+
+        /* overflow check */
+        if ( p + ( num_glyphs + 1 ) * 4 < p )
+          goto Failure;
+
+        if ( p + ( num_glyphs + 1 ) * 4 > p_limit )
+          goto NoBitmap;
+
+        for ( mm = 0; mm < num_glyphs; mm++ )
+        {
+          FT_UInt  gindex = FT_NEXT_USHORT( p );
+
+
+          if ( gindex == glyph_index )
+          {
+            image_start = FT_NEXT_USHORT( p );
+            p          += 2;
+            image_end   = FT_PEEK_USHORT( p );
+            break;
+          }
+          p += 2;
+        }
+
+        if ( mm >= num_glyphs )
+          goto NoBitmap;
+      }
+      break;
+
+    case 5: /* constant metrics with sparse glyph codes */
+      {
+        FT_ULong  image_size, mm, num_glyphs;
+
+
+        if ( p + 16 > p_limit )
+          goto NoBitmap;
+
+        image_size = FT_NEXT_ULONG( p );
+
+        if ( tt_sbit_decoder_load_metrics( decoder, &p, p_limit, 1 ) )
+          goto NoBitmap;
+
+        num_glyphs = FT_NEXT_ULONG( p );
+
+        /* overflow check */
+        if ( p + 2 * num_glyphs < p )
+          goto Failure;
+
+        if ( p + 2 * num_glyphs > p_limit )
+          goto NoBitmap;
+
+        for ( mm = 0; mm < num_glyphs; mm++ )
+        {
+          FT_UInt  gindex = FT_NEXT_USHORT( p );
+
+
+          if ( gindex == glyph_index )
+            break;
+        }
+
+        if ( mm >= num_glyphs )
+          goto NoBitmap;
+
+        image_start = image_size * mm;
+        image_end   = image_start + image_size;
+      }
+      break;
+
+    default:
+      goto NoBitmap;
+    }
+
+    if ( image_start > image_end )
+      goto NoBitmap;
+
+    image_end  -= image_start;
+    image_start = image_offset + image_start;
+
+    return tt_sbit_decoder_load_bitmap( decoder,
+                                        image_format,
+                                        image_start,
+                                        image_end,
+                                        x_pos,
+                                        y_pos );
+
+  Failure:
+    return SFNT_Err_Invalid_Table;
+
+  NoBitmap:
+    return SFNT_Err_Invalid_Argument;
+  }
+
+
+  FT_LOCAL( FT_Error )
+  tt_face_load_sbit_image( TT_Face              face,
+                           FT_ULong             strike_index,
+                           FT_UInt              glyph_index,
+                           FT_UInt              load_flags,
+                           FT_Stream            stream,
+                           FT_Bitmap           *map,
+                           TT_SBit_MetricsRec  *metrics )
+  {
+    TT_SBitDecoderRec  decoder[1];
+    FT_Error           error;
+
+    FT_UNUSED( load_flags );
+    FT_UNUSED( stream );
+    FT_UNUSED( map );
+
+
+    error = tt_sbit_decoder_init( decoder, face, strike_index, metrics );
+    if ( !error )
+    {
+      error = tt_sbit_decoder_load_image( decoder, glyph_index, 0, 0 );
+      tt_sbit_decoder_done( decoder );
+    }
+
+    return error;
+  }
+
+/* EOF */