1
/***************************************************************************/
5
/* The FreeType basic cache interface (body). */
7
/* Copyright 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2011 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
33
#ifdef FT_CONFIG_OPTION_OLD_INTERNALS
36
* These structures correspond to the FTC_Font and FTC_ImageDesc types
37
* that were defined in version 2.1.7.
39
typedef struct FTC_OldFontRec_
45
} FTC_OldFontRec, *FTC_OldFont;
48
typedef struct FTC_OldImageDescRec_
53
} FTC_OldImageDescRec, *FTC_OldImageDesc;
57
* Notice that FTC_OldImageDescRec and FTC_ImageTypeRec are nearly
58
* identical, bit-wise. The only difference is that the `width' and
59
* `height' fields are expressed as 16-bit integers in the old structure,
60
* and as normal `int' in the new one.
62
* We are going to perform a weird hack to detect which structure is
63
* being passed to the image and sbit caches. If the new structure's
64
* `width' is larger than 0x10000, we assume that we are really receiving
65
* an FTC_OldImageDesc.
68
#endif /* FT_CONFIG_OPTION_OLD_INTERNALS */
75
typedef struct FTC_BasicAttrRec_
80
} FTC_BasicAttrRec, *FTC_BasicAttrs;
82
#define FTC_BASIC_ATTR_COMPARE( a, b ) \
83
FT_BOOL( FTC_SCALER_COMPARE( &(a)->scaler, &(b)->scaler ) && \
84
(a)->load_flags == (b)->load_flags )
86
#define FTC_BASIC_ATTR_HASH( a ) \
87
( FTC_SCALER_HASH( &(a)->scaler ) + 31*(a)->load_flags )
90
typedef struct FTC_BasicQueryRec_
93
FTC_BasicAttrRec attrs;
95
} FTC_BasicQueryRec, *FTC_BasicQuery;
98
typedef struct FTC_BasicFamilyRec_
100
FTC_FamilyRec family;
101
FTC_BasicAttrRec attrs;
103
} FTC_BasicFamilyRec, *FTC_BasicFamily;
106
FT_CALLBACK_DEF( FT_Bool )
107
ftc_basic_family_compare( FTC_MruNode ftcfamily,
108
FT_Pointer ftcquery )
110
FTC_BasicFamily family = (FTC_BasicFamily)ftcfamily;
111
FTC_BasicQuery query = (FTC_BasicQuery)ftcquery;
114
return FTC_BASIC_ATTR_COMPARE( &family->attrs, &query->attrs );
118
FT_CALLBACK_DEF( FT_Error )
119
ftc_basic_family_init( FTC_MruNode ftcfamily,
121
FT_Pointer ftccache )
123
FTC_BasicFamily family = (FTC_BasicFamily)ftcfamily;
124
FTC_BasicQuery query = (FTC_BasicQuery)ftcquery;
125
FTC_Cache cache = (FTC_Cache)ftccache;
128
FTC_Family_Init( FTC_FAMILY( family ), cache );
129
family->attrs = query->attrs;
134
FT_CALLBACK_DEF( FT_UInt )
135
ftc_basic_family_get_count( FTC_Family ftcfamily,
136
FTC_Manager manager )
138
FTC_BasicFamily family = (FTC_BasicFamily)ftcfamily;
144
error = FTC_Manager_LookupFace( manager, family->attrs.scaler.face_id,
147
if ( error || !face )
150
if ( (FT_ULong)face->num_glyphs > FT_UINT_MAX || 0 > face->num_glyphs )
152
FT_TRACE1(( "ftc_basic_family_get_count: too large number of glyphs " ));
153
FT_TRACE1(( "in this face, truncated\n", face->num_glyphs ));
157
result = (FT_UInt)face->num_glyphs;
163
FT_CALLBACK_DEF( FT_Error )
164
ftc_basic_family_load_bitmap( FTC_Family ftcfamily,
169
FTC_BasicFamily family = (FTC_BasicFamily)ftcfamily;
174
error = FTC_Manager_LookupSize( manager, &family->attrs.scaler, &size );
177
FT_Face face = size->face;
180
error = FT_Load_Glyph( face, gindex,
181
family->attrs.load_flags | FT_LOAD_RENDER );
190
FT_CALLBACK_DEF( FT_Error )
191
ftc_basic_family_load_glyph( FTC_Family ftcfamily,
196
FTC_BasicFamily family = (FTC_BasicFamily)ftcfamily;
198
FTC_Scaler scaler = &family->attrs.scaler;
203
/* we will now load the glyph image */
204
error = FTC_Manager_LookupSize( cache->manager,
211
error = FT_Load_Glyph( face, gindex, family->attrs.load_flags );
214
if ( face->glyph->format == FT_GLYPH_FORMAT_BITMAP ||
215
face->glyph->format == FT_GLYPH_FORMAT_OUTLINE )
221
error = FT_Get_Glyph( face->glyph, &glyph );
229
error = FTC_Err_Invalid_Argument;
238
FT_CALLBACK_DEF( FT_Bool )
239
ftc_basic_gnode_compare_faceid( FTC_Node ftcgnode,
240
FT_Pointer ftcface_id,
242
FT_Bool* list_changed )
244
FTC_GNode gnode = (FTC_GNode)ftcgnode;
245
FTC_FaceID face_id = (FTC_FaceID)ftcface_id;
246
FTC_BasicFamily family = (FTC_BasicFamily)gnode->family;
251
*list_changed = FALSE;
252
result = FT_BOOL( family->attrs.scaler.face_id == face_id );
255
/* we must call this function to avoid this node from appearing
256
* in later lookups with the same face_id!
258
FTC_GNode_UnselectFamily( gnode, cache );
270
FT_CALLBACK_TABLE_DEF
271
const FTC_IFamilyClassRec ftc_basic_image_family_class =
274
sizeof ( FTC_BasicFamilyRec ),
275
ftc_basic_family_compare,
276
ftc_basic_family_init,
277
0, /* FTC_MruNode_ResetFunc */
278
0 /* FTC_MruNode_DoneFunc */
280
ftc_basic_family_load_glyph
284
FT_CALLBACK_TABLE_DEF
285
const FTC_GCacheClassRec ftc_basic_image_cache_class =
291
ftc_basic_gnode_compare_faceid,
294
sizeof ( FTC_GCacheRec ),
298
(FTC_MruListClass)&ftc_basic_image_family_class
302
/* documentation is in ftcache.h */
304
FT_EXPORT_DEF( FT_Error )
305
FTC_ImageCache_New( FTC_Manager manager,
306
FTC_ImageCache *acache )
308
return FTC_GCache_New( manager, &ftc_basic_image_cache_class,
309
(FTC_GCache*)acache );
313
/* documentation is in ftcache.h */
315
FT_EXPORT_DEF( FT_Error )
316
FTC_ImageCache_Lookup( FTC_ImageCache cache,
322
FTC_BasicQueryRec query;
323
FTC_Node node = 0; /* make compiler happy */
328
/* some argument checks are delayed to FTC_Cache_Lookup */
331
error = FTC_Err_Invalid_Argument;
339
#if defined( FT_CONFIG_OPTION_OLD_INTERNALS ) && ( FT_INT_MAX > 0xFFFFU )
342
* This one is a major hack used to detect whether we are passed a
343
* regular FTC_ImageType handle, or a legacy FTC_OldImageDesc one.
345
if ( (FT_ULong)type->width >= 0x10000L )
347
FTC_OldImageDesc desc = (FTC_OldImageDesc)type;
350
query.attrs.scaler.face_id = desc->font.face_id;
351
query.attrs.scaler.width = desc->font.pix_width;
352
query.attrs.scaler.height = desc->font.pix_height;
353
query.attrs.load_flags = desc->flags;
357
#endif /* FT_CONFIG_OPTION_OLD_INTERNALS */
360
if ( (FT_ULong)(type->flags - FT_INT_MIN) > FT_UINT_MAX )
362
FT_TRACE1(( "FTC_ImageCache_Lookup: higher bits in load_flags" ));
363
FT_TRACE1(( "0x%x are dropped\n", (type->flags & ~((FT_ULong)FT_UINT_MAX)) ));
366
query.attrs.scaler.face_id = type->face_id;
367
query.attrs.scaler.width = type->width;
368
query.attrs.scaler.height = type->height;
369
query.attrs.load_flags = (FT_UInt)type->flags;
372
query.attrs.scaler.pixel = 1;
373
query.attrs.scaler.x_res = 0; /* make compilers happy */
374
query.attrs.scaler.y_res = 0;
376
hash = FTC_BASIC_ATTR_HASH( &query.attrs ) + gindex;
378
#if 1 /* inlining is about 50% faster! */
379
FTC_GCACHE_LOOKUP_CMP( cache,
380
ftc_basic_family_compare,
387
error = FTC_GCache_Lookup( FTC_GCACHE( cache ),
389
FTC_GQUERY( &query ),
394
*aglyph = FTC_INODE( node )->glyph;
408
/* documentation is in ftcache.h */
410
FT_EXPORT_DEF( FT_Error )
411
FTC_ImageCache_LookupScaler( FTC_ImageCache cache,
418
FTC_BasicQueryRec query;
419
FTC_Node node = 0; /* make compiler happy */
424
/* some argument checks are delayed to FTC_Cache_Lookup */
425
if ( !aglyph || !scaler )
427
error = FTC_Err_Invalid_Argument;
435
/* FT_Load_Glyph(), FT_Load_Char() take FT_UInt flags */
436
if ( load_flags > FT_UINT_MAX )
438
FT_TRACE1(( "FTC_ImageCache_LookupScaler: higher bits in load_flags" ));
439
FT_TRACE1(( "0x%x are dropped\n", (load_flags & ~((FT_ULong)FT_UINT_MAX)) ));
442
query.attrs.scaler = scaler[0];
443
query.attrs.load_flags = (FT_UInt)load_flags;
445
hash = FTC_BASIC_ATTR_HASH( &query.attrs ) + gindex;
447
FTC_GCACHE_LOOKUP_CMP( cache,
448
ftc_basic_family_compare,
456
*aglyph = FTC_INODE( node )->glyph;
471
#ifdef FT_CONFIG_OPTION_OLD_INTERNALS
473
/* yet another backwards-legacy structure */
474
typedef struct FTC_OldImage_Desc_
482
#define FTC_OLD_IMAGE_FORMAT( x ) ( (x) & 7 )
485
#define ftc_old_image_format_bitmap 0x0000
486
#define ftc_old_image_format_outline 0x0001
488
#define ftc_old_image_format_mask 0x000F
490
#define ftc_old_image_flag_monochrome 0x0010
491
#define ftc_old_image_flag_unhinted 0x0020
492
#define ftc_old_image_flag_autohinted 0x0040
493
#define ftc_old_image_flag_unscaled 0x0080
494
#define ftc_old_image_flag_no_sbits 0x0100
496
/* monochrome bitmap */
497
#define ftc_old_image_mono ftc_old_image_format_bitmap | \
498
ftc_old_image_flag_monochrome
500
/* anti-aliased bitmap */
501
#define ftc_old_image_grays ftc_old_image_format_bitmap
504
#define ftc_old_image_outline ftc_old_image_format_outline
508
ftc_image_type_from_old_desc( FTC_ImageType typ,
509
FTC_OldImage_Desc* desc )
511
typ->face_id = desc->font.face_id;
512
typ->width = desc->font.pix_width;
513
typ->height = desc->font.pix_height;
515
/* convert image type flags to load flags */
517
FT_UInt load_flags = FT_LOAD_DEFAULT;
518
FT_UInt type = desc->image_type;
521
/* determine load flags, depending on the font description's */
524
if ( FTC_OLD_IMAGE_FORMAT( type ) == ftc_old_image_format_bitmap )
526
if ( type & ftc_old_image_flag_monochrome )
527
load_flags |= FT_LOAD_MONOCHROME;
529
/* disable embedded bitmaps loading if necessary */
530
if ( type & ftc_old_image_flag_no_sbits )
531
load_flags |= FT_LOAD_NO_BITMAP;
535
/* we want an outline, don't load embedded bitmaps */
536
load_flags |= FT_LOAD_NO_BITMAP;
538
if ( type & ftc_old_image_flag_unscaled )
539
load_flags |= FT_LOAD_NO_SCALE;
542
/* always render glyphs to bitmaps */
543
load_flags |= FT_LOAD_RENDER;
545
if ( type & ftc_old_image_flag_unhinted )
546
load_flags |= FT_LOAD_NO_HINTING;
548
if ( type & ftc_old_image_flag_autohinted )
549
load_flags |= FT_LOAD_FORCE_AUTOHINT;
551
typ->flags = load_flags;
556
FT_EXPORT( FT_Error )
557
FTC_Image_Cache_New( FTC_Manager manager,
558
FTC_ImageCache *acache );
560
FT_EXPORT( FT_Error )
561
FTC_Image_Cache_Lookup( FTC_ImageCache icache,
562
FTC_OldImage_Desc* desc,
567
FT_EXPORT_DEF( FT_Error )
568
FTC_Image_Cache_New( FTC_Manager manager,
569
FTC_ImageCache *acache )
571
return FTC_ImageCache_New( manager, (FTC_ImageCache*)acache );
576
FT_EXPORT_DEF( FT_Error )
577
FTC_Image_Cache_Lookup( FTC_ImageCache icache,
578
FTC_OldImage_Desc* desc,
582
FTC_ImageTypeRec type0;
586
return FTC_Err_Invalid_Argument;
588
ftc_image_type_from_old_desc( &type0, desc );
590
return FTC_ImageCache_Lookup( (FTC_ImageCache)icache,
597
#endif /* FT_CONFIG_OPTION_OLD_INTERNALS */
602
* basic small bitmap cache
607
FT_CALLBACK_TABLE_DEF
608
const FTC_SFamilyClassRec ftc_basic_sbit_family_class =
611
sizeof( FTC_BasicFamilyRec ),
612
ftc_basic_family_compare,
613
ftc_basic_family_init,
614
0, /* FTC_MruNode_ResetFunc */
615
0 /* FTC_MruNode_DoneFunc */
617
ftc_basic_family_get_count,
618
ftc_basic_family_load_bitmap
622
FT_CALLBACK_TABLE_DEF
623
const FTC_GCacheClassRec ftc_basic_sbit_cache_class =
629
ftc_basic_gnode_compare_faceid,
632
sizeof ( FTC_GCacheRec ),
636
(FTC_MruListClass)&ftc_basic_sbit_family_class
640
/* documentation is in ftcache.h */
642
FT_EXPORT_DEF( FT_Error )
643
FTC_SBitCache_New( FTC_Manager manager,
644
FTC_SBitCache *acache )
646
return FTC_GCache_New( manager, &ftc_basic_sbit_cache_class,
647
(FTC_GCache*)acache );
651
/* documentation is in ftcache.h */
653
FT_EXPORT_DEF( FT_Error )
654
FTC_SBitCache_Lookup( FTC_SBitCache cache,
661
FTC_BasicQueryRec query;
662
FTC_Node node = 0; /* make compiler happy */
669
/* other argument checks delayed to FTC_Cache_Lookup */
671
return FTC_Err_Invalid_Argument;
675
#if defined( FT_CONFIG_OPTION_OLD_INTERNALS ) && ( FT_INT_MAX > 0xFFFFU )
677
/* This one is a major hack used to detect whether we are passed a
678
* regular FTC_ImageType handle, or a legacy FTC_OldImageDesc one.
680
if ( (FT_ULong)type->width >= 0x10000L )
682
FTC_OldImageDesc desc = (FTC_OldImageDesc)type;
685
query.attrs.scaler.face_id = desc->font.face_id;
686
query.attrs.scaler.width = desc->font.pix_width;
687
query.attrs.scaler.height = desc->font.pix_height;
688
query.attrs.load_flags = desc->flags;
692
#endif /* FT_CONFIG_OPTION_OLD_INTERNALS */
695
if ( (FT_ULong)(type->flags - FT_INT_MIN) > FT_UINT_MAX )
697
FT_TRACE1(( "FTC_ImageCache_Lookup: higher bits in load_flags" ));
698
FT_TRACE1(( "0x%x are dropped\n", (type->flags & ~((FT_ULong)FT_UINT_MAX)) ));
701
query.attrs.scaler.face_id = type->face_id;
702
query.attrs.scaler.width = type->width;
703
query.attrs.scaler.height = type->height;
704
query.attrs.load_flags = (FT_UInt)type->flags;
707
query.attrs.scaler.pixel = 1;
708
query.attrs.scaler.x_res = 0; /* make compilers happy */
709
query.attrs.scaler.y_res = 0;
711
/* beware, the hash must be the same for all glyph ranges! */
712
hash = FTC_BASIC_ATTR_HASH( &query.attrs ) +
713
gindex / FTC_SBIT_ITEMS_PER_NODE;
715
#if 1 /* inlining is about 50% faster! */
716
FTC_GCACHE_LOOKUP_CMP( cache,
717
ftc_basic_family_compare,
724
error = FTC_GCache_Lookup( FTC_GCACHE( cache ),
727
FTC_GQUERY( &query ),
733
*ansbit = FTC_SNODE( node )->sbits +
734
( gindex - FTC_GNODE( node )->gindex );
747
/* documentation is in ftcache.h */
749
FT_EXPORT_DEF( FT_Error )
750
FTC_SBitCache_LookupScaler( FTC_SBitCache cache,
758
FTC_BasicQueryRec query;
759
FTC_Node node = 0; /* make compiler happy */
766
/* other argument checks delayed to FTC_Cache_Lookup */
767
if ( !ansbit || !scaler )
768
return FTC_Err_Invalid_Argument;
772
/* FT_Load_Glyph(), FT_Load_Char() take FT_UInt flags */
773
if ( load_flags > FT_UINT_MAX )
775
FT_TRACE1(( "FTC_ImageCache_LookupScaler: higher bits in load_flags" ));
776
FT_TRACE1(( "0x%x are dropped\n", (load_flags & ~((FT_ULong)FT_UINT_MAX)) ));
779
query.attrs.scaler = scaler[0];
780
query.attrs.load_flags = (FT_UInt)load_flags;
782
/* beware, the hash must be the same for all glyph ranges! */
783
hash = FTC_BASIC_ATTR_HASH( &query.attrs ) +
784
gindex / FTC_SBIT_ITEMS_PER_NODE;
786
FTC_GCACHE_LOOKUP_CMP( cache,
787
ftc_basic_family_compare,
796
*ansbit = FTC_SNODE( node )->sbits +
797
( gindex - FTC_GNODE( node )->gindex );
810
#ifdef FT_CONFIG_OPTION_OLD_INTERNALS
812
FT_EXPORT( FT_Error )
813
FTC_SBit_Cache_New( FTC_Manager manager,
814
FTC_SBitCache *acache );
816
FT_EXPORT( FT_Error )
817
FTC_SBit_Cache_Lookup( FTC_SBitCache cache,
818
FTC_OldImage_Desc* desc,
823
FT_EXPORT_DEF( FT_Error )
824
FTC_SBit_Cache_New( FTC_Manager manager,
825
FTC_SBitCache *acache )
827
return FTC_SBitCache_New( manager, (FTC_SBitCache*)acache );
831
FT_EXPORT_DEF( FT_Error )
832
FTC_SBit_Cache_Lookup( FTC_SBitCache cache,
833
FTC_OldImage_Desc* desc,
837
FTC_ImageTypeRec type0;
841
return FTC_Err_Invalid_Argument;
843
ftc_image_type_from_old_desc( &type0, desc );
845
return FTC_SBitCache_Lookup( (FTC_SBitCache)cache,
852
#endif /* FT_CONFIG_OPTION_OLD_INTERNALS */