📄 pcfread.c
字号:
/* pcfread.c
FreeType font driver for pcf fonts
Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007 by
Francesco Zappa Nardelli
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <ft2build.h>
#include FT_INTERNAL_DEBUG_H
#include FT_INTERNAL_STREAM_H
#include FT_INTERNAL_OBJECTS_H
#include "pcf.h"
#include "pcfdrivr.h"
#include "pcfread.h"
#include "pcferror.h"
/*************************************************************************/
/* */
/* The macro FT_COMPONENT is used in trace mode. It is an implicit */
/* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
/* messages during execution. */
/* */
#undef FT_COMPONENT
#define FT_COMPONENT trace_pcfread
#if defined( FT_DEBUG_LEVEL_TRACE )
static const char* const tableNames[] =
{
"prop", "accl", "mtrcs", "bmps", "imtrcs",
"enc", "swidth", "names", "accel"
};
#endif
static
const FT_Frame_Field pcf_toc_header[] =
{
#undef FT_STRUCTURE
#define FT_STRUCTURE PCF_TocRec
FT_FRAME_START( 8 ),
FT_FRAME_ULONG_LE( version ),
FT_FRAME_ULONG_LE( count ),
FT_FRAME_END
};
static
const FT_Frame_Field pcf_table_header[] =
{
#undef FT_STRUCTURE
#define FT_STRUCTURE PCF_TableRec
FT_FRAME_START( 16 ),
FT_FRAME_ULONG_LE( type ),
FT_FRAME_ULONG_LE( format ),
FT_FRAME_ULONG_LE( size ),
FT_FRAME_ULONG_LE( offset ),
FT_FRAME_END
};
static FT_Error
pcf_read_TOC( FT_Stream stream,
PCF_Face face )
{
FT_Error error;
PCF_Toc toc = &face->toc;
PCF_Table tables;
FT_Memory memory = FT_FACE(face)->memory;
FT_UInt n;
if ( FT_STREAM_SEEK ( 0 ) ||
FT_STREAM_READ_FIELDS ( pcf_toc_header, toc ) )
return PCF_Err_Cannot_Open_Resource;
if ( toc->version != PCF_FILE_VERSION ||
toc->count > FT_ARRAY_MAX( face->toc.tables ) ||
toc->count == 0 )
return PCF_Err_Invalid_File_Format;
if ( FT_NEW_ARRAY( face->toc.tables, toc->count ) )
return PCF_Err_Out_Of_Memory;
tables = face->toc.tables;
for ( n = 0; n < toc->count; n++ )
{
if ( FT_STREAM_READ_FIELDS( pcf_table_header, tables ) )
goto Exit;
tables++;
}
/* Sort tables and check for overlaps. Because they are almost */
/* always ordered already, an in-place bubble sort with simultaneous */
/* boundary checking seems appropriate. */
tables = face->toc.tables;
for ( n = 0; n < toc->count - 1; n++ )
{
FT_UInt i, have_change;
have_change = 0;
for ( i = 0; i < toc->count - 1 - n; i++ )
{
PCF_TableRec tmp;
if ( tables[i].offset > tables[i + 1].offset )
{
tmp = tables[i];
tables[i] = tables[i + 1];
tables[i + 1] = tmp;
have_change = 1;
}
if ( ( tables[i].size > tables[i + 1].offset ) ||
( tables[i].offset > tables[i + 1].offset - tables[i].size ) )
return PCF_Err_Invalid_Offset;
}
if ( !have_change )
break;
}
#if defined( FT_DEBUG_LEVEL_TRACE )
{
FT_UInt i, j;
const char* name = "?";
FT_TRACE4(( "pcf_read_TOC:\n" ));
FT_TRACE4(( " number of tables: %ld\n", face->toc.count ));
tables = face->toc.tables;
for ( i = 0; i < toc->count; i++ )
{
for ( j = 0; j < sizeof ( tableNames ) / sizeof ( tableNames[0] );
j++ )
if ( tables[i].type == (FT_UInt)( 1 << j ) )
name = tableNames[j];
FT_TRACE4(( " %d: type=%s, format=0x%X, "
"size=%ld (0x%lX), offset=%ld (0x%lX)\n",
i, name,
tables[i].format,
tables[i].size, tables[i].size,
tables[i].offset, tables[i].offset ));
}
}
#endif
return PCF_Err_Ok;
Exit:
FT_FREE( face->toc.tables );
return error;
}
#define PCF_METRIC_SIZE 12
static
const FT_Frame_Field pcf_metric_header[] =
{
#undef FT_STRUCTURE
#define FT_STRUCTURE PCF_MetricRec
FT_FRAME_START( PCF_METRIC_SIZE ),
FT_FRAME_SHORT_LE( leftSideBearing ),
FT_FRAME_SHORT_LE( rightSideBearing ),
FT_FRAME_SHORT_LE( characterWidth ),
FT_FRAME_SHORT_LE( ascent ),
FT_FRAME_SHORT_LE( descent ),
FT_FRAME_SHORT_LE( attributes ),
FT_FRAME_END
};
static
const FT_Frame_Field pcf_metric_msb_header[] =
{
#undef FT_STRUCTURE
#define FT_STRUCTURE PCF_MetricRec
FT_FRAME_START( PCF_METRIC_SIZE ),
FT_FRAME_SHORT( leftSideBearing ),
FT_FRAME_SHORT( rightSideBearing ),
FT_FRAME_SHORT( characterWidth ),
FT_FRAME_SHORT( ascent ),
FT_FRAME_SHORT( descent ),
FT_FRAME_SHORT( attributes ),
FT_FRAME_END
};
#define PCF_COMPRESSED_METRIC_SIZE 5
static
const FT_Frame_Field pcf_compressed_metric_header[] =
{
#undef FT_STRUCTURE
#define FT_STRUCTURE PCF_Compressed_MetricRec
FT_FRAME_START( PCF_COMPRESSED_METRIC_SIZE ),
FT_FRAME_BYTE( leftSideBearing ),
FT_FRAME_BYTE( rightSideBearing ),
FT_FRAME_BYTE( characterWidth ),
FT_FRAME_BYTE( ascent ),
FT_FRAME_BYTE( descent ),
FT_FRAME_END
};
static FT_Error
pcf_get_metric( FT_Stream stream,
FT_ULong format,
PCF_Metric metric )
{
FT_Error error = PCF_Err_Ok;
if ( PCF_FORMAT_MATCH( format, PCF_DEFAULT_FORMAT ) )
{
const FT_Frame_Field* fields;
/* parsing normal metrics */
fields = PCF_BYTE_ORDER( format ) == MSBFirst
? pcf_metric_msb_header
: pcf_metric_header;
/* the following sets `error' but doesn't return in case of failure */
(void)FT_STREAM_READ_FIELDS( fields, metric );
}
else
{
PCF_Compressed_MetricRec compr;
/* parsing compressed metrics */
if ( FT_STREAM_READ_FIELDS( pcf_compressed_metric_header, &compr ) )
goto Exit;
metric->leftSideBearing = (FT_Short)( compr.leftSideBearing - 0x80 );
metric->rightSideBearing = (FT_Short)( compr.rightSideBearing - 0x80 );
metric->characterWidth = (FT_Short)( compr.characterWidth - 0x80 );
metric->ascent = (FT_Short)( compr.ascent - 0x80 );
metric->descent = (FT_Short)( compr.descent - 0x80 );
metric->attributes = 0;
}
Exit:
return error;
}
static FT_Error
pcf_seek_to_table_type( FT_Stream stream,
PCF_Table tables,
FT_Int ntables,
FT_ULong type,
FT_ULong *aformat,
FT_ULong *asize )
{
FT_Error error = PCF_Err_Invalid_File_Format;
FT_Int i;
for ( i = 0; i < ntables; i++ )
if ( tables[i].type == type )
{
if ( stream->pos > tables[i].offset )
{
error = PCF_Err_Invalid_Stream_Skip;
goto Fail;
}
if ( FT_STREAM_SKIP( tables[i].offset - stream->pos ) )
{
error = PCF_Err_Invalid_Stream_Skip;
goto Fail;
}
*asize = tables[i].size;
*aformat = tables[i].format;
return PCF_Err_Ok;
}
Fail:
*asize = 0;
return error;
}
static FT_Bool
pcf_has_table_type( PCF_Table tables,
FT_Int ntables,
FT_ULong type )
{
FT_Int i;
for ( i = 0; i < ntables; i++ )
if ( tables[i].type == type )
return TRUE;
return FALSE;
}
#define PCF_PROPERTY_SIZE 9
static
const FT_Frame_Field pcf_property_header[] =
{
#undef FT_STRUCTURE
#define FT_STRUCTURE PCF_ParsePropertyRec
FT_FRAME_START( PCF_PROPERTY_SIZE ),
FT_FRAME_LONG_LE( name ),
FT_FRAME_BYTE ( isString ),
FT_FRAME_LONG_LE( value ),
FT_FRAME_END
};
static
const FT_Frame_Field pcf_property_msb_header[] =
{
#undef FT_STRUCTURE
#define FT_STRUCTURE PCF_ParsePropertyRec
FT_FRAME_START( PCF_PROPERTY_SIZE ),
FT_FRAME_LONG( name ),
FT_FRAME_BYTE( isString ),
FT_FRAME_LONG( value ),
FT_FRAME_END
};
FT_LOCAL_DEF( PCF_Property )
pcf_find_property( PCF_Face face,
const FT_String* prop )
{
PCF_Property properties = face->properties;
FT_Bool found = 0;
int i;
for ( i = 0 ; i < face->nprops && !found; i++ )
{
if ( !ft_strcmp( properties[i].name, prop ) )
found = 1;
}
if ( found )
return properties + i - 1;
else
return NULL;
}
static FT_Error
pcf_get_properties( FT_Stream stream,
PCF_Face face )
{
PCF_ParseProperty props = 0;
PCF_Property properties;
FT_UInt nprops, i;
FT_ULong format, size;
FT_Error error;
FT_Memory memory = FT_FACE(face)->memory;
FT_ULong string_size;
FT_String* strings = 0;
error = pcf_seek_to_table_type( stream,
face->toc.tables,
face->toc.count,
PCF_PROPERTIES,
&format,
&size );
if ( error )
goto Bail;
if ( FT_READ_ULONG_LE( format ) )
goto Bail;
FT_TRACE4(( "pcf_get_properties:\n" ));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -