1 /***************************************************************************/
5 /* Type 42 objects manager (body). */
7 /* Copyright 2002-2009, 2011, 2013 */
8 /* by Roberto Alameda. */
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 /***************************************************************************/
22 #include FT_INTERNAL_DEBUG_H
24 #include FT_TRUETYPE_IDS_H
28 #define FT_COMPONENT trace_t42
32 T42_Open_Face( T42_Face face )
36 T1_Font type1 = &face->type1;
37 FT_Memory memory = face->root.memory;
40 PSAux_Service psaux = (PSAux_Service)face->psaux;
43 t42_loader_init( &loader, face );
45 parser = &loader.parser;
47 if ( FT_ALLOC( face->ttf_data, 12 ) )
50 /* while parsing the font we always update `face->ttf_size' so that */
51 /* even in case of buggy data (which might lead to premature end of */
52 /* scanning without causing an error) the call to `FT_Open_Face' in */
53 /* `T42_Face_Init' passes the correct size */
56 error = t42_parser_init( parser,
63 error = t42_parse_dict( face, &loader,
64 parser->base_dict, parser->base_len );
68 if ( type1->font_type != 42 )
70 FT_ERROR(( "T42_Open_Face: cannot handle FontType %d\n",
72 error = FT_THROW( Unknown_File_Format );
76 /* now, propagate the charstrings and glyphnames tables */
77 /* to the Type1 data */
78 type1->num_glyphs = loader.num_glyphs;
80 if ( !loader.charstrings.init )
82 FT_ERROR(( "T42_Open_Face: no charstrings array in face\n" ));
83 error = FT_THROW( Invalid_File_Format );
86 loader.charstrings.init = 0;
87 type1->charstrings_block = loader.charstrings.block;
88 type1->charstrings = loader.charstrings.elements;
89 type1->charstrings_len = loader.charstrings.lengths;
91 /* we copy the glyph names `block' and `elements' fields; */
92 /* the `lengths' field must be released later */
93 type1->glyph_names_block = loader.glyph_names.block;
94 type1->glyph_names = (FT_String**)loader.glyph_names.elements;
95 loader.glyph_names.block = 0;
96 loader.glyph_names.elements = 0;
98 /* we must now build type1.encoding when we have a custom array */
99 if ( type1->encoding_type == T1_ENCODING_TYPE_ARRAY )
101 FT_Int charcode, idx, min_char, max_char;
105 /* OK, we do the following: for each element in the encoding */
106 /* table, look up the index of the glyph having the same name */
107 /* as defined in the CharStrings array. */
108 /* The index is then stored in type1.encoding.char_index, and */
109 /* the name in type1.encoding.char_name */
115 for ( ; charcode < loader.encoding_table.max_elems; charcode++ )
120 type1->encoding.char_index[charcode] = 0;
121 type1->encoding.char_name [charcode] = (char *)".notdef";
123 char_name = loader.encoding_table.elements[charcode];
125 for ( idx = 0; idx < type1->num_glyphs; idx++ )
127 glyph_name = (FT_Byte*)type1->glyph_names[idx];
128 if ( ft_strcmp( (const char*)char_name,
129 (const char*)glyph_name ) == 0 )
131 type1->encoding.char_index[charcode] = (FT_UShort)idx;
132 type1->encoding.char_name [charcode] = (char*)glyph_name;
134 /* Change min/max encoded char only if glyph name is */
136 if ( ft_strcmp( (const char*)".notdef",
137 (const char*)glyph_name ) != 0 )
139 if ( charcode < min_char )
141 if ( charcode >= max_char )
142 max_char = charcode + 1;
149 type1->encoding.code_first = min_char;
150 type1->encoding.code_last = max_char;
151 type1->encoding.num_chars = loader.num_chars;
155 t42_loader_done( &loader );
160 /***************** Driver Functions *************/
163 FT_LOCAL_DEF( FT_Error )
164 T42_Face_Init( FT_Stream stream,
165 FT_Face t42face, /* T42_Face */
168 FT_Parameter* params )
170 T42_Face face = (T42_Face)t42face;
172 FT_Service_PsCMaps psnames;
174 FT_Face root = (FT_Face)&face->root;
175 T1_Font type1 = &face->type1;
176 PS_FontInfo info = &type1->font_info;
178 FT_UNUSED( num_params );
183 face->ttf_face = NULL;
184 face->root.num_faces = 1;
186 FT_FACE_FIND_GLOBAL_SERVICE( face, psnames, POSTSCRIPT_CMAPS );
187 face->psnames = psnames;
189 face->psaux = FT_Get_Module_Interface( FT_FACE_LIBRARY( face ),
191 psaux = (PSAux_Service)face->psaux;
194 FT_ERROR(( "T42_Face_Init: cannot access `psaux' module\n" ));
195 error = FT_THROW( Missing_Module );
199 FT_TRACE2(( "Type 42 driver\n" ));
201 /* open the tokenizer, this will also check the font format */
202 error = T42_Open_Face( face );
206 /* if we just wanted to check the format, leave successfully now */
207 if ( face_index < 0 )
210 /* check the face index */
211 if ( face_index > 0 )
213 FT_ERROR(( "T42_Face_Init: invalid face index\n" ));
214 error = FT_THROW( Invalid_Argument );
218 /* Now load the font program into the face object */
220 /* Init the face object fields */
221 /* Now set up root face fields */
223 root->num_glyphs = type1->num_glyphs;
224 root->num_charmaps = 0;
225 root->face_index = 0;
227 root->face_flags |= FT_FACE_FLAG_SCALABLE |
228 FT_FACE_FLAG_HORIZONTAL |
229 FT_FACE_FLAG_GLYPH_NAMES;
231 if ( info->is_fixed_pitch )
232 root->face_flags |= FT_FACE_FLAG_FIXED_WIDTH;
234 /* We only set this flag if we have the patented bytecode interpreter. */
235 /* There are no known `tricky' Type42 fonts that could be loaded with */
236 /* the unpatented interpreter. */
237 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
238 root->face_flags |= FT_FACE_FLAG_HINTER;
241 /* XXX: TODO -- add kerning with .afm support */
243 /* get style name -- be careful, some broken fonts only */
244 /* have a `/FontName' dictionary entry! */
245 root->family_name = info->family_name;
246 /* assume "Regular" style if we don't know better */
247 root->style_name = (char *)"Regular";
248 if ( root->family_name )
250 char* full = info->full_name;
251 char* family = root->family_name;
258 if ( *full == *family )
265 if ( *full == ' ' || *full == '-' )
267 else if ( *family == ' ' || *family == '-' )
272 root->style_name = full;
281 /* do we have a `/FontName'? */
282 if ( type1->font_name )
283 root->family_name = type1->font_name;
286 /* no embedded bitmap support */
287 root->num_fixed_sizes = 0;
288 root->available_sizes = 0;
290 /* Load the TTF font embedded in the T42 font */
295 args.flags = FT_OPEN_MEMORY | FT_OPEN_DRIVER;
296 args.driver = FT_Get_Module( FT_FACE_LIBRARY( face ),
298 args.memory_base = face->ttf_data;
299 args.memory_size = face->ttf_size;
303 args.flags |= FT_OPEN_PARAMS;
304 args.num_params = num_params;
305 args.params = params;
308 error = FT_Open_Face( FT_FACE_LIBRARY( face ),
309 &args, 0, &face->ttf_face );
315 FT_Done_Size( face->ttf_face->size );
317 /* Ignore info in FontInfo dictionary and use the info from the */
318 /* loaded TTF font. The PostScript interpreter also ignores it. */
319 root->bbox = face->ttf_face->bbox;
320 root->units_per_EM = face->ttf_face->units_per_EM;
322 root->ascender = face->ttf_face->ascender;
323 root->descender = face->ttf_face->descender;
324 root->height = face->ttf_face->height;
326 root->max_advance_width = face->ttf_face->max_advance_width;
327 root->max_advance_height = face->ttf_face->max_advance_height;
329 root->underline_position = (FT_Short)info->underline_position;
330 root->underline_thickness = (FT_Short)info->underline_thickness;
332 /* compute style flags */
333 root->style_flags = 0;
334 if ( info->italic_angle )
335 root->style_flags |= FT_STYLE_FLAG_ITALIC;
337 if ( face->ttf_face->style_flags & FT_STYLE_FLAG_BOLD )
338 root->style_flags |= FT_STYLE_FLAG_BOLD;
340 if ( face->ttf_face->face_flags & FT_FACE_FLAG_VERTICAL )
341 root->face_flags |= FT_FACE_FLAG_VERTICAL;
346 FT_CharMapRec charmap;
347 T1_CMap_Classes cmap_classes = psaux->t1_cmap_classes;
353 /* first of all, try to synthesize a Unicode charmap */
354 charmap.platform_id = TT_PLATFORM_MICROSOFT;
355 charmap.encoding_id = TT_MS_ID_UNICODE_CS;
356 charmap.encoding = FT_ENCODING_UNICODE;
358 error = FT_CMap_New( cmap_classes->unicode, NULL, &charmap, NULL );
360 FT_ERR_NEQ( error, No_Unicode_Glyph_Name ) )
364 /* now, generate an Adobe Standard encoding when appropriate */
365 charmap.platform_id = TT_PLATFORM_ADOBE;
368 switch ( type1->encoding_type )
370 case T1_ENCODING_TYPE_STANDARD:
371 charmap.encoding = FT_ENCODING_ADOBE_STANDARD;
372 charmap.encoding_id = TT_ADOBE_ID_STANDARD;
373 clazz = cmap_classes->standard;
376 case T1_ENCODING_TYPE_EXPERT:
377 charmap.encoding = FT_ENCODING_ADOBE_EXPERT;
378 charmap.encoding_id = TT_ADOBE_ID_EXPERT;
379 clazz = cmap_classes->expert;
382 case T1_ENCODING_TYPE_ARRAY:
383 charmap.encoding = FT_ENCODING_ADOBE_CUSTOM;
384 charmap.encoding_id = TT_ADOBE_ID_CUSTOM;
385 clazz = cmap_classes->custom;
388 case T1_ENCODING_TYPE_ISOLATIN1:
389 charmap.encoding = FT_ENCODING_ADOBE_LATIN_1;
390 charmap.encoding_id = TT_ADOBE_ID_LATIN_1;
391 clazz = cmap_classes->unicode;
399 error = FT_CMap_New( clazz, NULL, &charmap, NULL );
402 /* Select default charmap */
403 if ( root->num_charmaps )
404 root->charmap = root->charmaps[0];
414 T42_Face_Done( FT_Face t42face )
416 T42_Face face = (T42_Face)t42face;
425 type1 = &face->type1;
426 info = &type1->font_info;
427 memory = face->root.memory;
429 /* delete internal ttf face prior to freeing face->ttf_data */
430 if ( face->ttf_face )
431 FT_Done_Face( face->ttf_face );
433 /* release font info strings */
434 FT_FREE( info->version );
435 FT_FREE( info->notice );
436 FT_FREE( info->full_name );
437 FT_FREE( info->family_name );
438 FT_FREE( info->weight );
440 /* release top dictionary */
441 FT_FREE( type1->charstrings_len );
442 FT_FREE( type1->charstrings );
443 FT_FREE( type1->glyph_names );
445 FT_FREE( type1->charstrings_block );
446 FT_FREE( type1->glyph_names_block );
448 FT_FREE( type1->encoding.char_index );
449 FT_FREE( type1->encoding.char_name );
450 FT_FREE( type1->font_name );
452 FT_FREE( face->ttf_data );
455 /* release afm data if present */
456 if ( face->afm_data )
457 T1_Done_AFM( memory, (T1_AFM*)face->afm_data );
460 /* release unicode map, if any */
461 FT_FREE( face->unicode_map.maps );
462 face->unicode_map.num_maps = 0;
464 face->root.family_name = 0;
465 face->root.style_name = 0;
469 /*************************************************************************/
472 /* T42_Driver_Init */
475 /* Initializes a given Type 42 driver object. */
478 /* driver :: A handle to the target driver object. */
481 /* FreeType error code. 0 means success. */
483 FT_LOCAL_DEF( FT_Error )
484 T42_Driver_Init( FT_Module module ) /* T42_Driver */
486 T42_Driver driver = (T42_Driver)module;
490 ttmodule = FT_Get_Module( module->library, "truetype" );
493 FT_ERROR(( "T42_Driver_Init: cannot access `truetype' module\n" ));
494 return FT_THROW( Missing_Module );
497 driver->ttclazz = (FT_Driver_Class)ttmodule->clazz;
504 T42_Driver_Done( FT_Module module )
510 FT_LOCAL_DEF( FT_Error )
511 T42_Size_Init( FT_Size size ) /* T42_Size */
513 T42_Size t42size = (T42_Size)size;
514 FT_Face face = size->face;
515 T42_Face t42face = (T42_Face)face;
520 error = FT_New_Size( t42face->ttf_face, &ttsize );
521 t42size->ttsize = ttsize;
523 FT_Activate_Size( ttsize );
529 FT_LOCAL_DEF( FT_Error )
530 T42_Size_Request( FT_Size t42size, /* T42_Size */
531 FT_Size_Request req )
533 T42_Size size = (T42_Size)t42size;
534 T42_Face face = (T42_Face)t42size->face;
538 FT_Activate_Size( size->ttsize );
540 error = FT_Request_Size( face->ttf_face, req );
542 t42size->metrics = face->ttf_face->size->metrics;
548 FT_LOCAL_DEF( FT_Error )
549 T42_Size_Select( FT_Size t42size, /* T42_Size */
550 FT_ULong strike_index )
552 T42_Size size = (T42_Size)t42size;
553 T42_Face face = (T42_Face)t42size->face;
557 FT_Activate_Size( size->ttsize );
559 error = FT_Select_Size( face->ttf_face, (FT_Int)strike_index );
561 t42size->metrics = face->ttf_face->size->metrics;
569 T42_Size_Done( FT_Size t42size ) /* T42_Size */
571 T42_Size size = (T42_Size)t42size;
572 FT_Face face = t42size->face;
573 T42_Face t42face = (T42_Face)face;
577 node = FT_List_Find( &t42face->ttf_face->sizes_list, size->ttsize );
580 FT_Done_Size( size->ttsize );
586 FT_LOCAL_DEF( FT_Error )
587 T42_GlyphSlot_Init( FT_GlyphSlot t42slot ) /* T42_GlyphSlot */
589 T42_GlyphSlot slot = (T42_GlyphSlot)t42slot;
590 FT_Face face = t42slot->face;
591 T42_Face t42face = (T42_Face)face;
593 FT_Error error = FT_Err_Ok;
596 if ( face->glyph == NULL )
598 /* First glyph slot for this face */
599 slot->ttslot = t42face->ttf_face->glyph;
603 error = FT_New_GlyphSlot( t42face->ttf_face, &ttslot );
604 slot->ttslot = ttslot;
612 T42_GlyphSlot_Done( FT_GlyphSlot t42slot ) /* T42_GlyphSlot */
614 T42_GlyphSlot slot = (T42_GlyphSlot)t42slot;
617 FT_Done_GlyphSlot( slot->ttslot );
622 t42_glyphslot_clear( FT_GlyphSlot slot )
624 /* free bitmap if needed */
625 ft_glyphslot_free_bitmap( slot );
627 /* clear all public fields in the glyph slot */
628 FT_ZERO( &slot->metrics );
629 FT_ZERO( &slot->outline );
630 FT_ZERO( &slot->bitmap );
632 slot->bitmap_left = 0;
633 slot->bitmap_top = 0;
634 slot->num_subglyphs = 0;
636 slot->control_data = 0;
637 slot->control_len = 0;
639 slot->format = FT_GLYPH_FORMAT_NONE;
641 slot->linearHoriAdvance = 0;
642 slot->linearVertAdvance = 0;
646 FT_LOCAL_DEF( FT_Error )
647 T42_GlyphSlot_Load( FT_GlyphSlot glyph,
650 FT_Int32 load_flags )
653 T42_GlyphSlot t42slot = (T42_GlyphSlot)glyph;
654 T42_Size t42size = (T42_Size)size;
655 FT_Driver_Class ttclazz = ((T42_Driver)glyph->face->driver)->ttclazz;
658 FT_TRACE1(( "T42_GlyphSlot_Load: glyph index %d\n", glyph_index ));
660 t42_glyphslot_clear( t42slot->ttslot );
661 error = ttclazz->load_glyph( t42slot->ttslot,
664 load_flags | FT_LOAD_NO_BITMAP );
668 glyph->metrics = t42slot->ttslot->metrics;
670 glyph->linearHoriAdvance = t42slot->ttslot->linearHoriAdvance;
671 glyph->linearVertAdvance = t42slot->ttslot->linearVertAdvance;
673 glyph->format = t42slot->ttslot->format;
674 glyph->outline = t42slot->ttslot->outline;
676 glyph->bitmap = t42slot->ttslot->bitmap;
677 glyph->bitmap_left = t42slot->ttslot->bitmap_left;
678 glyph->bitmap_top = t42slot->ttslot->bitmap_top;
680 glyph->num_subglyphs = t42slot->ttslot->num_subglyphs;
681 glyph->subglyphs = t42slot->ttslot->subglyphs;
683 glyph->control_data = t42slot->ttslot->control_data;
684 glyph->control_len = t42slot->ttslot->control_len;