📄 ftxgsub.c
字号:
/******************************************************************* * * ftxgsub.c * * TrueType Open GSUB table support. * * Copyright 1996-2000 by * David Turner, Robert Wilhelm, and Werner Lemberg. * * This file is part of the FreeType project, and may only be used * modified and distributed under the terms of the FreeType project * license, LICENSE.TXT. By continuing to use, modify, or distribute * this file you indicate that you have read the license and * understand and accept it fully. * ******************************************************************//* XXX There is *a lot* of duplicated code (cf. formats 5 and 6), but I don't care currently. I believe that it would be possible to save about 50% of TTO code by carefully designing the structures, sharing as much as possible with extensive use of macros. This is something for a volunteer :-) */#ifndef EXPORT_FUNC#define EXPORT_FUNC#endif#include <freetype/tttags.h>#include <freetype/internal/ftstream.h>#include <freetype/internal/ftmemory.h>#include <freetype/internal/tttypes.h>#include "fterrcompat.h"#include "ftxopen.h"#include "ftxopenf.h"#include <assert.h>#define GSUB_ID Build_Extension_ID( 'G', 'S', 'U', 'B' )#define ADD_String( in, num_in, out, num_out, glyph_data, component ) \ ( ( error = TT_GSUB_Add_String( (in), (num_in), \ (out), (num_out), \ (glyph_data), (component) \ ) ) != TT_Err_Ok ) static FT_Error GSub_Do_Glyph_Lookup( TTO_GSUBHeader* gsub, FT_UShort lookup_index, TTO_GSUB_String* in, TTO_GSUB_String* out, FT_UShort context_length, int nesting_level ); /********************** * Auxiliary functions **********************/ /* The following function copies `num_out' elements from `glyph_data' to `out', advancing the array pointer in the `in' structure by `num_in' elements, and in `out' by `num_out' elements. If the string (resp. the properties) array in `out' is empty or too small, it allocates resp. reallocates the string (and properties) array. Finally, it sets the `length' field of `out' equal to `pos' of the `out' structure. If `component' is 0xFFFF, the value `in->component[in->pos]' will be copied `num_out' times, otherwise `component' itself will be used to fill `out->component'. If `ligID' is 0xFFFF, the value `in->lig_IDs[in->pos]' will be copied `num_out' times, otherwise `ligID' itself will be used to fill `out->ligIDs'. The properties (if defined) for all replaced glyphs are taken from the glyph at position `in->pos'. The logClusters[] value for the glyph at position in->pos is used for all replacement glyphs */ EXPORT_FUNC FT_Error TT_GSUB_Add_String( TTO_GSUB_String* in, FT_UShort num_in, TTO_GSUB_String* out, FT_UShort num_out, FT_UShort* glyph_data, FT_UShort component) { /* sanity check */ //assert( !( !in || !out || in->length == 0 || in->pos >= in->length || in->length < in->pos + num_in ) ); if ( out->pos + num_out > out->allocated ) TT_GSUB_String_Allocate( out, out->pos + num_out ); if ( num_out ) { int n = num_out; FT_UShort *dest_glyph = out->string + out->pos; TTO_Glyph_property *dest_prop = out->glyph_properties + out->pos; FT_Int *dest_ci = out->character_index + out->pos; FT_Int src_ci = in->character_index[in->pos]; if ( component == 0xFFFF ) component = in->glyph_properties[in->pos].component; while ( n-- ) { *(dest_glyph++) = *(glyph_data++); (dest_prop++)->component = component; *(dest_ci++) = src_ci; } out->pos += num_out; } in->pos += num_in; out->length = out->pos; return TT_Err_Ok; }/* optimised function for the common case of copying copying 1 glyph to out, replacing n in glyphs */static inline void glyph_copy( TTO_GSUB_String* in, FT_UShort num_in, TTO_GSUB_String* out, FT_UShort glyph, FT_UShort component){ /* sanity check */ //assert( !( !in || !out || in->length == 0 || in->pos >= in->length || in->length < in->pos + num_in ) ); if ( out->pos >= out->allocated ) TT_GSUB_String_Allocate( out, out->pos + 1 ); out->string[out->pos] = glyph; out->glyph_properties[out->pos].component = component; out->character_index[out->pos] = in->character_index[in->pos]; out->pos++; in->pos += num_in; out->length = out->pos;}#if 0 /********************** * Extension Functions **********************/ static FT_Error GSUB_Create( void* ext, PFace face ) { DEFINE_LOAD_LOCALS( face->stream ); TTO_GSUBHeader* gsub = (TTO_GSUBHeader*)ext; Long table; /* by convention */ if ( !gsub ) return TT_Err_Ok; /* a null offset indicates that there is no GSUB table */ gsub->offset = 0; /* we store the start offset and the size of the subtable */ table = TT_LookUp_Table( face, TTAG_GSUB ); if ( table < 0 ) return TT_Err_Ok; /* The table is optional */ if ( FILE_Seek( face->dirTables[table].Offset ) || ACCESS_Frame( 4L ) ) return error; gsub->offset = FILE_Pos() - 4L; /* undo ACCESS_Frame() */ gsub->Version = GET_ULong(); FORGET_Frame(); gsub->loaded = FALSE; return TT_Err_Ok; } static FT_Error GSUB_Destroy( void* ext, PFace face ) { TTO_GSUBHeader* gsub = (TTO_GSUBHeader*)ext; /* by convention */ if ( !gsub ) return TT_Err_Ok; if ( gsub->loaded ) { Free_LookupList( &gsub->LookupList, GSUB, memory ); Free_FeatureList( &gsub->FeatureList, memory ); Free_ScriptList( &gsub->ScriptList, memory ); } return TT_Err_Ok; } EXPORT_FUNC FT_Error TT_Init_GSUB_Extension( TT_Engine engine ) { PEngine_Instance _engine = HANDLE_Engine( engine ); if ( !_engine ) return TT_Err_Invalid_Engine; return TT_Register_Extension( _engine, GSUB_ID, sizeof ( TTO_GSUBHeader ), GSUB_Create, GSUB_Destroy ); }#endif EXPORT_FUNC FT_Error TT_Load_GSUB_Table( FT_Face face, TTO_GSUBHeader** retptr, TTO_GDEFHeader* gdef ) { FT_Stream stream = face->stream; FT_Memory memory = face->memory; FT_Error error; FT_ULong cur_offset, new_offset, base_offset; TT_Face tt_face = (TT_Face)face; /* FT_UShort i, num_lookups; */ TTO_GSUBHeader* gsub; /* TTO_Lookup* lo; */ if ( !retptr ) return TT_Err_Invalid_Argument; if (( error = tt_face->goto_table( tt_face, TTAG_GSUB, stream, 0 ) )) return error; base_offset = FILE_Pos(); if ( ALLOC ( gsub, sizeof( *gsub ) ) ) return error; gsub->memory = memory; /* skip version */ if ( FILE_Seek( base_offset + 4L ) || ACCESS_Frame( 2L ) ) goto Fail4; new_offset = GET_UShort() + base_offset; FORGET_Frame(); cur_offset = FILE_Pos(); if ( FILE_Seek( new_offset ) || ( error = Load_ScriptList( &gsub->ScriptList, stream ) ) != TT_Err_Ok ) goto Fail4; (void)FILE_Seek( cur_offset ); if ( ACCESS_Frame( 2L ) ) goto Fail3; new_offset = GET_UShort() + base_offset; FORGET_Frame(); cur_offset = FILE_Pos(); if ( FILE_Seek( new_offset ) || ( error = Load_FeatureList( &gsub->FeatureList, stream ) ) != TT_Err_Ok ) goto Fail3; (void)FILE_Seek( cur_offset ); if ( ACCESS_Frame( 2L ) ) goto Fail2; new_offset = GET_UShort() + base_offset; FORGET_Frame(); cur_offset = FILE_Pos(); if ( FILE_Seek( new_offset ) || ( error = Load_LookupList( &gsub->LookupList, stream, GSUB ) ) != TT_Err_Ok ) goto Fail2; gsub->gdef = gdef; /* can be NULL */ /* We now check the LookupFlags for values larger than 0xFF to find out whether we need to load the `MarkAttachClassDef' field of the GDEF table -- this hack is necessary for OpenType 1.2 tables since the version field of the GDEF table hasn't been incremented. For constructed GDEF tables, we only load it if `MarkAttachClassDef_offset' is not zero (nevertheless, a build of a constructed mark attach table is not supported currently). */#if 0 if ( gdef && gdef->MarkAttachClassDef_offset && !gdef->MarkAttachClassDef.loaded ) { lo = gsub->LookupList.Lookup; num_lookups = gsub->LookupList.LookupCount; for ( i = 0; i < num_lookups; i++ ) { if ( lo[i].LookupFlag & IGNORE_SPECIAL_MARKS ) { if ( FILE_Seek( gdef->MarkAttachClassDef_offset ) || ( error = Load_ClassDefinition( &gdef->MarkAttachClassDef, 256, stream ) ) != TT_Err_Ok ) goto Fail1; break; } } }#endif *retptr = gsub; return TT_Err_Ok;#if 0 Fail1: Free_LookupList( &gsub->LookupList, GSUB, memory );#endif Fail2: Free_FeatureList( &gsub->FeatureList, memory ); Fail3: Free_ScriptList( &gsub->ScriptList, memory ); Fail4: FREE ( gsub ); return error; } EXPORT_FUNC FT_Error TT_Done_GSUB_Table( TTO_GSUBHeader* gsub ) { FT_Memory memory = gsub->memory; Free_LookupList( &gsub->LookupList, GSUB, memory ); Free_FeatureList( &gsub->FeatureList, memory ); Free_ScriptList( &gsub->ScriptList, memory ); FREE( gsub ); return TT_Err_Ok; } /***************************** * SubTable related functions *****************************/ /* LookupType 1 */ /* SingleSubstFormat1 */ /* SingleSubstFormat2 */ FT_Error Load_SingleSubst( TTO_SingleSubst* ss, FT_Stream stream ) { FT_Error error; FT_Memory memory = stream->memory; FT_UShort n, count; FT_ULong cur_offset, new_offset, base_offset; FT_UShort* s; base_offset = FILE_Pos(); if ( ACCESS_Frame( 4L ) ) return error; ss->SubstFormat = GET_UShort(); new_offset = GET_UShort() + base_offset; FORGET_Frame(); cur_offset = FILE_Pos(); if ( FILE_Seek( new_offset ) || ( error = Load_Coverage( &ss->Coverage, stream ) ) != TT_Err_Ok ) return error; (void)FILE_Seek( cur_offset ); switch ( ss->SubstFormat ) { case 1: if ( ACCESS_Frame( 2L ) ) goto Fail2; ss->ssf.ssf1.DeltaGlyphID = GET_UShort(); FORGET_Frame(); break; case 2: if ( ACCESS_Frame( 2L ) ) goto Fail2; count = ss->ssf.ssf2.GlyphCount = GET_UShort(); FORGET_Frame(); ss->ssf.ssf2.Substitute = NULL; if ( ALLOC_ARRAY( ss->ssf.ssf2.Substitute, count, FT_UShort ) ) goto Fail2; s = ss->ssf.ssf2.Substitute; if ( ACCESS_Frame( count * 2L ) ) goto Fail1; for ( n = 0; n < count; n++ ) s[n] = GET_UShort(); FORGET_Frame(); break; default: return TTO_Err_Invalid_GSUB_SubTable_Format; } return TT_Err_Ok; Fail1: FREE( s ); Fail2: Free_Coverage( &ss->Coverage, memory ); return error; } void Free_SingleSubst( TTO_SingleSubst* ss, FT_Memory memory ) { switch ( ss->SubstFormat ) { case 1: break; case 2: FREE( ss->ssf.ssf2.Substitute ); break; } Free_Coverage( &ss->Coverage, memory ); } static FT_Error Lookup_SingleSubst( TTO_SingleSubst* ss, TTO_GSUB_String* in, TTO_GSUB_String* out, FT_UShort flags, FT_UShort context_length, TTO_GDEFHeader* gdef ) { FT_UShort index, value, property; FT_Error error; if ( context_length != 0xFFFF && context_length < 1 ) return TTO_Err_Not_Covered; error = Coverage_Index( &ss->Coverage, in->string[in->pos], &index ); if ( error ) return error; if ( CHECK_Property( gdef, in->string[in->pos], flags, &property ) ) return error; switch ( ss->SubstFormat ) { case 1: value = ( in->string[in->pos] + ss->ssf.ssf1.DeltaGlyphID ) & 0xFFFF; glyph_copy( in, 1, out, value, in->glyph_properties[in->pos].component); break; case 2: if ( index >= ss->ssf.ssf2.GlyphCount ) return TTO_Err_Invalid_GSUB_SubTable; value = ss->ssf.ssf2.Substitute[index]; glyph_copy( in, 1, out, value, in->glyph_properties[in->pos].component); break; default: return TTO_Err_Invalid_GSUB_SubTable; } if ( gdef && gdef->NewGlyphClasses ) { /* we inherit the old glyph class to the substituted glyph */ error = Add_Glyph_Property( gdef, value, property ); if ( error && error != TTO_Err_Not_Covered ) return error; } return TT_Err_Ok; } /* LookupType 2 */ /* Sequence */ static FT_Error Load_Sequence( TTO_Sequence* s, FT_Stream stream ) { FT_Error error; FT_Memory memory = stream->memory; FT_UShort n, count; FT_UShort* sub; if ( ACCESS_Frame( 2L ) ) return error; count = s->GlyphCount = GET_UShort(); FORGET_Frame(); s->Substitute = NULL; if ( count ) { if ( ALLOC_ARRAY( s->Substitute, count, FT_UShort ) ) return error; sub = s->Substitute; if ( ACCESS_Frame( count * 2L ) )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -