Disabling auto-refresh of game list by default, as it is causing bugs sometimes
[open-ps2-loader.git] / thirdparty / freetype-2.3.12 / src / sfnt / sfdriver.c
blob1097efb86d75d6c4a74288987added4bd71d80d6
1 /***************************************************************************/
2 /* */
3 /* sfdriver.c */
4 /* */
5 /* High-level SFNT driver interface (body). */
6 /* */
7 /* Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 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_INTERNAL_DEBUG_H
21 #include FT_INTERNAL_SFNT_H
22 #include FT_INTERNAL_OBJECTS_H
24 #include "sfdriver.h"
25 #include "ttload.h"
26 #include "sfobjs.h"
27 #include "sfntpic.h"
29 #include "sferrors.h"
31 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
32 #include "ttsbit.h"
33 #endif
35 #ifdef TT_CONFIG_OPTION_POSTSCRIPT_NAMES
36 #include "ttpost.h"
37 #endif
39 #ifdef TT_CONFIG_OPTION_BDF
40 #include "ttbdf.h"
41 #include FT_SERVICE_BDF_H
42 #endif
44 #include "ttcmap.h"
45 #include "ttkern.h"
46 #include "ttmtx.h"
48 #include FT_SERVICE_GLYPH_DICT_H
49 #include FT_SERVICE_POSTSCRIPT_NAME_H
50 #include FT_SERVICE_SFNT_H
51 #include FT_SERVICE_TT_CMAP_H
53 /*************************************************************************/
54 /* */
55 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
56 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
57 /* messages during execution. */
58 /* */
59 #undef FT_COMPONENT
60 #define FT_COMPONENT trace_sfdriver
64 * SFNT TABLE SERVICE
68 static void*
69 get_sfnt_table( TT_Face face,
70 FT_Sfnt_Tag tag )
72 void* table;
75 switch ( tag )
77 case ft_sfnt_head:
78 table = &face->header;
79 break;
81 case ft_sfnt_hhea:
82 table = &face->horizontal;
83 break;
85 case ft_sfnt_vhea:
86 table = face->vertical_info ? &face->vertical : 0;
87 break;
89 case ft_sfnt_os2:
90 table = face->os2.version == 0xFFFFU ? 0 : &face->os2;
91 break;
93 case ft_sfnt_post:
94 table = &face->postscript;
95 break;
97 case ft_sfnt_maxp:
98 table = &face->max_profile;
99 break;
101 case ft_sfnt_pclt:
102 table = face->pclt.Version ? &face->pclt : 0;
103 break;
105 default:
106 table = 0;
109 return table;
113 static FT_Error
114 sfnt_table_info( TT_Face face,
115 FT_UInt idx,
116 FT_ULong *tag,
117 FT_ULong *offset,
118 FT_ULong *length )
120 if ( !tag || !offset || !length )
121 return SFNT_Err_Invalid_Argument;
123 if ( idx >= face->num_tables )
124 return SFNT_Err_Table_Missing;
126 *tag = face->dir_tables[idx].Tag;
127 *offset = face->dir_tables[idx].Offset;
128 *length = face->dir_tables[idx].Length;
130 return SFNT_Err_Ok;
134 FT_DEFINE_SERVICE_SFNT_TABLEREC(sfnt_service_sfnt_table,
135 (FT_SFNT_TableLoadFunc)tt_face_load_any,
136 (FT_SFNT_TableGetFunc) get_sfnt_table,
137 (FT_SFNT_TableInfoFunc)sfnt_table_info
141 #ifdef TT_CONFIG_OPTION_POSTSCRIPT_NAMES
144 * GLYPH DICT SERVICE
148 static FT_Error
149 sfnt_get_glyph_name( TT_Face face,
150 FT_UInt glyph_index,
151 FT_Pointer buffer,
152 FT_UInt buffer_max )
154 FT_String* gname;
155 FT_Error error;
158 error = tt_face_get_ps_name( face, glyph_index, &gname );
159 if ( !error )
160 FT_STRCPYN( buffer, gname, buffer_max );
162 return error;
166 static FT_UInt
167 sfnt_get_name_index( TT_Face face,
168 FT_String* glyph_name )
170 FT_Face root = &face->root;
171 FT_UInt i, max_gid = FT_UINT_MAX;
174 if ( root->num_glyphs < 0 )
175 return 0;
176 else if ( ( FT_ULong ) root->num_glyphs < FT_UINT_MAX )
177 max_gid = ( FT_UInt ) root->num_glyphs;
178 else
179 FT_TRACE0(( "Ignore glyph names for invalid GID 0x%08x - 0x%08x\n",
180 FT_UINT_MAX, root->num_glyphs ));
182 for ( i = 0; i < max_gid; i++ )
184 FT_String* gname;
185 FT_Error error = tt_face_get_ps_name( face, i, &gname );
188 if ( error )
189 continue;
191 if ( !ft_strcmp( glyph_name, gname ) )
192 return i;
195 return 0;
199 FT_DEFINE_SERVICE_GLYPHDICTREC(sfnt_service_glyph_dict,
200 (FT_GlyphDict_GetNameFunc) sfnt_get_glyph_name,
201 (FT_GlyphDict_NameIndexFunc)sfnt_get_name_index
204 #endif /* TT_CONFIG_OPTION_POSTSCRIPT_NAMES */
208 * POSTSCRIPT NAME SERVICE
212 static const char*
213 sfnt_get_ps_name( TT_Face face )
215 FT_Int n, found_win, found_apple;
216 const char* result = NULL;
219 /* shouldn't happen, but just in case to avoid memory leaks */
220 if ( face->postscript_name )
221 return face->postscript_name;
223 /* scan the name table to see whether we have a Postscript name here, */
224 /* either in Macintosh or Windows platform encodings */
225 found_win = -1;
226 found_apple = -1;
228 for ( n = 0; n < face->num_names; n++ )
230 TT_NameEntryRec* name = face->name_table.names + n;
233 if ( name->nameID == 6 && name->stringLength > 0 )
235 if ( name->platformID == 3 &&
236 name->encodingID == 1 &&
237 name->languageID == 0x409 )
238 found_win = n;
240 if ( name->platformID == 1 &&
241 name->encodingID == 0 &&
242 name->languageID == 0 )
243 found_apple = n;
247 if ( found_win != -1 )
249 FT_Memory memory = face->root.memory;
250 TT_NameEntryRec* name = face->name_table.names + found_win;
251 FT_UInt len = name->stringLength / 2;
252 FT_Error error = SFNT_Err_Ok;
254 FT_UNUSED( error );
257 if ( !FT_ALLOC( result, name->stringLength + 1 ) )
259 FT_Stream stream = face->name_table.stream;
260 FT_String* r = (FT_String*)result;
261 FT_Byte* p = (FT_Byte*)name->string;
264 if ( FT_STREAM_SEEK( name->stringOffset ) ||
265 FT_FRAME_ENTER( name->stringLength ) )
267 FT_FREE( result );
268 name->stringLength = 0;
269 name->stringOffset = 0;
270 FT_FREE( name->string );
272 goto Exit;
275 p = (FT_Byte*)stream->cursor;
277 for ( ; len > 0; len--, p += 2 )
279 if ( p[0] == 0 && p[1] >= 32 && p[1] < 128 )
280 *r++ = p[1];
282 *r = '\0';
284 FT_FRAME_EXIT();
286 goto Exit;
289 if ( found_apple != -1 )
291 FT_Memory memory = face->root.memory;
292 TT_NameEntryRec* name = face->name_table.names + found_apple;
293 FT_UInt len = name->stringLength;
294 FT_Error error = SFNT_Err_Ok;
296 FT_UNUSED( error );
299 if ( !FT_ALLOC( result, len + 1 ) )
301 FT_Stream stream = face->name_table.stream;
304 if ( FT_STREAM_SEEK( name->stringOffset ) ||
305 FT_STREAM_READ( result, len ) )
307 name->stringOffset = 0;
308 name->stringLength = 0;
309 FT_FREE( name->string );
310 FT_FREE( result );
311 goto Exit;
313 ((char*)result)[len] = '\0';
317 Exit:
318 face->postscript_name = result;
319 return result;
322 FT_DEFINE_SERVICE_PSFONTNAMEREC(sfnt_service_ps_name,
323 (FT_PsName_GetFunc)sfnt_get_ps_name
328 * TT CMAP INFO
330 FT_DEFINE_SERVICE_TTCMAPSREC(tt_service_get_cmap_info,
331 (TT_CMap_Info_GetFunc)tt_get_cmap_info
335 #ifdef TT_CONFIG_OPTION_BDF
337 static FT_Error
338 sfnt_get_charset_id( TT_Face face,
339 const char* *acharset_encoding,
340 const char* *acharset_registry )
342 BDF_PropertyRec encoding, registry;
343 FT_Error error;
346 /* XXX: I don't know whether this is correct, since
347 * tt_face_find_bdf_prop only returns something correct if we have
348 * previously selected a size that is listed in the BDF table.
349 * Should we change the BDF table format to include single offsets
350 * for `CHARSET_REGISTRY' and `CHARSET_ENCODING'?
352 error = tt_face_find_bdf_prop( face, "CHARSET_REGISTRY", &registry );
353 if ( !error )
355 error = tt_face_find_bdf_prop( face, "CHARSET_ENCODING", &encoding );
356 if ( !error )
358 if ( registry.type == BDF_PROPERTY_TYPE_ATOM &&
359 encoding.type == BDF_PROPERTY_TYPE_ATOM )
361 *acharset_encoding = encoding.u.atom;
362 *acharset_registry = registry.u.atom;
364 else
365 error = FT_Err_Invalid_Argument;
369 return error;
373 FT_DEFINE_SERVICE_BDFRec(sfnt_service_bdf,
374 (FT_BDF_GetCharsetIdFunc) sfnt_get_charset_id,
375 (FT_BDF_GetPropertyFunc) tt_face_find_bdf_prop
378 #endif /* TT_CONFIG_OPTION_BDF */
382 * SERVICE LIST
385 #if defined TT_CONFIG_OPTION_POSTSCRIPT_NAMES && defined TT_CONFIG_OPTION_BDF
386 FT_DEFINE_SERVICEDESCREC5(sfnt_services,
387 FT_SERVICE_ID_SFNT_TABLE, &FT_SFNT_SERVICE_SFNT_TABLE_GET,
388 FT_SERVICE_ID_POSTSCRIPT_FONT_NAME, &FT_SFNT_SERVICE_PS_NAME_GET,
389 FT_SERVICE_ID_GLYPH_DICT, &FT_SFNT_SERVICE_GLYPH_DICT_GET,
390 FT_SERVICE_ID_BDF, &FT_SFNT_SERVICE_BDF_GET,
391 FT_SERVICE_ID_TT_CMAP, &FT_TT_SERVICE_GET_CMAP_INFO_GET
393 #elif defined TT_CONFIG_OPTION_POSTSCRIPT_NAMES
394 FT_DEFINE_SERVICEDESCREC4(sfnt_services,
395 FT_SERVICE_ID_SFNT_TABLE, &FT_SFNT_SERVICE_SFNT_TABLE_GET,
396 FT_SERVICE_ID_POSTSCRIPT_FONT_NAME, &FT_SFNT_SERVICE_PS_NAME_GET,
397 FT_SERVICE_ID_GLYPH_DICT, &FT_SFNT_SERVICE_GLYPH_DICT_GET,
398 FT_SERVICE_ID_TT_CMAP, &FT_TT_SERVICE_GET_CMAP_INFO_GET
400 #elif defined TT_CONFIG_OPTION_BDF
401 FT_DEFINE_SERVICEDESCREC4(sfnt_services,
402 FT_SERVICE_ID_SFNT_TABLE, &FT_SFNT_SERVICE_SFNT_TABLE_GET,
403 FT_SERVICE_ID_POSTSCRIPT_FONT_NAME, &FT_SFNT_SERVICE_PS_NAME_GET,
404 FT_SERVICE_ID_BDF, &FT_SFNT_SERVICE_BDF_GET,
405 FT_SERVICE_ID_TT_CMAP, &FT_TT_SERVICE_GET_CMAP_INFO_GET
407 #else
408 FT_DEFINE_SERVICEDESCREC3(sfnt_services,
409 FT_SERVICE_ID_SFNT_TABLE, &FT_SFNT_SERVICE_SFNT_TABLE_GET,
410 FT_SERVICE_ID_POSTSCRIPT_FONT_NAME, &FT_SFNT_SERVICE_PS_NAME_GET,
411 FT_SERVICE_ID_TT_CMAP, &FT_TT_SERVICE_GET_CMAP_INFO_GET
413 #endif
416 FT_CALLBACK_DEF( FT_Module_Interface )
417 sfnt_get_interface( FT_Module module,
418 const char* module_interface )
420 FT_UNUSED( module );
422 return ft_service_list_lookup( FT_SFNT_SERVICES_GET, module_interface );
426 #ifdef FT_CONFIG_OPTION_OLD_INTERNALS
428 FT_CALLBACK_DEF( FT_Error )
429 tt_face_load_sfnt_header_stub( TT_Face face,
430 FT_Stream stream,
431 FT_Long face_index,
432 SFNT_Header header )
434 FT_UNUSED( face );
435 FT_UNUSED( stream );
436 FT_UNUSED( face_index );
437 FT_UNUSED( header );
439 return FT_Err_Unimplemented_Feature;
443 FT_CALLBACK_DEF( FT_Error )
444 tt_face_load_directory_stub( TT_Face face,
445 FT_Stream stream,
446 SFNT_Header header )
448 FT_UNUSED( face );
449 FT_UNUSED( stream );
450 FT_UNUSED( header );
452 return FT_Err_Unimplemented_Feature;
456 FT_CALLBACK_DEF( FT_Error )
457 tt_face_load_hdmx_stub( TT_Face face,
458 FT_Stream stream )
460 FT_UNUSED( face );
461 FT_UNUSED( stream );
463 return FT_Err_Unimplemented_Feature;
467 FT_CALLBACK_DEF( void )
468 tt_face_free_hdmx_stub( TT_Face face )
470 FT_UNUSED( face );
474 FT_CALLBACK_DEF( FT_Error )
475 tt_face_set_sbit_strike_stub( TT_Face face,
476 FT_UInt x_ppem,
477 FT_UInt y_ppem,
478 FT_ULong* astrike_index )
481 * We simply forge a FT_Size_Request and call the real function
482 * that does all the work.
484 * This stub might be called by libXfont in the X.Org Xserver,
485 * compiled against version 2.1.8 or newer.
488 FT_Size_RequestRec req;
491 req.type = FT_SIZE_REQUEST_TYPE_NOMINAL;
492 req.width = (FT_F26Dot6)x_ppem;
493 req.height = (FT_F26Dot6)y_ppem;
494 req.horiResolution = 0;
495 req.vertResolution = 0;
497 *astrike_index = 0x7FFFFFFFUL;
499 return tt_face_set_sbit_strike( face, &req, astrike_index );
503 FT_CALLBACK_DEF( FT_Error )
504 tt_face_load_sbit_stub( TT_Face face,
505 FT_Stream stream )
507 FT_UNUSED( face );
508 FT_UNUSED( stream );
511 * This function was originally implemented to load the sbit table.
512 * However, it has been replaced by `tt_face_load_eblc', and this stub
513 * is only there for some rogue clients which would want to call it
514 * directly (which doesn't make much sense).
516 return FT_Err_Unimplemented_Feature;
520 FT_CALLBACK_DEF( void )
521 tt_face_free_sbit_stub( TT_Face face )
523 /* nothing to do in this stub */
524 FT_UNUSED( face );
528 FT_CALLBACK_DEF( FT_Error )
529 tt_face_load_charmap_stub( TT_Face face,
530 void* cmap,
531 FT_Stream input )
533 FT_UNUSED( face );
534 FT_UNUSED( cmap );
535 FT_UNUSED( input );
537 return FT_Err_Unimplemented_Feature;
541 FT_CALLBACK_DEF( FT_Error )
542 tt_face_free_charmap_stub( TT_Face face,
543 void* cmap )
545 FT_UNUSED( face );
546 FT_UNUSED( cmap );
548 return 0;
551 #endif /* FT_CONFIG_OPTION_OLD_INTERNALS */
553 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
554 #define PUT_EMBEDDED_BITMAPS(a) a
555 #else
556 #define PUT_EMBEDDED_BITMAPS(a) 0
557 #endif
558 #ifdef TT_CONFIG_OPTION_POSTSCRIPT_NAMES
559 #define PUT_PS_NAMES(a) a
560 #else
561 #define PUT_PS_NAMES(a) 0
562 #endif
564 FT_DEFINE_SFNT_INTERFACE(sfnt_interface,
565 tt_face_goto_table,
567 sfnt_init_face,
568 sfnt_load_face,
569 sfnt_done_face,
570 sfnt_get_interface,
572 tt_face_load_any,
574 tt_face_load_sfnt_header_stub, /* FT_CONFIG_OPTION_OLD_INTERNALS */
575 tt_face_load_directory_stub, /* FT_CONFIG_OPTION_OLD_INTERNALS */
577 tt_face_load_head,
578 tt_face_load_hhea,
579 tt_face_load_cmap,
580 tt_face_load_maxp,
581 tt_face_load_os2,
582 tt_face_load_post,
584 tt_face_load_name,
585 tt_face_free_name,
587 tt_face_load_hdmx_stub, /* FT_CONFIG_OPTION_OLD_INTERNALS */
588 tt_face_free_hdmx_stub, /* FT_CONFIG_OPTION_OLD_INTERNALS */
590 tt_face_load_kern,
591 tt_face_load_gasp,
592 tt_face_load_pclt,
594 /* see `ttload.h' */
595 PUT_EMBEDDED_BITMAPS(tt_face_load_bhed),
597 tt_face_set_sbit_strike_stub, /* FT_CONFIG_OPTION_OLD_INTERNALS */
598 tt_face_load_sbit_stub, /* FT_CONFIG_OPTION_OLD_INTERNALS */
600 tt_find_sbit_image, /* FT_CONFIG_OPTION_OLD_INTERNALS */
601 tt_load_sbit_metrics, /* FT_CONFIG_OPTION_OLD_INTERNALS */
603 PUT_EMBEDDED_BITMAPS(tt_face_load_sbit_image),
605 tt_face_free_sbit_stub, /* FT_CONFIG_OPTION_OLD_INTERNALS */
607 /* see `ttpost.h' */
608 PUT_PS_NAMES(tt_face_get_ps_name),
609 PUT_PS_NAMES(tt_face_free_ps_names),
611 tt_face_load_charmap_stub, /* FT_CONFIG_OPTION_OLD_INTERNALS */
612 tt_face_free_charmap_stub, /* FT_CONFIG_OPTION_OLD_INTERNALS */
614 /* since version 2.1.8 */
616 tt_face_get_kerning,
618 /* since version 2.2 */
620 tt_face_load_font_dir,
621 tt_face_load_hmtx,
623 /* see `ttsbit.h' and `sfnt.h' */
624 PUT_EMBEDDED_BITMAPS(tt_face_load_eblc),
625 PUT_EMBEDDED_BITMAPS(tt_face_free_eblc),
627 PUT_EMBEDDED_BITMAPS(tt_face_set_sbit_strike),
628 PUT_EMBEDDED_BITMAPS(tt_face_load_strike_metrics),
630 tt_face_get_metrics
634 FT_DEFINE_MODULE(sfnt_module_class,
636 0, /* not a font driver or renderer */
637 sizeof( FT_ModuleRec ),
639 "sfnt", /* driver name */
640 0x10000L, /* driver version 1.0 */
641 0x20000L, /* driver requires FreeType 2.0 or higher */
643 (const void*)&FT_SFNT_INTERFACE_GET, /* module specific interface */
645 (FT_Module_Constructor)0,
646 (FT_Module_Destructor) 0,
647 (FT_Module_Requester) sfnt_get_interface
651 /* END */