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 error
= t42_parser_init( parser
,
57 error
= t42_parse_dict( face
, &loader
,
58 parser
->base_dict
, parser
->base_len
);
62 if ( type1
->font_type
!= 42 )
64 FT_ERROR(( "T42_Open_Face: cannot handle FontType %d\n",
66 error
= FT_THROW( Unknown_File_Format
);
70 /* now, propagate the charstrings and glyphnames tables */
71 /* to the Type1 data */
72 type1
->num_glyphs
= loader
.num_glyphs
;
74 if ( !loader
.charstrings
.init
)
76 FT_ERROR(( "T42_Open_Face: no charstrings array in face\n" ));
77 error
= FT_THROW( Invalid_File_Format
);
80 loader
.charstrings
.init
= 0;
81 type1
->charstrings_block
= loader
.charstrings
.block
;
82 type1
->charstrings
= loader
.charstrings
.elements
;
83 type1
->charstrings_len
= loader
.charstrings
.lengths
;
85 /* we copy the glyph names `block' and `elements' fields; */
86 /* the `lengths' field must be released later */
87 type1
->glyph_names_block
= loader
.glyph_names
.block
;
88 type1
->glyph_names
= (FT_String
**)loader
.glyph_names
.elements
;
89 loader
.glyph_names
.block
= 0;
90 loader
.glyph_names
.elements
= 0;
92 /* we must now build type1.encoding when we have a custom array */
93 if ( type1
->encoding_type
== T1_ENCODING_TYPE_ARRAY
)
95 FT_Int charcode
, idx
, min_char
, max_char
;
99 /* OK, we do the following: for each element in the encoding */
100 /* table, look up the index of the glyph having the same name */
101 /* as defined in the CharStrings array. */
102 /* The index is then stored in type1.encoding.char_index, and */
103 /* the name in type1.encoding.char_name */
109 for ( ; charcode
< loader
.encoding_table
.max_elems
; charcode
++ )
114 type1
->encoding
.char_index
[charcode
] = 0;
115 type1
->encoding
.char_name
[charcode
] = (char *)".notdef";
117 char_name
= loader
.encoding_table
.elements
[charcode
];
119 for ( idx
= 0; idx
< type1
->num_glyphs
; idx
++ )
121 glyph_name
= (FT_Byte
*)type1
->glyph_names
[idx
];
122 if ( ft_strcmp( (const char*)char_name
,
123 (const char*)glyph_name
) == 0 )
125 type1
->encoding
.char_index
[charcode
] = (FT_UShort
)idx
;
126 type1
->encoding
.char_name
[charcode
] = (char*)glyph_name
;
128 /* Change min/max encoded char only if glyph name is */
130 if ( ft_strcmp( (const char*)".notdef",
131 (const char*)glyph_name
) != 0 )
133 if ( charcode
< min_char
)
135 if ( charcode
>= max_char
)
136 max_char
= charcode
+ 1;
143 type1
->encoding
.code_first
= min_char
;
144 type1
->encoding
.code_last
= max_char
;
145 type1
->encoding
.num_chars
= loader
.num_chars
;
149 t42_loader_done( &loader
);
154 /***************** Driver Functions *************/
157 FT_LOCAL_DEF( FT_Error
)
158 T42_Face_Init( FT_Stream stream
,
159 FT_Face t42face
, /* T42_Face */
162 FT_Parameter
* params
)
164 T42_Face face
= (T42_Face
)t42face
;
166 FT_Service_PsCMaps psnames
;
168 FT_Face root
= (FT_Face
)&face
->root
;
169 T1_Font type1
= &face
->type1
;
170 PS_FontInfo info
= &type1
->font_info
;
172 FT_UNUSED( num_params
);
174 FT_UNUSED( face_index
);
178 face
->ttf_face
= NULL
;
179 face
->root
.num_faces
= 1;
181 FT_FACE_FIND_GLOBAL_SERVICE( face
, psnames
, POSTSCRIPT_CMAPS
);
182 face
->psnames
= psnames
;
184 face
->psaux
= FT_Get_Module_Interface( FT_FACE_LIBRARY( face
),
186 psaux
= (PSAux_Service
)face
->psaux
;
189 FT_ERROR(( "T42_Face_Init: cannot access `psaux' module\n" ));
190 error
= FT_THROW( Missing_Module
);
194 FT_TRACE2(( "Type 42 driver\n" ));
196 /* open the tokenizer, this will also check the font format */
197 error
= T42_Open_Face( face
);
201 /* if we just wanted to check the format, leave successfully now */
202 if ( face_index
< 0 )
205 /* check the face index */
206 if ( face_index
> 0 )
208 FT_ERROR(( "T42_Face_Init: invalid face index\n" ));
209 error
= FT_THROW( Invalid_Argument
);
213 /* Now load the font program into the face object */
215 /* Init the face object fields */
216 /* Now set up root face fields */
218 root
->num_glyphs
= type1
->num_glyphs
;
219 root
->num_charmaps
= 0;
220 root
->face_index
= 0;
222 root
->face_flags
= FT_FACE_FLAG_SCALABLE
|
223 FT_FACE_FLAG_HORIZONTAL
|
224 FT_FACE_FLAG_GLYPH_NAMES
;
226 if ( info
->is_fixed_pitch
)
227 root
->face_flags
|= FT_FACE_FLAG_FIXED_WIDTH
;
229 /* We only set this flag if we have the patented bytecode interpreter. */
230 /* There are no known `tricky' Type42 fonts that could be loaded with */
231 /* the unpatented interpreter. */
232 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
233 root
->face_flags
|= FT_FACE_FLAG_HINTER
;
236 /* XXX: TODO -- add kerning with .afm support */
238 /* get style name -- be careful, some broken fonts only */
239 /* have a `/FontName' dictionary entry! */
240 root
->family_name
= info
->family_name
;
241 /* assume "Regular" style if we don't know better */
242 root
->style_name
= (char *)"Regular";
243 if ( root
->family_name
)
245 char* full
= info
->full_name
;
246 char* family
= root
->family_name
;
253 if ( *full
== *family
)
260 if ( *full
== ' ' || *full
== '-' )
262 else if ( *family
== ' ' || *family
== '-' )
267 root
->style_name
= full
;
276 /* do we have a `/FontName'? */
277 if ( type1
->font_name
)
278 root
->family_name
= type1
->font_name
;
281 /* no embedded bitmap support */
282 root
->num_fixed_sizes
= 0;
283 root
->available_sizes
= 0;
285 /* Load the TTF font embedded in the T42 font */
290 args
.flags
= FT_OPEN_MEMORY
;
291 args
.memory_base
= face
->ttf_data
;
292 args
.memory_size
= face
->ttf_size
;
296 args
.flags
|= FT_OPEN_PARAMS
;
297 args
.num_params
= num_params
;
298 args
.params
= params
;
301 error
= FT_Open_Face( FT_FACE_LIBRARY( face
),
302 &args
, 0, &face
->ttf_face
);
308 FT_Done_Size( face
->ttf_face
->size
);
310 /* Ignore info in FontInfo dictionary and use the info from the */
311 /* loaded TTF font. The PostScript interpreter also ignores it. */
312 root
->bbox
= face
->ttf_face
->bbox
;
313 root
->units_per_EM
= face
->ttf_face
->units_per_EM
;
315 root
->ascender
= face
->ttf_face
->ascender
;
316 root
->descender
= face
->ttf_face
->descender
;
317 root
->height
= face
->ttf_face
->height
;
319 root
->max_advance_width
= face
->ttf_face
->max_advance_width
;
320 root
->max_advance_height
= face
->ttf_face
->max_advance_height
;
322 root
->underline_position
= (FT_Short
)info
->underline_position
;
323 root
->underline_thickness
= (FT_Short
)info
->underline_thickness
;
325 /* compute style flags */
326 root
->style_flags
= 0;
327 if ( info
->italic_angle
)
328 root
->style_flags
|= FT_STYLE_FLAG_ITALIC
;
330 if ( face
->ttf_face
->style_flags
& FT_STYLE_FLAG_BOLD
)
331 root
->style_flags
|= FT_STYLE_FLAG_BOLD
;
333 if ( face
->ttf_face
->face_flags
& FT_FACE_FLAG_VERTICAL
)
334 root
->face_flags
|= FT_FACE_FLAG_VERTICAL
;
339 FT_CharMapRec charmap
;
340 T1_CMap_Classes cmap_classes
= psaux
->t1_cmap_classes
;
346 /* first of all, try to synthesize a Unicode charmap */
347 charmap
.platform_id
= TT_PLATFORM_MICROSOFT
;
348 charmap
.encoding_id
= TT_MS_ID_UNICODE_CS
;
349 charmap
.encoding
= FT_ENCODING_UNICODE
;
351 error
= FT_CMap_New( cmap_classes
->unicode
, NULL
, &charmap
, NULL
);
353 FT_ERR_NEQ( error
, No_Unicode_Glyph_Name
) )
357 /* now, generate an Adobe Standard encoding when appropriate */
358 charmap
.platform_id
= TT_PLATFORM_ADOBE
;
361 switch ( type1
->encoding_type
)
363 case T1_ENCODING_TYPE_STANDARD
:
364 charmap
.encoding
= FT_ENCODING_ADOBE_STANDARD
;
365 charmap
.encoding_id
= TT_ADOBE_ID_STANDARD
;
366 clazz
= cmap_classes
->standard
;
369 case T1_ENCODING_TYPE_EXPERT
:
370 charmap
.encoding
= FT_ENCODING_ADOBE_EXPERT
;
371 charmap
.encoding_id
= TT_ADOBE_ID_EXPERT
;
372 clazz
= cmap_classes
->expert
;
375 case T1_ENCODING_TYPE_ARRAY
:
376 charmap
.encoding
= FT_ENCODING_ADOBE_CUSTOM
;
377 charmap
.encoding_id
= TT_ADOBE_ID_CUSTOM
;
378 clazz
= cmap_classes
->custom
;
381 case T1_ENCODING_TYPE_ISOLATIN1
:
382 charmap
.encoding
= FT_ENCODING_ADOBE_LATIN_1
;
383 charmap
.encoding_id
= TT_ADOBE_ID_LATIN_1
;
384 clazz
= cmap_classes
->unicode
;
392 error
= FT_CMap_New( clazz
, NULL
, &charmap
, NULL
);
395 /* Select default charmap */
396 if ( root
->num_charmaps
)
397 root
->charmap
= root
->charmaps
[0];
407 T42_Face_Done( FT_Face t42face
)
409 T42_Face face
= (T42_Face
)t42face
;
418 type1
= &face
->type1
;
419 info
= &type1
->font_info
;
420 memory
= face
->root
.memory
;
422 /* delete internal ttf face prior to freeing face->ttf_data */
423 if ( face
->ttf_face
)
424 FT_Done_Face( face
->ttf_face
);
426 /* release font info strings */
427 FT_FREE( info
->version
);
428 FT_FREE( info
->notice
);
429 FT_FREE( info
->full_name
);
430 FT_FREE( info
->family_name
);
431 FT_FREE( info
->weight
);
433 /* release top dictionary */
434 FT_FREE( type1
->charstrings_len
);
435 FT_FREE( type1
->charstrings
);
436 FT_FREE( type1
->glyph_names
);
438 FT_FREE( type1
->charstrings_block
);
439 FT_FREE( type1
->glyph_names_block
);
441 FT_FREE( type1
->encoding
.char_index
);
442 FT_FREE( type1
->encoding
.char_name
);
443 FT_FREE( type1
->font_name
);
445 FT_FREE( face
->ttf_data
);
448 /* release afm data if present */
449 if ( face
->afm_data
)
450 T1_Done_AFM( memory
, (T1_AFM
*)face
->afm_data
);
453 /* release unicode map, if any */
454 FT_FREE( face
->unicode_map
.maps
);
455 face
->unicode_map
.num_maps
= 0;
457 face
->root
.family_name
= 0;
458 face
->root
.style_name
= 0;
462 /*************************************************************************/
465 /* T42_Driver_Init */
468 /* Initializes a given Type 42 driver object. */
471 /* driver :: A handle to the target driver object. */
474 /* FreeType error code. 0 means success. */
476 FT_LOCAL_DEF( FT_Error
)
477 T42_Driver_Init( FT_Module module
) /* T42_Driver */
479 T42_Driver driver
= (T42_Driver
)module
;
483 ttmodule
= FT_Get_Module( module
->library
, "truetype" );
486 FT_ERROR(( "T42_Driver_Init: cannot access `truetype' module\n" ));
487 return FT_THROW( Missing_Module
);
490 driver
->ttclazz
= (FT_Driver_Class
)ttmodule
->clazz
;
497 T42_Driver_Done( FT_Module module
)
503 FT_LOCAL_DEF( FT_Error
)
504 T42_Size_Init( FT_Size size
) /* T42_Size */
506 T42_Size t42size
= (T42_Size
)size
;
507 FT_Face face
= size
->face
;
508 T42_Face t42face
= (T42_Face
)face
;
510 FT_Error error
= FT_Err_Ok
;
513 error
= FT_New_Size( t42face
->ttf_face
, &ttsize
);
514 t42size
->ttsize
= ttsize
;
516 FT_Activate_Size( ttsize
);
522 FT_LOCAL_DEF( FT_Error
)
523 T42_Size_Request( FT_Size t42size
, /* T42_Size */
524 FT_Size_Request req
)
526 T42_Size size
= (T42_Size
)t42size
;
527 T42_Face face
= (T42_Face
)t42size
->face
;
531 FT_Activate_Size( size
->ttsize
);
533 error
= FT_Request_Size( face
->ttf_face
, req
);
535 t42size
->metrics
= face
->ttf_face
->size
->metrics
;
541 FT_LOCAL_DEF( FT_Error
)
542 T42_Size_Select( FT_Size t42size
, /* T42_Size */
543 FT_ULong strike_index
)
545 T42_Size size
= (T42_Size
)t42size
;
546 T42_Face face
= (T42_Face
)t42size
->face
;
550 FT_Activate_Size( size
->ttsize
);
552 error
= FT_Select_Size( face
->ttf_face
, (FT_Int
)strike_index
);
554 t42size
->metrics
= face
->ttf_face
->size
->metrics
;
562 T42_Size_Done( FT_Size t42size
) /* T42_Size */
564 T42_Size size
= (T42_Size
)t42size
;
565 FT_Face face
= t42size
->face
;
566 T42_Face t42face
= (T42_Face
)face
;
570 node
= FT_List_Find( &t42face
->ttf_face
->sizes_list
, size
->ttsize
);
573 FT_Done_Size( size
->ttsize
);
579 FT_LOCAL_DEF( FT_Error
)
580 T42_GlyphSlot_Init( FT_GlyphSlot t42slot
) /* T42_GlyphSlot */
582 T42_GlyphSlot slot
= (T42_GlyphSlot
)t42slot
;
583 FT_Face face
= t42slot
->face
;
584 T42_Face t42face
= (T42_Face
)face
;
586 FT_Error error
= FT_Err_Ok
;
589 if ( face
->glyph
== NULL
)
591 /* First glyph slot for this face */
592 slot
->ttslot
= t42face
->ttf_face
->glyph
;
596 error
= FT_New_GlyphSlot( t42face
->ttf_face
, &ttslot
);
597 slot
->ttslot
= ttslot
;
605 T42_GlyphSlot_Done( FT_GlyphSlot t42slot
) /* T42_GlyphSlot */
607 T42_GlyphSlot slot
= (T42_GlyphSlot
)t42slot
;
610 FT_Done_GlyphSlot( slot
->ttslot
);
615 t42_glyphslot_clear( FT_GlyphSlot slot
)
617 /* free bitmap if needed */
618 ft_glyphslot_free_bitmap( slot
);
620 /* clear all public fields in the glyph slot */
621 FT_ZERO( &slot
->metrics
);
622 FT_ZERO( &slot
->outline
);
623 FT_ZERO( &slot
->bitmap
);
625 slot
->bitmap_left
= 0;
626 slot
->bitmap_top
= 0;
627 slot
->num_subglyphs
= 0;
629 slot
->control_data
= 0;
630 slot
->control_len
= 0;
632 slot
->format
= FT_GLYPH_FORMAT_NONE
;
634 slot
->linearHoriAdvance
= 0;
635 slot
->linearVertAdvance
= 0;
639 FT_LOCAL_DEF( FT_Error
)
640 T42_GlyphSlot_Load( FT_GlyphSlot glyph
,
643 FT_Int32 load_flags
)
646 T42_GlyphSlot t42slot
= (T42_GlyphSlot
)glyph
;
647 T42_Size t42size
= (T42_Size
)size
;
648 FT_Driver_Class ttclazz
= ((T42_Driver
)glyph
->face
->driver
)->ttclazz
;
651 t42_glyphslot_clear( t42slot
->ttslot
);
652 error
= ttclazz
->load_glyph( t42slot
->ttslot
,
655 load_flags
| FT_LOAD_NO_BITMAP
);
659 glyph
->metrics
= t42slot
->ttslot
->metrics
;
661 glyph
->linearHoriAdvance
= t42slot
->ttslot
->linearHoriAdvance
;
662 glyph
->linearVertAdvance
= t42slot
->ttslot
->linearVertAdvance
;
664 glyph
->format
= t42slot
->ttslot
->format
;
665 glyph
->outline
= t42slot
->ttslot
->outline
;
667 glyph
->bitmap
= t42slot
->ttslot
->bitmap
;
668 glyph
->bitmap_left
= t42slot
->ttslot
->bitmap_left
;
669 glyph
->bitmap_top
= t42slot
->ttslot
->bitmap_top
;
671 glyph
->num_subglyphs
= t42slot
->ttslot
->num_subglyphs
;
672 glyph
->subglyphs
= t42slot
->ttslot
->subglyphs
;
674 glyph
->control_data
= t42slot
->ttslot
->control_data
;
675 glyph
->control_len
= t42slot
->ttslot
->control_len
;