📄 ftcache.c
字号:
/* now finalize it */
manager->cur_weight -= cache->clazz.node_weight( node, cache );
cache->clazz.node_free( node, cache );
node = next;
}
cache->buckets[i] = NULL;
}
ftc_cache_resize( cache );
}
}
FT_LOCAL_DEF( void )
ftc_cache_done( FTC_Cache cache )
{
if ( cache->memory )
{
FT_Memory memory = cache->memory;
FTC_Cache_Clear( cache );
FT_FREE( cache->buckets );
cache->mask = 0;
cache->p = 0;
cache->slack = 0;
cache->memory = NULL;
}
}
FT_LOCAL_DEF( void )
FTC_Cache_Done( FTC_Cache cache )
{
ftc_cache_done( cache );
}
static void
ftc_cache_add( FTC_Cache cache,
FT_UInt32 hash,
FTC_Node node )
{
node->hash = hash;
node->cache_index = (FT_UInt16) cache->index;
node->ref_count = 0;
ftc_node_hash_link( node, cache );
ftc_node_mru_link( node, cache->manager );
{
FTC_Manager manager = cache->manager;
manager->cur_weight += cache->clazz.node_weight( node, cache );
if ( manager->cur_weight >= manager->max_weight )
{
node->ref_count++;
FTC_Manager_Compress( manager );
node->ref_count--;
}
}
}
FT_LOCAL_DEF( FT_Error )
FTC_Cache_NewNode( FTC_Cache cache,
FT_UInt32 hash,
FT_Pointer query,
FTC_Node *anode )
{
FT_Error error;
FTC_Node node;
/*
* We use the FTC_CACHE_TRYLOOP macros to support out-of-memory
* errors (OOM) correctly, i.e., by flushing the cache progressively
* in order to make more room.
*/
FTC_CACHE_TRYLOOP( cache )
{
error = cache->clazz.node_new( &node, query, cache );
}
FTC_CACHE_TRYLOOP_END();
if ( error )
node = NULL;
else
{
/* don't assume that the cache has the same number of buckets, since
* our allocation request might have triggered global cache flushing
*/
ftc_cache_add( cache, hash, node );
}
*anode = node;
return error;
}
#ifndef FTC_INLINE
FT_LOCAL_DEF( FT_Error )
FTC_Cache_Lookup( FTC_Cache cache,
FT_UInt32 hash,
FT_Pointer query,
FTC_Node *anode )
{
FT_UFast idx;
FTC_Node* bucket;
FTC_Node* pnode;
FTC_Node node;
FT_Error error = 0;
FTC_Node_CompareFunc compare = cache->clazz.node_compare;
if ( cache == NULL || anode == NULL )
return FT_Err_Invalid_Argument;
idx = hash & cache->mask;
if ( idx < cache->p )
idx = hash & ( cache->mask * 2 + 1 );
bucket = cache->buckets + idx;
pnode = bucket;
for (;;)
{
node = *pnode;
if ( node == NULL )
goto NewNode;
if ( node->hash == hash && compare( node, query, cache ) )
break;
pnode = &node->link;
}
if ( node != *bucket )
{
*pnode = node->link;
node->link = *bucket;
*bucket = node;
}
/* move to head of MRU list */
{
FTC_Manager manager = cache->manager;
if ( node != manager->nodes_list )
ftc_node_mru_up( node, manager );
}
*anode = node;
return error;
NewNode:
return FTC_Cache_NewNode( cache, hash, query, anode );
}
#endif /* !FTC_INLINE */
FT_LOCAL_DEF( void )
FTC_Cache_RemoveFaceID( FTC_Cache cache,
FTC_FaceID face_id )
{
FT_UFast i, count;
FTC_Manager manager = cache->manager;
FTC_Node frees = NULL;
count = cache->p + cache->mask;
for ( i = 0; i < count; i++ )
{
FTC_Node* bucket = cache->buckets + i;
FTC_Node* pnode = bucket;
for ( ;; )
{
FTC_Node node = *pnode;
if ( node == NULL )
break;
if ( cache->clazz.node_remove_faceid( node, face_id, cache ) )
{
*pnode = node->link;
node->link = frees;
frees = node;
}
else
pnode = &node->link;
}
}
/* remove all nodes in the free list */
while ( frees )
{
FTC_Node node;
node = frees;
frees = node->link;
manager->cur_weight -= cache->clazz.node_weight( node, cache );
ftc_node_mru_unlink( node, manager );
cache->clazz.node_free( node, cache );
cache->slack++;
}
ftc_cache_resize( cache );
}
/* END */
/***************************************************************************/
/* */
/* ftccmap.c */
/* */
/* FreeType CharMap cache (body) */
/* */
/* Copyright 2000-2001, 2002, 2003, 2004, 2005, 2006 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. */
/* */
/***************************************************************************/
#include "ft2build.h"
#include FT_FREETYPE_H
#include FT_CACHE_H
#include "ftcmanag.h"
#include FT_INTERNAL_MEMORY_H
#include FT_INTERNAL_DEBUG_H
#include FT_TRUETYPE_IDS_H
#include "ftccback.h"
#include "ftcerror.h"
#undef FT_COMPONENT
#define FT_COMPONENT trace_cache
#ifdef FT_CONFIG_OPTION_OLD_INTERNALS
typedef enum FTC_OldCMapType_
{
FTC_OLD_CMAP_BY_INDEX = 0,
FTC_OLD_CMAP_BY_ENCODING = 1,
FTC_OLD_CMAP_BY_ID = 2
} FTC_OldCMapType;
typedef struct FTC_OldCMapIdRec_
{
FT_UInt platform;
FT_UInt encoding;
} FTC_OldCMapIdRec, *FTC_OldCMapId;
typedef struct FTC_OldCMapDescRec_
{
FTC_FaceID face_id;
FTC_OldCMapType type;
union
{
FT_UInt index;
FT_Encoding encoding;
FTC_OldCMapIdRec id;
} u;
} FTC_OldCMapDescRec, *FTC_OldCMapDesc;
#endif /* FT_CONFIG_OLD_INTERNALS */
/*************************************************************************/
/* */
/* Each FTC_CMapNode contains a simple array to map a range of character */
/* codes to equivalent glyph indices. */
/* */
/* For now, the implementation is very basic: Each node maps a range of */
/* 128 consecutive character codes to their corresponding glyph indices. */
/* */
/* We could do more complex things, but I don't think it is really very */
/* useful. */
/* */
/*************************************************************************/
/* number of glyph indices / character code per node */
#define FTC_CMAP_INDICES_MAX 128
/* compute a query/node hash */
#define FTC_CMAP_HASH( faceid, index, charcode ) \
( FTC_FACE_ID_HASH( faceid ) + 211 * ( index ) + \
( (char_code) / FTC_CMAP_INDICES_MAX ) )
/* the charmap query */
typedef struct FTC_CMapQueryRec_
{
FTC_FaceID face_id;
FT_UInt cmap_index;
FT_UInt32 char_code;
} FTC_CMapQueryRec, *FTC_CMapQuery;
#define FTC_CMAP_QUERY( x ) ((FTC_CMapQuery)(x))
#define FTC_CMAP_QUERY_HASH( x ) \
FTC_CMAP_HASH( (x)->face_id, (x)->cmap_index, (x)->char_code )
/* the cmap cache node */
typedef struct FTC_CMapNodeRec_
{
FTC_NodeRec node;
FTC_FaceID face_id;
FT_UInt cmap_index;
FT_UInt32 first; /* first character in node */
FT_UInt16 indices[FTC_CMAP_INDICES_MAX]; /* array of glyph indices */
} FTC_CMapNodeRec, *FTC_CMapNode;
#define FTC_CMAP_NODE( x ) ( (FTC_CMapNode)( x ) )
#define FTC_CMAP_NODE_HASH( x ) \
FTC_CMAP_HASH( (x)->face_id, (x)->cmap_index, (x)->first )
/* if (indices[n] == FTC_CMAP_UNKNOWN), we assume that the corresponding */
/* glyph indices haven't been queried through FT_Get_Glyph_Index() yet */
#define FTC_CMAP_UNKNOWN ( (FT_UInt16)-1 )
/*************************************************************************/
/*************************************************************************/
/***** *****/
/***** CHARMAP NODES *****/
/***** *****/
/*************************************************************************/
/*************************************************************************/
FT_CALLBACK_DEF( void )
ftc_cmap_node_free( FTC_Node ftcnode,
FTC_Cache cache )
{
FTC_CMapNode node = (FTC_CMapNode)ftcnode;
FT_Memory memory = cache->memory;
FT_FREE( node );
}
/* initialize a new cmap node */
FT_CALLBACK_DEF( FT_Error )
ftc_cmap_node_new( FTC_Node *ftcanode,
FT_Pointer ftcquery,
FTC_Cache cache )
{
FTC_CMapNode *anode = (FTC_CMapNode*)ftcanode;
FTC_CMapQuery query = (FTC_CMapQuery)ftcquery;
FT_Error error;
FT_Memory memory = cache->memory;
FTC_CMapNode node;
FT_UInt nn;
if ( !FT_NEW( node ) )
{
node->face_id = query->face_id;
node->cmap_index = query->cmap_index;
node->first = (query->char_code / FTC_CMAP_INDICES_MAX) *
FTC_CMAP_INDICES_MAX;
for ( nn = 0; nn < FTC_CMAP_INDICES_MAX; nn++ )
node->indices[nn] = FTC_CMAP_UNKNOWN;
}
*anode = node;
return error;
}
/* compute the weight of a given cmap node */
FT_CALLBACK_DEF( FT_ULong )
ftc_cmap_node_weight( FTC_Node cnode,
FTC_Cache cache )
{
FT_UNUSED( cnode );
FT_UNUSED( cache );
return sizeof ( *cnode );
}
/* compare a cmap node to a given query */
FT_CALLBACK_DEF( FT_Bool )
ftc_cmap_node_compare( FTC_Node ftcnode,
FT_Pointer ftcquery,
FTC_Cache cache )
{
FTC_CMapNode node = (FTC_CMapNode)ftcnode;
FTC_CMapQuery query = (FTC_CMapQuery)ftcquery;
FT_UNUSED( cache );
if ( node->face_id == query->face_id &&
node->cmap_index == query->cmap_index )
{
FT_UInt32 offset = (FT_UInt32)( query->char_code - node->first );
return FT_BOOL( offset < FTC_CMAP_INDICES_MAX );
}
return 0;
}
FT_CALLBACK_DEF( FT_Bool )
ftc_cmap_node_remove_faceid( FTC_Node ftcnode,
FT_Pointer ftcface_id,
FTC_Cache cache )
{
FTC_CMapNode node = (FTC_CMapNode)ftcnode;
FTC_FaceID face_id = (FTC_FaceID)ftcface_id;
FT_UNUSED( cache );
return FT_BOOL( node->face_id == face_id );
}
/*************************************************************************/
/*************************************************************************/
/***** *****/
/***** GLYPH IMAGE CACHE *****/
/***** *****/
/*************************************************************************/
/*************************************************************************/
FT_CALLBACK_TABLE_DEF
const FTC_CacheClassRec ftc_cmap_cache_class =
{
ftc_cmap_node_new,
ftc_cmap_node_weight,
ftc_cmap_node_compare,
ftc_cmap_node_remove_faceid,
ftc_cmap_node_free,
sizeof ( FTC_CacheRec ),
ftc_cache_init,
ftc_cache_done,
};
/* documentation is in ftcache.h */
FT_EXPORT_DEF( FT_Error )
FTC_CMapCache_New( FTC_Manager manager,
FTC_CMapCache *acache )
{
return FTC_Manager_RegisterCache( manager,
&ftc_cmap_cache_class,
FTC_CACHE_P( acache ) );
}
#ifdef FT_CONFIG_OPTION_OLD_INTERNALS
/*
* Unfortunately, it is not possible to support binary backwards
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -