From fba3b6d1979c1d1ad0d56d46fc2d787f111c07fb Mon Sep 17 00:00:00 2001 From: marha Date: Thu, 26 Jun 2014 09:46:14 +0200 Subject: Updated to freetype 2.5.3 --- freetype/src/sfnt/pngshim.c | 57 +++- freetype/src/sfnt/pngshim.h | 5 +- freetype/src/sfnt/sfdriver.c | 8 +- freetype/src/sfnt/sfobjs.c | 421 +++++++++++++++++++++++++++++- freetype/src/sfnt/ttcmap.c | 74 ++++-- freetype/src/sfnt/ttkern.c | 6 +- freetype/src/sfnt/ttload.c | 52 ++-- freetype/src/sfnt/ttmtx.c | 19 +- freetype/src/sfnt/ttsbit.c | 607 +++++++++++++++++++++++++++++++------------ freetype/src/sfnt/ttsbit.h | 4 +- 10 files changed, 1004 insertions(+), 249 deletions(-) (limited to 'freetype/src/sfnt') diff --git a/freetype/src/sfnt/pngshim.c b/freetype/src/sfnt/pngshim.c index 408f879c3..878de1fef 100644 --- a/freetype/src/sfnt/pngshim.c +++ b/freetype/src/sfnt/pngshim.c @@ -122,7 +122,7 @@ error_callback( png_structp png, png_const_charp error_msg ) { - FT_Error* error = png_get_error_ptr( png ); + FT_Error* error = (FT_Error*)png_get_error_ptr( png ); FT_UNUSED( error_msg ); @@ -159,7 +159,7 @@ if ( FT_FRAME_ENTER( length ) ) { - FT_Error* e = png_get_error_ptr( png ); + FT_Error* e = (FT_Error*)png_get_error_ptr( png ); *e = FT_THROW( Invalid_Stream_Read ); @@ -174,16 +174,18 @@ } - static FT_Error - Load_SBit_Png( FT_Bitmap* map, + FT_LOCAL_DEF( FT_Error ) + Load_SBit_Png( FT_GlyphSlot slot, FT_Int x_offset, FT_Int y_offset, FT_Int pix_bits, TT_SBit_Metrics metrics, FT_Memory memory, FT_Byte* data, - FT_UInt png_len ) + FT_UInt png_len, + FT_Bool populate_map_and_metrics ) { + FT_Bitmap *map = &slot->bitmap; FT_Error error = FT_Err_Ok; FT_StreamRec stream; @@ -193,12 +195,21 @@ int bitdepth, color_type, interlace; FT_Int i; - png_byte* *rows; + png_byte* *rows = NULL; /* pacify compiler */ - if ( x_offset < 0 || x_offset + metrics->width > map->width || - y_offset < 0 || y_offset + metrics->height > map->rows || - pix_bits != 32 || map->pixel_mode != FT_PIXEL_MODE_BGRA ) + if ( x_offset < 0 || + y_offset < 0 ) + { + error = FT_THROW( Invalid_Argument ); + goto Exit; + } + + if ( !populate_map_and_metrics && + ( x_offset + metrics->width > map->width || + y_offset + metrics->height > map->rows || + pix_bits != 32 || + map->pixel_mode != FT_PIXEL_MODE_BGRA ) ) { error = FT_THROW( Invalid_Argument ); goto Exit; @@ -238,11 +249,33 @@ &bitdepth, &color_type, &interlace, NULL, NULL ); - if ( error != FT_Err_Ok || - (FT_Int)imgWidth != metrics->width || - (FT_Int)imgHeight != metrics->height ) + if ( error || + ( !populate_map_and_metrics && + ( (FT_Int)imgWidth != metrics->width || + (FT_Int)imgHeight != metrics->height ) ) ) goto DestroyExit; + if ( populate_map_and_metrics ) + { + FT_Long size; + + + metrics->width = (FT_Int)imgWidth; + metrics->height = (FT_Int)imgHeight; + + map->width = metrics->width; + map->rows = metrics->height; + map->pixel_mode = FT_PIXEL_MODE_BGRA; + map->pitch = map->width * 4; + map->num_grays = 256; + + size = map->rows * map->pitch; + + error = ft_glyphslot_alloc_bitmap( slot, size ); + if ( error ) + goto DestroyExit; + } + /* convert palette/gray image to rgb */ if ( color_type == PNG_COLOR_TYPE_PALETTE ) png_set_palette_to_rgb( png ); diff --git a/freetype/src/sfnt/pngshim.h b/freetype/src/sfnt/pngshim.h index 8a2e69ccf..dc9ecaf91 100644 --- a/freetype/src/sfnt/pngshim.h +++ b/freetype/src/sfnt/pngshim.h @@ -29,14 +29,15 @@ FT_BEGIN_HEADER #ifdef FT_CONFIG_OPTION_USE_PNG FT_LOCAL( FT_Error ) - Load_SBit_Png( FT_Bitmap* map, + Load_SBit_Png( FT_GlyphSlot slot, FT_Int x_offset, FT_Int y_offset, FT_Int pix_bits, TT_SBit_Metrics metrics, FT_Memory memory, FT_Byte* data, - FT_UInt png_len ); + FT_UInt png_len, + FT_Bool populate_map_and_metrics ); #endif diff --git a/freetype/src/sfnt/sfdriver.c b/freetype/src/sfnt/sfdriver.c index a368b8cae..e4fcda5ec 100644 --- a/freetype/src/sfnt/sfdriver.c +++ b/freetype/src/sfnt/sfdriver.c @@ -4,7 +4,7 @@ /* */ /* High-level SFNT driver interface (body). */ /* */ -/* Copyright 1996-2007, 2009-2013 by */ +/* Copyright 1996-2007, 2009-2014 by */ /* David Turner, Robert Wilhelm, and Werner Lemberg. */ /* */ /* This file is part of the FreeType project, and may only be used, */ @@ -266,7 +266,7 @@ { FT_Stream stream = face->name_table.stream; FT_String* r = (FT_String*)result; - FT_Byte* p = (FT_Byte*)name->string; + FT_Byte* p; if ( FT_STREAM_SEEK( name->stringOffset ) || @@ -499,8 +499,8 @@ tt_face_load_hmtx, /* see `ttsbit.h' and `sfnt.h' */ - PUT_EMBEDDED_BITMAPS( tt_face_load_eblc ), - PUT_EMBEDDED_BITMAPS( tt_face_free_eblc ), + PUT_EMBEDDED_BITMAPS( tt_face_load_sbit ), + PUT_EMBEDDED_BITMAPS( tt_face_free_sbit ), PUT_EMBEDDED_BITMAPS( tt_face_set_sbit_strike ), PUT_EMBEDDED_BITMAPS( tt_face_load_strike_metrics ), diff --git a/freetype/src/sfnt/sfobjs.c b/freetype/src/sfnt/sfobjs.c index f975e71c3..a31c77cbe 100644 --- a/freetype/src/sfnt/sfobjs.c +++ b/freetype/src/sfnt/sfobjs.c @@ -27,6 +27,7 @@ #include FT_TRUETYPE_TAGS_H #include FT_SERVICE_POSTSCRIPT_CMAPS_H #include FT_SFNT_NAMES_H +#include FT_GZIP_H #include "sferrors.h" #ifdef TT_CONFIG_OPTION_BDF @@ -347,6 +348,380 @@ } +#define WRITE_BYTE( p, v ) \ + do \ + { \ + *(p)++ = (v) >> 0; \ + \ + } while ( 0 ) + +#define WRITE_USHORT( p, v ) \ + do \ + { \ + *(p)++ = (v) >> 8; \ + *(p)++ = (v) >> 0; \ + \ + } while ( 0 ) + +#define WRITE_ULONG( p, v ) \ + do \ + { \ + *(p)++ = (v) >> 24; \ + *(p)++ = (v) >> 16; \ + *(p)++ = (v) >> 8; \ + *(p)++ = (v) >> 0; \ + \ + } while ( 0 ) + + + static void + sfnt_stream_close( FT_Stream stream ) + { + FT_Memory memory = stream->memory; + + + FT_FREE( stream->base ); + + stream->size = 0; + stream->base = 0; + stream->close = 0; + } + + + FT_CALLBACK_DEF( int ) + compare_offsets( const void* a, + const void* b ) + { + WOFF_Table table1 = *(WOFF_Table*)a; + WOFF_Table table2 = *(WOFF_Table*)b; + + FT_ULong offset1 = table1->Offset; + FT_ULong offset2 = table2->Offset; + + + if ( offset1 > offset2 ) + return 1; + else if ( offset1 < offset2 ) + return -1; + else + return 0; + } + + + /* Replace `face->root.stream' with a stream containing the extracted */ + /* SFNT of a WOFF font. */ + + static FT_Error + woff_open_font( FT_Stream stream, + TT_Face face ) + { + FT_Memory memory = stream->memory; + FT_Error error = FT_Err_Ok; + + WOFF_HeaderRec woff; + WOFF_Table tables = NULL; + WOFF_Table* indices = NULL; + + FT_ULong woff_offset; + + FT_Byte* sfnt = NULL; + FT_Stream sfnt_stream = NULL; + + FT_Byte* sfnt_header; + FT_ULong sfnt_offset; + + FT_Int nn; + FT_ULong old_tag = 0; + + static const FT_Frame_Field woff_header_fields[] = + { +#undef FT_STRUCTURE +#define FT_STRUCTURE WOFF_HeaderRec + + FT_FRAME_START( 44 ), + FT_FRAME_ULONG ( signature ), + FT_FRAME_ULONG ( flavor ), + FT_FRAME_ULONG ( length ), + FT_FRAME_USHORT( num_tables ), + FT_FRAME_USHORT( reserved ), + FT_FRAME_ULONG ( totalSfntSize ), + FT_FRAME_USHORT( majorVersion ), + FT_FRAME_USHORT( minorVersion ), + FT_FRAME_ULONG ( metaOffset ), + FT_FRAME_ULONG ( metaLength ), + FT_FRAME_ULONG ( metaOrigLength ), + FT_FRAME_ULONG ( privOffset ), + FT_FRAME_ULONG ( privLength ), + FT_FRAME_END + }; + + + FT_ASSERT( stream == face->root.stream ); + FT_ASSERT( FT_STREAM_POS() == 0 ); + + if ( FT_STREAM_READ_FIELDS( woff_header_fields, &woff ) ) + return error; + + /* Make sure we don't recurse back here or hit TTC code. */ + if ( woff.flavor == TTAG_wOFF || woff.flavor == TTAG_ttcf ) + return FT_THROW( Invalid_Table ); + + /* Miscellaneous checks. */ + if ( woff.length != stream->size || + woff.num_tables == 0 || + 44 + woff.num_tables * 20UL >= woff.length || + 12 + woff.num_tables * 16UL >= woff.totalSfntSize || + ( woff.totalSfntSize & 3 ) != 0 || + ( woff.metaOffset == 0 && ( woff.metaLength != 0 || + woff.metaOrigLength != 0 ) ) || + ( woff.metaLength != 0 && woff.metaOrigLength == 0 ) || + ( woff.privOffset == 0 && woff.privLength != 0 ) ) + return FT_THROW( Invalid_Table ); + + if ( FT_ALLOC( sfnt, woff.totalSfntSize ) || + FT_NEW( sfnt_stream ) ) + goto Exit; + + sfnt_header = sfnt; + + /* Write sfnt header. */ + { + FT_UInt searchRange, entrySelector, rangeShift, x; + + + x = woff.num_tables; + entrySelector = 0; + while ( x ) + { + x >>= 1; + entrySelector += 1; + } + entrySelector--; + + searchRange = ( 1 << entrySelector ) * 16; + rangeShift = woff.num_tables * 16 - searchRange; + + WRITE_ULONG ( sfnt_header, woff.flavor ); + WRITE_USHORT( sfnt_header, woff.num_tables ); + WRITE_USHORT( sfnt_header, searchRange ); + WRITE_USHORT( sfnt_header, entrySelector ); + WRITE_USHORT( sfnt_header, rangeShift ); + } + + /* While the entries in the sfnt header must be sorted by the */ + /* tag value, the tables themselves are not. We thus have to */ + /* sort them by offset and check that they don't overlap. */ + + if ( FT_NEW_ARRAY( tables, woff.num_tables ) || + FT_NEW_ARRAY( indices, woff.num_tables ) ) + goto Exit; + + FT_TRACE2(( "\n" + " tag offset compLen origLen checksum\n" + " -------------------------------------------\n" )); + + if ( FT_FRAME_ENTER( 20L * woff.num_tables ) ) + goto Exit; + + for ( nn = 0; nn < woff.num_tables; nn++ ) + { + WOFF_Table table = tables + nn; + + table->Tag = FT_GET_TAG4(); + table->Offset = FT_GET_ULONG(); + table->CompLength = FT_GET_ULONG(); + table->OrigLength = FT_GET_ULONG(); + table->CheckSum = FT_GET_ULONG(); + + FT_TRACE2(( " %c%c%c%c %08lx %08lx %08lx %08lx\n", + (FT_Char)( table->Tag >> 24 ), + (FT_Char)( table->Tag >> 16 ), + (FT_Char)( table->Tag >> 8 ), + (FT_Char)( table->Tag ), + table->Offset, + table->CompLength, + table->OrigLength, + table->CheckSum )); + + if ( table->Tag <= old_tag ) + { + FT_FRAME_EXIT(); + error = FT_THROW( Invalid_Table ); + goto Exit; + } + + old_tag = table->Tag; + indices[nn] = table; + } + + FT_FRAME_EXIT(); + + /* Sort by offset. */ + + ft_qsort( indices, + woff.num_tables, + sizeof ( WOFF_Table ), + compare_offsets ); + + /* Check offsets and lengths. */ + + woff_offset = 44 + woff.num_tables * 20L; + sfnt_offset = 12 + woff.num_tables * 16L; + + for ( nn = 0; nn < woff.num_tables; nn++ ) + { + WOFF_Table table = indices[nn]; + + + if ( table->Offset != woff_offset || + table->Offset + table->CompLength > woff.length || + sfnt_offset + table->OrigLength > woff.totalSfntSize || + table->CompLength > table->OrigLength ) + { + error = FT_THROW( Invalid_Table ); + goto Exit; + } + + table->OrigOffset = sfnt_offset; + + /* The offsets must be multiples of 4. */ + woff_offset += ( table->CompLength + 3 ) & ~3; + sfnt_offset += ( table->OrigLength + 3 ) & ~3; + } + + /* + * Final checks! + * + * We don't decode and check the metadata block. + * We don't check table checksums either. + * But other than those, I think we implement all + * `MUST' checks from the spec. + */ + + if ( woff.metaOffset ) + { + if ( woff.metaOffset != woff_offset || + woff.metaOffset + woff.metaLength > woff.length ) + { + error = FT_THROW( Invalid_Table ); + goto Exit; + } + + /* We have padding only ... */ + woff_offset += woff.metaLength; + } + + if ( woff.privOffset ) + { + /* ... if it isn't the last block. */ + woff_offset = ( woff_offset + 3 ) & ~3; + + if ( woff.privOffset != woff_offset || + woff.privOffset + woff.privLength > woff.length ) + { + error = FT_THROW( Invalid_Table ); + goto Exit; + } + + /* No padding for the last block. */ + woff_offset += woff.privLength; + } + + if ( sfnt_offset != woff.totalSfntSize || + woff_offset != woff.length ) + { + error = FT_THROW( Invalid_Table ); + goto Exit; + } + + /* Write the tables. */ + + for ( nn = 0; nn < woff.num_tables; nn++ ) + { + WOFF_Table table = tables + nn; + + + /* Write SFNT table entry. */ + WRITE_ULONG( sfnt_header, table->Tag ); + WRITE_ULONG( sfnt_header, table->CheckSum ); + WRITE_ULONG( sfnt_header, table->OrigOffset ); + WRITE_ULONG( sfnt_header, table->OrigLength ); + + /* Write table data. */ + if ( FT_STREAM_SEEK( table->Offset ) || + FT_FRAME_ENTER( table->CompLength ) ) + goto Exit; + + if ( table->CompLength == table->OrigLength ) + { + /* Uncompressed data; just copy. */ + ft_memcpy( sfnt + table->OrigOffset, + stream->cursor, + table->OrigLength ); + } + else + { + /* Uncompress with zlib. */ + FT_ULong output_len = table->OrigLength; + + + error = FT_Gzip_Uncompress( memory, + sfnt + table->OrigOffset, &output_len, + stream->cursor, table->CompLength ); + if ( error ) + goto Exit; + if ( output_len != table->OrigLength ) + { + error = FT_THROW( Invalid_Table ); + goto Exit; + } + } + + FT_FRAME_EXIT(); + + /* We don't check whether the padding bytes in the WOFF file are */ + /* actually '\0'. For the output, however, we do set them properly. */ + sfnt_offset = table->OrigOffset + table->OrigLength; + while ( sfnt_offset & 3 ) + { + sfnt[sfnt_offset] = '\0'; + sfnt_offset++; + } + } + + /* Ok! Finally ready. Swap out stream and return. */ + FT_Stream_OpenMemory( sfnt_stream, sfnt, woff.totalSfntSize ); + sfnt_stream->memory = stream->memory; + sfnt_stream->close = sfnt_stream_close; + + FT_Stream_Free( + face->root.stream, + ( face->root.face_flags & FT_FACE_FLAG_EXTERNAL_STREAM ) != 0 ); + + face->root.stream = sfnt_stream; + + face->root.face_flags &= ~FT_FACE_FLAG_EXTERNAL_STREAM; + + Exit: + FT_FREE( tables ); + FT_FREE( indices ); + + if ( error ) + { + FT_FREE( sfnt ); + FT_Stream_Close( sfnt_stream ); + FT_FREE( sfnt_stream ); + } + + return error; + } + + +#undef WRITE_BYTE +#undef WRITE_USHORT +#undef WRITE_ULONG + + /* Fill in face->ttc_header. If the font is not a TTC, it is */ /* synthesized into a TTC with one offset table. */ static FT_Error @@ -373,11 +748,28 @@ face->ttc_header.version = 0; face->ttc_header.count = 0; + retry: offset = FT_STREAM_POS(); if ( FT_READ_ULONG( tag ) ) return error; + if ( tag == TTAG_wOFF ) + { + FT_TRACE2(( "sfnt_open_font: file is a WOFF; synthesizing SFNT\n" )); + + if ( FT_STREAM_SEEK( offset ) ) + return error; + + error = woff_open_font( stream, face ); + if ( error ) + return error; + + /* Swap out stream and retry! */ + stream = face->root.stream; + goto retry; + } + if ( tag != 0x00010000UL && tag != TTAG_ttcf && tag != TTAG_OTTO && @@ -480,6 +872,9 @@ if ( error ) return error; + /* Stream may have changed in sfnt_open_font. */ + stream = face->root.stream; + FT_TRACE2(( "sfnt_init_face: %08p, %ld\n", face, face_index )); if ( face_index < 0 ) @@ -504,7 +899,8 @@ #define LOAD_( x ) \ - do { \ + do \ + { \ FT_TRACE2(( "`" #x "' " )); \ FT_TRACE3(( "-->\n" )); \ \ @@ -519,7 +915,8 @@ } while ( 0 ) #define LOADM_( x, vertical ) \ - do { \ + do \ + { \ FT_TRACE2(( "`%s" #x "' ", \ vertical ? "vertical " : "" )); \ FT_TRACE3(( "-->\n" )); \ @@ -535,7 +932,8 @@ } while ( 0 ) #define GET_NAME( id, field ) \ - do { \ + do \ + { \ error = tt_face_get_name( face, TT_NAME_ID_ ## id, field ); \ if ( error ) \ goto Exit; \ @@ -555,6 +953,7 @@ #endif FT_Bool has_outline; FT_Bool is_apple_sbit; + FT_Bool is_apple_sbix; FT_Bool ignore_preferred_family = FALSE; FT_Bool ignore_preferred_subfamily = FALSE; @@ -608,6 +1007,14 @@ #endif is_apple_sbit = 0; + is_apple_sbix = !face->goto_table( face, TTAG_sbix, stream, 0 ); + + /* Apple 'sbix' color bitmaps are rendered scaled and then the 'glyf' + * outline rendered on top. We don't support that yet, so just ignore + * the 'glyf' outline and advertise it as a bitmap-only font. */ + if ( is_apple_sbix ) + has_outline = FALSE; + /* if this font doesn't contain outlines, we try to load */ /* a `bhed' table */ @@ -619,7 +1026,7 @@ /* load the font header (`head' table) if this isn't an Apple */ /* sbit font file */ - if ( !is_apple_sbit ) + if ( !is_apple_sbit || is_apple_sbix ) { LOAD_( head ); if ( error ) @@ -803,6 +1210,10 @@ /* */ /* Compute face flags. */ /* */ + if ( face->sbit_table_type == TT_SBIT_TABLE_TYPE_CBLC || + face->sbit_table_type == TT_SBIT_TABLE_TYPE_SBIX ) + flags |= FT_FACE_FLAG_COLOR; /* color glyphs */ + if ( has_outline == TRUE ) flags |= FT_FACE_FLAG_SCALABLE; /* scalable outlines */ @@ -931,7 +1342,7 @@ if ( em_size == 0 || face->os2.version == 0xFFFFU ) { - avgwidth = 0; + avgwidth = 1; em_size = 1; } diff --git a/freetype/src/sfnt/ttcmap.c b/freetype/src/sfnt/ttcmap.c index 1507202ea..c717f5ffc 100644 --- a/freetype/src/sfnt/ttcmap.c +++ b/freetype/src/sfnt/ttcmap.c @@ -4,7 +4,7 @@ /* */ /* TrueType character mapping table (cmap) support (body). */ /* */ -/* Copyright 2002-2010, 2012, 2013 by */ +/* Copyright 2002-2010, 2012-2014 by */ /* David Turner, Robert Wilhelm, and Werner Lemberg. */ /* */ /* This file is part of the FreeType project, and may only be used, */ @@ -88,9 +88,15 @@ tt_cmap0_validate( FT_Byte* table, FT_Validator valid ) { - FT_Byte* p = table + 2; - FT_UInt length = TT_NEXT_USHORT( p ); + FT_Byte* p; + FT_UInt length; + + if ( table + 2 + 2 > valid->limit ) + FT_INVALID_TOO_SHORT; + + p = table + 2; /* skip format */ + length = TT_NEXT_USHORT( p ); if ( table + length > valid->limit || length < 262 ) FT_INVALID_TOO_SHORT; @@ -279,14 +285,21 @@ tt_cmap2_validate( FT_Byte* table, FT_Validator valid ) { - FT_Byte* p = table + 2; /* skip format */ - FT_UInt length = TT_PEEK_USHORT( p ); + FT_Byte* p; + FT_UInt length; + FT_UInt n, max_subs; - FT_Byte* keys; /* keys table */ - FT_Byte* subs; /* sub-headers */ - FT_Byte* glyph_ids; /* glyph ID array */ + FT_Byte* keys; /* keys table */ + FT_Byte* subs; /* sub-headers */ + FT_Byte* glyph_ids; /* glyph ID array */ + if ( table + 2 + 2 > valid->limit ) + FT_INVALID_TOO_SHORT; + + p = table + 2; /* skip format */ + length = TT_NEXT_USHORT( p ); + if ( table + length > valid->limit || length < 6 + 512 ) FT_INVALID_TOO_SHORT; @@ -320,9 +333,8 @@ /* parse sub-headers */ for ( n = 0; n <= max_subs; n++ ) { - FT_UInt first_code, code_count, offset; - FT_Int delta; - FT_Byte* ids; + FT_UInt first_code, code_count, offset; + FT_Int delta; first_code = TT_NEXT_USHORT( p ); @@ -344,6 +356,9 @@ /* check offset */ if ( offset != 0 ) { + FT_Byte* ids; + + ids = p - 2 + offset; if ( ids < glyph_ids || ids + code_count*2 > table + length ) FT_INVALID_OFFSET; @@ -816,13 +831,20 @@ tt_cmap4_validate( FT_Byte* table, FT_Validator valid ) { - FT_Byte* p = table + 2; /* skip format */ - FT_UInt length = TT_NEXT_USHORT( p ); + FT_Byte* p; + FT_UInt length; + FT_Byte *ends, *starts, *offsets, *deltas, *glyph_ids; FT_UInt num_segs; FT_Error error = FT_Err_Ok; + if ( table + 2 + 2 > valid->limit ) + FT_INVALID_TOO_SHORT; + + p = table + 2; /* skip format */ + length = TT_NEXT_USHORT( p ); + if ( length < 16 ) FT_INVALID_TOO_SHORT; @@ -2042,9 +2064,9 @@ tt_cmap12_validate( FT_Byte* table, FT_Validator valid ) { - FT_Byte* p; - FT_ULong length; - FT_ULong num_groups; + FT_Byte* p; + FT_ULong length; + FT_ULong num_groups; if ( table + 16 > valid->limit ) @@ -2108,8 +2130,6 @@ char_code = cmap->cur_charcode + 1; - n = cmap->cur_group; - for ( n = cmap->cur_group; n < cmap->num_groups; n++ ) { p = cmap->cmap.data + 16 + 12 * n; @@ -2432,8 +2452,6 @@ char_code = cmap->cur_charcode + 1; - n = cmap->cur_group; - for ( n = cmap->cur_group; n < cmap->num_groups; n++ ) { p = cmap->cmap.data + 16 + 12 * n; @@ -2756,10 +2774,17 @@ tt_cmap14_validate( FT_Byte* table, FT_Validator valid ) { - FT_Byte* p = table + 2; - FT_ULong length = TT_NEXT_ULONG( p ); - FT_ULong num_selectors = TT_NEXT_ULONG( p ); + FT_Byte* p; + FT_ULong length; + FT_ULong num_selectors; + + if ( table + 2 + 4 + 4 > valid->limit ) + FT_INVALID_TOO_SHORT; + + p = table + 2; + length = TT_NEXT_ULONG( p ); + num_selectors = TT_NEXT_ULONG( p ); if ( length > (FT_ULong)( valid->limit - table ) || length < 10 + 11 * num_selectors ) @@ -3208,7 +3233,6 @@ { FT_Byte *p = tt_cmap14_find_variant( cmap->data + 6, variantSelector ); - FT_UInt32 *ret; FT_Int i; FT_ULong defOff; FT_ULong nondefOff; @@ -3242,6 +3266,8 @@ FT_Byte* dp; FT_UInt di, ni, k; + FT_UInt32 *ret; + p = cmap->data + nondefOff; dp = cmap->data + defOff; diff --git a/freetype/src/sfnt/ttkern.c b/freetype/src/sfnt/ttkern.c index 60ee546d7..32c4008b2 100644 --- a/freetype/src/sfnt/ttkern.c +++ b/freetype/src/sfnt/ttkern.c @@ -5,7 +5,7 @@ /* Load the basic TrueType kerning table. This doesn't handle */ /* kerning data within the GPOS table at the moment. */ /* */ -/* Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010 by */ +/* Copyright 1996-2007, 2009, 2010, 2013 by */ /* David Turner, Robert Wilhelm, and Werner Lemberg. */ /* */ /* This file is part of the FreeType project, and may only be used, */ @@ -183,7 +183,7 @@ FT_UInt right_glyph ) { FT_Int result = 0; - FT_UInt count, mask = 1; + FT_UInt count, mask; FT_Byte* p = face->kern_table; FT_Byte* p_limit = p + face->kern_table_size; @@ -196,7 +196,7 @@ count--, mask <<= 1 ) { FT_Byte* base = p; - FT_Byte* next = base; + FT_Byte* next; FT_UInt version = FT_NEXT_USHORT( p ); FT_UInt length = FT_NEXT_USHORT( p ); FT_UInt coverage = FT_NEXT_USHORT( p ); diff --git a/freetype/src/sfnt/ttload.c b/freetype/src/sfnt/ttload.c index fbe70f797..0a3cd29db 100644 --- a/freetype/src/sfnt/ttload.c +++ b/freetype/src/sfnt/ttload.c @@ -236,7 +236,8 @@ */ if ( table.Length < 0x36 ) { - FT_TRACE2(( "check_table_dir: `head' table too small\n" )); + FT_TRACE2(( "check_table_dir:" + " `head' or `bhed' table too small\n" )); error = FT_THROW( Table_Missing ); goto Exit; } @@ -246,12 +247,8 @@ goto Exit; if ( magic != 0x5F0F3CF5UL ) - { FT_TRACE2(( "check_table_dir:" - " no magic number found in `head' table\n")); - error = FT_THROW( Table_Missing ); - goto Exit; - } + " invalid magic number in `head' or `bhed' table\n")); if ( FT_STREAM_SEEK( offset + ( nn + 1 ) * 16 ) ) goto Exit; @@ -394,8 +391,8 @@ { entry->Tag = FT_GET_TAG4(); entry->CheckSum = FT_GET_ULONG(); - entry->Offset = FT_GET_LONG(); - entry->Length = FT_GET_LONG(); + entry->Offset = FT_GET_ULONG(); + entry->Length = FT_GET_ULONG(); /* ignore invalid tables */ if ( entry->Offset + entry->Length > stream->size ) @@ -1006,7 +1003,8 @@ FT_FRAME_END }; - static const FT_Frame_Field os2_fields_extra[] = + /* `OS/2' version 1 and newer */ + static const FT_Frame_Field os2_fields_extra1[] = { FT_FRAME_START( 8 ), FT_FRAME_ULONG( ulCodePageRange1 ), @@ -1014,6 +1012,7 @@ FT_FRAME_END }; + /* `OS/2' version 2 and newer */ static const FT_Frame_Field os2_fields_extra2[] = { FT_FRAME_START( 10 ), @@ -1025,6 +1024,15 @@ FT_FRAME_END }; + /* `OS/2' version 5 and newer */ + static const FT_Frame_Field os2_fields_extra5[] = + { + FT_FRAME_START( 4 ), + FT_FRAME_USHORT( usLowerOpticalPointSize ), + FT_FRAME_USHORT( usUpperOpticalPointSize ), + FT_FRAME_END + }; + /* We now support old Mac fonts where the OS/2 table doesn't */ /* exist. Simply put, we set the `version' field to 0xFFFF */ @@ -1038,18 +1046,20 @@ if ( FT_STREAM_READ_FIELDS( os2_fields, os2 ) ) goto Exit; - os2->ulCodePageRange1 = 0; - os2->ulCodePageRange2 = 0; - os2->sxHeight = 0; - os2->sCapHeight = 0; - os2->usDefaultChar = 0; - os2->usBreakChar = 0; - os2->usMaxContext = 0; + os2->ulCodePageRange1 = 0; + os2->ulCodePageRange2 = 0; + os2->sxHeight = 0; + os2->sCapHeight = 0; + os2->usDefaultChar = 0; + os2->usBreakChar = 0; + os2->usMaxContext = 0; + os2->usLowerOpticalPointSize = 0; + os2->usUpperOpticalPointSize = 0xFFFF; if ( os2->version >= 0x0001 ) { /* only version 1 tables */ - if ( FT_STREAM_READ_FIELDS( os2_fields_extra, os2 ) ) + if ( FT_STREAM_READ_FIELDS( os2_fields_extra1, os2 ) ) goto Exit; if ( os2->version >= 0x0002 ) @@ -1057,6 +1067,13 @@ /* only version 2 tables */ if ( FT_STREAM_READ_FIELDS( os2_fields_extra2, os2 ) ) goto Exit; + + if ( os2->version >= 0x0005 ) + { + /* only version 5 tables */ + if ( FT_STREAM_READ_FIELDS( os2_fields_extra5, os2 ) ) + goto Exit; + } } } @@ -1164,6 +1181,7 @@ FT_FRAME_USHORT( Style ), FT_FRAME_USHORT( TypeFamily ), FT_FRAME_USHORT( CapHeight ), + FT_FRAME_USHORT( SymbolSet ), FT_FRAME_BYTES ( TypeFace, 16 ), FT_FRAME_BYTES ( CharacterComplement, 8 ), FT_FRAME_BYTES ( FileName, 6 ), diff --git a/freetype/src/sfnt/ttmtx.c b/freetype/src/sfnt/ttmtx.c index 371a9edab..a8cc63a11 100644 --- a/freetype/src/sfnt/ttmtx.c +++ b/freetype/src/sfnt/ttmtx.c @@ -183,20 +183,23 @@ /* tt_face_get_metrics */ /* */ /* */ - /* Returns the horizontal or vertical metrics in font units for a */ - /* given glyph. The metrics are the left side bearing (resp. top */ - /* side bearing) and advance width (resp. advance height). */ + /* Return the horizontal or vertical metrics in font units for a */ + /* given glyph. The values are the left side bearing (top side */ + /* bearing for vertical metrics) and advance width (advance height */ + /* for vertical metrics). */ /* */ /* */ - /* header :: A pointer to either the horizontal or vertical metrics */ - /* structure. */ + /* face :: A pointer to the TrueType face structure. */ /* */ - /* idx :: The glyph index. */ + /* vertical :: If set to TRUE, get vertical metrics. */ + /* */ + /* gindex :: The glyph index. */ /* */ /* */ - /* bearing :: The bearing, either left side or top side. */ + /* abearing :: The bearing, either left side or top side. */ /* */ - /* advance :: The advance width resp. advance height. */ + /* aadvance :: The advance width or advance height, depending on */ + /* the `vertical' flag. */ /* */ FT_LOCAL_DEF( FT_Error ) tt_face_get_metrics( TT_Face face, diff --git a/freetype/src/sfnt/ttsbit.c b/freetype/src/sfnt/ttsbit.c index cd3e5a4a0..8ff7b9d6a 100644 --- a/freetype/src/sfnt/ttsbit.c +++ b/freetype/src/sfnt/ttsbit.c @@ -4,7 +4,7 @@ /* */ /* TrueType and OpenType embedded bitmap support (body). */ /* */ -/* Copyright 2005-2009, 2013 by */ +/* Copyright 2005-2009, 2013, 2014 by */ /* David Turner, Robert Wilhelm, and Werner Lemberg. */ /* */ /* Copyright 2013 by Google, Inc. */ @@ -28,6 +28,7 @@ #include "sferrors.h" +#include "ttmtx.h" #include "pngshim.h" @@ -42,25 +43,36 @@ FT_LOCAL_DEF( FT_Error ) - tt_face_load_eblc( TT_Face face, + tt_face_load_sbit( TT_Face face, FT_Stream stream ) { - FT_Error error = FT_Err_Ok; - FT_Fixed version; - FT_ULong num_strikes, table_size; - FT_Byte* p; - FT_Byte* p_limit; - FT_UInt count; + FT_Error error; + FT_ULong table_size; + face->sbit_table = NULL; + face->sbit_table_size = 0; + face->sbit_table_type = TT_SBIT_TABLE_TYPE_NONE; face->sbit_num_strikes = 0; - /* this table is optional */ error = face->goto_table( face, TTAG_CBLC, stream, &table_size ); - if ( error ) + if ( !error ) + face->sbit_table_type = TT_SBIT_TABLE_TYPE_CBLC; + else + { error = face->goto_table( face, TTAG_EBLC, stream, &table_size ); + if ( error ) + error = face->goto_table( face, TTAG_bloc, stream, &table_size ); + if ( !error ) + face->sbit_table_type = TT_SBIT_TABLE_TYPE_EBLC; + } + if ( error ) - error = face->goto_table( face, TTAG_bloc, stream, &table_size ); + { + error = face->goto_table( face, TTAG_sbix, stream, &table_size ); + if ( !error ) + face->sbit_table_type = TT_SBIT_TABLE_TYPE_SBIX; + } if ( error ) goto Exit; @@ -71,53 +83,130 @@ goto Exit; } - if ( FT_FRAME_EXTRACT( table_size, face->sbit_table ) ) - goto Exit; + switch ( (FT_UInt)face->sbit_table_type ) + { + case TT_SBIT_TABLE_TYPE_EBLC: + case TT_SBIT_TABLE_TYPE_CBLC: + { + FT_Byte* p; + FT_Fixed version; + FT_ULong num_strikes; + FT_UInt count; - face->sbit_table_size = table_size; - p = face->sbit_table; - p_limit = p + table_size; + if ( FT_FRAME_EXTRACT( table_size, face->sbit_table ) ) + goto Exit; - version = FT_NEXT_ULONG( p ); - num_strikes = FT_NEXT_ULONG( p ); + face->sbit_table_size = table_size; - if ( version != 0x00020000UL || num_strikes >= 0x10000UL ) - { - FT_ERROR(( "tt_face_load_sbit_strikes: invalid table version\n" )); - error = FT_THROW( Invalid_File_Format ); - goto Fail; + p = face->sbit_table; + + version = FT_NEXT_ULONG( p ); + num_strikes = FT_NEXT_ULONG( p ); + + if ( ( version & 0xFFFF0000UL ) != 0x00020000UL ) + { + error = FT_THROW( Unknown_File_Format ); + goto Exit; + } + + if ( num_strikes >= 0x10000UL ) + { + error = FT_THROW( Invalid_File_Format ); + goto Exit; + } + + /* + * 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)( ( table_size - 8 ) / 48 ); + + face->sbit_num_strikes = count; + } + break; + + case TT_SBIT_TABLE_TYPE_SBIX: + { + FT_UShort version; + FT_UShort flags; + FT_ULong num_strikes; + FT_UInt count; + + + if ( FT_FRAME_ENTER( 8 ) ) + goto Exit; + + version = FT_GET_USHORT(); + flags = FT_GET_USHORT(); + num_strikes = FT_GET_ULONG(); + + FT_FRAME_EXIT(); + + if ( version < 1 ) + { + error = FT_THROW( Unknown_File_Format ); + goto Exit; + } + if ( flags != 0x0001 || num_strikes >= 0x10000UL ) + { + error = FT_THROW( Invalid_File_Format ); + goto Exit; + } + + /* + * 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 + 4UL * count > table_size ) + count = (FT_UInt)( ( table_size - 8 ) / 4 ); + + if ( FT_STREAM_SEEK( FT_STREAM_POS() - 8 ) ) + goto Exit; + + face->sbit_table_size = 8 + count * 4; + if ( FT_FRAME_EXTRACT( face->sbit_table_size, face->sbit_table ) ) + goto Exit; + + face->sbit_num_strikes = count; + } + break; + + default: + error = FT_THROW( Unknown_File_Format ); + break; } - /* - * 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 ); + if ( !error ) + FT_TRACE3(( "sbit_num_strikes: %u\n", face->sbit_num_strikes )); - face->sbit_num_strikes = count; + return FT_Err_Ok; - FT_TRACE3(( "sbit_num_strikes: %u\n", count )); Exit: - return error; + if ( error ) + { + if ( face->sbit_table ) + FT_FRAME_RELEASE( face->sbit_table ); + face->sbit_table_size = 0; + face->sbit_table_type = TT_SBIT_TABLE_TYPE_NONE; + } - Fail: - FT_FRAME_RELEASE( face->sbit_table ); - face->sbit_table_size = 0; - goto Exit; + return error; } FT_LOCAL_DEF( void ) - tt_face_free_eblc( TT_Face face ) + tt_face_free_sbit( TT_Face face ) { FT_Stream stream = face->root.stream; FT_FRAME_RELEASE( face->sbit_table ); face->sbit_table_size = 0; + face->sbit_table_type = TT_SBIT_TABLE_TYPE_NONE; face->sbit_num_strikes = 0; } @@ -136,28 +225,85 @@ FT_ULong strike_index, FT_Size_Metrics* metrics ) { - FT_Byte* strike; - - if ( strike_index >= (FT_ULong)face->sbit_num_strikes ) return FT_THROW( Invalid_Argument ); - strike = face->sbit_table + 8 + strike_index * 48; + switch ( (FT_UInt)face->sbit_table_type ) + { + case TT_SBIT_TABLE_TYPE_EBLC: + case TT_SBIT_TABLE_TYPE_CBLC: + { + FT_Byte* strike; - 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; + strike = face->sbit_table + 8 + strike_index * 48; - /* 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; + metrics->x_ppem = (FT_UShort)strike[44]; + metrics->y_ppem = (FT_UShort)strike[45]; - return FT_Err_Ok; + metrics->ascender = (FT_Char)strike[16] << 6; /* hori.ascender */ + metrics->descender = (FT_Char)strike[17] << 6; /* hori.descender */ + metrics->height = metrics->ascender - metrics->descender; + + /* 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 FT_Err_Ok; + } + + case TT_SBIT_TABLE_TYPE_SBIX: + { + FT_Stream stream = face->root.stream; + FT_UInt offset, ppem, resolution, upem; + TT_HoriHeader *hori; + FT_ULong table_size; + + FT_Error error; + FT_Byte* p; + + + p = face->sbit_table + 8 + 4 * strike_index; + offset = FT_NEXT_ULONG( p ); + + error = face->goto_table( face, TTAG_sbix, stream, &table_size ); + if ( error ) + return error; + + if ( offset + 4 > table_size ) + return FT_THROW( Invalid_File_Format ); + + if ( FT_STREAM_SEEK( FT_STREAM_POS() + offset ) || + FT_FRAME_ENTER( 4 ) ) + return error; + + ppem = FT_GET_USHORT(); + resolution = FT_GET_USHORT(); + + FT_UNUSED( resolution ); /* What to do with this? */ + + FT_FRAME_EXIT(); + + upem = face->header.Units_Per_EM; + hori = &face->horizontal; + + metrics->x_ppem = ppem; + metrics->y_ppem = ppem; + + metrics->ascender = ppem * hori->Ascender * 64 / upem; + metrics->descender = ppem * hori->Descender * 64 / upem; + metrics->height = ppem * ( hori->Ascender - + hori->Descender + + hori->Line_Gap ) * 64 / upem; + metrics->max_advance = ppem * hori->advance_Width_Max * 64 / upem; + + return error; + } + + default: + return FT_THROW( Unknown_File_Format ); + } } @@ -253,8 +399,7 @@ static FT_Error - tt_sbit_decoder_alloc_bitmap( TT_SBitDecoder decoder, - FT_UInt load_flags ) + tt_sbit_decoder_alloc_bitmap( TT_SBitDecoder decoder ) { FT_Error error = FT_Err_Ok; FT_UInt width, height; @@ -301,18 +446,9 @@ break; case 32: - if ( load_flags & FT_LOAD_COLOR ) - { - map->pixel_mode = FT_PIXEL_MODE_BGRA; - map->pitch = map->width * 4; - map->num_grays = 256; - } - else - { - map->pixel_mode = FT_PIXEL_MODE_GRAY; - map->pitch = map->width; - map->num_grays = 256; - } + map->pixel_mode = FT_PIXEL_MODE_BGRA; + map->pitch = map->width * 4; + map->num_grays = 256; break; default: @@ -382,13 +518,11 @@ /* forward declaration */ static FT_Error tt_sbit_decoder_load_image( TT_SBitDecoder decoder, - FT_UInt load_flags, FT_UInt glyph_index, FT_Int x_pos, FT_Int y_pos ); typedef FT_Error (*TT_SBitDecoder_LoadFunc)( TT_SBitDecoder decoder, - FT_UInt load_flags, FT_Byte* p, FT_Byte* plimit, FT_Int x_pos, @@ -397,7 +531,6 @@ static FT_Error tt_sbit_decoder_load_byte_aligned( TT_SBitDecoder decoder, - FT_UInt load_flags, FT_Byte* p, FT_Byte* limit, FT_Int x_pos, @@ -408,8 +541,6 @@ FT_Int bit_height, bit_width, pitch, width, height, line_bits, h; FT_Bitmap* bitmap; - FT_UNUSED( load_flags ); - /* check that we can write the glyph into the bitmap */ bitmap = decoder->bitmap; @@ -538,7 +669,6 @@ static FT_Error tt_sbit_decoder_load_bit_aligned( TT_SBitDecoder decoder, - FT_UInt load_flags, FT_Byte* p, FT_Byte* limit, FT_Int x_pos, @@ -550,8 +680,6 @@ FT_Bitmap* bitmap; FT_UShort rval; - FT_UNUSED( load_flags ); - /* check that we can write the glyph into the bitmap */ bitmap = decoder->bitmap; @@ -664,7 +792,6 @@ static FT_Error tt_sbit_decoder_load_compound( TT_SBitDecoder decoder, - FT_UInt load_flags, FT_Byte* p, FT_Byte* limit, FT_Int x_pos, @@ -702,7 +829,7 @@ /* NB: a recursive call */ - error = tt_sbit_decoder_load_image( decoder, load_flags, gindex, + error = tt_sbit_decoder_load_image( decoder, gindex, x_pos + dx, y_pos + dy ); if ( error ) break; @@ -732,7 +859,6 @@ static FT_Error tt_sbit_decoder_load_png( TT_SBitDecoder decoder, - FT_UInt load_flags, FT_Byte* p, FT_Byte* limit, FT_Int x_pos, @@ -741,8 +867,6 @@ FT_Error error = FT_Err_Ok; FT_ULong png_len; - FT_UNUSED( load_flags ); - if ( limit - p < 4 ) { @@ -759,14 +883,15 @@ goto Exit; } - error = Load_SBit_Png( decoder->bitmap, + error = Load_SBit_Png( decoder->face->root.glyph, x_pos, y_pos, decoder->bit_depth, decoder->metrics, decoder->stream->memory, p, - png_len ); + png_len, + FALSE ); Exit: if ( !error ) @@ -779,7 +904,6 @@ static FT_Error tt_sbit_decoder_load_bitmap( TT_SBitDecoder decoder, - FT_UInt load_flags, FT_UInt glyph_format, FT_ULong glyph_start, FT_ULong glyph_size, @@ -843,8 +967,36 @@ break; case 2: - case 5: case 7: + { + /* Don't trust `glyph_format'. For example, Apple's main Korean */ + /* system font, `AppleMyungJo.ttf' (version 7.0d2e6), uses glyph */ + /* format 7, but the data is format 6. We check whether we have */ + /* an excessive number of bytes in the image: If it is equal to */ + /* the value for a byte-aligned glyph, use the other loading */ + /* routine. */ + /* */ + /* Note that for some (width,height) combinations, where the */ + /* width is not a multiple of 8, the sizes for bit- and */ + /* byte-aligned data are equal, for example (7,7) or (15,6). We */ + /* then prefer what `glyph_format' specifies. */ + + FT_UInt width = decoder->metrics->width; + FT_UInt height = decoder->metrics->height; + + FT_UInt bit_size = ( width * height + 7 ) >> 3; + FT_UInt byte_size = height * ( ( width + 7 ) >> 3 ); + + + if ( bit_size < byte_size && + byte_size == (FT_UInt)( p_limit - p ) ) + loader = tt_sbit_decoder_load_byte_aligned; + else + loader = tt_sbit_decoder_load_bit_aligned; + } + break; + + case 5: loader = tt_sbit_decoder_load_bit_aligned; break; @@ -859,12 +1011,15 @@ loader = tt_sbit_decoder_load_compound; break; -#ifdef FT_CONFIG_OPTION_USE_PNG case 17: /* small metrics, PNG image data */ case 18: /* big metrics, PNG image data */ case 19: /* metrics in EBLC, PNG image data */ +#ifdef FT_CONFIG_OPTION_USE_PNG loader = tt_sbit_decoder_load_png; break; +#else + error = FT_THROW( Unimplemented_Feature ); + goto Fail; #endif /* FT_CONFIG_OPTION_USE_PNG */ default: @@ -874,64 +1029,12 @@ if ( !decoder->bitmap_allocated ) { - error = tt_sbit_decoder_alloc_bitmap( decoder, load_flags ); + error = tt_sbit_decoder_alloc_bitmap( decoder ); if ( error ) goto Fail; } - if ( decoder->bit_depth == 32 && - decoder->bitmap->pixel_mode != FT_PIXEL_MODE_BGRA ) - { - /* Flatten color bitmaps if color was not requested. */ - - FT_Library library = decoder->face->root.glyph->library; - FT_Memory memory = decoder->stream->memory; - - FT_Bitmap color, *orig; - - - if ( decoder->bitmap->pixel_mode != FT_PIXEL_MODE_GRAY || - x_pos != 0 || y_pos != 0 ) - { - /* Shouldn't happen. */ - error = FT_THROW( Invalid_Table ); - goto Fail; - } - - FT_Bitmap_New( &color ); - - color.rows = decoder->bitmap->rows; - color.width = decoder->bitmap->width; - color.pitch = color.width * 4; - color.pixel_mode = FT_PIXEL_MODE_BGRA; - - if ( FT_ALLOC( color.buffer, color.rows * color.pitch ) ) - goto Fail; - - orig = decoder->bitmap; - decoder->bitmap = &color; - - error = loader( decoder, load_flags, p, p_limit, x_pos, y_pos ); - - decoder->bitmap = orig; - - /* explicitly test against FT_Err_Ok to avoid compiler warnings */ - /* (we do an assignment within a conditional) */ - if ( error || - ( error = FT_Bitmap_Convert( library, - &color, - decoder->bitmap, - 1 ) ) != FT_Err_Ok ) - { - FT_Bitmap_Done( library, &color ); - goto Fail; - } - - FT_Bitmap_Done( library, &color ); - } - - else - error = loader( decoder, load_flags, p, p_limit, x_pos, y_pos ); + error = loader( decoder, p, p_limit, x_pos, y_pos ); } Fail: @@ -944,7 +1047,6 @@ static FT_Error tt_sbit_decoder_load_image( TT_SBitDecoder decoder, - FT_UInt load_flags, FT_UInt glyph_index, FT_Int x_pos, FT_Int y_pos ) @@ -993,17 +1095,15 @@ switch ( index_format ) { case 1: /* 4-byte offsets relative to `image_offset' */ - { - p += 4 * ( glyph_index - start ); - if ( p + 8 > p_limit ) - goto NoBitmap; + p += 4 * ( glyph_index - start ); + if ( p + 8 > p_limit ) + goto NoBitmap; - image_start = FT_NEXT_ULONG( p ); - image_end = FT_NEXT_ULONG( p ); + image_start = FT_NEXT_ULONG( p ); + image_end = FT_NEXT_ULONG( p ); - if ( image_start == image_end ) /* missing glyph */ - goto NoBitmap; - } + if ( image_start == image_end ) /* missing glyph */ + goto NoBitmap; break; case 2: /* big metrics, constant image size */ @@ -1025,17 +1125,15 @@ break; case 3: /* 2-byte offsets relative to 'image_offset' */ - { - p += 2 * ( glyph_index - start ); - if ( p + 4 > p_limit ) - goto NoBitmap; + p += 2 * ( glyph_index - start ); + if ( p + 4 > p_limit ) + goto NoBitmap; - image_start = FT_NEXT_USHORT( p ); - image_end = FT_NEXT_USHORT( p ); + image_start = FT_NEXT_USHORT( p ); + image_end = FT_NEXT_USHORT( p ); - if ( image_start == image_end ) /* missing glyph */ - goto NoBitmap; - } + if ( image_start == image_end ) /* missing glyph */ + goto NoBitmap; break; case 4: /* sparse glyph array with (glyph,offset) pairs */ @@ -1124,7 +1222,6 @@ image_format, glyph_index )); return tt_sbit_decoder_load_bitmap( decoder, - load_flags, image_format, image_start, image_end, @@ -1142,6 +1239,128 @@ } + static FT_Error + tt_face_load_sbix_image( TT_Face face, + FT_ULong strike_index, + FT_UInt glyph_index, + FT_Stream stream, + FT_Bitmap *map, + TT_SBit_MetricsRec *metrics ) + { + FT_UInt sbix_pos, strike_offset, glyph_start, glyph_end; + FT_ULong table_size; + FT_Int originOffsetX, originOffsetY; + FT_Tag graphicType; + FT_Int recurse_depth = 0; + + FT_Error error; + FT_Byte* p; + + FT_UNUSED( map ); + + + metrics->width = 0; + metrics->height = 0; + + p = face->sbit_table + 8 + 4 * strike_index; + strike_offset = FT_NEXT_ULONG( p ); + + error = face->goto_table( face, TTAG_sbix, stream, &table_size ); + if ( error ) + return error; + sbix_pos = FT_STREAM_POS(); + + retry: + if ( glyph_index > (FT_UInt)face->root.num_glyphs ) + return FT_THROW( Invalid_Argument ); + + if ( strike_offset >= table_size || + table_size - strike_offset < 4 + glyph_index * 4 + 8 ) + return FT_THROW( Invalid_File_Format ); + + if ( FT_STREAM_SEEK( sbix_pos + strike_offset + 4 + glyph_index * 4 ) || + FT_FRAME_ENTER( 8 ) ) + return error; + + glyph_start = FT_GET_ULONG(); + glyph_end = FT_GET_ULONG(); + + FT_FRAME_EXIT(); + + if ( glyph_start == glyph_end ) + return FT_THROW( Invalid_Argument ); + if ( glyph_start > glyph_end || + glyph_end - glyph_start < 8 || + table_size - strike_offset < glyph_end ) + return FT_THROW( Invalid_File_Format ); + + if ( FT_STREAM_SEEK( sbix_pos + strike_offset + glyph_start ) || + FT_FRAME_ENTER( glyph_end - glyph_start ) ) + return error; + + originOffsetX = FT_GET_SHORT(); + originOffsetY = FT_GET_SHORT(); + + graphicType = FT_GET_TAG4(); + + switch ( graphicType ) + { + case FT_MAKE_TAG( 'd', 'u', 'p', 'e' ): + if ( recurse_depth < 4 ) + { + glyph_index = FT_GET_USHORT(); + FT_FRAME_EXIT(); + recurse_depth++; + goto retry; + } + error = FT_THROW( Invalid_File_Format ); + break; + + case FT_MAKE_TAG( 'p', 'n', 'g', ' ' ): +#ifdef FT_CONFIG_OPTION_USE_PNG + error = Load_SBit_Png( face->root.glyph, + 0, + 0, + 32, + metrics, + stream->memory, + stream->cursor, + glyph_end - glyph_start - 8, + TRUE ); +#else + error = FT_THROW( Unimplemented_Feature ); +#endif + break; + + case FT_MAKE_TAG( 'j', 'p', 'g', ' ' ): + case FT_MAKE_TAG( 't', 'i', 'f', 'f' ): + error = FT_THROW( Unknown_File_Format ); + break; + + default: + error = FT_THROW( Unimplemented_Feature ); + break; + } + + FT_FRAME_EXIT(); + + if ( !error ) + { + FT_Short abearing; + FT_UShort aadvance; + + + tt_face_get_metrics( face, FALSE, glyph_index, &abearing, &aadvance ); + + metrics->horiBearingX = originOffsetX; + metrics->horiBearingY = -originOffsetY + metrics->height; + metrics->horiAdvance = aadvance * face->root.size->metrics.x_ppem / + face->header.Units_Per_EM; + } + + return error; + } + FT_LOCAL( FT_Error ) tt_face_load_sbit_image( TT_Face face, FT_ULong strike_index, @@ -1151,23 +1370,67 @@ FT_Bitmap *map, TT_SBit_MetricsRec *metrics ) { - TT_SBitDecoderRec decoder[1]; - FT_Error error; + FT_Error error = FT_Err_Ok; - FT_UNUSED( load_flags ); - FT_UNUSED( stream ); - FT_UNUSED( map ); + switch ( (FT_UInt)face->sbit_table_type ) + { + case TT_SBIT_TABLE_TYPE_EBLC: + case TT_SBIT_TABLE_TYPE_CBLC: + { + TT_SBitDecoderRec decoder[1]; - error = tt_sbit_decoder_init( decoder, face, strike_index, metrics ); - if ( !error ) + + 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 ); + } + } + break; + + case TT_SBIT_TABLE_TYPE_SBIX: + error = tt_face_load_sbix_image( face, + strike_index, + glyph_index, + stream, + map, + metrics ); + break; + + default: + error = FT_THROW( Unknown_File_Format ); + break; + } + + /* Flatten color bitmaps if color was not requested. */ + if ( !error && + !( load_flags & FT_LOAD_COLOR ) && + map->pixel_mode == FT_PIXEL_MODE_BGRA ) { - error = tt_sbit_decoder_load_image( decoder, - load_flags, - glyph_index, - 0, - 0 ); - tt_sbit_decoder_done( decoder ); + FT_Bitmap new_map; + FT_Library library = face->root.glyph->library; + + + FT_Bitmap_New( &new_map ); + + /* Convert to 8bit grayscale. */ + error = FT_Bitmap_Convert( library, map, &new_map, 1 ); + if ( error ) + FT_Bitmap_Done( library, &new_map ); + else + { + map->pixel_mode = new_map.pixel_mode; + map->pitch = new_map.pitch; + map->num_grays = new_map.num_grays; + + ft_glyphslot_set_bitmap( face->root.glyph, new_map.buffer ); + face->root.glyph->internal->flags |= FT_GLYPH_OWN_BITMAP; + } } return error; diff --git a/freetype/src/sfnt/ttsbit.h b/freetype/src/sfnt/ttsbit.h index ea0b5f8ad..695d0d8d0 100644 --- a/freetype/src/sfnt/ttsbit.h +++ b/freetype/src/sfnt/ttsbit.h @@ -28,11 +28,11 @@ FT_BEGIN_HEADER FT_LOCAL( FT_Error ) - tt_face_load_eblc( TT_Face face, + tt_face_load_sbit( TT_Face face, FT_Stream stream ); FT_LOCAL( void ) - tt_face_free_eblc( TT_Face face ); + tt_face_free_sbit( TT_Face face ); FT_LOCAL( FT_Error ) -- cgit v1.2.3