static FT_Error CFF_Load_FD_Select( CFF_FDSelect fdselect, FT_UInt num_glyphs, FT_Stream stream, FT_ULong offset ) { FT_Error error; FT_Byte format; FT_UInt num_ranges; /* read format */ if ( FT_STREAM_SEEK( offset ) || FT_READ_BYTE( format ) ) goto Exit; fdselect->format = format; fdselect->cache_count = 0; /* clear cache */ switch ( format ) { case 0: /* format 0, that's simple */ fdselect->data_size = num_glyphs; goto Load_Data; case 3: /* format 3, a tad more complex */ if ( FT_READ_USHORT( num_ranges ) ) goto Exit; if ( !num_ranges ) { FT_TRACE0(( "CFF_Load_FD_Select: empty FDSelect array\n" )); error = FT_THROW( Invalid_File_Format ); goto Exit; } fdselect->data_size = num_ranges * 3 + 2; Load_Data: if ( FT_FRAME_EXTRACT( fdselect->data_size, fdselect->data ) ) goto Exit; break; default: /* hmm... that's wrong */ error = FT_THROW( Invalid_File_Format ); } Exit: return error; }
static FT_Error cff_encoding_load( CFF_Encoding encoding, CFF_Charset charset, FT_UInt num_glyphs, FT_Stream stream, FT_ULong base_offset, FT_ULong offset ) { FT_Error error = CFF_Err_Ok; FT_UInt count; FT_UInt j; FT_UShort glyph_sid; FT_UInt glyph_code; /* Check for charset->sids. If we do not have this, we fail. */ if ( !charset->sids ) { error = CFF_Err_Invalid_File_Format; goto Exit; } /* Zero out the code to gid/sid mappings. */ for ( j = 0; j < 256; j++ ) { encoding->sids [j] = 0; encoding->codes[j] = 0; } /* Note: The encoding table in a CFF font is indexed by glyph index; */ /* the first encoded glyph index is 1. Hence, we read the character */ /* code (`glyph_code') at index j and make the assignment: */ /* */ /* encoding->codes[glyph_code] = j + 1 */ /* */ /* We also make the assignment: */ /* */ /* encoding->sids[glyph_code] = charset->sids[j + 1] */ /* */ /* This gives us both a code to GID and a code to SID mapping. */ if ( offset > 1 ) { encoding->offset = base_offset + offset; /* we need to parse the table to determine its size */ if ( FT_STREAM_SEEK( encoding->offset ) || FT_READ_BYTE( encoding->format ) || FT_READ_BYTE( count ) ) goto Exit; switch ( encoding->format & 0x7F ) { case 0: { FT_Byte* p; /* By convention, GID 0 is always ".notdef" and is never */ /* coded in the font. Hence, the number of codes found */ /* in the table is `count+1'. */ /* */ encoding->count = count + 1; if ( FT_FRAME_ENTER( count ) ) goto Exit; p = (FT_Byte*)stream->cursor; for ( j = 1; j <= count; j++ ) { glyph_code = *p++; /* Make sure j is not too big. */ if ( j < num_glyphs ) { /* Assign code to GID mapping. */ encoding->codes[glyph_code] = (FT_UShort)j; /* Assign code to SID mapping. */ encoding->sids[glyph_code] = charset->sids[j]; } } FT_FRAME_EXIT(); } break; case 1: { FT_UInt nleft; FT_UInt i = 1; FT_UInt k; encoding->count = 0; /* Parse the Format1 ranges. */ for ( j = 0; j < count; j++, i += nleft ) { /* Read the first glyph code of the range. */ if ( FT_READ_BYTE( glyph_code ) ) goto Exit; /* Read the number of codes in the range. */ if ( FT_READ_BYTE( nleft ) ) goto Exit; /* Increment nleft, so we read `nleft + 1' codes/sids. */ nleft++; /* compute max number of character codes */ if ( (FT_UInt)nleft > encoding->count ) encoding->count = nleft; /* Fill in the range of codes/sids. */ for ( k = i; k < nleft + i; k++, glyph_code++ ) { /* Make sure k is not too big. */ if ( k < num_glyphs && glyph_code < 256 ) { /* Assign code to GID mapping. */ encoding->codes[glyph_code] = (FT_UShort)k; /* Assign code to SID mapping. */ encoding->sids[glyph_code] = charset->sids[k]; } } } /* simple check; one never knows what can be found in a font */ if ( encoding->count > 256 ) encoding->count = 256; } break; default: FT_ERROR(( "cff_encoding_load: invalid table format!\n" )); error = CFF_Err_Invalid_File_Format; goto Exit; } /* Parse supplemental encodings, if any. */ if ( encoding->format & 0x80 ) { FT_UInt gindex; /* count supplements */ if ( FT_READ_BYTE( count ) ) goto Exit; for ( j = 0; j < count; j++ ) { /* Read supplemental glyph code. */ if ( FT_READ_BYTE( glyph_code ) ) goto Exit; /* Read the SID associated with this glyph code. */ if ( FT_READ_USHORT( glyph_sid ) ) goto Exit; /* Assign code to SID mapping. */ encoding->sids[glyph_code] = glyph_sid; /* First, look up GID which has been assigned to */ /* SID glyph_sid. */ for ( gindex = 0; gindex < num_glyphs; gindex++ ) { if ( charset->sids[gindex] == glyph_sid ) { encoding->codes[glyph_code] = (FT_UShort)gindex; break; } } } } } else { /* We take into account the fact a CFF font can use a predefined */ /* encoding without containing all of the glyphs encoded by this */ /* encoding (see the note at the end of section 12 in the CFF */ /* specification). */ switch ( (FT_UInt)offset ) { case 0: /* First, copy the code to SID mapping. */ FT_ARRAY_COPY( encoding->sids, cff_standard_encoding, 256 ); goto Populate; case 1: /* First, copy the code to SID mapping. */ FT_ARRAY_COPY( encoding->sids, cff_expert_encoding, 256 ); Populate: /* Construct code to GID mapping from code to SID mapping */ /* and charset. */ encoding->count = 0; error = cff_charset_compute_cids( charset, num_glyphs, stream->memory ); if ( error ) goto Exit; for ( j = 0; j < 256; j++ ) { FT_UInt sid = encoding->sids[j]; FT_UInt gid = 0; if ( sid ) gid = cff_charset_cid_to_gindex( charset, sid ); if ( gid != 0 ) { encoding->codes[j] = (FT_UShort)gid; if ( encoding->count < j + 1 ) encoding->count = j + 1; } else { encoding->codes[j] = 0; encoding->sids [j] = 0; } } break; default: FT_ERROR(( "cff_encoding_load: invalid table format!\n" )); error = CFF_Err_Invalid_File_Format; goto Exit; } } Exit: /* Clean up if there was an error. */ return error; }
static FT_Error cff_charset_load( CFF_Charset charset, FT_UInt num_glyphs, FT_Stream stream, FT_ULong base_offset, FT_ULong offset, FT_Bool invert ) { FT_Memory memory = stream->memory; FT_Error error = CFF_Err_Ok; FT_UShort glyph_sid; /* If the the offset is greater than 2, we have to parse the */ /* charset table. */ if ( offset > 2 ) { FT_UInt j; charset->offset = base_offset + offset; /* Get the format of the table. */ if ( FT_STREAM_SEEK( charset->offset ) || FT_READ_BYTE( charset->format ) ) goto Exit; /* Allocate memory for sids. */ if ( FT_NEW_ARRAY( charset->sids, num_glyphs ) ) goto Exit; /* assign the .notdef glyph */ charset->sids[0] = 0; switch ( charset->format ) { case 0: if ( num_glyphs > 0 ) { if ( FT_FRAME_ENTER( ( num_glyphs - 1 ) * 2 ) ) goto Exit; for ( j = 1; j < num_glyphs; j++ ) charset->sids[j] = FT_GET_USHORT(); FT_FRAME_EXIT(); } break; case 1: case 2: { FT_UInt nleft; FT_UInt i; j = 1; while ( j < num_glyphs ) { /* Read the first glyph sid of the range. */ if ( FT_READ_USHORT( glyph_sid ) ) goto Exit; /* Read the number of glyphs in the range. */ if ( charset->format == 2 ) { if ( FT_READ_USHORT( nleft ) ) goto Exit; } else { if ( FT_READ_BYTE( nleft ) ) goto Exit; } /* Fill in the range of sids -- `nleft + 1' glyphs. */ for ( i = 0; j < num_glyphs && i <= nleft; i++, j++, glyph_sid++ ) charset->sids[j] = glyph_sid; } } break; default: FT_ERROR(( "cff_charset_load: invalid table format!\n" )); error = CFF_Err_Invalid_File_Format; goto Exit; } } else { /* Parse default tables corresponding to offset == 0, 1, or 2. */ /* CFF specification intimates the following: */ /* */ /* In order to use a predefined charset, the following must be */ /* true: The charset constructed for the glyphs in the font's */ /* charstrings dictionary must match the predefined charset in */ /* the first num_glyphs. */ charset->offset = offset; /* record charset type */ switch ( (FT_UInt)offset ) { case 0: if ( num_glyphs > 229 ) { FT_ERROR(( "cff_charset_load: implicit charset larger than\n" "predefined charset (Adobe ISO-Latin)!\n" )); error = CFF_Err_Invalid_File_Format; goto Exit; } /* Allocate memory for sids. */ if ( FT_NEW_ARRAY( charset->sids, num_glyphs ) ) goto Exit; /* Copy the predefined charset into the allocated memory. */ FT_ARRAY_COPY( charset->sids, cff_isoadobe_charset, num_glyphs ); break; case 1: if ( num_glyphs > 166 ) { FT_ERROR(( "cff_charset_load: implicit charset larger than\n" "predefined charset (Adobe Expert)!\n" )); error = CFF_Err_Invalid_File_Format; goto Exit; } /* Allocate memory for sids. */ if ( FT_NEW_ARRAY( charset->sids, num_glyphs ) ) goto Exit; /* Copy the predefined charset into the allocated memory. */ FT_ARRAY_COPY( charset->sids, cff_expert_charset, num_glyphs ); break; case 2: if ( num_glyphs > 87 ) { FT_ERROR(( "cff_charset_load: implicit charset larger than\n" "predefined charset (Adobe Expert Subset)!\n" )); error = CFF_Err_Invalid_File_Format; goto Exit; } /* Allocate memory for sids. */ if ( FT_NEW_ARRAY( charset->sids, num_glyphs ) ) goto Exit; /* Copy the predefined charset into the allocated memory. */ FT_ARRAY_COPY( charset->sids, cff_expertsubset_charset, num_glyphs ); break; default: error = CFF_Err_Invalid_File_Format; goto Exit; } } /* we have to invert the `sids' array for subsetted CID-keyed fonts */ if ( invert ) error = cff_charset_compute_cids( charset, num_glyphs, memory ); Exit: /* Clean up if there was an error. */ if ( error ) { FT_FREE( charset->sids ); FT_FREE( charset->cids ); charset->format = 0; charset->offset = 0; charset->sids = 0; } return error; }
static FT_Error cff_index_init( CFF_Index idx, FT_Stream stream, FT_Bool load ) { FT_Error error; FT_Memory memory = stream->memory; FT_UShort count; FT_MEM_ZERO( idx, sizeof ( *idx ) ); idx->stream = stream; idx->start = FT_STREAM_POS(); if ( !FT_READ_USHORT( count ) && count > 0 ) { FT_Byte offsize; FT_ULong size; /* there is at least one element; read the offset size, */ /* then access the offset table to compute the index's total size */ if ( FT_READ_BYTE( offsize ) ) goto Exit; if ( offsize < 1 || offsize > 4 ) { error = FT_Err_Invalid_Table; goto Exit; } idx->count = count; idx->off_size = offsize; size = (FT_ULong)( count + 1 ) * offsize; idx->data_offset = idx->start + 3 + size; if ( FT_STREAM_SKIP( size - offsize ) ) goto Exit; size = cff_index_read_offset( idx, &error ); if ( error ) goto Exit; if ( size == 0 ) { error = CFF_Err_Invalid_Table; goto Exit; } idx->data_size = --size; if ( load ) { /* load the data */ if ( FT_FRAME_EXTRACT( size, idx->bytes ) ) goto Exit; } else { /* skip the data */ if ( FT_STREAM_SKIP( size ) ) goto Exit; } } Exit: if ( error ) FT_FREE( idx->offsets ); return error; }
/* check and skip .gz header - we don't support `transparent' compression */ static FT_Error ft_gzip_check_header( FT_Stream stream ) { FT_Error error; FT_Byte head[4]; if ( FT_STREAM_SEEK( 0 ) || FT_STREAM_READ( head, 4 ) ) goto Exit; /* head[0] && head[1] are the magic numbers; */ /* head[2] is the method, and head[3] the flags */ if ( head[0] != 0x1f || head[1] != 0x8b || head[2] != Z_DEFLATED || (head[3] & FT_GZIP_RESERVED) ) { error = Gzip_Err_Invalid_File_Format; goto Exit; } /* skip time, xflags and os code */ (void)FT_STREAM_SKIP( 6 ); /* skip the extra field */ if ( head[3] & FT_GZIP_EXTRA_FIELD ) { FT_UInt len; if ( FT_READ_USHORT_LE( len ) || FT_STREAM_SKIP( len ) ) goto Exit; } /* skip original file name */ if ( head[3] & FT_GZIP_ORIG_NAME ) for (;;) { FT_UInt c; if ( FT_READ_BYTE( c ) ) goto Exit; if ( c == 0 ) break; } /* skip .gz comment */ if ( head[3] & FT_GZIP_COMMENT ) for (;;) { FT_UInt c; if ( FT_READ_BYTE( c ) ) goto Exit; if ( c == 0 ) break; } /* skip CRC */ if ( head[3] & FT_GZIP_HEAD_CRC ) if ( FT_STREAM_SKIP( 2 ) ) goto Exit; Exit: return error; }
static FT_Error load_format_20( TT_Face face, FT_Stream stream, FT_ULong post_limit ) { FT_Memory memory = stream->memory; FT_Error error; FT_Int num_glyphs; FT_UShort num_names; FT_UShort* glyph_indices = NULL; FT_Char** name_strings = NULL; if ( FT_READ_USHORT( num_glyphs ) ) goto Exit; /* UNDOCUMENTED! The number of glyphs in this table can be smaller */ /* than the value in the maxp table (cf. cyberbit.ttf). */ /* There already exist fonts which have more than 32768 glyph names */ /* in this table, so the test for this threshold has been dropped. */ if ( num_glyphs > face->max_profile.numGlyphs ) { error = FT_THROW( Invalid_File_Format ); goto Exit; } /* load the indices */ { FT_Int n; if ( FT_NEW_ARRAY ( glyph_indices, num_glyphs ) || FT_FRAME_ENTER( num_glyphs * 2L ) ) goto Fail; for ( n = 0; n < num_glyphs; n++ ) glyph_indices[n] = FT_GET_USHORT(); FT_FRAME_EXIT(); } /* compute number of names stored in table */ { FT_Int n; num_names = 0; for ( n = 0; n < num_glyphs; n++ ) { FT_Int idx; idx = glyph_indices[n]; if ( idx >= 258 ) { idx -= 257; if ( idx > num_names ) num_names = (FT_UShort)idx; } } } /* now load the name strings */ { FT_UShort n; if ( FT_NEW_ARRAY( name_strings, num_names ) ) goto Fail; for ( n = 0; n < num_names; n++ ) { FT_UInt len; if ( FT_STREAM_POS() >= post_limit ) break; else { FT_TRACE6(( "load_format_20: %d byte left in post table\n", post_limit - FT_STREAM_POS() )); if ( FT_READ_BYTE( len ) ) goto Fail1; } if ( len > post_limit || FT_STREAM_POS() > post_limit - len ) { FT_Int d = (FT_Int)post_limit - (FT_Int)FT_STREAM_POS(); FT_ERROR(( "load_format_20:" " exceeding string length (%d)," " truncating at end of post table (%d byte left)\n", len, d )); len = (FT_UInt)FT_MAX( 0, d ); } if ( FT_NEW_ARRAY( name_strings[n], len + 1 ) || FT_STREAM_READ( name_strings[n], len ) ) goto Fail1; name_strings[n][len] = '\0'; } if ( n < num_names ) { FT_ERROR(( "load_format_20:" " all entries in post table are already parsed," " using NULL names for gid %d - %d\n", n, num_names - 1 )); for ( ; n < num_names; n++ ) if ( FT_NEW_ARRAY( name_strings[n], 1 ) ) goto Fail1; else name_strings[n][0] = '\0'; } } /* all right, set table fields and exit successfully */ { TT_Post_20 table = &face->postscript_names.names.format_20; table->num_glyphs = (FT_UShort)num_glyphs; table->num_names = (FT_UShort)num_names; table->glyph_indices = glyph_indices; table->glyph_names = name_strings; } return FT_Err_Ok; Fail1: { FT_UShort n; for ( n = 0; n < num_names; n++ ) FT_FREE( name_strings[n] ); } Fail: FT_FREE( name_strings ); FT_FREE( glyph_indices ); Exit: return error; }
static FT_Error cff_new_index( CFF_Index idx, FT_Stream stream, FT_Bool load ) { FT_Error error; FT_Memory memory = stream->memory; FT_UShort count; FT_MEM_ZERO( idx, sizeof ( *idx ) ); idx->stream = stream; if ( !FT_READ_USHORT( count ) && count > 0 ) { FT_Byte* p; FT_Byte offsize; FT_ULong data_size; FT_ULong* poff; /* there is at least one element; read the offset size, */ /* then access the offset table to compute the index's total size */ if ( FT_READ_BYTE( offsize ) ) goto Exit; idx->stream = stream; idx->count = count; idx->off_size = offsize; data_size = (FT_ULong)( count + 1 ) * offsize; if ( FT_NEW_ARRAY( idx->offsets, count + 1 ) || FT_FRAME_ENTER( data_size ) ) goto Exit; poff = idx->offsets; p = (FT_Byte*)stream->cursor; for ( ; (FT_Short)count >= 0; count-- ) { poff[0] = cff_get_offset( p, offsize ); poff++; p += offsize; } FT_FRAME_EXIT(); idx->data_offset = FT_STREAM_POS(); data_size = poff[-1] - 1; if ( load ) { /* load the data */ if ( FT_FRAME_EXTRACT( data_size, idx->bytes ) ) goto Exit; } else { /* skip the data */ if ( FT_STREAM_SKIP( data_size ) ) goto Exit; } } Exit: if ( error ) FT_FREE( idx->offsets ); return error; }
static FT_Error load_format_20( TT_Face face, FT_Stream stream ) { FT_Memory memory = stream->memory; FT_Error error; FT_Int num_glyphs; FT_UShort num_names; FT_UShort* glyph_indices = 0; FT_Char** name_strings = 0; if ( FT_READ_USHORT( num_glyphs ) ) goto Exit; /* UNDOCUMENTED! The number of glyphs in this table can be smaller */ /* than the value in the maxp table (cf. cyberbit.ttf). */ /* There already exist fonts which have more than 32768 glyph names */ /* in this table, so the test for this threshold has been dropped. */ if ( num_glyphs > face->root.num_glyphs ) { error = SFNT_Err_Invalid_File_Format; goto Exit; } /* load the indices */ { FT_Int n; if ( FT_NEW_ARRAY ( glyph_indices, num_glyphs ) || FT_FRAME_ENTER( num_glyphs * 2L ) ) goto Fail; for ( n = 0; n < num_glyphs; n++ ) glyph_indices[n] = FT_GET_USHORT(); FT_FRAME_EXIT(); } /* compute number of names stored in table */ { FT_Int n; num_names = 0; for ( n = 0; n < num_glyphs; n++ ) { FT_Int idx; idx = glyph_indices[n]; if ( idx >= 258 ) { idx -= 257; if ( idx > num_names ) num_names = (FT_UShort)idx; } } } /* now load the name strings */ { FT_UShort n; if ( FT_NEW_ARRAY( name_strings, num_names ) ) goto Fail; for ( n = 0; n < num_names; n++ ) { FT_UInt len; if ( FT_READ_BYTE ( len ) || FT_NEW_ARRAY( name_strings[n], len + 1 ) || FT_STREAM_READ ( name_strings[n], len ) ) goto Fail1; name_strings[n][len] = '\0'; } } /* all right, set table fields and exit successfuly */ { TT_Post_20 table = &face->postscript_names.names.format_20; table->num_glyphs = (FT_UShort)num_glyphs; table->num_names = (FT_UShort)num_names; table->glyph_indices = glyph_indices; table->glyph_names = name_strings; } return SFNT_Err_Ok; Fail1: { FT_UShort n; for ( n = 0; n < num_names; n++ ) FT_FREE( name_strings[n] ); } Fail: FT_FREE( name_strings ); FT_FREE( glyph_indices ); Exit: return error; }