Disabling auto-refresh of game list by default, as it is causing bugs sometimes
[open-ps2-loader.git] / thirdparty / freetype-2.3.12 / src / type42 / t42objs.c
blob9081ffc6d22ba872a842f1a8dd0f8ad00a5121ff
1 /***************************************************************************/
2 /* */
3 /* t42objs.c */
4 /* */
5 /* Type 42 objects manager (body). */
6 /* */
7 /* Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 */
8 /* by Roberto Alameda. */
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 "t42objs.h"
20 #include "t42parse.h"
21 #include "t42error.h"
22 #include FT_INTERNAL_DEBUG_H
23 #include FT_LIST_H
26 #undef FT_COMPONENT
27 #define FT_COMPONENT trace_t42
30 static FT_Error
31 T42_Open_Face( T42_Face face )
33 T42_LoaderRec loader;
34 T42_Parser parser;
35 T1_Font type1 = &face->type1;
36 FT_Memory memory = face->root.memory;
37 FT_Error error;
39 PSAux_Service psaux = (PSAux_Service)face->psaux;
42 t42_loader_init( &loader, face );
44 parser = &loader.parser;
46 if ( FT_ALLOC( face->ttf_data, 12 ) )
47 goto Exit;
49 error = t42_parser_init( parser,
50 face->root.stream,
51 memory,
52 psaux);
53 if ( error )
54 goto Exit;
56 error = t42_parse_dict( face, &loader,
57 parser->base_dict, parser->base_len );
58 if ( error )
59 goto Exit;
61 if ( type1->font_type != 42 )
63 error = T42_Err_Unknown_File_Format;
64 goto Exit;
67 /* now, propagate the charstrings and glyphnames tables */
68 /* to the Type1 data */
69 type1->num_glyphs = loader.num_glyphs;
71 if ( !loader.charstrings.init )
73 FT_ERROR(( "T42_Open_Face: no charstrings array in face\n" ));
74 error = T42_Err_Invalid_File_Format;
77 loader.charstrings.init = 0;
78 type1->charstrings_block = loader.charstrings.block;
79 type1->charstrings = loader.charstrings.elements;
80 type1->charstrings_len = loader.charstrings.lengths;
82 /* we copy the glyph names `block' and `elements' fields; */
83 /* the `lengths' field must be released later */
84 type1->glyph_names_block = loader.glyph_names.block;
85 type1->glyph_names = (FT_String**)loader.glyph_names.elements;
86 loader.glyph_names.block = 0;
87 loader.glyph_names.elements = 0;
89 /* we must now build type1.encoding when we have a custom array */
90 if ( type1->encoding_type == T1_ENCODING_TYPE_ARRAY )
92 FT_Int charcode, idx, min_char, max_char;
93 FT_Byte* char_name;
94 FT_Byte* glyph_name;
97 /* OK, we do the following: for each element in the encoding */
98 /* table, look up the index of the glyph having the same name */
99 /* as defined in the CharStrings array. */
100 /* The index is then stored in type1.encoding.char_index, and */
101 /* the name in type1.encoding.char_name */
103 min_char = 0;
104 max_char = 0;
106 charcode = 0;
107 for ( ; charcode < loader.encoding_table.max_elems; charcode++ )
109 type1->encoding.char_index[charcode] = 0;
110 type1->encoding.char_name [charcode] = (char *)".notdef";
112 char_name = loader.encoding_table.elements[charcode];
113 if ( char_name )
114 for ( idx = 0; idx < type1->num_glyphs; idx++ )
116 glyph_name = (FT_Byte*)type1->glyph_names[idx];
117 if ( ft_strcmp( (const char*)char_name,
118 (const char*)glyph_name ) == 0 )
120 type1->encoding.char_index[charcode] = (FT_UShort)idx;
121 type1->encoding.char_name [charcode] = (char*)glyph_name;
123 /* Change min/max encoded char only if glyph name is */
124 /* not /.notdef */
125 if ( ft_strcmp( (const char*)".notdef",
126 (const char*)glyph_name ) != 0 )
128 if ( charcode < min_char )
129 min_char = charcode;
130 if ( charcode >= max_char )
131 max_char = charcode + 1;
133 break;
138 type1->encoding.code_first = min_char;
139 type1->encoding.code_last = max_char;
140 type1->encoding.num_chars = loader.num_chars;
143 Exit:
144 t42_loader_done( &loader );
145 return error;
149 /***************** Driver Functions *************/
152 FT_LOCAL_DEF( FT_Error )
153 T42_Face_Init( FT_Stream stream,
154 T42_Face face,
155 FT_Int face_index,
156 FT_Int num_params,
157 FT_Parameter* params )
159 FT_Error error;
160 FT_Service_PsCMaps psnames;
161 PSAux_Service psaux;
162 FT_Face root = (FT_Face)&face->root;
163 T1_Font type1 = &face->type1;
164 PS_FontInfo info = &type1->font_info;
166 FT_UNUSED( num_params );
167 FT_UNUSED( params );
168 FT_UNUSED( face_index );
169 FT_UNUSED( stream );
172 face->ttf_face = NULL;
173 face->root.num_faces = 1;
175 FT_FACE_FIND_GLOBAL_SERVICE( face, psnames, POSTSCRIPT_CMAPS );
176 face->psnames = psnames;
178 face->psaux = FT_Get_Module_Interface( FT_FACE_LIBRARY( face ),
179 "psaux" );
180 psaux = (PSAux_Service)face->psaux;
182 /* open the tokenizer, this will also check the font format */
183 error = T42_Open_Face( face );
184 if ( error )
185 goto Exit;
187 /* if we just wanted to check the format, leave successfully now */
188 if ( face_index < 0 )
189 goto Exit;
191 /* check the face index */
192 if ( face_index > 0 )
194 FT_ERROR(( "T42_Face_Init: invalid face index\n" ));
195 error = T42_Err_Invalid_Argument;
196 goto Exit;
199 /* Now load the font program into the face object */
201 /* Init the face object fields */
202 /* Now set up root face fields */
204 root->num_glyphs = type1->num_glyphs;
205 root->num_charmaps = 0;
206 root->face_index = 0;
208 root->face_flags = FT_FACE_FLAG_SCALABLE |
209 FT_FACE_FLAG_HORIZONTAL |
210 FT_FACE_FLAG_GLYPH_NAMES;
212 if ( info->is_fixed_pitch )
213 root->face_flags |= FT_FACE_FLAG_FIXED_WIDTH;
215 /* We only set this flag if we have the patented bytecode interpreter. */
216 /* There are no known `tricky' Type42 fonts that could be loaded with */
217 /* the unpatented interpreter. */
218 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
219 root->face_flags |= FT_FACE_FLAG_HINTER;
220 #endif
222 /* XXX: TODO -- add kerning with .afm support */
224 /* get style name -- be careful, some broken fonts only */
225 /* have a `/FontName' dictionary entry! */
226 root->family_name = info->family_name;
227 /* assume "Regular" style if we don't know better */
228 root->style_name = (char *)"Regular";
229 if ( root->family_name )
231 char* full = info->full_name;
232 char* family = root->family_name;
235 if ( full )
237 while ( *full )
239 if ( *full == *family )
241 family++;
242 full++;
244 else
246 if ( *full == ' ' || *full == '-' )
247 full++;
248 else if ( *family == ' ' || *family == '-' )
249 family++;
250 else
252 if ( !*family )
253 root->style_name = full;
254 break;
260 else
262 /* do we have a `/FontName'? */
263 if ( type1->font_name )
264 root->family_name = type1->font_name;
267 /* no embedded bitmap support */
268 root->num_fixed_sizes = 0;
269 root->available_sizes = 0;
271 /* Load the TTF font embedded in the T42 font */
273 FT_Open_Args args;
276 args.flags = FT_OPEN_MEMORY;
277 args.memory_base = face->ttf_data;
278 args.memory_size = face->ttf_size;
280 if ( num_params )
282 args.flags |= FT_OPEN_PARAMS;
283 args.num_params = num_params;
284 args.params = params;
287 error = FT_Open_Face( FT_FACE_LIBRARY( face ),
288 &args, 0, &face->ttf_face );
291 if ( error )
292 goto Exit;
294 FT_Done_Size( face->ttf_face->size );
296 /* Ignore info in FontInfo dictionary and use the info from the */
297 /* loaded TTF font. The PostScript interpreter also ignores it. */
298 root->bbox = face->ttf_face->bbox;
299 root->units_per_EM = face->ttf_face->units_per_EM;
301 root->ascender = face->ttf_face->ascender;
302 root->descender = face->ttf_face->descender;
303 root->height = face->ttf_face->height;
305 root->max_advance_width = face->ttf_face->max_advance_width;
306 root->max_advance_height = face->ttf_face->max_advance_height;
308 root->underline_position = (FT_Short)info->underline_position;
309 root->underline_thickness = (FT_Short)info->underline_thickness;
311 /* compute style flags */
312 root->style_flags = 0;
313 if ( info->italic_angle )
314 root->style_flags |= FT_STYLE_FLAG_ITALIC;
316 if ( face->ttf_face->style_flags & FT_STYLE_FLAG_BOLD )
317 root->style_flags |= FT_STYLE_FLAG_BOLD;
319 if ( face->ttf_face->face_flags & FT_FACE_FLAG_VERTICAL )
320 root->face_flags |= FT_FACE_FLAG_VERTICAL;
323 if ( psnames && psaux )
325 FT_CharMapRec charmap;
326 T1_CMap_Classes cmap_classes = psaux->t1_cmap_classes;
327 FT_CMap_Class clazz;
330 charmap.face = root;
332 /* first of all, try to synthesize a Unicode charmap */
333 charmap.platform_id = 3;
334 charmap.encoding_id = 1;
335 charmap.encoding = FT_ENCODING_UNICODE;
337 FT_CMap_New( cmap_classes->unicode, NULL, &charmap, NULL );
339 /* now, generate an Adobe Standard encoding when appropriate */
340 charmap.platform_id = 7;
341 clazz = NULL;
343 switch ( type1->encoding_type )
345 case T1_ENCODING_TYPE_STANDARD:
346 charmap.encoding = FT_ENCODING_ADOBE_STANDARD;
347 charmap.encoding_id = 0;
348 clazz = cmap_classes->standard;
349 break;
351 case T1_ENCODING_TYPE_EXPERT:
352 charmap.encoding = FT_ENCODING_ADOBE_EXPERT;
353 charmap.encoding_id = 1;
354 clazz = cmap_classes->expert;
355 break;
357 case T1_ENCODING_TYPE_ARRAY:
358 charmap.encoding = FT_ENCODING_ADOBE_CUSTOM;
359 charmap.encoding_id = 2;
360 clazz = cmap_classes->custom;
361 break;
363 case T1_ENCODING_TYPE_ISOLATIN1:
364 charmap.encoding = FT_ENCODING_ADOBE_LATIN_1;
365 charmap.encoding_id = 3;
366 clazz = cmap_classes->unicode;
367 break;
369 default:
373 if ( clazz )
374 FT_CMap_New( clazz, NULL, &charmap, NULL );
376 #if 0
377 /* Select default charmap */
378 if ( root->num_charmaps )
379 root->charmap = root->charmaps[0];
380 #endif
383 Exit:
384 return error;
388 FT_LOCAL_DEF( void )
389 T42_Face_Done( T42_Face face )
391 T1_Font type1;
392 PS_FontInfo info;
393 FT_Memory memory;
396 if ( !face )
397 return;
399 type1 = &face->type1;
400 info = &type1->font_info;
401 memory = face->root.memory;
403 /* delete internal ttf face prior to freeing face->ttf_data */
404 if ( face->ttf_face )
405 FT_Done_Face( face->ttf_face );
407 /* release font info strings */
408 FT_FREE( info->version );
409 FT_FREE( info->notice );
410 FT_FREE( info->full_name );
411 FT_FREE( info->family_name );
412 FT_FREE( info->weight );
414 /* release top dictionary */
415 FT_FREE( type1->charstrings_len );
416 FT_FREE( type1->charstrings );
417 FT_FREE( type1->glyph_names );
419 FT_FREE( type1->charstrings_block );
420 FT_FREE( type1->glyph_names_block );
422 FT_FREE( type1->encoding.char_index );
423 FT_FREE( type1->encoding.char_name );
424 FT_FREE( type1->font_name );
426 FT_FREE( face->ttf_data );
428 #if 0
429 /* release afm data if present */
430 if ( face->afm_data )
431 T1_Done_AFM( memory, (T1_AFM*)face->afm_data );
432 #endif
434 /* release unicode map, if any */
435 FT_FREE( face->unicode_map.maps );
436 face->unicode_map.num_maps = 0;
438 face->root.family_name = 0;
439 face->root.style_name = 0;
443 /*************************************************************************/
444 /* */
445 /* <Function> */
446 /* T42_Driver_Init */
447 /* */
448 /* <Description> */
449 /* Initializes a given Type 42 driver object. */
450 /* */
451 /* <Input> */
452 /* driver :: A handle to the target driver object. */
453 /* */
454 /* <Return> */
455 /* FreeType error code. 0 means success. */
456 /* */
457 FT_LOCAL_DEF( FT_Error )
458 T42_Driver_Init( T42_Driver driver )
460 FT_Module ttmodule;
463 ttmodule = FT_Get_Module( FT_MODULE(driver)->library, "truetype" );
464 driver->ttclazz = (FT_Driver_Class)ttmodule->clazz;
466 return T42_Err_Ok;
470 FT_LOCAL_DEF( void )
471 T42_Driver_Done( T42_Driver driver )
473 FT_UNUSED( driver );
477 FT_LOCAL_DEF( FT_Error )
478 T42_Size_Init( T42_Size size )
480 FT_Face face = size->root.face;
481 T42_Face t42face = (T42_Face)face;
482 FT_Size ttsize;
483 FT_Error error = T42_Err_Ok;
486 error = FT_New_Size( t42face->ttf_face, &ttsize );
487 size->ttsize = ttsize;
489 FT_Activate_Size( ttsize );
491 return error;
495 FT_LOCAL_DEF( FT_Error )
496 T42_Size_Request( T42_Size size,
497 FT_Size_Request req )
499 T42_Face face = (T42_Face)size->root.face;
500 FT_Error error;
503 FT_Activate_Size( size->ttsize );
505 error = FT_Request_Size( face->ttf_face, req );
506 if ( !error )
507 ( (FT_Size)size )->metrics = face->ttf_face->size->metrics;
509 return error;
513 FT_LOCAL_DEF( FT_Error )
514 T42_Size_Select( T42_Size size,
515 FT_ULong strike_index )
517 T42_Face face = (T42_Face)size->root.face;
518 FT_Error error;
521 FT_Activate_Size( size->ttsize );
523 error = FT_Select_Size( face->ttf_face, (FT_Int)strike_index );
524 if ( !error )
525 ( (FT_Size)size )->metrics = face->ttf_face->size->metrics;
527 return error;
532 FT_LOCAL_DEF( void )
533 T42_Size_Done( T42_Size size )
535 FT_Face face = size->root.face;
536 T42_Face t42face = (T42_Face)face;
537 FT_ListNode node;
540 node = FT_List_Find( &t42face->ttf_face->sizes_list, size->ttsize );
541 if ( node )
543 FT_Done_Size( size->ttsize );
544 size->ttsize = NULL;
549 FT_LOCAL_DEF( FT_Error )
550 T42_GlyphSlot_Init( T42_GlyphSlot slot )
552 FT_Face face = slot->root.face;
553 T42_Face t42face = (T42_Face)face;
554 FT_GlyphSlot ttslot;
555 FT_Error error = T42_Err_Ok;
558 if ( face->glyph == NULL )
560 /* First glyph slot for this face */
561 slot->ttslot = t42face->ttf_face->glyph;
563 else
565 error = FT_New_GlyphSlot( t42face->ttf_face, &ttslot );
566 slot->ttslot = ttslot;
569 return error;
573 FT_LOCAL_DEF( void )
574 T42_GlyphSlot_Done( T42_GlyphSlot slot )
576 FT_Done_GlyphSlot( slot->ttslot );
580 static void
581 t42_glyphslot_clear( FT_GlyphSlot slot )
583 /* free bitmap if needed */
584 ft_glyphslot_free_bitmap( slot );
586 /* clear all public fields in the glyph slot */
587 FT_ZERO( &slot->metrics );
588 FT_ZERO( &slot->outline );
589 FT_ZERO( &slot->bitmap );
591 slot->bitmap_left = 0;
592 slot->bitmap_top = 0;
593 slot->num_subglyphs = 0;
594 slot->subglyphs = 0;
595 slot->control_data = 0;
596 slot->control_len = 0;
597 slot->other = 0;
598 slot->format = FT_GLYPH_FORMAT_NONE;
600 slot->linearHoriAdvance = 0;
601 slot->linearVertAdvance = 0;
605 FT_LOCAL_DEF( FT_Error )
606 T42_GlyphSlot_Load( FT_GlyphSlot glyph,
607 FT_Size size,
608 FT_UInt glyph_index,
609 FT_Int32 load_flags )
611 FT_Error error;
612 T42_GlyphSlot t42slot = (T42_GlyphSlot)glyph;
613 T42_Size t42size = (T42_Size)size;
614 FT_Driver_Class ttclazz = ((T42_Driver)glyph->face->driver)->ttclazz;
617 t42_glyphslot_clear( t42slot->ttslot );
618 error = ttclazz->load_glyph( t42slot->ttslot,
619 t42size->ttsize,
620 glyph_index,
621 load_flags | FT_LOAD_NO_BITMAP );
623 if ( !error )
625 glyph->metrics = t42slot->ttslot->metrics;
627 glyph->linearHoriAdvance = t42slot->ttslot->linearHoriAdvance;
628 glyph->linearVertAdvance = t42slot->ttslot->linearVertAdvance;
630 glyph->format = t42slot->ttslot->format;
631 glyph->outline = t42slot->ttslot->outline;
633 glyph->bitmap = t42slot->ttslot->bitmap;
634 glyph->bitmap_left = t42slot->ttslot->bitmap_left;
635 glyph->bitmap_top = t42slot->ttslot->bitmap_top;
637 glyph->num_subglyphs = t42slot->ttslot->num_subglyphs;
638 glyph->subglyphs = t42slot->ttslot->subglyphs;
640 glyph->control_data = t42slot->ttslot->control_data;
641 glyph->control_len = t42slot->ttslot->control_len;
644 return error;
648 /* END */