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
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 error
= T42_Err_Unknown_File_Format
;
68 /* now, propagate the charstrings and glyphnames tables */
69 /* to the Type1 data */
70 type1
->num_glyphs
= loader
.num_glyphs
;
72 if ( !loader
.charstrings
.init
)
74 FT_ERROR(( "T42_Open_Face: no charstrings array in face\n" ));
75 error
= T42_Err_Invalid_File_Format
;
78 loader
.charstrings
.init
= 0;
79 type1
->charstrings_block
= loader
.charstrings
.block
;
80 type1
->charstrings
= loader
.charstrings
.elements
;
81 type1
->charstrings_len
= loader
.charstrings
.lengths
;
83 /* we copy the glyph names `block' and `elements' fields; */
84 /* the `lengths' field must be released later */
85 type1
->glyph_names_block
= loader
.glyph_names
.block
;
86 type1
->glyph_names
= (FT_String
**)loader
.glyph_names
.elements
;
87 loader
.glyph_names
.block
= 0;
88 loader
.glyph_names
.elements
= 0;
90 /* we must now build type1.encoding when we have a custom array */
91 if ( type1
->encoding_type
== T1_ENCODING_TYPE_ARRAY
)
93 FT_Int charcode
, idx
, min_char
, max_char
;
98 /* OK, we do the following: for each element in the encoding */
99 /* table, look up the index of the glyph having the same name */
100 /* as defined in the CharStrings array. */
101 /* The index is then stored in type1.encoding.char_index, and */
102 /* the name in type1.encoding.char_name */
108 for ( ; charcode
< loader
.encoding_table
.max_elems
; charcode
++ )
110 type1
->encoding
.char_index
[charcode
] = 0;
111 type1
->encoding
.char_name
[charcode
] = (char *)".notdef";
113 char_name
= loader
.encoding_table
.elements
[charcode
];
115 for ( idx
= 0; idx
< type1
->num_glyphs
; idx
++ )
117 glyph_name
= (FT_Byte
*)type1
->glyph_names
[idx
];
118 if ( ft_strcmp( (const char*)char_name
,
119 (const char*)glyph_name
) == 0 )
121 type1
->encoding
.char_index
[charcode
] = (FT_UShort
)idx
;
122 type1
->encoding
.char_name
[charcode
] = (char*)glyph_name
;
124 /* Change min/max encoded char only if glyph name is */
126 if ( ft_strcmp( (const char*)".notdef",
127 (const char*)glyph_name
) != 0 )
129 if ( charcode
< min_char
)
131 if ( charcode
>= max_char
)
132 max_char
= charcode
+ 1;
139 type1
->encoding
.code_first
= min_char
;
140 type1
->encoding
.code_last
= max_char
;
141 type1
->encoding
.num_chars
= loader
.num_chars
;
145 t42_loader_done( &loader
);
150 /***************** Driver Functions *************/
153 FT_LOCAL_DEF( FT_Error
)
154 T42_Face_Init( FT_Stream stream
,
158 FT_Parameter
* params
)
161 FT_Service_PsCMaps psnames
;
163 FT_Face root
= (FT_Face
)&face
->root
;
164 T1_Font type1
= &face
->type1
;
165 PS_FontInfo info
= &type1
->font_info
;
167 FT_UNUSED( num_params
);
169 FT_UNUSED( face_index
);
173 face
->ttf_face
= NULL
;
174 face
->root
.num_faces
= 1;
176 FT_FACE_FIND_GLOBAL_SERVICE( face
, psnames
, POSTSCRIPT_CMAPS
);
177 face
->psnames
= psnames
;
179 face
->psaux
= FT_Get_Module_Interface( FT_FACE_LIBRARY( face
),
181 psaux
= (PSAux_Service
)face
->psaux
;
183 /* open the tokenizer, this will also check the font format */
184 error
= T42_Open_Face( face
);
188 /* if we just wanted to check the format, leave successfully now */
189 if ( face_index
< 0 )
192 /* check the face index */
193 if ( face_index
> 0 )
195 FT_ERROR(( "T42_Face_Init: invalid face index\n" ));
196 error
= T42_Err_Invalid_Argument
;
200 /* Now load the font program into the face object */
202 /* Init the face object fields */
203 /* Now set up root face fields */
205 root
->num_glyphs
= type1
->num_glyphs
;
206 root
->num_charmaps
= 0;
207 root
->face_index
= 0;
209 root
->face_flags
= FT_FACE_FLAG_SCALABLE
|
210 FT_FACE_FLAG_HORIZONTAL
|
211 FT_FACE_FLAG_GLYPH_NAMES
;
213 if ( info
->is_fixed_pitch
)
214 root
->face_flags
|= FT_FACE_FLAG_FIXED_WIDTH
;
216 /* We only set this flag if we have the patented bytecode interpreter. */
217 /* There are no known `tricky' Type42 fonts that could be loaded with */
218 /* the unpatented interpreter. */
219 #ifdef TT_CONFIG_OPTION_BYTECODE_INTERPRETER
220 root
->face_flags
|= FT_FACE_FLAG_HINTER
;
223 /* XXX: TODO -- add kerning with .afm support */
225 /* get style name -- be careful, some broken fonts only */
226 /* have a `/FontName' dictionary entry! */
227 root
->family_name
= info
->family_name
;
228 /* assume "Regular" style if we don't know better */
229 root
->style_name
= (char *)"Regular";
230 if ( root
->family_name
)
232 char* full
= info
->full_name
;
233 char* family
= root
->family_name
;
240 if ( *full
== *family
)
247 if ( *full
== ' ' || *full
== '-' )
249 else if ( *family
== ' ' || *family
== '-' )
254 root
->style_name
= full
;
263 /* do we have a `/FontName'? */
264 if ( type1
->font_name
)
265 root
->family_name
= type1
->font_name
;
268 /* no embedded bitmap support */
269 root
->num_fixed_sizes
= 0;
270 root
->available_sizes
= 0;
272 /* Load the TTF font embedded in the T42 font */
277 args
.flags
= FT_OPEN_MEMORY
;
278 args
.memory_base
= face
->ttf_data
;
279 args
.memory_size
= face
->ttf_size
;
283 args
.flags
|= FT_OPEN_PARAMS
;
284 args
.num_params
= num_params
;
285 args
.params
= params
;
288 error
= FT_Open_Face( FT_FACE_LIBRARY( face
),
289 &args
, 0, &face
->ttf_face
);
295 FT_Done_Size( face
->ttf_face
->size
);
297 /* Ignore info in FontInfo dictionary and use the info from the */
298 /* loaded TTF font. The PostScript interpreter also ignores it. */
299 root
->bbox
= face
->ttf_face
->bbox
;
300 root
->units_per_EM
= face
->ttf_face
->units_per_EM
;
302 root
->ascender
= face
->ttf_face
->ascender
;
303 root
->descender
= face
->ttf_face
->descender
;
304 root
->height
= face
->ttf_face
->height
;
306 root
->max_advance_width
= face
->ttf_face
->max_advance_width
;
307 root
->max_advance_height
= face
->ttf_face
->max_advance_height
;
309 root
->underline_position
= (FT_Short
)info
->underline_position
;
310 root
->underline_thickness
= (FT_Short
)info
->underline_thickness
;
312 /* compute style flags */
313 root
->style_flags
= 0;
314 if ( info
->italic_angle
)
315 root
->style_flags
|= FT_STYLE_FLAG_ITALIC
;
317 if ( face
->ttf_face
->style_flags
& FT_STYLE_FLAG_BOLD
)
318 root
->style_flags
|= FT_STYLE_FLAG_BOLD
;
320 if ( face
->ttf_face
->face_flags
& FT_FACE_FLAG_VERTICAL
)
321 root
->face_flags
|= FT_FACE_FLAG_VERTICAL
;
324 if ( psnames
&& psaux
)
326 FT_CharMapRec charmap
;
327 T1_CMap_Classes cmap_classes
= psaux
->t1_cmap_classes
;
333 /* first of all, try to synthesize a Unicode charmap */
334 charmap
.platform_id
= TT_PLATFORM_MICROSOFT
;
335 charmap
.encoding_id
= TT_MS_ID_UNICODE_CS
;
336 charmap
.encoding
= FT_ENCODING_UNICODE
;
338 error
= FT_CMap_New( cmap_classes
->unicode
, NULL
, &charmap
, NULL
);
339 if ( error
&& FT_Err_No_Unicode_Glyph_Name
!= error
)
343 /* now, generate an Adobe Standard encoding when appropriate */
344 charmap
.platform_id
= TT_PLATFORM_ADOBE
;
347 switch ( type1
->encoding_type
)
349 case T1_ENCODING_TYPE_STANDARD
:
350 charmap
.encoding
= FT_ENCODING_ADOBE_STANDARD
;
351 charmap
.encoding_id
= TT_ADOBE_ID_STANDARD
;
352 clazz
= cmap_classes
->standard
;
355 case T1_ENCODING_TYPE_EXPERT
:
356 charmap
.encoding
= FT_ENCODING_ADOBE_EXPERT
;
357 charmap
.encoding_id
= TT_ADOBE_ID_EXPERT
;
358 clazz
= cmap_classes
->expert
;
361 case T1_ENCODING_TYPE_ARRAY
:
362 charmap
.encoding
= FT_ENCODING_ADOBE_CUSTOM
;
363 charmap
.encoding_id
= TT_ADOBE_ID_CUSTOM
;
364 clazz
= cmap_classes
->custom
;
367 case T1_ENCODING_TYPE_ISOLATIN1
:
368 charmap
.encoding
= FT_ENCODING_ADOBE_LATIN_1
;
369 charmap
.encoding_id
= TT_ADOBE_ID_LATIN_1
;
370 clazz
= cmap_classes
->unicode
;
378 error
= FT_CMap_New( clazz
, NULL
, &charmap
, NULL
);
381 /* Select default charmap */
382 if ( root
->num_charmaps
)
383 root
->charmap
= root
->charmaps
[0];
393 T42_Face_Done( T42_Face face
)
403 type1
= &face
->type1
;
404 info
= &type1
->font_info
;
405 memory
= face
->root
.memory
;
407 /* delete internal ttf face prior to freeing face->ttf_data */
408 if ( face
->ttf_face
)
409 FT_Done_Face( face
->ttf_face
);
411 /* release font info strings */
412 FT_FREE( info
->version
);
413 FT_FREE( info
->notice
);
414 FT_FREE( info
->full_name
);
415 FT_FREE( info
->family_name
);
416 FT_FREE( info
->weight
);
418 /* release top dictionary */
419 FT_FREE( type1
->charstrings_len
);
420 FT_FREE( type1
->charstrings
);
421 FT_FREE( type1
->glyph_names
);
423 FT_FREE( type1
->charstrings_block
);
424 FT_FREE( type1
->glyph_names_block
);
426 FT_FREE( type1
->encoding
.char_index
);
427 FT_FREE( type1
->encoding
.char_name
);
428 FT_FREE( type1
->font_name
);
430 FT_FREE( face
->ttf_data
);
433 /* release afm data if present */
434 if ( face
->afm_data
)
435 T1_Done_AFM( memory
, (T1_AFM
*)face
->afm_data
);
438 /* release unicode map, if any */
439 FT_FREE( face
->unicode_map
.maps
);
440 face
->unicode_map
.num_maps
= 0;
442 face
->root
.family_name
= 0;
443 face
->root
.style_name
= 0;
447 /*************************************************************************/
450 /* T42_Driver_Init */
453 /* Initializes a given Type 42 driver object. */
456 /* driver :: A handle to the target driver object. */
459 /* FreeType error code. 0 means success. */
461 FT_LOCAL_DEF( FT_Error
)
462 T42_Driver_Init( T42_Driver driver
)
467 ttmodule
= FT_Get_Module( FT_MODULE(driver
)->library
, "truetype" );
468 driver
->ttclazz
= (FT_Driver_Class
)ttmodule
->clazz
;
475 T42_Driver_Done( T42_Driver driver
)
481 FT_LOCAL_DEF( FT_Error
)
482 T42_Size_Init( T42_Size size
)
484 FT_Face face
= size
->root
.face
;
485 T42_Face t42face
= (T42_Face
)face
;
487 FT_Error error
= T42_Err_Ok
;
490 error
= FT_New_Size( t42face
->ttf_face
, &ttsize
);
491 size
->ttsize
= ttsize
;
493 FT_Activate_Size( ttsize
);
499 FT_LOCAL_DEF( FT_Error
)
500 T42_Size_Request( T42_Size size
,
501 FT_Size_Request req
)
503 T42_Face face
= (T42_Face
)size
->root
.face
;
507 FT_Activate_Size( size
->ttsize
);
509 error
= FT_Request_Size( face
->ttf_face
, req
);
511 ( (FT_Size
)size
)->metrics
= face
->ttf_face
->size
->metrics
;
517 FT_LOCAL_DEF( FT_Error
)
518 T42_Size_Select( T42_Size size
,
519 FT_ULong strike_index
)
521 T42_Face face
= (T42_Face
)size
->root
.face
;
525 FT_Activate_Size( size
->ttsize
);
527 error
= FT_Select_Size( face
->ttf_face
, (FT_Int
)strike_index
);
529 ( (FT_Size
)size
)->metrics
= face
->ttf_face
->size
->metrics
;
537 T42_Size_Done( T42_Size size
)
539 FT_Face face
= size
->root
.face
;
540 T42_Face t42face
= (T42_Face
)face
;
544 node
= FT_List_Find( &t42face
->ttf_face
->sizes_list
, size
->ttsize
);
547 FT_Done_Size( size
->ttsize
);
553 FT_LOCAL_DEF( FT_Error
)
554 T42_GlyphSlot_Init( T42_GlyphSlot slot
)
556 FT_Face face
= slot
->root
.face
;
557 T42_Face t42face
= (T42_Face
)face
;
559 FT_Error error
= T42_Err_Ok
;
562 if ( face
->glyph
== NULL
)
564 /* First glyph slot for this face */
565 slot
->ttslot
= t42face
->ttf_face
->glyph
;
569 error
= FT_New_GlyphSlot( t42face
->ttf_face
, &ttslot
);
570 slot
->ttslot
= ttslot
;
578 T42_GlyphSlot_Done( T42_GlyphSlot slot
)
580 FT_Done_GlyphSlot( slot
->ttslot
);
585 t42_glyphslot_clear( FT_GlyphSlot slot
)
587 /* free bitmap if needed */
588 ft_glyphslot_free_bitmap( slot
);
590 /* clear all public fields in the glyph slot */
591 FT_ZERO( &slot
->metrics
);
592 FT_ZERO( &slot
->outline
);
593 FT_ZERO( &slot
->bitmap
);
595 slot
->bitmap_left
= 0;
596 slot
->bitmap_top
= 0;
597 slot
->num_subglyphs
= 0;
599 slot
->control_data
= 0;
600 slot
->control_len
= 0;
602 slot
->format
= FT_GLYPH_FORMAT_NONE
;
604 slot
->linearHoriAdvance
= 0;
605 slot
->linearVertAdvance
= 0;
609 FT_LOCAL_DEF( FT_Error
)
610 T42_GlyphSlot_Load( FT_GlyphSlot glyph
,
613 FT_Int32 load_flags
)
616 T42_GlyphSlot t42slot
= (T42_GlyphSlot
)glyph
;
617 T42_Size t42size
= (T42_Size
)size
;
618 FT_Driver_Class ttclazz
= ((T42_Driver
)glyph
->face
->driver
)->ttclazz
;
621 t42_glyphslot_clear( t42slot
->ttslot
);
622 error
= ttclazz
->load_glyph( t42slot
->ttslot
,
625 load_flags
| FT_LOAD_NO_BITMAP
);
629 glyph
->metrics
= t42slot
->ttslot
->metrics
;
631 glyph
->linearHoriAdvance
= t42slot
->ttslot
->linearHoriAdvance
;
632 glyph
->linearVertAdvance
= t42slot
->ttslot
->linearVertAdvance
;
634 glyph
->format
= t42slot
->ttslot
->format
;
635 glyph
->outline
= t42slot
->ttslot
->outline
;
637 glyph
->bitmap
= t42slot
->ttslot
->bitmap
;
638 glyph
->bitmap_left
= t42slot
->ttslot
->bitmap_left
;
639 glyph
->bitmap_top
= t42slot
->ttslot
->bitmap_top
;
641 glyph
->num_subglyphs
= t42slot
->ttslot
->num_subglyphs
;
642 glyph
->subglyphs
= t42slot
->ttslot
->subglyphs
;
644 glyph
->control_data
= t42slot
->ttslot
->control_data
;
645 glyph
->control_len
= t42slot
->ttslot
->control_len
;