--- ftxpost.c.orig Tue Jul 9 12:02:53 2002 +++ ftxpost.c Tue Jul 9 11:56:18 2002 @@ -126,14 +126,14 @@ { DEFINE_LOAD_LOCALS( input->stream ); - UShort nameindex, n, num; + UShort n, num_glyphs, num_names, idx; Byte len; if ( ACCESS_Frame( 2L ) ) return error; - num = GET_UShort(); + num_glyphs = GET_UShort(); FORGET_Frame(); @@ -142,71 +142,77 @@ /* 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 > input->numGlyphs ) + if ( num_glyphs > input->numGlyphs ) return TT_Err_Invalid_Post_Table; - post20->numGlyphs = num; + post20->numGlyphs = num_glyphs; - if ( ALLOC_ARRAY( post20->glyphNameIndex, num, TT_UShort ) ) + if ( ALLOC_ARRAY( post20->glyphNameIndex, num_glyphs, TT_UShort ) ) return error; - if ( ACCESS_Frame( num * 2L ) ) + if ( ACCESS_Frame( num_glyphs * 2L ) ) goto Fail; - for ( n = 0; n < num; n++ ) + num_names=0; + for ( n = 0; n < num_glyphs; n++ ) { - post20->glyphNameIndex[n] = GET_UShort(); + idx = post20->glyphNameIndex[n] = GET_UShort(); - if ( post20->glyphNameIndex[n] > 258 + num ) +#if 0 /* FIXME This test has been dropped in Freetype 2. Why? */ + if ( idx > 258 + num_glyphs ) { FORGET_Frame(); error = TT_Err_Invalid_Post_Table; goto Fail; } +#endif + if ( idx > num_names ) + num_names = idx; } - FORGET_Frame(); - if ( ALLOC_ARRAY( post20->glyphNames, num, Char* ) ) + /* Now, substract 257 to get the number of non-standard names */ + + if ( num_names >= 258 ) { num_names -= 257; } + else { return TT_Err_Ok; } /* Nothing more to do */ + + /* Now load the names strings */ + + if ( ALLOC_ARRAY( post20->glyphNames, num_names, Char* ) ) goto Fail; /* We must initialize the glyphNames array for proper */ /* deallocation. */ - for ( n = 0; n < num; n++ ) + /* FIXME Antoine Leca wonders if still necessary. I believe it is */ + /* in case of a an error occuring while reading in the names */ + + for ( n = 0; n < num_names; n++ ) post20->glyphNames[n] = NULL; /* Now we can read the glyph names which are stored in */ /* Pascal string format. */ - for ( n = 0; n < num; n++ ) + for ( n = 0; n < num_names; n++ ) { - nameindex = post20->glyphNameIndex[n]; - - if ( nameindex < 258 ) - ; /* default Mac glyph, do nothing */ - else - { - if ( ACCESS_Frame( 1L ) ) - goto Fail1; - - len = GET_Byte(); - - FORGET_Frame(); - - if ( ALLOC_ARRAY( post20->glyphNames[nameindex - 258], - len + 1, Char ) || - FILE_Read( post20->glyphNames[nameindex - 258], len ) ) - goto Fail1; - - /* we make a C string */ - post20->glyphNames[nameindex - 258][len] = '\0'; - } + if ( ACCESS_Frame( 1L ) ) + goto Fail1; + + len = GET_Byte(); + + FORGET_Frame(); + + if ( ALLOC_ARRAY( post20->glyphNames[n], len + 1, Char ) || + FILE_Read( post20->glyphNames[n], len ) ) + goto Fail1; + + /* we make a C string */ + post20->glyphNames[n][len] = '\0'; } - + return TT_Err_Ok; Fail1: - for ( n = 0; n < num; n++ ) + for ( n = 0; n < num_names; n++ ) if ( post20->glyphNames[n] ) FREE( post20->glyphNames[n] ); @@ -296,7 +302,7 @@ PFace face ) { TT_Post* post = (TT_Post*)ext; - UShort n; + UShort n, num_names, idx; /* by convention */ @@ -311,10 +317,21 @@ break; case 0x00020000: - for ( n = 0; n < post->p.post20.numGlyphs; n++ ) - if ( post->p.post20.glyphNames[n] ) - FREE( post->p.post20.glyphNames[n] ); - FREE( post->p.post20.glyphNames ); + num_names=0; + /* One has to recompute the number of names that have been read in */ + /* Loop is reversed so as to reduce the number of affectations */ + for ( n = post->p.post20.numGlyphs; n > 0;) + { + idx = post->p.post20.glyphNameIndex[--n]; + if ( idx > num_names ) num_names=idx; + } + if ( num_names >= 258 ) { + num_names -= 257; + for ( n = 0; n < num_names; n++ ) + if ( post->p.post20.glyphNames[n] ) + FREE( post->p.post20.glyphNames[n] ); + FREE( post->p.post20.glyphNames ); + } FREE( post->p.post20.glyphNameIndex ); break;