📄 harfbuzz-gpos.c
字号:
/******************************************************************* * * Copyright 1996-2000 by * David Turner, Robert Wilhelm, and Werner Lemberg. * * Copyright 2006 Behdad Esfahbod * * This is part of HarfBuzz, an OpenType Layout engine library. * * See the file name COPYING for licensing information. * ******************************************************************/#include "harfbuzz-impl.h"#include "harfbuzz-gpos-private.h"#include "harfbuzz-open-private.h"#include "harfbuzz-gdef-private.h"struct GPOS_Instance_{ HB_GPOSHeader* gpos; FT_Face face; FT_Bool dvi; FT_UShort load_flags; /* how the glyph should be loaded */ FT_Bool r2l; FT_UShort last; /* the last valid glyph -- used with cursive positioning */ FT_Pos anchor_x; /* the coordinates of the anchor point */ FT_Pos anchor_y; /* of the last valid glyph */};typedef struct GPOS_Instance_ GPOS_Instance;static FT_Error GPOS_Do_Glyph_Lookup( GPOS_Instance* gpi, FT_UShort lookup_index, HB_Buffer buffer, FT_UShort context_length, int nesting_level );/* the client application must replace this with something more meaningful if multiple master fonts are to be supported. */static FT_Error default_mmfunc( FT_Face face, FT_UShort metric_id, FT_Pos* metric_value, void* data ){ FT_UNUSED(face); FT_UNUSED(metric_id); FT_UNUSED(metric_value); FT_UNUSED(data); return HB_Err_No_MM_Interpreter;}FT_Error HB_Load_GPOS_Table( FT_Face face, HB_GPOSHeader** retptr, HB_GDEFHeader* gdef ){ FT_ULong cur_offset, new_offset, base_offset; FT_UShort i, num_lookups; HB_GPOSHeader* gpos; HB_Lookup* lo; FT_Stream stream = face->stream; FT_Error error; FT_Memory memory = face->memory; if ( !retptr ) return FT_Err_Invalid_Argument; if ( !stream ) return FT_Err_Invalid_Face_Handle; if (( error = _hb_ftglue_face_goto_table( face, TTAG_GPOS, stream ) )) return error; base_offset = FILE_Pos(); if ( ALLOC ( gpos, sizeof( *gpos ) ) ) return error; gpos->memory = memory; gpos->gfunc = FT_Load_Glyph; gpos->mmfunc = default_mmfunc; /* 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 = _HB_OPEN_Load_ScriptList( &gpos->ScriptList, stream ) ) != FT_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 = _HB_OPEN_Load_FeatureList( &gpos->FeatureList, stream ) ) != FT_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 = _HB_OPEN_Load_LookupList( &gpos->LookupList, stream, HB_Type_GPOS ) ) != FT_Err_Ok ) goto Fail2; gpos->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 ( gdef && gdef->MarkAttachClassDef_offset && !gdef->MarkAttachClassDef.loaded ) { lo = gpos->LookupList.Lookup; num_lookups = gpos->LookupList.LookupCount; for ( i = 0; i < num_lookups; i++ ) { if ( lo[i].LookupFlag & HB_LOOKUP_FLAG_IGNORE_SPECIAL_MARKS ) { if ( FILE_Seek( gdef->MarkAttachClassDef_offset ) || ( error = _HB_OPEN_Load_ClassDefinition( &gdef->MarkAttachClassDef, 256, stream ) ) != FT_Err_Ok ) goto Fail1; break; } } } *retptr = gpos; return FT_Err_Ok;Fail1: _HB_OPEN_Free_LookupList( &gpos->LookupList, HB_Type_GPOS, memory );Fail2: _HB_OPEN_Free_FeatureList( &gpos->FeatureList, memory );Fail3: _HB_OPEN_Free_ScriptList( &gpos->ScriptList, memory );Fail4: FREE( gpos ); return error;}FT_Error HB_Done_GPOS_Table( HB_GPOSHeader* gpos ){ FT_Memory memory = gpos->memory; _HB_OPEN_Free_LookupList( &gpos->LookupList, HB_Type_GPOS, memory ); _HB_OPEN_Free_FeatureList( &gpos->FeatureList, memory ); _HB_OPEN_Free_ScriptList( &gpos->ScriptList, memory ); return FT_Err_Ok;}/***************************** * SubTable related functions *****************************//* shared tables *//* ValueRecord *//* There is a subtle difference in the specs between a `table' and a `record' -- offsets for device tables in ValueRecords are taken from the parent table and not the parent record. */static FT_Error Load_ValueRecord( HB_ValueRecord* vr, FT_UShort format, FT_ULong base_offset, FT_Stream stream ){ FT_Error error; FT_Memory memory = stream->memory; FT_ULong cur_offset, new_offset; if ( format & HB_GPOS_FORMAT_HAVE_X_PLACEMENT ) { if ( ACCESS_Frame( 2L ) ) return error; vr->XPlacement = GET_Short(); FORGET_Frame(); } else vr->XPlacement = 0; if ( format & HB_GPOS_FORMAT_HAVE_Y_PLACEMENT ) { if ( ACCESS_Frame( 2L ) ) return error; vr->YPlacement = GET_Short(); FORGET_Frame(); } else vr->YPlacement = 0; if ( format & HB_GPOS_FORMAT_HAVE_X_ADVANCE ) { if ( ACCESS_Frame( 2L ) ) return error; vr->XAdvance = GET_Short(); FORGET_Frame(); } else vr->XAdvance = 0; if ( format & HB_GPOS_FORMAT_HAVE_Y_ADVANCE ) { if ( ACCESS_Frame( 2L ) ) return error; vr->YAdvance = GET_Short(); FORGET_Frame(); } else vr->YAdvance = 0; if ( format & HB_GPOS_FORMAT_HAVE_X_PLACEMENT_DEVICE ) { if ( ACCESS_Frame( 2L ) ) return error; new_offset = GET_UShort(); FORGET_Frame(); if ( new_offset ) { new_offset += base_offset; cur_offset = FILE_Pos(); if ( FILE_Seek( new_offset ) || ( error = _HB_OPEN_Load_Device( &vr->XPlacementDevice, stream ) ) != FT_Err_Ok ) return error; (void)FILE_Seek( cur_offset ); } else goto empty1; } else { empty1: vr->XPlacementDevice.StartSize = 0; vr->XPlacementDevice.EndSize = 0; vr->XPlacementDevice.DeltaValue = NULL; } if ( format & HB_GPOS_FORMAT_HAVE_Y_PLACEMENT_DEVICE ) { if ( ACCESS_Frame( 2L ) ) goto Fail3; new_offset = GET_UShort(); FORGET_Frame(); if ( new_offset ) { new_offset += base_offset; cur_offset = FILE_Pos(); if ( FILE_Seek( new_offset ) || ( error = _HB_OPEN_Load_Device( &vr->YPlacementDevice, stream ) ) != FT_Err_Ok ) goto Fail3; (void)FILE_Seek( cur_offset ); } else goto empty2; } else { empty2: vr->YPlacementDevice.StartSize = 0; vr->YPlacementDevice.EndSize = 0; vr->YPlacementDevice.DeltaValue = NULL; } if ( format & HB_GPOS_FORMAT_HAVE_X_ADVANCE_DEVICE ) { if ( ACCESS_Frame( 2L ) ) goto Fail2; new_offset = GET_UShort(); FORGET_Frame(); if ( new_offset ) { new_offset += base_offset; cur_offset = FILE_Pos(); if ( FILE_Seek( new_offset ) || ( error = _HB_OPEN_Load_Device( &vr->XAdvanceDevice, stream ) ) != FT_Err_Ok ) goto Fail2; (void)FILE_Seek( cur_offset ); } else goto empty3; } else { empty3: vr->XAdvanceDevice.StartSize = 0; vr->XAdvanceDevice.EndSize = 0; vr->XAdvanceDevice.DeltaValue = NULL; } if ( format & HB_GPOS_FORMAT_HAVE_Y_ADVANCE_DEVICE ) { if ( ACCESS_Frame( 2L ) ) goto Fail1; new_offset = GET_UShort(); FORGET_Frame(); if ( new_offset ) { new_offset += base_offset; cur_offset = FILE_Pos(); if ( FILE_Seek( new_offset ) || ( error = _HB_OPEN_Load_Device( &vr->YAdvanceDevice, stream ) ) != FT_Err_Ok ) goto Fail1; (void)FILE_Seek( cur_offset ); } else goto empty4; } else { empty4: vr->YAdvanceDevice.StartSize = 0; vr->YAdvanceDevice.EndSize = 0; vr->YAdvanceDevice.DeltaValue = NULL; } if ( format & HB_GPOS_FORMAT_HAVE_X_ID_PLACEMENT ) { if ( ACCESS_Frame( 2L ) ) goto Fail1; vr->XIdPlacement = GET_UShort(); FORGET_Frame(); } else vr->XIdPlacement = 0; if ( format & HB_GPOS_FORMAT_HAVE_Y_ID_PLACEMENT ) { if ( ACCESS_Frame( 2L ) ) goto Fail1; vr->YIdPlacement = GET_UShort(); FORGET_Frame(); } else vr->YIdPlacement = 0; if ( format & HB_GPOS_FORMAT_HAVE_X_ID_ADVANCE ) { if ( ACCESS_Frame( 2L ) ) goto Fail1; vr->XIdAdvance = GET_UShort(); FORGET_Frame(); } else vr->XIdAdvance = 0; if ( format & HB_GPOS_FORMAT_HAVE_Y_ID_ADVANCE ) { if ( ACCESS_Frame( 2L ) ) goto Fail1; vr->YIdAdvance = GET_UShort(); FORGET_Frame(); } else vr->YIdAdvance = 0; return FT_Err_Ok;Fail1: _HB_OPEN_Free_Device( &vr->YAdvanceDevice, memory );Fail2: _HB_OPEN_Free_Device( &vr->XAdvanceDevice, memory );Fail3: _HB_OPEN_Free_Device( &vr->YPlacementDevice, memory ); return error;}static void Free_ValueRecord( HB_ValueRecord* vr, FT_UShort format, FT_Memory memory ){ if ( format & HB_GPOS_FORMAT_HAVE_Y_ADVANCE_DEVICE ) _HB_OPEN_Free_Device( &vr->YAdvanceDevice, memory ); if ( format & HB_GPOS_FORMAT_HAVE_X_ADVANCE_DEVICE ) _HB_OPEN_Free_Device( &vr->XAdvanceDevice, memory ); if ( format & HB_GPOS_FORMAT_HAVE_Y_PLACEMENT_DEVICE ) _HB_OPEN_Free_Device( &vr->YPlacementDevice, memory ); if ( format & HB_GPOS_FORMAT_HAVE_X_PLACEMENT_DEVICE ) _HB_OPEN_Free_Device( &vr->XPlacementDevice, memory );}static FT_Error Get_ValueRecord( GPOS_Instance* gpi, HB_ValueRecord* vr, FT_UShort format, HB_Position gd ){ FT_Pos value; FT_Short pixel_value; FT_Error error = FT_Err_Ok; HB_GPOSHeader* gpos = gpi->gpos; FT_UShort x_ppem, y_ppem; FT_Fixed x_scale, y_scale; if ( !format ) return FT_Err_Ok; x_ppem = gpi->face->size->metrics.x_ppem; y_ppem = gpi->face->size->metrics.y_ppem; x_scale = gpi->face->size->metrics.x_scale; y_scale = gpi->face->size->metrics.y_scale; /* design units -> fractional pixel */ if ( format & HB_GPOS_FORMAT_HAVE_X_PLACEMENT ) gd->x_pos += x_scale * vr->XPlacement / 0x10000; if ( format & HB_GPOS_FORMAT_HAVE_Y_PLACEMENT ) gd->y_pos += y_scale * vr->YPlacement / 0x10000; if ( format & HB_GPOS_FORMAT_HAVE_X_ADVANCE ) gd->x_advance += x_scale * vr->XAdvance / 0x10000; if ( format & HB_GPOS_FORMAT_HAVE_Y_ADVANCE ) gd->y_advance += y_scale * vr->YAdvance / 0x10000; if ( !gpi->dvi ) { /* pixel -> fractional pixel */ if ( format & HB_GPOS_FORMAT_HAVE_X_PLACEMENT_DEVICE ) { _HB_OPEN_Get_Device( &vr->XPlacementDevice, x_ppem, &pixel_value ); gd->x_pos += pixel_value << 6; } if ( format & HB_GPOS_FORMAT_HAVE_Y_PLACEMENT_DEVICE ) { _HB_OPEN_Get_Device( &vr->YPlacementDevice, y_ppem, &pixel_value ); gd->y_pos += pixel_value << 6; } if ( format & HB_GPOS_FORMAT_HAVE_X_ADVANCE_DEVICE ) { _HB_OPEN_Get_Device( &vr->XAdvanceDevice, x_ppem, &pixel_value ); gd->x_advance += pixel_value << 6; } if ( format & HB_GPOS_FORMAT_HAVE_Y_ADVANCE_DEVICE ) { _HB_OPEN_Get_Device( &vr->YAdvanceDevice, y_ppem, &pixel_value ); gd->y_advance += pixel_value << 6; } } /* values returned from mmfunc() are already in fractional pixels */ if ( format & HB_GPOS_FORMAT_HAVE_X_ID_PLACEMENT ) { error = (gpos->mmfunc)( gpi->face, vr->XIdPlacement, &value, gpos->data ); if ( error ) return error; gd->x_pos += value; } if ( format & HB_GPOS_FORMAT_HAVE_Y_ID_PLACEMENT ) { error = (gpos->mmfunc)( gpi->face, vr->YIdPlacement, &value, gpos->data ); if ( error ) return error; gd->y_pos += value; } if ( format & HB_GPOS_FORMAT_HAVE_X_ID_ADVANCE ) { error = (gpos->mmfunc)( gpi->face, vr->XIdAdvance, &value, gpos->data ); if ( error ) return error; gd->x_advance += value; } if ( format & HB_GPOS_FORMAT_HAVE_Y_ID_ADVANCE ) { error = (gpos->mmfunc)( gpi->face, vr->YIdAdvance, &value, gpos->data ); if ( error ) return error; gd->y_advance += value; } return error;}/* AnchorFormat1 *//* AnchorFormat2 *//* AnchorFormat3 *//* AnchorFormat4 */static FT_Error Load_Anchor( HB_Anchor* an, FT_Stream stream ){ FT_Error error; FT_Memory memory = stream->memory; FT_ULong cur_offset, new_offset, base_offset; base_offset = FILE_Pos(); if ( ACCESS_Frame( 2L ) ) return error; an->PosFormat = GET_UShort(); FORGET_Frame(); switch ( an->PosFormat ) { case 1: if ( ACCESS_Frame( 4L ) ) return error; an->af.af1.XCoordinate = GET_Short();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -