📄 cffload.c
字号:
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 void
cff_encoding_done( CFF_Encoding encoding )
{
encoding->format = 0;
encoding->offset = 0;
encoding->count = 0;
}
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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -