Disabling auto-refresh of game list by default, as it is causing bugs sometimes
[open-ps2-loader.git] / thirdparty / freetype-2.3.12 / src / cache / ftcsbits.c
blob60d46aa7a068ec247fd7b740811ca52cf811b98f
1 /***************************************************************************/
2 /* */
3 /* ftcsbits.c */
4 /* */
5 /* FreeType sbits manager (body). */
6 /* */
7 /* Copyright 2000-2001, 2002, 2003, 2004, 2005, 2006, 2009 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 /***************************************************************************/
19 #include <ft2build.h>
20 #include FT_CACHE_H
21 #include "ftcsbits.h"
22 #include FT_INTERNAL_OBJECTS_H
23 #include FT_INTERNAL_DEBUG_H
24 #include FT_ERRORS_H
26 #include "ftccback.h"
27 #include "ftcerror.h"
29 #undef FT_COMPONENT
30 #define FT_COMPONENT trace_cache
33 /*************************************************************************/
34 /*************************************************************************/
35 /***** *****/
36 /***** SBIT CACHE NODES *****/
37 /***** *****/
38 /*************************************************************************/
39 /*************************************************************************/
42 static FT_Error
43 ftc_sbit_copy_bitmap( FTC_SBit sbit,
44 FT_Bitmap* bitmap,
45 FT_Memory memory )
47 FT_Error error;
48 FT_Int pitch = bitmap->pitch;
49 FT_ULong size;
52 if ( pitch < 0 )
53 pitch = -pitch;
55 size = (FT_ULong)( pitch * bitmap->rows );
57 if ( !FT_ALLOC( sbit->buffer, size ) )
58 FT_MEM_COPY( sbit->buffer, bitmap->buffer, size );
60 return error;
64 FT_LOCAL_DEF( void )
65 ftc_snode_free( FTC_Node ftcsnode,
66 FTC_Cache cache )
68 FTC_SNode snode = (FTC_SNode)ftcsnode;
69 FTC_SBit sbit = snode->sbits;
70 FT_UInt count = snode->count;
71 FT_Memory memory = cache->memory;
74 for ( ; count > 0; sbit++, count-- )
75 FT_FREE( sbit->buffer );
77 FTC_GNode_Done( FTC_GNODE( snode ), cache );
79 FT_FREE( snode );
83 FT_LOCAL_DEF( void )
84 FTC_SNode_Free( FTC_SNode snode,
85 FTC_Cache cache )
87 ftc_snode_free( FTC_NODE( snode ), cache );
92 * This function tries to load a small bitmap within a given FTC_SNode.
93 * Note that it returns a non-zero error code _only_ in the case of
94 * out-of-memory condition. For all other errors (e.g., corresponding
95 * to a bad font file), this function will mark the sbit as `unavailable'
96 * and return a value of 0.
98 * You should also read the comment within the @ftc_snode_compare
99 * function below to see how out-of-memory is handled during a lookup.
101 static FT_Error
102 ftc_snode_load( FTC_SNode snode,
103 FTC_Manager manager,
104 FT_UInt gindex,
105 FT_ULong *asize )
107 FT_Error error;
108 FTC_GNode gnode = FTC_GNODE( snode );
109 FTC_Family family = gnode->family;
110 FT_Memory memory = manager->memory;
111 FT_Face face;
112 FTC_SBit sbit;
113 FTC_SFamilyClass clazz;
116 if ( (FT_UInt)(gindex - gnode->gindex) >= snode->count )
118 FT_ERROR(( "ftc_snode_load: invalid glyph index" ));
119 return FTC_Err_Invalid_Argument;
122 sbit = snode->sbits + ( gindex - gnode->gindex );
123 clazz = (FTC_SFamilyClass)family->clazz;
125 sbit->buffer = 0;
127 error = clazz->family_load_glyph( family, gindex, manager, &face );
128 if ( error )
129 goto BadGlyph;
132 FT_Int temp;
133 FT_GlyphSlot slot = face->glyph;
134 FT_Bitmap* bitmap = &slot->bitmap;
135 FT_Pos xadvance, yadvance; /* FT_GlyphSlot->advance.{x|y} */
138 if ( slot->format != FT_GLYPH_FORMAT_BITMAP )
140 FT_TRACE0(( "ftc_snode_load:"
141 " glyph loaded didn't return a bitmap\n" ));
142 goto BadGlyph;
145 /* Check that our values fit into 8-bit containers! */
146 /* If this is not the case, our bitmap is too large */
147 /* and we will leave it as `missing' with sbit.buffer = 0 */
149 #define CHECK_CHAR( d ) ( temp = (FT_Char)d, temp == d )
150 #define CHECK_BYTE( d ) ( temp = (FT_Byte)d, temp == d )
152 /* horizontal advance in pixels */
153 xadvance = ( slot->advance.x + 32 ) >> 6;
154 yadvance = ( slot->advance.y + 32 ) >> 6;
156 if ( !CHECK_BYTE( bitmap->rows ) ||
157 !CHECK_BYTE( bitmap->width ) ||
158 !CHECK_CHAR( bitmap->pitch ) ||
159 !CHECK_CHAR( slot->bitmap_left ) ||
160 !CHECK_CHAR( slot->bitmap_top ) ||
161 !CHECK_CHAR( xadvance ) ||
162 !CHECK_CHAR( yadvance ) )
163 goto BadGlyph;
165 sbit->width = (FT_Byte)bitmap->width;
166 sbit->height = (FT_Byte)bitmap->rows;
167 sbit->pitch = (FT_Char)bitmap->pitch;
168 sbit->left = (FT_Char)slot->bitmap_left;
169 sbit->top = (FT_Char)slot->bitmap_top;
170 sbit->xadvance = (FT_Char)xadvance;
171 sbit->yadvance = (FT_Char)yadvance;
172 sbit->format = (FT_Byte)bitmap->pixel_mode;
173 sbit->max_grays = (FT_Byte)(bitmap->num_grays - 1);
175 /* copy the bitmap into a new buffer -- ignore error */
176 error = ftc_sbit_copy_bitmap( sbit, bitmap, memory );
178 /* now, compute size */
179 if ( asize )
180 *asize = FT_ABS( sbit->pitch ) * sbit->height;
182 } /* glyph loading successful */
184 /* ignore the errors that might have occurred -- */
185 /* we mark unloaded glyphs with `sbit.buffer == 0' */
186 /* and `width == 255', `height == 0' */
187 /* */
188 if ( error && error != FTC_Err_Out_Of_Memory )
190 BadGlyph:
191 sbit->width = 255;
192 sbit->height = 0;
193 sbit->buffer = NULL;
194 error = 0;
195 if ( asize )
196 *asize = 0;
199 return error;
203 FT_LOCAL_DEF( FT_Error )
204 FTC_SNode_New( FTC_SNode *psnode,
205 FTC_GQuery gquery,
206 FTC_Cache cache )
208 FT_Memory memory = cache->memory;
209 FT_Error error;
210 FTC_SNode snode = NULL;
211 FT_UInt gindex = gquery->gindex;
212 FTC_Family family = gquery->family;
214 FTC_SFamilyClass clazz = FTC_CACHE__SFAMILY_CLASS( cache );
215 FT_UInt total;
218 total = clazz->family_get_count( family, cache->manager );
219 if ( total == 0 || gindex >= total )
221 error = FT_Err_Invalid_Argument;
222 goto Exit;
225 if ( !FT_NEW( snode ) )
227 FT_UInt count, start;
230 start = gindex - ( gindex % FTC_SBIT_ITEMS_PER_NODE );
231 count = total - start;
232 if ( count > FTC_SBIT_ITEMS_PER_NODE )
233 count = FTC_SBIT_ITEMS_PER_NODE;
235 FTC_GNode_Init( FTC_GNODE( snode ), start, family );
237 snode->count = count;
239 error = ftc_snode_load( snode,
240 cache->manager,
241 gindex,
242 NULL );
243 if ( error )
245 FTC_SNode_Free( snode, cache );
246 snode = NULL;
250 Exit:
251 *psnode = snode;
252 return error;
256 FT_LOCAL_DEF( FT_Error )
257 ftc_snode_new( FTC_Node *ftcpsnode,
258 FT_Pointer ftcgquery,
259 FTC_Cache cache )
261 FTC_SNode *psnode = (FTC_SNode*)ftcpsnode;
262 FTC_GQuery gquery = (FTC_GQuery)ftcgquery;
265 return FTC_SNode_New( psnode, gquery, cache );
269 FT_LOCAL_DEF( FT_Offset )
270 ftc_snode_weight( FTC_Node ftcsnode,
271 FTC_Cache cache )
273 FTC_SNode snode = (FTC_SNode)ftcsnode;
274 FT_UInt count = snode->count;
275 FTC_SBit sbit = snode->sbits;
276 FT_Int pitch;
277 FT_Offset size;
279 FT_UNUSED( cache );
282 FT_ASSERT( snode->count <= FTC_SBIT_ITEMS_PER_NODE );
284 /* the node itself */
285 size = sizeof ( *snode );
287 for ( ; count > 0; count--, sbit++ )
289 if ( sbit->buffer )
291 pitch = sbit->pitch;
292 if ( pitch < 0 )
293 pitch = -pitch;
295 /* add the size of a given glyph image */
296 size += pitch * sbit->height;
300 return size;
304 #if 0
306 FT_LOCAL_DEF( FT_Offset )
307 FTC_SNode_Weight( FTC_SNode snode )
309 return ftc_snode_weight( FTC_NODE( snode ), NULL );
312 #endif /* 0 */
315 FT_LOCAL_DEF( FT_Bool )
316 ftc_snode_compare( FTC_Node ftcsnode,
317 FT_Pointer ftcgquery,
318 FTC_Cache cache )
320 FTC_SNode snode = (FTC_SNode)ftcsnode;
321 FTC_GQuery gquery = (FTC_GQuery)ftcgquery;
322 FTC_GNode gnode = FTC_GNODE( snode );
323 FT_UInt gindex = gquery->gindex;
324 FT_Bool result;
327 result = FT_BOOL( gnode->family == gquery->family &&
328 (FT_UInt)( gindex - gnode->gindex ) < snode->count );
329 if ( result )
331 /* check if we need to load the glyph bitmap now */
332 FTC_SBit sbit = snode->sbits + ( gindex - gnode->gindex );
336 * The following code illustrates what to do when you want to
337 * perform operations that may fail within a lookup function.
339 * Here, we want to load a small bitmap on-demand; we thus
340 * need to call the `ftc_snode_load' function which may return
341 * a non-zero error code only when we are out of memory (OOM).
343 * The correct thing to do is to use @FTC_CACHE_TRYLOOP and
344 * @FTC_CACHE_TRYLOOP_END in order to implement a retry loop
345 * that is capable of flushing the cache incrementally when
346 * an OOM errors occur.
348 * However, we need to `lock' the node before this operation to
349 * prevent it from being flushed within the loop.
351 * When we exit the loop, we unlock the node, then check the `error'
352 * variable. If it is non-zero, this means that the cache was
353 * completely flushed and that no usable memory was found to load
354 * the bitmap.
356 * We then prefer to return a value of 0 (i.e., NO MATCH). This
357 * ensures that the caller will try to allocate a new node.
358 * This operation consequently _fail_ and the lookup function
359 * returns the appropriate OOM error code.
361 * Note that `buffer == NULL && width == 255' is a hack used to
362 * tag `unavailable' bitmaps in the array. We should never try
363 * to load these.
367 if ( sbit->buffer == NULL && sbit->width != 255 )
369 FT_ULong size;
370 FT_Error error;
373 ftcsnode->ref_count++; /* lock node to prevent flushing */
374 /* in retry loop */
376 FTC_CACHE_TRYLOOP( cache )
378 error = ftc_snode_load( snode, cache->manager, gindex, &size );
380 FTC_CACHE_TRYLOOP_END();
382 ftcsnode->ref_count--; /* unlock the node */
384 if ( error )
385 result = 0;
386 else
387 cache->manager->cur_weight += size;
391 return result;
395 FT_LOCAL_DEF( FT_Bool )
396 FTC_SNode_Compare( FTC_SNode snode,
397 FTC_GQuery gquery,
398 FTC_Cache cache )
400 return ftc_snode_compare( FTC_NODE( snode ), gquery, cache );
404 /* END */