xref: /aosp_15_r20/external/freetype/src/cache/ftcglyph.c (revision 63949dbd25bcc50c4e1178497ff9e9574d44fc5a)
1 /****************************************************************************
2  *
3  * ftcglyph.c
4  *
5  *   FreeType Glyph Image (FT_Glyph) cache (body).
6  *
7  * Copyright (C) 2000-2023 by
8  * David Turner, Robert Wilhelm, and Werner Lemberg.
9  *
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.
15  *
16  */
17 
18 
19 #include <freetype/internal/ftobjs.h>
20 #include <freetype/ftcache.h>
21 #include "ftcglyph.h"
22 #include <freetype/fterrors.h>
23 
24 #include "ftccback.h"
25 #include "ftcerror.h"
26 
27 
28   /* create a new chunk node, setting its cache index and ref count */
29   FT_LOCAL_DEF( void )
FTC_GNode_Init(FTC_GNode gnode,FT_UInt gindex,FTC_Family family)30   FTC_GNode_Init( FTC_GNode   gnode,
31                   FT_UInt     gindex,
32                   FTC_Family  family )
33   {
34     gnode->family = family;
35     gnode->gindex = gindex;
36     family->num_nodes++;
37   }
38 
39 
40   FT_LOCAL_DEF( void )
FTC_GNode_UnselectFamily(FTC_GNode gnode,FTC_Cache cache)41   FTC_GNode_UnselectFamily( FTC_GNode  gnode,
42                             FTC_Cache  cache )
43   {
44     FTC_Family  family = gnode->family;
45 
46 
47     gnode->family = NULL;
48     if ( family && --family->num_nodes == 0 )
49       FTC_FAMILY_FREE( family, cache );
50   }
51 
52 
53   FT_LOCAL_DEF( void )
FTC_GNode_Done(FTC_GNode gnode,FTC_Cache cache)54   FTC_GNode_Done( FTC_GNode  gnode,
55                   FTC_Cache  cache )
56   {
57     /* finalize the node */
58     gnode->gindex = 0;
59 
60     FTC_GNode_UnselectFamily( gnode, cache );
61   }
62 
63 
64   FT_LOCAL_DEF( FT_Bool )
ftc_gnode_compare(FTC_Node ftcgnode,FT_Pointer ftcgquery,FTC_Cache cache,FT_Bool * list_changed)65   ftc_gnode_compare( FTC_Node    ftcgnode,
66                      FT_Pointer  ftcgquery,
67                      FTC_Cache   cache,
68                      FT_Bool*    list_changed )
69   {
70     FTC_GNode   gnode  = (FTC_GNode)ftcgnode;
71     FTC_GQuery  gquery = (FTC_GQuery)ftcgquery;
72     FT_UNUSED( cache );
73 
74 
75     if ( list_changed )
76       *list_changed = FALSE;
77     return FT_BOOL( gnode->family == gquery->family &&
78                     gnode->gindex == gquery->gindex );
79   }
80 
81 
82   /*************************************************************************/
83   /*************************************************************************/
84   /*****                                                               *****/
85   /*****                      CHUNK SETS                               *****/
86   /*****                                                               *****/
87   /*************************************************************************/
88   /*************************************************************************/
89 
90   FT_LOCAL_DEF( void )
FTC_Family_Init(FTC_Family family,FTC_Cache cache)91   FTC_Family_Init( FTC_Family  family,
92                    FTC_Cache   cache )
93   {
94     FTC_GCacheClass  clazz = FTC_CACHE_GCACHE_CLASS( cache );
95 
96 
97     family->clazz     = clazz->family_class;
98     family->num_nodes = 0;
99     family->cache     = cache;
100   }
101 
102 
103   FT_LOCAL_DEF( FT_Error )
ftc_gcache_init(FTC_Cache cache)104   ftc_gcache_init( FTC_Cache  cache )
105   {
106     FTC_GCache  gcache = (FTC_GCache)cache;
107     FT_Error    error;
108 
109 
110     error = FTC_Cache_Init( cache );
111     if ( !error )
112     {
113       FTC_GCacheClass   clazz = (FTC_GCacheClass)cache->org_class;
114 
115       FTC_MruList_Init( &gcache->families,
116                         clazz->family_class,
117                         0,  /* no maximum here! */
118                         cache,
119                         cache->memory );
120     }
121 
122     return error;
123   }
124 
125 
126 #if 0
127 
128   FT_LOCAL_DEF( FT_Error )
129   FTC_GCache_Init( FTC_GCache  gcache )
130   {
131     return ftc_gcache_init( FTC_CACHE( gcache ) );
132   }
133 
134 #endif /* 0 */
135 
136 
137   FT_LOCAL_DEF( void )
ftc_gcache_done(FTC_Cache cache)138   ftc_gcache_done( FTC_Cache  cache )
139   {
140     FTC_GCache  gcache = (FTC_GCache)cache;
141 
142 
143     FTC_Cache_Done( cache );
144     FTC_MruList_Done( &gcache->families );
145   }
146 
147 
148 #if 0
149 
150   FT_LOCAL_DEF( void )
151   FTC_GCache_Done( FTC_GCache  gcache )
152   {
153     ftc_gcache_done( FTC_CACHE( gcache ) );
154   }
155 
156 #endif /* 0 */
157 
158 
159   FT_LOCAL_DEF( FT_Error )
FTC_GCache_New(FTC_Manager manager,FTC_GCacheClass clazz,FTC_GCache * acache)160   FTC_GCache_New( FTC_Manager       manager,
161                   FTC_GCacheClass   clazz,
162                   FTC_GCache       *acache )
163   {
164     return FTC_Manager_RegisterCache( manager, (FTC_CacheClass)clazz,
165                                       (FTC_Cache*)acache );
166   }
167 
168 
169 #ifndef FTC_INLINE
170 
171   FT_LOCAL_DEF( FT_Error )
FTC_GCache_Lookup(FTC_GCache gcache,FT_Offset hash,FT_UInt gindex,FTC_GQuery query,FTC_Node * anode)172   FTC_GCache_Lookup( FTC_GCache   gcache,
173                      FT_Offset    hash,
174                      FT_UInt      gindex,
175                      FTC_GQuery   query,
176                      FTC_Node    *anode )
177   {
178     FT_Error  error;
179 
180 
181     query->gindex = gindex;
182 
183     FTC_MRULIST_LOOKUP( &cache->families, query, query->family, error );
184     if ( !error )
185     {
186       FTC_Family  family = query->family;
187 
188 
189       /* prevent the family from being destroyed too early when an        */
190       /* out-of-memory condition occurs during glyph node initialization. */
191       family->num_nodes++;
192 
193       error = FTC_Cache_Lookup( FTC_CACHE( gcache ), hash, query, anode );
194 
195       if ( --family->num_nodes == 0 )
196         FTC_FAMILY_FREE( family, cache );
197     }
198     return error;
199   }
200 
201 #endif /* !FTC_INLINE */
202 
203 
204 /* END */
205