1 /***************************************************************************/
5 /* Type 1 objects manager (body). */
7 /* Copyright 1996-2016 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( FT_Size t1size ) /* T1_Size */
77 T1_Size size = (T1_Size)t1size;
80 if ( size->root.internal )
82 PSH_Globals_Funcs funcs;
85 funcs = T1_Size_Get_Globals_Funcs( size );
87 funcs->destroy( (PSH_Globals)size->root.internal );
89 size->root.internal = NULL;
94 FT_LOCAL_DEF( FT_Error )
95 T1_Size_Init( FT_Size t1size ) /* T1_Size */
97 T1_Size size = (T1_Size)t1size;
98 FT_Error error = FT_Err_Ok;
99 PSH_Globals_Funcs funcs = T1_Size_Get_Globals_Funcs( size );
105 T1_Face face = (T1_Face)size->root.face;
108 error = funcs->create( size->root.face->memory,
109 &face->type1.private_dict, &globals );
111 size->root.internal = (FT_Size_Internal)(void*)globals;
118 FT_LOCAL_DEF( FT_Error )
119 T1_Size_Request( FT_Size t1size, /* T1_Size */
120 FT_Size_Request req )
122 T1_Size size = (T1_Size)t1size;
123 PSH_Globals_Funcs funcs = T1_Size_Get_Globals_Funcs( size );
126 FT_Request_Metrics( size->root.face, req );
129 funcs->set_scale( (PSH_Globals)size->root.internal,
130 size->root.metrics.x_scale,
131 size->root.metrics.y_scale,
138 /*************************************************************************/
142 /*************************************************************************/
145 T1_GlyphSlot_Done( FT_GlyphSlot slot )
147 slot->internal->glyph_hints = NULL;
151 FT_LOCAL_DEF( FT_Error )
152 T1_GlyphSlot_Init( FT_GlyphSlot slot )
155 PSHinter_Service pshinter;
158 face = (T1_Face)slot->face;
159 pshinter = (PSHinter_Service)face->pshinter;
166 module = FT_Get_Module( slot->face->driver->root.library,
170 T1_Hints_Funcs funcs;
173 funcs = pshinter->get_t1_funcs( module );
174 slot->internal->glyph_hints = (void*)funcs;
182 /*************************************************************************/
186 /*************************************************************************/
189 /*************************************************************************/
195 /* The face object destructor. */
198 /* face :: A typeless pointer to the face object to destroy. */
201 T1_Face_Done( FT_Face t1face ) /* T1_Face */
203 T1_Face face = (T1_Face)t1face;
211 memory = face->root.memory;
212 type1 = &face->type1;
214 #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
215 /* release multiple masters information */
216 FT_ASSERT( ( face->len_buildchar == 0 ) == ( face->buildchar == NULL ) );
218 if ( face->buildchar )
220 FT_FREE( face->buildchar );
222 face->buildchar = NULL;
223 face->len_buildchar = 0;
226 T1_Done_Blend( face );
230 /* release font info strings */
232 PS_FontInfo info = &type1->font_info;
235 FT_FREE( info->version );
236 FT_FREE( info->notice );
237 FT_FREE( info->full_name );
238 FT_FREE( info->family_name );
239 FT_FREE( info->weight );
242 /* release top dictionary */
243 FT_FREE( type1->charstrings_len );
244 FT_FREE( type1->charstrings );
245 FT_FREE( type1->glyph_names );
247 FT_FREE( type1->subrs );
248 FT_FREE( type1->subrs_len );
250 ft_hash_num_free( type1->subrs_hash, memory );
251 FT_FREE( type1->subrs_hash );
253 FT_FREE( type1->subrs_block );
254 FT_FREE( type1->charstrings_block );
255 FT_FREE( type1->glyph_names_block );
257 FT_FREE( type1->encoding.char_index );
258 FT_FREE( type1->encoding.char_name );
259 FT_FREE( type1->font_name );
261 #ifndef T1_CONFIG_OPTION_NO_AFM
262 /* release afm data if present */
263 if ( face->afm_data )
264 T1_Done_Metrics( memory, (AFM_FontInfo)face->afm_data );
267 /* release unicode map, if any */
269 FT_FREE( face->unicode_map_rec.maps );
270 face->unicode_map_rec.num_maps = 0;
271 face->unicode_map = NULL;
274 face->root.family_name = NULL;
275 face->root.style_name = NULL;
279 /*************************************************************************/
285 /* The face object constructor. */
288 /* stream :: input stream where to load font data. */
290 /* face_index :: The index of the font face in the resource. */
292 /* num_params :: Number of additional generic parameters. Ignored. */
294 /* params :: Additional generic parameters. Ignored. */
297 /* face :: The face record to build. */
300 /* FreeType error code. 0 means success. */
302 FT_LOCAL_DEF( FT_Error )
303 T1_Face_Init( FT_Stream stream,
304 FT_Face t1face, /* T1_Face */
307 FT_Parameter* params )
309 T1_Face face = (T1_Face)t1face;
311 FT_Service_PsCMaps psnames;
313 T1_Font type1 = &face->type1;
314 PS_FontInfo info = &type1->font_info;
316 FT_UNUSED( num_params );
321 face->root.num_faces = 1;
323 FT_FACE_FIND_GLOBAL_SERVICE( face, psnames, POSTSCRIPT_CMAPS );
324 face->psnames = psnames;
326 face->psaux = FT_Get_Module_Interface( FT_FACE_LIBRARY( face ),
328 psaux = (PSAux_Service)face->psaux;
331 FT_ERROR(( "T1_Face_Init: cannot access `psaux' module\n" ));
332 error = FT_THROW( Missing_Module );
336 face->pshinter = FT_Get_Module_Interface( FT_FACE_LIBRARY( face ),
339 FT_TRACE2(( "Type 1 driver\n" ));
341 /* open the tokenizer; this will also check the font format */
342 error = T1_Open_Face( face );
346 /* if we just wanted to check the format, leave successfully now */
347 if ( face_index < 0 )
350 /* check the face index */
351 if ( ( face_index & 0xFFFF ) > 0 )
353 FT_ERROR(( "T1_Face_Init: invalid face index\n" ));
354 error = FT_THROW( Invalid_Argument );
358 /* now load the font program into the face object */
360 /* initialize the face object fields */
362 /* set up root face fields */
364 FT_Face root = (FT_Face)&face->root;
367 root->num_glyphs = type1->num_glyphs;
368 root->face_index = 0;
370 root->face_flags |= FT_FACE_FLAG_SCALABLE |
371 FT_FACE_FLAG_HORIZONTAL |
372 FT_FACE_FLAG_GLYPH_NAMES |
375 if ( info->is_fixed_pitch )
376 root->face_flags |= FT_FACE_FLAG_FIXED_WIDTH;
379 root->face_flags |= FT_FACE_FLAG_MULTIPLE_MASTERS;
381 /* The following code to extract the family and the style is very */
382 /* simplistic and might get some things wrong. For a full-featured */
383 /* algorithm you might have a look at the whitepaper given at */
385 /* http://blogs.msdn.com/text/archive/2007/04/23/wpf-font-selection-model.aspx */
387 /* get style name -- be careful, some broken fonts only */
388 /* have a `/FontName' dictionary entry! */
389 root->family_name = info->family_name;
390 root->style_name = NULL;
392 if ( root->family_name )
394 char* full = info->full_name;
395 char* family = root->family_name;
400 FT_Bool the_same = TRUE;
405 if ( *full == *family )
412 if ( *full == ' ' || *full == '-' )
414 else if ( *family == ' ' || *family == '-' )
421 root->style_name = full;
428 root->style_name = (char *)"Regular";
433 /* do we have a `/FontName'? */
434 if ( type1->font_name )
435 root->family_name = type1->font_name;
438 if ( !root->style_name )
441 root->style_name = info->weight;
443 /* assume `Regular' style because we don't know better */
444 root->style_name = (char *)"Regular";
447 /* compute style flags */
448 root->style_flags = 0;
449 if ( info->italic_angle )
450 root->style_flags |= FT_STYLE_FLAG_ITALIC;
453 if ( !ft_strcmp( info->weight, "Bold" ) ||
454 !ft_strcmp( info->weight, "Black" ) )
455 root->style_flags |= FT_STYLE_FLAG_BOLD;
458 /* no embedded bitmap support */
459 root->num_fixed_sizes = 0;
460 root->available_sizes = NULL;
462 root->bbox.xMin = type1->font_bbox.xMin >> 16;
463 root->bbox.yMin = type1->font_bbox.yMin >> 16;
464 /* no `U' suffix here to 0xFFFF! */
465 root->bbox.xMax = ( type1->font_bbox.xMax + 0xFFFF ) >> 16;
466 root->bbox.yMax = ( type1->font_bbox.yMax + 0xFFFF ) >> 16;
468 /* Set units_per_EM if we didn't set it in t1_parse_font_matrix. */
469 if ( !root->units_per_EM )
470 root->units_per_EM = 1000;
472 root->ascender = (FT_Short)( root->bbox.yMax );
473 root->descender = (FT_Short)( root->bbox.yMin );
475 root->height = (FT_Short)( ( root->units_per_EM * 12 ) / 10 );
476 if ( root->height < root->ascender - root->descender )
477 root->height = (FT_Short)( root->ascender - root->descender );
479 /* now compute the maximum advance width */
480 root->max_advance_width =
481 (FT_Short)( root->bbox.xMax );
486 error = T1_Compute_Max_Advance( face, &max_advance );
488 /* in case of error, keep the standard width */
490 root->max_advance_width = (FT_Short)FIXED_TO_INT( max_advance );
492 error = FT_Err_Ok; /* clear error */
495 root->max_advance_height = root->height;
497 root->underline_position = (FT_Short)info->underline_position;
498 root->underline_thickness = (FT_Short)info->underline_thickness;
502 FT_Face root = &face->root;
507 FT_CharMapRec charmap;
508 T1_CMap_Classes cmap_classes = psaux->t1_cmap_classes;
514 /* first of all, try to synthesize a Unicode charmap */
515 charmap.platform_id = TT_PLATFORM_MICROSOFT;
516 charmap.encoding_id = TT_MS_ID_UNICODE_CS;
517 charmap.encoding = FT_ENCODING_UNICODE;
519 error = FT_CMap_New( cmap_classes->unicode, NULL, &charmap, NULL );
521 FT_ERR_NEQ( error, No_Unicode_Glyph_Name ) )
525 /* now, generate an Adobe Standard encoding when appropriate */
526 charmap.platform_id = TT_PLATFORM_ADOBE;
529 switch ( type1->encoding_type )
531 case T1_ENCODING_TYPE_STANDARD:
532 charmap.encoding = FT_ENCODING_ADOBE_STANDARD;
533 charmap.encoding_id = TT_ADOBE_ID_STANDARD;
534 clazz = cmap_classes->standard;
537 case T1_ENCODING_TYPE_EXPERT:
538 charmap.encoding = FT_ENCODING_ADOBE_EXPERT;
539 charmap.encoding_id = TT_ADOBE_ID_EXPERT;
540 clazz = cmap_classes->expert;
543 case T1_ENCODING_TYPE_ARRAY:
544 charmap.encoding = FT_ENCODING_ADOBE_CUSTOM;
545 charmap.encoding_id = TT_ADOBE_ID_CUSTOM;
546 clazz = cmap_classes->custom;
549 case T1_ENCODING_TYPE_ISOLATIN1:
550 charmap.encoding = FT_ENCODING_ADOBE_LATIN_1;
551 charmap.encoding_id = TT_ADOBE_ID_LATIN_1;
552 clazz = cmap_classes->unicode;
560 error = FT_CMap_New( clazz, NULL, &charmap, NULL );
563 /* Select default charmap */
564 if (root->num_charmaps)
565 root->charmap = root->charmaps[0];
575 /*************************************************************************/
581 /* Initializes a given Type 1 driver object. */
584 /* driver :: A handle to the target driver object. */
587 /* FreeType error code. 0 means success. */
589 FT_LOCAL_DEF( FT_Error )
590 T1_Driver_Init( FT_Module driver )
598 /*************************************************************************/
604 /* Finalizes a given Type 1 driver. */
607 /* driver :: A handle to the target Type 1 driver. */
610 T1_Driver_Done( FT_Module driver )