1 /***************************************************************************/
5 /* The FreeType basic cache interface (body). */
7 /* Copyright 2003-2016 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 /***************************************************************************/
20 #include FT_INTERNAL_OBJECTS_H
21 #include FT_INTERNAL_DEBUG_H
30 #define FT_COMPONENT trace_cache
37 typedef struct FTC_BasicAttrRec_
42 } FTC_BasicAttrRec, *FTC_BasicAttrs;
44 #define FTC_BASIC_ATTR_COMPARE( a, b ) \
45 FT_BOOL( FTC_SCALER_COMPARE( &(a)->scaler, &(b)->scaler ) && \
46 (a)->load_flags == (b)->load_flags )
48 #define FTC_BASIC_ATTR_HASH( a ) \
49 ( FTC_SCALER_HASH( &(a)->scaler ) + 31 * (a)->load_flags )
52 typedef struct FTC_BasicQueryRec_
55 FTC_BasicAttrRec attrs;
57 } FTC_BasicQueryRec, *FTC_BasicQuery;
60 typedef struct FTC_BasicFamilyRec_
63 FTC_BasicAttrRec attrs;
65 } FTC_BasicFamilyRec, *FTC_BasicFamily;
68 FT_CALLBACK_DEF( FT_Bool )
69 ftc_basic_family_compare( FTC_MruNode ftcfamily,
72 FTC_BasicFamily family = (FTC_BasicFamily)ftcfamily;
73 FTC_BasicQuery query = (FTC_BasicQuery)ftcquery;
76 return FTC_BASIC_ATTR_COMPARE( &family->attrs, &query->attrs );
80 FT_CALLBACK_DEF( FT_Error )
81 ftc_basic_family_init( FTC_MruNode ftcfamily,
85 FTC_BasicFamily family = (FTC_BasicFamily)ftcfamily;
86 FTC_BasicQuery query = (FTC_BasicQuery)ftcquery;
87 FTC_Cache cache = (FTC_Cache)ftccache;
90 FTC_Family_Init( FTC_FAMILY( family ), cache );
91 family->attrs = query->attrs;
96 FT_CALLBACK_DEF( FT_UInt )
97 ftc_basic_family_get_count( FTC_Family ftcfamily,
100 FTC_BasicFamily family = (FTC_BasicFamily)ftcfamily;
106 error = FTC_Manager_LookupFace( manager, family->attrs.scaler.face_id,
109 if ( error || !face )
112 if ( (FT_ULong)face->num_glyphs > FT_UINT_MAX || 0 > face->num_glyphs )
113 FT_TRACE1(( "ftc_basic_family_get_count:"
114 " too large number of glyphs in this face, truncated\n",
118 result = (FT_UInt)face->num_glyphs;
124 FT_CALLBACK_DEF( FT_Error )
125 ftc_basic_family_load_bitmap( FTC_Family ftcfamily,
130 FTC_BasicFamily family = (FTC_BasicFamily)ftcfamily;
135 error = FTC_Manager_LookupSize( manager, &family->attrs.scaler, &size );
138 FT_Face face = size->face;
141 error = FT_Load_Glyph(
144 (FT_Int)family->attrs.load_flags | FT_LOAD_RENDER );
153 FT_CALLBACK_DEF( FT_Error )
154 ftc_basic_family_load_glyph( FTC_Family ftcfamily,
159 FTC_BasicFamily family = (FTC_BasicFamily)ftcfamily;
161 FTC_Scaler scaler = &family->attrs.scaler;
166 /* we will now load the glyph image */
167 error = FTC_Manager_LookupSize( cache->manager,
174 error = FT_Load_Glyph( face,
176 (FT_Int)family->attrs.load_flags );
179 if ( face->glyph->format == FT_GLYPH_FORMAT_BITMAP ||
180 face->glyph->format == FT_GLYPH_FORMAT_OUTLINE )
186 error = FT_Get_Glyph( face->glyph, &glyph );
194 error = FT_THROW( Invalid_Argument );
203 FT_CALLBACK_DEF( FT_Bool )
204 ftc_basic_gnode_compare_faceid( FTC_Node ftcgnode,
205 FT_Pointer ftcface_id,
207 FT_Bool* list_changed )
209 FTC_GNode gnode = (FTC_GNode)ftcgnode;
210 FTC_FaceID face_id = (FTC_FaceID)ftcface_id;
211 FTC_BasicFamily family = (FTC_BasicFamily)gnode->family;
216 *list_changed = FALSE;
217 result = FT_BOOL( family->attrs.scaler.face_id == face_id );
220 /* we must call this function to avoid this node from appearing
221 * in later lookups with the same face_id!
223 FTC_GNode_UnselectFamily( gnode, cache );
236 const FTC_IFamilyClassRec ftc_basic_image_family_class =
239 sizeof ( FTC_BasicFamilyRec ),
240 ftc_basic_family_compare,
241 ftc_basic_family_init,
242 0, /* FTC_MruNode_ResetFunc */
243 0 /* FTC_MruNode_DoneFunc */
245 ftc_basic_family_load_glyph
250 const FTC_GCacheClassRec ftc_basic_image_cache_class =
256 ftc_basic_gnode_compare_faceid,
259 sizeof ( FTC_GCacheRec ),
263 (FTC_MruListClass)&ftc_basic_image_family_class
267 /* documentation is in ftcache.h */
269 FT_EXPORT_DEF( FT_Error )
270 FTC_ImageCache_New( FTC_Manager manager,
271 FTC_ImageCache *acache )
273 return FTC_GCache_New( manager, &ftc_basic_image_cache_class,
274 (FTC_GCache*)acache );
278 /* documentation is in ftcache.h */
280 FT_EXPORT_DEF( FT_Error )
281 FTC_ImageCache_Lookup( FTC_ImageCache cache,
287 FTC_BasicQueryRec query;
288 FTC_Node node = 0; /* make compiler happy */
293 /* some argument checks are delayed to `FTC_Cache_Lookup' */
296 error = FT_THROW( Invalid_Argument );
304 if ( (FT_ULong)( type->flags - FT_INT_MIN ) > FT_UINT_MAX )
305 FT_TRACE1(( "FTC_ImageCache_Lookup:"
306 " higher bits in load_flags 0x%x are dropped\n",
307 (FT_ULong)type->flags & ~((FT_ULong)FT_UINT_MAX) ));
309 query.attrs.scaler.face_id = type->face_id;
310 query.attrs.scaler.width = type->width;
311 query.attrs.scaler.height = type->height;
312 query.attrs.load_flags = (FT_UInt)type->flags;
314 query.attrs.scaler.pixel = 1;
315 query.attrs.scaler.x_res = 0; /* make compilers happy */
316 query.attrs.scaler.y_res = 0;
318 hash = FTC_BASIC_ATTR_HASH( &query.attrs ) + gindex;
320 #if 1 /* inlining is about 50% faster! */
321 FTC_GCACHE_LOOKUP_CMP( cache,
322 ftc_basic_family_compare,
329 error = FTC_GCache_Lookup( FTC_GCACHE( cache ),
331 FTC_GQUERY( &query ),
336 *aglyph = FTC_INODE( node )->glyph;
350 /* documentation is in ftcache.h */
352 FT_EXPORT_DEF( FT_Error )
353 FTC_ImageCache_LookupScaler( FTC_ImageCache cache,
360 FTC_BasicQueryRec query;
361 FTC_Node node = 0; /* make compiler happy */
366 /* some argument checks are delayed to `FTC_Cache_Lookup' */
367 if ( !aglyph || !scaler )
369 error = FT_THROW( Invalid_Argument );
377 /* `FT_Load_Glyph' and `FT_Load_Char' take FT_UInt flags */
378 if ( load_flags > FT_UINT_MAX )
379 FT_TRACE1(( "FTC_ImageCache_LookupScaler:"
380 " higher bits in load_flags 0x%x are dropped\n",
381 load_flags & ~((FT_ULong)FT_UINT_MAX) ));
383 query.attrs.scaler = scaler[0];
384 query.attrs.load_flags = (FT_UInt)load_flags;
386 hash = FTC_BASIC_ATTR_HASH( &query.attrs ) + gindex;
388 FTC_GCACHE_LOOKUP_CMP( cache,
389 ftc_basic_family_compare,
397 *aglyph = FTC_INODE( node )->glyph;
413 * basic small bitmap cache
418 const FTC_SFamilyClassRec ftc_basic_sbit_family_class =
421 sizeof ( FTC_BasicFamilyRec ),
422 ftc_basic_family_compare,
423 ftc_basic_family_init,
424 0, /* FTC_MruNode_ResetFunc */
425 0 /* FTC_MruNode_DoneFunc */
427 ftc_basic_family_get_count,
428 ftc_basic_family_load_bitmap
433 const FTC_GCacheClassRec ftc_basic_sbit_cache_class =
439 ftc_basic_gnode_compare_faceid,
442 sizeof ( FTC_GCacheRec ),
446 (FTC_MruListClass)&ftc_basic_sbit_family_class
450 /* documentation is in ftcache.h */
452 FT_EXPORT_DEF( FT_Error )
453 FTC_SBitCache_New( FTC_Manager manager,
454 FTC_SBitCache *acache )
456 return FTC_GCache_New( manager, &ftc_basic_sbit_cache_class,
457 (FTC_GCache*)acache );
461 /* documentation is in ftcache.h */
463 FT_EXPORT_DEF( FT_Error )
464 FTC_SBitCache_Lookup( FTC_SBitCache cache,
471 FTC_BasicQueryRec query;
472 FTC_Node node = 0; /* make compiler happy */
479 /* other argument checks delayed to `FTC_Cache_Lookup' */
481 return FT_THROW( Invalid_Argument );
485 if ( (FT_ULong)( type->flags - FT_INT_MIN ) > FT_UINT_MAX )
486 FT_TRACE1(( "FTC_ImageCache_Lookup:"
487 " higher bits in load_flags 0x%x are dropped\n",
488 (FT_ULong)type->flags & ~((FT_ULong)FT_UINT_MAX) ));
490 query.attrs.scaler.face_id = type->face_id;
491 query.attrs.scaler.width = type->width;
492 query.attrs.scaler.height = type->height;
493 query.attrs.load_flags = (FT_UInt)type->flags;
495 query.attrs.scaler.pixel = 1;
496 query.attrs.scaler.x_res = 0; /* make compilers happy */
497 query.attrs.scaler.y_res = 0;
499 /* beware, the hash must be the same for all glyph ranges! */
500 hash = FTC_BASIC_ATTR_HASH( &query.attrs ) +
501 gindex / FTC_SBIT_ITEMS_PER_NODE;
503 #if 1 /* inlining is about 50% faster! */
504 FTC_GCACHE_LOOKUP_CMP( cache,
505 ftc_basic_family_compare,
512 error = FTC_GCache_Lookup( FTC_GCACHE( cache ),
515 FTC_GQUERY( &query ),
521 *ansbit = FTC_SNODE( node )->sbits +
522 ( gindex - FTC_GNODE( node )->gindex );
535 /* documentation is in ftcache.h */
537 FT_EXPORT_DEF( FT_Error )
538 FTC_SBitCache_LookupScaler( FTC_SBitCache cache,
546 FTC_BasicQueryRec query;
547 FTC_Node node = 0; /* make compiler happy */
554 /* other argument checks delayed to `FTC_Cache_Lookup' */
555 if ( !ansbit || !scaler )
556 return FT_THROW( Invalid_Argument );
560 /* `FT_Load_Glyph' and `FT_Load_Char' take FT_UInt flags */
561 if ( load_flags > FT_UINT_MAX )
562 FT_TRACE1(( "FTC_ImageCache_LookupScaler:"
563 " higher bits in load_flags 0x%x are dropped\n",
564 load_flags & ~((FT_ULong)FT_UINT_MAX) ));
566 query.attrs.scaler = scaler[0];
567 query.attrs.load_flags = (FT_UInt)load_flags;
569 /* beware, the hash must be the same for all glyph ranges! */
570 hash = FTC_BASIC_ATTR_HASH( &query.attrs ) +
571 gindex / FTC_SBIT_ITEMS_PER_NODE;
573 FTC_GCACHE_LOOKUP_CMP( cache,
574 ftc_basic_family_compare,
583 *ansbit = FTC_SNODE( node )->sbits +
584 ( gindex - FTC_GNODE( node )->gindex );