1 /***************************************************************************/
5 /* Type 42 objects manager (body). */
7 /* Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 */
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
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
)
131 max_char
= charcode
+ 1;
138 type1
->encoding
.code_first
= min_char
;
139 type1
->encoding
.code_last
= max_char
;
140 type1
->encoding
.num_chars
= loader
.num_chars
;
144 t42_loader_done( &loader
);
149 /***************** Driver Functions *************/
152 FT_LOCAL_DEF( FT_Error
)
153 T42_Face_Init( FT_Stream stream
,
157 FT_Parameter
* params
)
160 FT_Service_PsCMaps psnames
;
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
);
168 FT_UNUSED( face_index
);
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
),
180 psaux
= (PSAux_Service
)face
->psaux
;
182 /* open the tokenizer, this will also check the font format */
183 error
= T42_Open_Face( face
);
187 /* if we just wanted to check the format, leave successfully now */
188 if ( face_index
< 0 )
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
;
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
;
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
;
239 if ( *full
== *family
)
246 if ( *full
== ' ' || *full
== '-' )
248 else if ( *family
== ' ' || *family
== '-' )
253 root
->style_name
= full
;
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 */
276 args
.flags
= FT_OPEN_MEMORY
;
277 args
.memory_base
= face
->ttf_data
;
278 args
.memory_size
= face
->ttf_size
;
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
);
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
;
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;
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
;
351 case T1_ENCODING_TYPE_EXPERT
:
352 charmap
.encoding
= FT_ENCODING_ADOBE_EXPERT
;
353 charmap
.encoding_id
= 1;
354 clazz
= cmap_classes
->expert
;
357 case T1_ENCODING_TYPE_ARRAY
:
358 charmap
.encoding
= FT_ENCODING_ADOBE_CUSTOM
;
359 charmap
.encoding_id
= 2;
360 clazz
= cmap_classes
->custom
;
363 case T1_ENCODING_TYPE_ISOLATIN1
:
364 charmap
.encoding
= FT_ENCODING_ADOBE_LATIN_1
;
365 charmap
.encoding_id
= 3;
366 clazz
= cmap_classes
->unicode
;
374 FT_CMap_New( clazz
, NULL
, &charmap
, NULL
);
377 /* Select default charmap */
378 if ( root
->num_charmaps
)
379 root
->charmap
= root
->charmaps
[0];
389 T42_Face_Done( T42_Face face
)
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
);
429 /* release afm data if present */
430 if ( face
->afm_data
)
431 T1_Done_AFM( memory
, (T1_AFM
*)face
->afm_data
);
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 /*************************************************************************/
446 /* T42_Driver_Init */
449 /* Initializes a given Type 42 driver object. */
452 /* driver :: A handle to the target driver object. */
455 /* FreeType error code. 0 means success. */
457 FT_LOCAL_DEF( FT_Error
)
458 T42_Driver_Init( T42_Driver driver
)
463 ttmodule
= FT_Get_Module( FT_MODULE(driver
)->library
, "truetype" );
464 driver
->ttclazz
= (FT_Driver_Class
)ttmodule
->clazz
;
471 T42_Driver_Done( T42_Driver 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
;
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
);
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
;
503 FT_Activate_Size( size
->ttsize
);
505 error
= FT_Request_Size( face
->ttf_face
, req
);
507 ( (FT_Size
)size
)->metrics
= face
->ttf_face
->size
->metrics
;
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
;
521 FT_Activate_Size( size
->ttsize
);
523 error
= FT_Select_Size( face
->ttf_face
, (FT_Int
)strike_index
);
525 ( (FT_Size
)size
)->metrics
= face
->ttf_face
->size
->metrics
;
533 T42_Size_Done( T42_Size size
)
535 FT_Face face
= size
->root
.face
;
536 T42_Face t42face
= (T42_Face
)face
;
540 node
= FT_List_Find( &t42face
->ttf_face
->sizes_list
, size
->ttsize
);
543 FT_Done_Size( size
->ttsize
);
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
;
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
;
565 error
= FT_New_GlyphSlot( t42face
->ttf_face
, &ttslot
);
566 slot
->ttslot
= ttslot
;
574 T42_GlyphSlot_Done( T42_GlyphSlot slot
)
576 FT_Done_GlyphSlot( slot
->ttslot
);
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;
595 slot
->control_data
= 0;
596 slot
->control_len
= 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
,
609 FT_Int32 load_flags
)
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
,
621 load_flags
| FT_LOAD_NO_BITMAP
);
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
;