1 /***************************************************************************/
5 /* Type 42 objects manager (body). */
7 /* Copyright 2002, 2003, 2004, 2005, 2006, 2007 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
,
57 parser
->base_dict
, parser
->base_len
);
61 if ( type1
->font_type
!= 42 )
63 error
= T42_Err_Unknown_File_Format
;
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
;
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 */
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
];
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 */
125 if ( ft_strcmp( (const char*)".notdef",
126 (const char*)glyph_name
) != 0 )
128 if ( charcode
< min_char
)
130 if ( charcode
> max_char
)
137 type1
->encoding
.code_first
= min_char
;
138 type1
->encoding
.code_last
= max_char
;
139 type1
->encoding
.num_chars
= loader
.num_chars
;
143 t42_loader_done( &loader
);
148 /***************** Driver Functions *************/
151 FT_LOCAL_DEF( FT_Error
)
152 T42_Face_Init( FT_Stream stream
,
156 FT_Parameter
* params
)
159 FT_Service_PsCMaps psnames
;
161 FT_Face root
= (FT_Face
)&face
->root
;
162 T1_Font type1
= &face
->type1
;
163 PS_FontInfo info
= &type1
->font_info
;
165 FT_UNUSED( num_params
);
167 FT_UNUSED( face_index
);
171 face
->ttf_face
= NULL
;
172 face
->root
.num_faces
= 1;
174 FT_FACE_FIND_GLOBAL_SERVICE( face
, psnames
, POSTSCRIPT_CMAPS
);
175 face
->psnames
= psnames
;
177 face
->psaux
= FT_Get_Module_Interface( FT_FACE_LIBRARY( face
),
179 psaux
= (PSAux_Service
)face
->psaux
;
181 /* open the tokenizer, this will also check the font format */
182 error
= T42_Open_Face( face
);
186 /* if we just wanted to check the format, leave successfully now */
187 if ( face_index
< 0 )
190 /* check the face index */
191 if ( face_index
!= 0 )
193 FT_ERROR(( "T42_Face_Init: invalid face index\n" ));
194 error
= T42_Err_Invalid_Argument
;
198 /* Now load the font program into the face object */
200 /* Init the face object fields */
201 /* Now set up root face fields */
203 root
->num_glyphs
= type1
->num_glyphs
;
204 root
->num_charmaps
= 0;
205 root
->face_index
= face_index
;
207 root
->face_flags
= FT_FACE_FLAG_SCALABLE
|
208 FT_FACE_FLAG_HORIZONTAL
|
209 FT_FACE_FLAG_GLYPH_NAMES
;
211 if ( info
->is_fixed_pitch
)
212 root
->face_flags
|= FT_FACE_FLAG_FIXED_WIDTH
;
214 /* We only set this flag if we have the patented bytecode interpreter. */
215 /* There are no known `tricky' Type42 fonts that could be loaded with */
216 /* the unpatented interpreter. */
217 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
218 root
->face_flags
|= FT_FACE_FLAG_HINTER
;
221 /* XXX: TODO -- add kerning with .afm support */
223 /* get style name -- be careful, some broken fonts only */
224 /* have a `/FontName' dictionary entry! */
225 root
->family_name
= info
->family_name
;
226 /* assume "Regular" style if we don't know better */
227 root
->style_name
= (char *)"Regular";
228 if ( root
->family_name
)
230 char* full
= info
->full_name
;
231 char* family
= root
->family_name
;
238 if ( *full
== *family
)
245 if ( *full
== ' ' || *full
== '-' )
247 else if ( *family
== ' ' || *family
== '-' )
252 root
->style_name
= full
;
261 /* do we have a `/FontName'? */
262 if ( type1
->font_name
)
263 root
->family_name
= type1
->font_name
;
266 /* no embedded bitmap support */
267 root
->num_fixed_sizes
= 0;
268 root
->available_sizes
= 0;
270 /* Load the TTF font embedded in the T42 font */
275 args
.flags
= FT_OPEN_MEMORY
;
276 args
.memory_base
= face
->ttf_data
;
277 args
.memory_size
= face
->ttf_size
;
281 args
.flags
|= FT_OPEN_PARAMS
;
282 args
.num_params
= num_params
;
283 args
.params
= params
;
286 error
= FT_Open_Face( FT_FACE_LIBRARY( face
),
287 &args
, 0, &face
->ttf_face
);
293 FT_Done_Size( face
->ttf_face
->size
);
295 /* Ignore info in FontInfo dictionary and use the info from the */
296 /* loaded TTF font. The PostScript interpreter also ignores it. */
297 root
->bbox
= face
->ttf_face
->bbox
;
298 root
->units_per_EM
= face
->ttf_face
->units_per_EM
;
300 root
->ascender
= face
->ttf_face
->ascender
;
301 root
->descender
= face
->ttf_face
->descender
;
302 root
->height
= face
->ttf_face
->height
;
304 root
->max_advance_width
= face
->ttf_face
->max_advance_width
;
305 root
->max_advance_height
= face
->ttf_face
->max_advance_height
;
307 root
->underline_position
= (FT_Short
)info
->underline_position
;
308 root
->underline_thickness
= (FT_Short
)info
->underline_thickness
;
310 /* compute style flags */
311 root
->style_flags
= 0;
312 if ( info
->italic_angle
)
313 root
->style_flags
|= FT_STYLE_FLAG_ITALIC
;
315 if ( face
->ttf_face
->style_flags
& FT_STYLE_FLAG_BOLD
)
316 root
->style_flags
|= FT_STYLE_FLAG_BOLD
;
318 if ( face
->ttf_face
->face_flags
& FT_FACE_FLAG_VERTICAL
)
319 root
->face_flags
|= FT_FACE_FLAG_VERTICAL
;
322 if ( psnames
&& psaux
)
324 FT_CharMapRec charmap
;
325 T1_CMap_Classes cmap_classes
= psaux
->t1_cmap_classes
;
331 /* first of all, try to synthetize a Unicode charmap */
332 charmap
.platform_id
= 3;
333 charmap
.encoding_id
= 1;
334 charmap
.encoding
= FT_ENCODING_UNICODE
;
336 FT_CMap_New( cmap_classes
->unicode
, NULL
, &charmap
, NULL
);
338 /* now, generate an Adobe Standard encoding when appropriate */
339 charmap
.platform_id
= 7;
342 switch ( type1
->encoding_type
)
344 case T1_ENCODING_TYPE_STANDARD
:
345 charmap
.encoding
= FT_ENCODING_ADOBE_STANDARD
;
346 charmap
.encoding_id
= 0;
347 clazz
= cmap_classes
->standard
;
350 case T1_ENCODING_TYPE_EXPERT
:
351 charmap
.encoding
= FT_ENCODING_ADOBE_EXPERT
;
352 charmap
.encoding_id
= 1;
353 clazz
= cmap_classes
->expert
;
356 case T1_ENCODING_TYPE_ARRAY
:
357 charmap
.encoding
= FT_ENCODING_ADOBE_CUSTOM
;
358 charmap
.encoding_id
= 2;
359 clazz
= cmap_classes
->custom
;
362 case T1_ENCODING_TYPE_ISOLATIN1
:
363 charmap
.encoding
= FT_ENCODING_ADOBE_LATIN_1
;
364 charmap
.encoding_id
= 3;
365 clazz
= cmap_classes
->unicode
;
373 FT_CMap_New( clazz
, NULL
, &charmap
, NULL
);
376 /* Select default charmap */
377 if ( root
->num_charmaps
)
378 root
->charmap
= root
->charmaps
[0];
388 T42_Face_Done( T42_Face face
)
397 type1
= &face
->type1
;
398 info
= &type1
->font_info
;
399 memory
= face
->root
.memory
;
401 /* delete internal ttf face prior to freeing face->ttf_data */
402 if ( face
->ttf_face
)
403 FT_Done_Face( face
->ttf_face
);
405 /* release font info strings */
406 FT_FREE( info
->version
);
407 FT_FREE( info
->notice
);
408 FT_FREE( info
->full_name
);
409 FT_FREE( info
->family_name
);
410 FT_FREE( info
->weight
);
412 /* release top dictionary */
413 FT_FREE( type1
->charstrings_len
);
414 FT_FREE( type1
->charstrings
);
415 FT_FREE( type1
->glyph_names
);
417 FT_FREE( type1
->charstrings_block
);
418 FT_FREE( type1
->glyph_names_block
);
420 FT_FREE( type1
->encoding
.char_index
);
421 FT_FREE( type1
->encoding
.char_name
);
422 FT_FREE( type1
->font_name
);
424 FT_FREE( face
->ttf_data
);
427 /* release afm data if present */
428 if ( face
->afm_data
)
429 T1_Done_AFM( memory
, (T1_AFM
*)face
->afm_data
);
432 /* release unicode map, if any */
433 FT_FREE( face
->unicode_map
.maps
);
434 face
->unicode_map
.num_maps
= 0;
436 face
->root
.family_name
= 0;
437 face
->root
.style_name
= 0;
442 /*************************************************************************/
445 /* T42_Driver_Init */
448 /* Initializes a given Type 42 driver object. */
451 /* driver :: A handle to the target driver object. */
454 /* FreeType error code. 0 means success. */
456 FT_LOCAL_DEF( FT_Error
)
457 T42_Driver_Init( T42_Driver driver
)
462 ttmodule
= FT_Get_Module( FT_MODULE(driver
)->library
, "truetype" );
463 driver
->ttclazz
= (FT_Driver_Class
)ttmodule
->clazz
;
470 T42_Driver_Done( T42_Driver driver
)
476 FT_LOCAL_DEF( FT_Error
)
477 T42_Size_Init( T42_Size size
)
479 FT_Face face
= size
->root
.face
;
480 T42_Face t42face
= (T42_Face
)face
;
482 FT_Error error
= T42_Err_Ok
;
485 error
= FT_New_Size( t42face
->ttf_face
, &ttsize
);
486 size
->ttsize
= ttsize
;
488 FT_Activate_Size( ttsize
);
494 FT_LOCAL_DEF( FT_Error
)
495 T42_Size_Request( T42_Size size
,
496 FT_Size_Request req
)
498 T42_Face face
= (T42_Face
)size
->root
.face
;
502 FT_Activate_Size( size
->ttsize
);
504 error
= FT_Request_Size( face
->ttf_face
, req
);
506 ( (FT_Size
)size
)->metrics
= face
->ttf_face
->size
->metrics
;
512 FT_LOCAL_DEF( FT_Error
)
513 T42_Size_Select( T42_Size size
,
514 FT_ULong strike_index
)
516 T42_Face face
= (T42_Face
)size
->root
.face
;
520 FT_Activate_Size( size
->ttsize
);
522 error
= FT_Select_Size( face
->ttf_face
, strike_index
);
524 ( (FT_Size
)size
)->metrics
= face
->ttf_face
->size
->metrics
;
532 T42_Size_Done( T42_Size size
)
534 FT_Face face
= size
->root
.face
;
535 T42_Face t42face
= (T42_Face
)face
;
539 node
= FT_List_Find( &t42face
->ttf_face
->sizes_list
, size
->ttsize
);
542 FT_Done_Size( size
->ttsize
);
548 FT_LOCAL_DEF( FT_Error
)
549 T42_GlyphSlot_Init( T42_GlyphSlot slot
)
551 FT_Face face
= slot
->root
.face
;
552 T42_Face t42face
= (T42_Face
)face
;
554 FT_Error error
= T42_Err_Ok
;
557 if ( face
->glyph
== NULL
)
559 /* First glyph slot for this face */
560 slot
->ttslot
= t42face
->ttf_face
->glyph
;
564 error
= FT_New_GlyphSlot( t42face
->ttf_face
, &ttslot
);
565 slot
->ttslot
= ttslot
;
573 T42_GlyphSlot_Done( T42_GlyphSlot slot
)
575 FT_Done_GlyphSlot( slot
->ttslot
);
580 t42_glyphslot_clear( FT_GlyphSlot slot
)
582 /* free bitmap if needed */
583 ft_glyphslot_free_bitmap( slot
);
585 /* clear all public fields in the glyph slot */
586 FT_ZERO( &slot
->metrics
);
587 FT_ZERO( &slot
->outline
);
588 FT_ZERO( &slot
->bitmap
);
590 slot
->bitmap_left
= 0;
591 slot
->bitmap_top
= 0;
592 slot
->num_subglyphs
= 0;
594 slot
->control_data
= 0;
595 slot
->control_len
= 0;
597 slot
->format
= FT_GLYPH_FORMAT_NONE
;
599 slot
->linearHoriAdvance
= 0;
600 slot
->linearVertAdvance
= 0;
604 FT_LOCAL_DEF( FT_Error
)
605 T42_GlyphSlot_Load( FT_GlyphSlot glyph
,
608 FT_Int32 load_flags
)
611 T42_GlyphSlot t42slot
= (T42_GlyphSlot
)glyph
;
612 T42_Size t42size
= (T42_Size
)size
;
613 FT_Driver_Class ttclazz
= ((T42_Driver
)glyph
->face
->driver
)->ttclazz
;
616 t42_glyphslot_clear( t42slot
->ttslot
);
617 error
= ttclazz
->load_glyph( t42slot
->ttslot
,
620 load_flags
| FT_LOAD_NO_BITMAP
);
624 glyph
->metrics
= t42slot
->ttslot
->metrics
;
626 glyph
->linearHoriAdvance
= t42slot
->ttslot
->linearHoriAdvance
;
627 glyph
->linearVertAdvance
= t42slot
->ttslot
->linearVertAdvance
;
629 glyph
->format
= t42slot
->ttslot
->format
;
630 glyph
->outline
= t42slot
->ttslot
->outline
;
632 glyph
->bitmap
= t42slot
->ttslot
->bitmap
;
633 glyph
->bitmap_left
= t42slot
->ttslot
->bitmap_left
;
634 glyph
->bitmap_top
= t42slot
->ttslot
->bitmap_top
;
636 glyph
->num_subglyphs
= t42slot
->ttslot
->num_subglyphs
;
637 glyph
->subglyphs
= t42slot
->ttslot
->subglyphs
;
639 glyph
->control_data
= t42slot
->ttslot
->control_data
;
640 glyph
->control_len
= t42slot
->ttslot
->control_len
;