1 /***************************************************************************/
5 /* FreeType Image cache (body). */
7 /* Copyright 2000-2001 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
10 /* This file is part of the FreeType project, and may only be used, */
11 /* modified, and distributed under the terms of the FreeType project */
12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
13 /* this file you indicate that you have read the license and */
14 /* understand and accept it fully. */
16 /***************************************************************************/
21 #include FT_CACHE_IMAGE_H
22 #include FT_INTERNAL_MEMORY_H
26 #include <string.h> /* memcmp() */
27 #include <stdlib.h> /* labs() */
30 /* the FT_Glyph image `glyph node' type */
31 typedef struct FTC_GlyphImageRec_
33 FTC_GlyphNodeRec root;
36 } FTC_GlyphImageRec, *FTC_GlyphImage;
39 /* the glyph image queue type */
40 typedef struct FTC_ImageSetRec_
43 FTC_Image_Desc description;
45 } FTC_ImageSetRec, *FTC_ImageSet;
48 typedef struct FTC_Image_CacheRec_
50 FTC_Glyph_CacheRec root;
56 /*************************************************************************/
57 /*************************************************************************/
59 /***** GLYPH IMAGE NODES *****/
61 /*************************************************************************/
62 /*************************************************************************/
65 FT_CALLBACK_DEF( void )
66 ftc_glyph_image_node_destroy( FTC_GlyphImage node,
69 FT_Memory memory = gset->memory;
72 FT_Done_Glyph( node->ft_glyph );
77 FT_CALLBACK_DEF( FT_Error )
78 ftc_glyph_image_node_new( FTC_GlyphSet gset,
80 FTC_GlyphImage *anode )
82 FT_Memory memory = gset->memory;
83 FTC_ImageSet imageset = (FTC_ImageSet)gset;
85 FTC_GlyphImage node = 0;
91 if ( ALLOC( node, sizeof ( *node ) ) )
94 /* initialize its inner fields */
95 FTC_GlyphNode_Init( FTC_GLYPHNODE( node ), gset, glyph_index );
97 /* we will now load the glyph image */
98 error = FTC_Manager_Lookup_Size( gset->manager,
99 &imageset->description.font,
103 FT_UInt gindex = node->root.glyph_index;
104 FT_UInt load_flags = FT_LOAD_DEFAULT;
105 FT_UInt image_type = imageset->description.image_type;
108 if ( FTC_IMAGE_FORMAT( image_type ) == ftc_image_format_bitmap )
110 load_flags |= FT_LOAD_RENDER;
111 if ( image_type & ftc_image_flag_monochrome )
112 load_flags |= FT_LOAD_MONOCHROME;
114 /* disable embedded bitmaps loading if necessary */
115 if ( image_type & ftc_image_flag_no_sbits )
116 load_flags |= FT_LOAD_NO_BITMAP;
118 else if ( FTC_IMAGE_FORMAT( image_type ) == ftc_image_format_outline )
120 /* disable embedded bitmaps loading */
121 load_flags |= FT_LOAD_NO_BITMAP;
123 if ( image_type & ftc_image_flag_unscaled )
124 load_flags |= FT_LOAD_NO_SCALE;
127 if ( image_type & ftc_image_flag_unhinted )
128 load_flags |= FT_LOAD_NO_HINTING;
130 if ( image_type & ftc_image_flag_autohinted )
131 load_flags |= FT_LOAD_FORCE_AUTOHINT;
133 error = FT_Load_Glyph( face, gindex, load_flags );
136 if ( face->glyph->format == ft_glyph_format_bitmap ||
137 face->glyph->format == ft_glyph_format_outline )
143 error = FT_Get_Glyph( face->glyph, &glyph );
145 node->ft_glyph = glyph;
148 error = FTC_Err_Invalid_Argument;
161 /* this function is important because it is both part of */
162 /* an FTC_GlyphSet_Class and an FTC_CacheNode_Class */
164 FT_CALLBACK_DEF( FT_ULong )
165 ftc_glyph_image_node_size( FTC_GlyphImage node )
168 FT_Glyph glyph = node->ft_glyph;
171 switch ( glyph->format )
173 case ft_glyph_format_bitmap:
178 bitg = (FT_BitmapGlyph)glyph;
179 size = bitg->bitmap.rows * labs( bitg->bitmap.pitch ) +
184 case ft_glyph_format_outline:
186 FT_OutlineGlyph outg;
189 outg = (FT_OutlineGlyph)glyph;
190 size = outg->outline.n_points *
191 ( sizeof( FT_Vector ) + sizeof ( FT_Byte ) ) +
192 outg->outline.n_contours * sizeof ( FT_Short ) +
201 size += sizeof ( *node );
206 /*************************************************************************/
207 /*************************************************************************/
209 /***** GLYPH IMAGE SETS *****/
211 /*************************************************************************/
212 /*************************************************************************/
215 FT_CALLBACK_DEF( FT_Error )
216 ftc_image_set_init( FTC_ImageSet iset,
217 FTC_Image_Desc* type )
219 iset->description = *type;
224 FT_CALLBACK_DEF( FT_Bool )
225 ftc_image_set_compare( FTC_ImageSet iset,
226 FTC_Image_Desc* type )
228 return FT_BOOL( !memcmp( &iset->description, type, sizeof ( *type ) ) );
232 FT_CALLBACK_TABLE_DEF
233 const FTC_GlyphSet_Class ftc_glyph_image_set_class =
235 sizeof( FTC_ImageSetRec ),
237 (FTC_GlyphSet_InitFunc) ftc_image_set_init,
238 (FTC_GlyphSet_DoneFunc) 0,
239 (FTC_GlyphSet_CompareFunc) ftc_image_set_compare,
241 (FTC_GlyphSet_NewNodeFunc) ftc_glyph_image_node_new,
242 (FTC_GlyphSet_SizeNodeFunc) ftc_glyph_image_node_size,
243 (FTC_GlyphSet_DestroyNodeFunc)ftc_glyph_image_node_destroy
247 /*************************************************************************/
248 /*************************************************************************/
250 /***** GLYPH IMAGE CACHE *****/
252 /*************************************************************************/
253 /*************************************************************************/
256 FT_CALLBACK_TABLE_DEF
257 const FTC_Glyph_Cache_Class ftc_glyph_image_cache_class =
260 sizeof( FTC_Image_CacheRec ),
261 (FTC_Cache_InitFunc) FTC_Glyph_Cache_Init,
262 (FTC_Cache_DoneFunc) FTC_Glyph_Cache_Done
264 (FTC_GlyphSet_Class*) &ftc_glyph_image_set_class
268 /* documentation is in ftcimage.h */
270 FT_EXPORT_DEF( FT_Error )
271 FTC_Image_Cache_New( FTC_Manager manager,
272 FTC_Image_Cache *acache )
274 return FTC_Manager_Register_Cache(
276 (FTC_Cache_Class*)&ftc_glyph_image_cache_class,
277 (FTC_Cache*)acache );
281 /* documentation is in ftcimage.h */
283 FT_EXPORT_DEF( FT_Error )
284 FTC_Image_Cache_Lookup( FTC_Image_Cache cache,
285 FTC_Image_Desc* desc,
293 /* some argument checks are delayed to FTC_Glyph_Cache_Lookup */
296 return FTC_Err_Invalid_Argument;
298 error = FTC_Glyph_Cache_Lookup( (FTC_Glyph_Cache)cache,
299 desc, gindex, &node );
302 *aglyph = ((FTC_GlyphImage)node)->ft_glyph;