Hash :
4e4a4363
Author :
Date :
2000-10-28T13:17:11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
#include <cache/ftcsbits.h>
/*************************************************************************/
/*************************************************************************/
/***** *****/
/***** GLYPH IMAGE NODES *****/
/***** *****/
/*************************************************************************/
/*************************************************************************/
LOCAL_FUNC_X
void ftc_sbit_chunk_node_destroy( FTC_ChunkNode node )
{
FTC_ChunkSet cset = node->cset;
FT_Memory memory = cset->memory;
FT_UInt count = node->num_elements;
FTC_SBit sbit = (FTC_SBit)node->elements;
for ( ; count > 0; sbit++, count-- )
FREE( sbit->buffer );
FREE( node );
}
static
FT_Error ftc_bitmap_copy( FT_Memory memory,
FT_Bitmap* source,
FTC_SBit target )
{
FT_Error error;
FT_Int pitch = source->pitch;
FT_ULong size;
if ( pitch < 0 )
pitch = -pitch;
size = (FT_ULong)( pitch * source->rows );
if ( !ALLOC( target->buffer, size ) )
MEM_Copy( target->buffer, source->buffer, size );
return error;
}
LOCAL_FUNC_X
FT_Error ftc_sbit_chunk_node_new( FTC_ChunkSet cset,
FT_UInt index,
FTC_SBitChunk *anode )
{
FT_Error error;
FT_Memory memory = cset->memory;
FTC_SBitSet sbitset = (FTC_SBitSet)cset;
FTC_SBitChunk node = 0;
FT_Face face;
FT_Size size;
/* allocate node */
if ( ALLOC( node, sizeof ( *node ) ) )
goto Exit;
/* init its inner fields */
error = FTC_ChunkNode_Init( FTC_CHUNKNODE(node), cset, index, 1 );
if (error)
goto Exit;
/* we will now load all glyph images */
error = FTC_Manager_Lookup_Size( cset->manager,
&sbitset->description.font,
&face, &size );
if ( !error )
{
FT_UInt glyph_index = index * cset->chunk_size;
FT_UInt load_flags = FT_LOAD_DEFAULT;
FT_UInt image_type = sbitset->description.image_type;
FT_UInt count = node->num_elements;
FTC_SBit sbit = (FTC_SBit)node->elements;
if ( FTC_IMAGE_FORMAT( image_type ) == ftc_image_format_bitmap )
{
if ( image_type & ftc_image_flag_monochrome )
load_flags |= FT_LOAD_MONOCHROME;
/* disable embedded bitmaps loading if necessary */
if ( image_type & ftc_image_flag_no_sbits )
load_flags |= FT_LOAD_NO_BITMAP;
}
else if ( FTC_IMAGE_FORMAT( image_type ) == ftc_image_format_outline )
{
/* disable embedded bitmaps loading */
load_flags |= FT_LOAD_NO_BITMAP;
if ( image_type & ftc_image_flag_unscaled )
{
FT_ERROR(( "FTC_SBit_Cache: cannot load vector outlines in a"
" sbit cache, please check your arguments !!\n" ));
error = FT_Err_Bad_Argument;
goto Exit;
}
}
/* always render glyphs to bitmaps */
load_flags |= FT_LOAD_RENDER;
if ( image_type & ftc_image_flag_unhinted )
load_flags |= FT_LOAD_NO_HINTING;
if ( image_type & ftc_image_flag_autohinted )
load_flags |= FT_LOAD_FORCE_AUTOHINT;
/* load a chunk of small bitmaps in a row */
for ( ; count > 0; count--, glyph_index++ )
{
error = FT_Load_Glyph( face, glyph_index, load_flags );
if (!error)
{
FT_Int temp;
FT_GlyphSlot slot = face->glyph;
FT_Bitmap* bitmap = &slot->bitmap;
FT_Int advance;
/* check that our values fit in 8-bit containers !! */
#define CHECK_SCHAR(d) ( temp = (FT_SChar)d, temp == d )
#define CHECK_BYTE(d) ( temp = (FT_Byte) d, temp == d )
advance = (slot->metrics.horiAdvance+32) >> 6;
if ( CHECK_BYTE ( bitmap->rows ) &&
CHECK_BYTE ( bitmap->width ) &&
CHECK_SCHAR( bitmap->pitch ) &&
CHECK_SCHAR( slot->bitmap_left ) &&
CHECK_SCHAR( slot->bitmap_top ) &&
CHECK_SCHAR( advance ) )
{
sbit->width = (FT_Byte) bitmap->width;
sbit->height = (FT_Byte) bitmap->height;
sbit->pitch = (FT_SChar)bitmap->pitch;
sbit->left = (FT_SChar)slot->bitmap_left;
sbit->top = (FT_SChar)slot->bitmap_top;
sbit->advance = (FT_SChar)advance;
/* grab the bitmap when possible */
if ( slot->flags & ft_glyph_own_bitmap )
{
slot->flags &= ~ft_glyph_own_bitmap;
sbit->buffer = bitmap->buffer;
}
else
{
/* copy the bitmap into a new buffer - ignore error */
ftc_bitmap_copy( memory, bitmap, sbit );
}
}
}
else
sbit->buffer = 0;
}
/* ignore the errors that might have occured there */
/* we recognize unloaded glyphs with "sbit.buffer == 0" */
error = 0;
}
Exit:
if ( error && node )
{
FREE( node->elements );
FREE( node );
}
*anode = node;
return error;
}
/* this function is important because it is both part of */
/* a FTC_ChunkSet_Class and a FTC_CacheNode_Class */
/* */
LOCAL_FUNC_X
FT_ULong ftc_sbit_chunk_node_size( FTC_SBitChunk node )
{
FT_ULong size = 0;
FT_Glyph glyph = node->ft_glyph;
switch ( glyph->format )
{
case ft_glyph_format_bitmap:
{
FT_BitmapGlyph bitg;
bitg = (FT_BitmapGlyph)glyph;
size = bitg->bitmap.rows * labs( bitg->bitmap.pitch ) +
sizeof ( *bitg );
}
break;
case ft_glyph_format_outline:
{
FT_OutlineGlyph outg;
outg = (FT_OutlineGlyph)glyph;
size = outg->outline.n_points *
( sizeof( FT_Vector ) + sizeof ( FT_Byte ) ) +
outg->outline.n_contours * sizeof ( FT_Short ) +
sizeof ( *outg );
}
break;
default:
;
}
size += sizeof ( *node );
return size;
}
/*************************************************************************/
/*************************************************************************/
/***** *****/
/***** GLYPH IMAGE QUEUES *****/
/***** *****/
/*************************************************************************/
/*************************************************************************/
LOCAL_FUNC_X
FT_Error ftc_image_set_init( FTC_ImageSet iset,
FTC_Image_Desc* type )
{
iset->description = *type;
return 0;
}
LOCAL_FUNC_X
FT_Bool ftc_image_set_compare( FTC_ImageSet iset,
FTC_Image_Desc* type )
{
return !memcmp( &iset->description, type, sizeof ( *type ) );
}
FT_CPLUSPLUS( const FTC_ChunkSet_Class ) ftc_sbit_chunk_set_class =
{
sizeof( FTC_ImageSetRec ),
(FTC_ChunkSet_InitFunc) ftc_image_set_init,
(FTC_ChunkSet_DoneFunc) 0,
(FTC_ChunkSet_CompareFunc) ftc_image_set_compare,
(FTC_ChunkSet_NewNodeFunc) ftc_sbit_chunk_node_new,
(FTC_ChunkSet_SizeNodeFunc) ftc_sbit_chunk_node_size,
(FTC_ChunkSet_DestroyNodeFunc)ftc_sbit_chunk_node_destroy
};
/*************************************************************************/
/*************************************************************************/
/***** *****/
/***** GLYPH IMAGE CACHE *****/
/***** *****/
/*************************************************************************/
/*************************************************************************/
FT_CPLUSPLUS( const FTC_Glyph_Cache_Class ) ftc_sbit_chunk_cache_class =
{
{
sizeof( FTC_Image_CacheRec ),
(FTC_Cache_InitFunc) FTC_Glyph_Cache_Init,
(FTC_Cache_DoneFunc) FTC_Glyph_Cache_Done
},
(FTC_ChunkSet_Class*) &ftc_sbit_chunk_set_class
};
FT_EXPORT_FUNC( FT_Error ) FTC_Image_Cache_New( FTC_Manager manager,
FTC_Image_Cache* acache )
{
return FTC_Manager_Register_Cache(
manager,
(FTC_Cache_Class*)&ftc_sbit_chunk_cache_class,
(FTC_Cache*)acache );
}
FT_EXPORT_DEF( FT_Error )
FTC_Image_Cache_Lookup( FTC_Image_Cache cache,
FTC_Image_Desc* desc,
FT_UInt gindex,
FT_Glyph* aglyph )
{
FT_Error error;
FTC_ChunkSet gset;
FTC_GlyphNode node;
FTC_Manager manager;
FTC_ImageSet img_set;
/* check for valid `desc' delayed to FT_Lru_Lookup() */
if ( !cache || !aglyph )
return FT_Err_Invalid_Argument;
*aglyph = 0;
gset = cache->root.last_gset;
img_set = (FTC_ImageSet)gset;
if ( !gset || memcmp( &img_set->description, desc, sizeof ( *desc ) ) )
{
error = FT_Lru_Lookup( cache->root.gsets_lru,
(FT_LruKey)desc,
(FT_Pointer*)&gset );
cache->root.last_gset = gset;
if ( error )
goto Exit;
}
error = FTC_ChunkSet_Lookup_Node( gset, gindex, &node );
if ( error )
goto Exit;
/* now compress the manager's cache pool if needed */
manager = cache->root.root.manager;
if ( manager->num_bytes > manager->max_bytes )
{
FTC_GlyphNode_Ref ( node );
FTC_Manager_Compress( manager );
FTC_GlyphNode_Unref ( node );
}
*aglyph = ((FTC_SBitChunk)node)->ft_glyph;
Exit:
return error;
}