1 /***************************************************************************/
5 /* Type 1 objects manager (body). */
7 /* Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
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 /***************************************************************************/
20 #include FT_INTERNAL_CALC_H
21 #include FT_INTERNAL_DEBUG_H
22 #include FT_INTERNAL_STREAM_H
23 #include FT_TRUETYPE_IDS_H
30 #ifndef T1_CONFIG_OPTION_NO_AFM
34 #include FT_SERVICE_POSTSCRIPT_CMAPS_H
35 #include FT_INTERNAL_POSTSCRIPT_AUX_H
38 /*************************************************************************/
40 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
41 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
42 /* messages during execution. */
45 #define FT_COMPONENT trace_t1objs
48 /*************************************************************************/
52 /* note that we store the global hints in the size's "internal" root */
55 /*************************************************************************/
58 static PSH_Globals_Funcs
59 T1_Size_Get_Globals_Funcs( T1_Size size
)
61 T1_Face face
= (T1_Face
)size
->root
.face
;
62 PSHinter_Service pshinter
= (PSHinter_Service
)face
->pshinter
;
66 module
= FT_Get_Module( size
->root
.face
->driver
->root
.library
,
68 return ( module
&& pshinter
&& pshinter
->get_globals_funcs
)
69 ? pshinter
->get_globals_funcs( module
)
75 T1_Size_Done( T1_Size size
)
77 if ( size
->root
.internal
)
79 PSH_Globals_Funcs funcs
;
82 funcs
= T1_Size_Get_Globals_Funcs( size
);
84 funcs
->destroy( (PSH_Globals
)size
->root
.internal
);
86 size
->root
.internal
= 0;
91 FT_LOCAL_DEF( FT_Error
)
92 T1_Size_Init( T1_Size size
)
94 FT_Error error
= T1_Err_Ok
;
95 PSH_Globals_Funcs funcs
= T1_Size_Get_Globals_Funcs( size
);
101 T1_Face face
= (T1_Face
)size
->root
.face
;
104 error
= funcs
->create( size
->root
.face
->memory
,
105 &face
->type1
.private_dict
, &globals
);
107 size
->root
.internal
= (FT_Size_Internal
)(void*)globals
;
114 FT_LOCAL_DEF( FT_Error
)
115 T1_Size_Request( T1_Size size
,
116 FT_Size_Request req
)
118 PSH_Globals_Funcs funcs
= T1_Size_Get_Globals_Funcs( size
);
121 FT_Request_Metrics( size
->root
.face
, req
);
124 funcs
->set_scale( (PSH_Globals
)size
->root
.internal
,
125 size
->root
.metrics
.x_scale
,
126 size
->root
.metrics
.y_scale
,
133 /*************************************************************************/
137 /*************************************************************************/
140 T1_GlyphSlot_Done( T1_GlyphSlot slot
)
142 slot
->root
.internal
->glyph_hints
= 0;
146 FT_LOCAL_DEF( FT_Error
)
147 T1_GlyphSlot_Init( T1_GlyphSlot slot
)
150 PSHinter_Service pshinter
;
153 face
= (T1_Face
)slot
->root
.face
;
154 pshinter
= (PSHinter_Service
)face
->pshinter
;
161 module
= FT_Get_Module( slot
->root
.face
->driver
->root
.library
, "pshinter" );
164 T1_Hints_Funcs funcs
;
166 funcs
= pshinter
->get_t1_funcs( module
);
167 slot
->root
.internal
->glyph_hints
= (void*)funcs
;
174 /*************************************************************************/
178 /*************************************************************************/
181 /*************************************************************************/
187 /* The face object destructor. */
190 /* face :: A typeless pointer to the face object to destroy. */
193 T1_Face_Done( T1_Face face
)
202 memory
= face
->root
.memory
;
203 type1
= &face
->type1
;
205 #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
206 /* release multiple masters information */
207 FT_ASSERT( ( face
->len_buildchar
== 0 ) == ( face
->buildchar
== NULL
) );
209 if ( face
->buildchar
)
211 FT_FREE( face
->buildchar
);
213 face
->buildchar
= NULL
;
214 face
->len_buildchar
= 0;
217 T1_Done_Blend( face
);
221 /* release font info strings */
223 PS_FontInfo info
= &type1
->font_info
;
226 FT_FREE( info
->version
);
227 FT_FREE( info
->notice
);
228 FT_FREE( info
->full_name
);
229 FT_FREE( info
->family_name
);
230 FT_FREE( info
->weight
);
233 /* release top dictionary */
234 FT_FREE( type1
->charstrings_len
);
235 FT_FREE( type1
->charstrings
);
236 FT_FREE( type1
->glyph_names
);
238 FT_FREE( type1
->subrs
);
239 FT_FREE( type1
->subrs_len
);
241 FT_FREE( type1
->subrs_block
);
242 FT_FREE( type1
->charstrings_block
);
243 FT_FREE( type1
->glyph_names_block
);
245 FT_FREE( type1
->encoding
.char_index
);
246 FT_FREE( type1
->encoding
.char_name
);
247 FT_FREE( type1
->font_name
);
249 #ifndef T1_CONFIG_OPTION_NO_AFM
250 /* release afm data if present */
251 if ( face
->afm_data
)
252 T1_Done_Metrics( memory
, (AFM_FontInfo
)face
->afm_data
);
255 /* release unicode map, if any */
257 FT_FREE( face
->unicode_map_rec
.maps
);
258 face
->unicode_map_rec
.num_maps
= 0;
259 face
->unicode_map
= NULL
;
262 face
->root
.family_name
= NULL
;
263 face
->root
.style_name
= NULL
;
267 /*************************************************************************/
273 /* The face object constructor. */
276 /* stream :: input stream where to load font data. */
278 /* face_index :: The index of the font face in the resource. */
280 /* num_params :: Number of additional generic parameters. Ignored. */
282 /* params :: Additional generic parameters. Ignored. */
285 /* face :: The face record to build. */
288 /* FreeType error code. 0 means success. */
290 FT_LOCAL_DEF( FT_Error
)
291 T1_Face_Init( FT_Stream stream
,
295 FT_Parameter
* params
)
298 FT_Service_PsCMaps psnames
;
300 T1_Font type1
= &face
->type1
;
301 PS_FontInfo info
= &type1
->font_info
;
303 FT_UNUSED( num_params
);
308 face
->root
.num_faces
= 1;
310 FT_FACE_FIND_GLOBAL_SERVICE( face
, psnames
, POSTSCRIPT_CMAPS
);
311 face
->psnames
= psnames
;
313 face
->psaux
= FT_Get_Module_Interface( FT_FACE_LIBRARY( face
),
315 psaux
= (PSAux_Service
)face
->psaux
;
317 face
->pshinter
= FT_Get_Module_Interface( FT_FACE_LIBRARY( face
),
320 /* open the tokenizer; this will also check the font format */
321 error
= T1_Open_Face( face
);
325 /* if we just wanted to check the format, leave successfully now */
326 if ( face_index
< 0 )
329 /* check the face index */
330 if ( face_index
> 0 )
332 FT_ERROR(( "T1_Face_Init: invalid face index\n" ));
333 error
= T1_Err_Invalid_Argument
;
337 /* now load the font program into the face object */
339 /* initialize the face object fields */
341 /* set up root face fields */
343 FT_Face root
= (FT_Face
)&face
->root
;
346 root
->num_glyphs
= type1
->num_glyphs
;
347 root
->face_index
= 0;
349 root
->face_flags
= FT_FACE_FLAG_SCALABLE
|
350 FT_FACE_FLAG_HORIZONTAL
|
351 FT_FACE_FLAG_GLYPH_NAMES
|
354 if ( info
->is_fixed_pitch
)
355 root
->face_flags
|= FT_FACE_FLAG_FIXED_WIDTH
;
358 root
->face_flags
|= FT_FACE_FLAG_MULTIPLE_MASTERS
;
360 /* XXX: TODO -- add kerning with .afm support */
363 /* The following code to extract the family and the style is very */
364 /* simplistic and might get some things wrong. For a full-featured */
365 /* algorithm you might have a look at the whitepaper given at */
367 /* http://blogs.msdn.com/text/archive/2007/04/23/wpf-font-selection-model.aspx */
369 /* get style name -- be careful, some broken fonts only */
370 /* have a `/FontName' dictionary entry! */
371 root
->family_name
= info
->family_name
;
372 root
->style_name
= NULL
;
374 if ( root
->family_name
)
376 char* full
= info
->full_name
;
377 char* family
= root
->family_name
;
382 FT_Bool the_same
= TRUE
;
387 if ( *full
== *family
)
394 if ( *full
== ' ' || *full
== '-' )
396 else if ( *family
== ' ' || *family
== '-' )
403 root
->style_name
= full
;
410 root
->style_name
= (char *)"Regular";
415 /* do we have a `/FontName'? */
416 if ( type1
->font_name
)
417 root
->family_name
= type1
->font_name
;
420 if ( !root
->style_name
)
423 root
->style_name
= info
->weight
;
425 /* assume `Regular' style because we don't know better */
426 root
->style_name
= (char *)"Regular";
429 /* compute style flags */
430 root
->style_flags
= 0;
431 if ( info
->italic_angle
)
432 root
->style_flags
|= FT_STYLE_FLAG_ITALIC
;
435 if ( !ft_strcmp( info
->weight
, "Bold" ) ||
436 !ft_strcmp( info
->weight
, "Black" ) )
437 root
->style_flags
|= FT_STYLE_FLAG_BOLD
;
440 /* no embedded bitmap support */
441 root
->num_fixed_sizes
= 0;
442 root
->available_sizes
= 0;
444 root
->bbox
.xMin
= type1
->font_bbox
.xMin
>> 16;
445 root
->bbox
.yMin
= type1
->font_bbox
.yMin
>> 16;
446 /* no `U' suffix here to 0xFFFF! */
447 root
->bbox
.xMax
= ( type1
->font_bbox
.xMax
+ 0xFFFF ) >> 16;
448 root
->bbox
.yMax
= ( type1
->font_bbox
.yMax
+ 0xFFFF ) >> 16;
450 /* Set units_per_EM if we didn't set it in parse_font_matrix. */
451 if ( !root
->units_per_EM
)
452 root
->units_per_EM
= 1000;
454 root
->ascender
= (FT_Short
)( root
->bbox
.yMax
);
455 root
->descender
= (FT_Short
)( root
->bbox
.yMin
);
457 root
->height
= (FT_Short
)( ( root
->units_per_EM
* 12 ) / 10 );
458 if ( root
->height
< root
->ascender
- root
->descender
)
459 root
->height
= (FT_Short
)( root
->ascender
- root
->descender
);
461 /* now compute the maximum advance width */
462 root
->max_advance_width
=
463 (FT_Short
)( root
->bbox
.xMax
);
468 error
= T1_Compute_Max_Advance( face
, &max_advance
);
470 /* in case of error, keep the standard width */
472 root
->max_advance_width
= (FT_Short
)FIXED_TO_INT( max_advance
);
474 error
= T1_Err_Ok
; /* clear error */
477 root
->max_advance_height
= root
->height
;
479 root
->underline_position
= (FT_Short
)info
->underline_position
;
480 root
->underline_thickness
= (FT_Short
)info
->underline_thickness
;
484 FT_Face root
= &face
->root
;
487 if ( psnames
&& psaux
)
489 FT_CharMapRec charmap
;
490 T1_CMap_Classes cmap_classes
= psaux
->t1_cmap_classes
;
496 /* first of all, try to synthesize a Unicode charmap */
497 charmap
.platform_id
= 3;
498 charmap
.encoding_id
= 1;
499 charmap
.encoding
= FT_ENCODING_UNICODE
;
501 FT_CMap_New( cmap_classes
->unicode
, NULL
, &charmap
, NULL
);
503 /* now, generate an Adobe Standard encoding when appropriate */
504 charmap
.platform_id
= 7;
507 switch ( type1
->encoding_type
)
509 case T1_ENCODING_TYPE_STANDARD
:
510 charmap
.encoding
= FT_ENCODING_ADOBE_STANDARD
;
511 charmap
.encoding_id
= TT_ADOBE_ID_STANDARD
;
512 clazz
= cmap_classes
->standard
;
515 case T1_ENCODING_TYPE_EXPERT
:
516 charmap
.encoding
= FT_ENCODING_ADOBE_EXPERT
;
517 charmap
.encoding_id
= TT_ADOBE_ID_EXPERT
;
518 clazz
= cmap_classes
->expert
;
521 case T1_ENCODING_TYPE_ARRAY
:
522 charmap
.encoding
= FT_ENCODING_ADOBE_CUSTOM
;
523 charmap
.encoding_id
= TT_ADOBE_ID_CUSTOM
;
524 clazz
= cmap_classes
->custom
;
527 case T1_ENCODING_TYPE_ISOLATIN1
:
528 charmap
.encoding
= FT_ENCODING_ADOBE_LATIN_1
;
529 charmap
.encoding_id
= TT_ADOBE_ID_LATIN_1
;
530 clazz
= cmap_classes
->unicode
;
538 FT_CMap_New( clazz
, NULL
, &charmap
, NULL
);
541 /* Select default charmap */
542 if (root
->num_charmaps
)
543 root
->charmap
= root
->charmaps
[0];
553 /*************************************************************************/
559 /* Initializes a given Type 1 driver object. */
562 /* driver :: A handle to the target driver object. */
565 /* FreeType error code. 0 means success. */
567 FT_LOCAL_DEF( FT_Error
)
568 T1_Driver_Init( T1_Driver driver
)
576 /*************************************************************************/
582 /* Finalizes a given Type 1 driver. */
585 /* driver :: A handle to the target Type 1 driver. */
588 T1_Driver_Done( T1_Driver driver
)