1 /***************************************************************************/
5 /* Type 42 objects manager (body). */
7 /* Copyright 2002 by Roberto Alameda. */
9 /* This file is part of the FreeType project, and may only be used, */
10 /* modified, and distributed under the terms of the FreeType project */
11 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
12 /* this file you indicate that you have read the license and */
13 /* understand and accept it fully. */
15 /***************************************************************************/
21 #include FT_INTERNAL_DEBUG_H
22 #include FT_INTERNAL_STREAM_H
27 #define FT_COMPONENT trace_t42
31 T42_Open_Face( T42_Face face )
35 T1_Font type1 = &face->type1;
36 FT_Memory memory = face->root.memory;
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 ) )
49 error = t42_parser_init( parser,
56 error = t42_parse_dict( face, &loader, parser->base_dict, parser->base_len );
58 if ( type1->font_type != 42 )
60 error = T42_Err_Unknown_File_Format;
64 /* now, propagate the charstrings and glyphnames tables */
65 /* to the Type1 data */
66 type1->num_glyphs = loader.num_glyphs;
68 if ( !loader.charstrings.init ) {
69 FT_ERROR(( "T42_Open_Face: no charstrings array in face!\n" ));
70 error = T42_Err_Invalid_File_Format;
73 loader.charstrings.init = 0;
74 type1->charstrings_block = loader.charstrings.block;
75 type1->charstrings = loader.charstrings.elements;
76 type1->charstrings_len = loader.charstrings.lengths;
78 /* we copy the glyph names `block' and `elements' fields; */
79 /* the `lengths' field must be released later */
80 type1->glyph_names_block = loader.glyph_names.block;
81 type1->glyph_names = (FT_String**)loader.glyph_names.elements;
82 loader.glyph_names.block = 0;
83 loader.glyph_names.elements = 0;
85 /* we must now build type1.encoding when we have a custom array */
86 if ( type1->encoding_type == T1_ENCODING_TYPE_ARRAY )
88 FT_Int charcode, idx, min_char, max_char;
93 /* OK, we do the following: for each element in the encoding */
94 /* table, look up the index of the glyph having the same name */
95 /* as defined in the CharStrings array. */
96 /* The index is then stored in type1.encoding.char_index, and */
97 /* the name in type1.encoding.char_name */
103 for ( ; charcode < loader.encoding_table.max_elems; charcode++ )
105 type1->encoding.char_index[charcode] = 0;
106 type1->encoding.char_name [charcode] = (char *)".notdef";
108 char_name = loader.encoding_table.elements[charcode];
110 for ( idx = 0; idx < type1->num_glyphs; idx++ )
112 glyph_name = (FT_Byte*)type1->glyph_names[idx];
113 if ( ft_strcmp( (const char*)char_name,
114 (const char*)glyph_name ) == 0 )
116 type1->encoding.char_index[charcode] = (FT_UShort)idx;
117 type1->encoding.char_name [charcode] = (char*)glyph_name;
119 /* Change min/max encoded char only if glyph name is */
121 if ( ft_strcmp( (const char*)".notdef",
122 (const char*)glyph_name ) != 0 )
124 if ( charcode < min_char ) min_char = charcode;
125 if ( charcode > max_char ) max_char = charcode;
131 type1->encoding.code_first = min_char;
132 type1->encoding.code_last = max_char;
133 type1->encoding.num_chars = loader.num_chars;
137 t42_loader_done( &loader );
142 /***************** Driver Functions *************/
145 FT_LOCAL_DEF( FT_Error )
146 T42_Face_Init( FT_Stream stream,
150 FT_Parameter* params )
153 PSNames_Service psnames;
155 FT_Face root = (FT_Face)&face->root;
157 FT_UNUSED( num_params );
159 FT_UNUSED( face_index );
163 face->ttf_face = NULL;
164 face->root.num_faces = 1;
166 face->psnames = FT_Get_Module_Interface( FT_FACE_LIBRARY( face ),
168 psnames = (PSNames_Service)face->psnames;
170 face->psaux = FT_Get_Module_Interface( FT_FACE_LIBRARY( face ),
172 psaux = (PSAux_Service)face->psaux;
174 /* open the tokenizer, this will also check the font format */
175 error = T42_Open_Face( face );
179 /* if we just wanted to check the format, leave successfully now */
180 if ( face_index < 0 )
183 /* check the face index */
184 if ( face_index != 0 )
186 FT_ERROR(( "T42_Face_Init: invalid face index\n" ));
187 error = T42_Err_Invalid_Argument;
191 /* Now, load the font program into the face object */
193 /* Init the face object fields */
194 /* Now set up root face fields */
196 root->num_glyphs = face->type1.num_glyphs;
197 root->num_charmaps = 0;
198 root->face_index = face_index;
200 root->face_flags = FT_FACE_FLAG_SCALABLE;
201 root->face_flags |= FT_FACE_FLAG_HORIZONTAL;
202 root->face_flags |= FT_FACE_FLAG_GLYPH_NAMES;
204 if ( face->type1.font_info.is_fixed_pitch )
205 root->face_flags |= FT_FACE_FLAG_FIXED_WIDTH;
207 /* XXX: TODO -- add kerning with .afm support */
209 /* get style name -- be careful, some broken fonts only */
210 /* have a `/FontName' dictionary entry! */
211 root->family_name = face->type1.font_info.family_name;
212 if ( root->family_name )
214 char* full = face->type1.font_info.full_name;
215 char* family = root->family_name;
220 while ( *family && *full == *family )
226 root->style_name = ( *full == ' ' ? full + 1
227 : (char *)"Regular" );
230 root->style_name = (char *)"Regular";
234 /* do we have a `/FontName'? */
235 if ( face->type1.font_name )
237 root->family_name = face->type1.font_name;
238 root->style_name = (char *)"Regular";
242 /* no embedded bitmap support */
243 root->num_fixed_sizes = 0;
244 root->available_sizes = 0;
246 /* Load the TTF font embedded in the T42 font */
247 error = FT_New_Memory_Face( FT_FACE_LIBRARY( face ),
255 FT_Done_Size( face->ttf_face->size );
257 /* Ignore info in FontInfo dictionary and use the info from the */
258 /* loaded TTF font. The PostScript interpreter also ignores it. */
259 root->bbox = face->ttf_face->bbox;
260 root->units_per_EM = face->ttf_face->units_per_EM;
262 root->ascender = face->ttf_face->ascender;
263 root->descender = face->ttf_face->descender;
264 root->height = face->ttf_face->height;
266 root->max_advance_width = face->ttf_face->max_advance_width;
267 root->max_advance_height = face->ttf_face->max_advance_height;
269 root->underline_position = face->type1.font_info.underline_position;
270 root->underline_thickness = face->type1.font_info.underline_thickness;
272 root->internal->max_points = 0;
273 root->internal->max_contours = 0;
275 /* compute style flags */
276 root->style_flags = 0;
277 if ( face->type1.font_info.italic_angle )
278 root->style_flags |= FT_STYLE_FLAG_ITALIC;
280 if ( face->ttf_face->style_flags & FT_STYLE_FLAG_BOLD )
281 root->style_flags |= FT_STYLE_FLAG_BOLD;
283 if ( face->ttf_face->face_flags & FT_FACE_FLAG_VERTICAL )
284 root->face_flags |= FT_FACE_FLAG_VERTICAL;
287 if ( psnames && psaux )
289 FT_CharMapRec charmap;
290 T1_CMap_Classes cmap_classes = psaux->t1_cmap_classes;
296 /* first of all, try to synthetize a Unicode charmap */
297 charmap.platform_id = 3;
298 charmap.encoding_id = 1;
299 charmap.encoding = FT_ENCODING_UNICODE;
301 FT_CMap_New( cmap_classes->unicode, NULL, &charmap, NULL );
303 /* now, generate an Adobe Standard encoding when appropriate */
304 charmap.platform_id = 7;
307 switch ( face->type1.encoding_type )
309 case T1_ENCODING_TYPE_STANDARD:
310 charmap.encoding = FT_ENCODING_ADOBE_STANDARD;
311 charmap.encoding_id = 0;
312 clazz = cmap_classes->standard;
315 case T1_ENCODING_TYPE_EXPERT:
316 charmap.encoding = FT_ENCODING_ADOBE_EXPERT;
317 charmap.encoding_id = 1;
318 clazz = cmap_classes->expert;
321 case T1_ENCODING_TYPE_ARRAY:
322 charmap.encoding = FT_ENCODING_ADOBE_CUSTOM;
323 charmap.encoding_id = 2;
324 clazz = cmap_classes->custom;
327 case T1_ENCODING_TYPE_ISOLATIN1:
328 charmap.encoding = FT_ENCODING_ADOBE_LATIN_1;
329 charmap.encoding_id = 3;
330 clazz = cmap_classes->unicode;
338 FT_CMap_New( clazz, NULL, &charmap, NULL );
341 /* Select default charmap */
342 if (root->num_charmaps)
343 root->charmap = root->charmaps[0];
353 T42_Face_Done( T42_Face face )
362 type1 = &face->type1;
363 info = &type1->font_info;
364 memory = face->root.memory;
366 /* delete internal ttf face prior to freeing face->ttf_data */
367 if ( face->ttf_face )
368 FT_Done_Face( face->ttf_face );
370 /* release font info strings */
371 FT_FREE( info->version );
372 FT_FREE( info->notice );
373 FT_FREE( info->full_name );
374 FT_FREE( info->family_name );
375 FT_FREE( info->weight );
377 /* release top dictionary */
378 FT_FREE( type1->charstrings_len );
379 FT_FREE( type1->charstrings );
380 FT_FREE( type1->glyph_names );
382 FT_FREE( type1->charstrings_block );
383 FT_FREE( type1->glyph_names_block );
385 FT_FREE( type1->encoding.char_index );
386 FT_FREE( type1->encoding.char_name );
387 FT_FREE( type1->font_name );
389 FT_FREE( face->ttf_data );
392 /* release afm data if present */
393 if ( face->afm_data )
394 T1_Done_AFM( memory, (T1_AFM*)face->afm_data );
397 /* release unicode map, if any */
398 FT_FREE( face->unicode_map.maps );
399 face->unicode_map.num_maps = 0;
401 face->root.family_name = 0;
402 face->root.style_name = 0;
407 /*************************************************************************/
410 /* T42_Driver_Init */
413 /* Initializes a given Type 42 driver object. */
416 /* driver :: A handle to the target driver object. */
419 /* FreeType error code. 0 means success. */
421 FT_LOCAL_DEF( FT_Error )
422 T42_Driver_Init( T42_Driver driver )
427 ttmodule = FT_Get_Module( FT_MODULE(driver)->library, "truetype" );
428 driver->ttclazz = (FT_Driver_Class)ttmodule->clazz;
435 T42_Driver_Done( T42_Driver driver )
443 FT_LOCAL_DEF( FT_Error )
444 T42_Size_Init( T42_Size size )
446 FT_Face face = size->root.face;
447 T42_Face t42face = (T42_Face)face;
449 FT_Error error = T42_Err_Ok;
452 error = FT_New_Size( t42face->ttf_face, &ttsize );
453 size->ttsize = ttsize;
455 FT_Activate_Size( ttsize );
462 T42_Size_Done( T42_Size size )
464 FT_Face face = size->root.face;
465 T42_Face t42face = (T42_Face)face;
469 node = FT_List_Find( &t42face->ttf_face->sizes_list, size->ttsize );
472 FT_Done_Size( size->ttsize );
478 FT_LOCAL_DEF( FT_Error )
479 T42_GlyphSlot_Init( T42_GlyphSlot slot )
481 FT_Face face = slot->root.face;
482 T42_Face t42face = (T42_Face)face;
484 FT_Error error = T42_Err_Ok;
487 if ( face->glyph == NULL )
489 /* First glyph slot for this face */
490 slot->ttslot = t42face->ttf_face->glyph;
494 error = FT_New_GlyphSlot( t42face->ttf_face, &ttslot );
495 slot->ttslot = ttslot;
503 T42_GlyphSlot_Done( T42_GlyphSlot slot )
505 FT_Face face = slot->root.face;
506 T42_Face t42face = (T42_Face)face;
507 FT_GlyphSlot cur = t42face->ttf_face->glyph;
512 if ( cur == slot->ttslot )
514 FT_Done_GlyphSlot( slot->ttslot );
524 FT_LOCAL_DEF( FT_Error )
525 T42_Size_SetChars( T42_Size size,
526 FT_F26Dot6 char_width,
527 FT_F26Dot6 char_height,
528 FT_UInt horz_resolution,
529 FT_UInt vert_resolution )
531 FT_Face face = size->root.face;
532 T42_Face t42face = (T42_Face)face;
535 FT_Activate_Size(size->ttsize);
537 return FT_Set_Char_Size( t42face->ttf_face,
545 FT_LOCAL_DEF( FT_Error )
546 T42_Size_SetPixels( T42_Size size,
548 FT_UInt pixel_height )
550 FT_Face face = size->root.face;
551 T42_Face t42face = (T42_Face)face;
554 FT_Activate_Size(size->ttsize);
556 return FT_Set_Pixel_Sizes( t42face->ttf_face,
563 t42_glyphslot_clear( FT_GlyphSlot slot )
565 /* free bitmap if needed */
566 ft_glyphslot_free_bitmap( slot );
568 /* clear all public fields in the glyph slot */
569 FT_ZERO( &slot->metrics );
570 FT_ZERO( &slot->outline );
571 FT_ZERO( &slot->bitmap );
573 slot->bitmap_left = 0;
574 slot->bitmap_top = 0;
575 slot->num_subglyphs = 0;
577 slot->control_data = 0;
578 slot->control_len = 0;
580 slot->format = FT_GLYPH_FORMAT_NONE;
582 slot->linearHoriAdvance = 0;
583 slot->linearVertAdvance = 0;
587 FT_LOCAL_DEF( FT_Error )
588 T42_GlyphSlot_Load( FT_GlyphSlot glyph,
591 FT_Int32 load_flags )
594 T42_GlyphSlot t42slot = (T42_GlyphSlot)glyph;
595 T42_Size t42size = (T42_Size)size;
596 FT_Driver_Class ttclazz = ((T42_Driver)glyph->face->driver)->ttclazz;
599 t42_glyphslot_clear( t42slot->ttslot );
600 error = ttclazz->load_glyph( t42slot->ttslot,
603 load_flags | FT_LOAD_NO_BITMAP );
607 glyph->metrics = t42slot->ttslot->metrics;
609 glyph->linearHoriAdvance = t42slot->ttslot->linearHoriAdvance;
610 glyph->linearVertAdvance = t42slot->ttslot->linearVertAdvance;
612 glyph->format = t42slot->ttslot->format;
613 glyph->outline = t42slot->ttslot->outline;
615 glyph->bitmap = t42slot->ttslot->bitmap;
616 glyph->bitmap_left = t42slot->ttslot->bitmap_left;
617 glyph->bitmap_top = t42slot->ttslot->bitmap_top;
619 glyph->num_subglyphs = t42slot->ttslot->num_subglyphs;
620 glyph->subglyphs = t42slot->ttslot->subglyphs;
622 glyph->control_data = t42slot->ttslot->control_data;
623 glyph->control_len = t42slot->ttslot->control_len;