1 /****************************************************************************
5 * CID objects manager (body).
7 * Copyright (C) 1996-2020 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.
19 #include <freetype/internal/ftdebug.h>
20 #include <freetype/internal/ftstream.h>
25 #include <freetype/internal/services/svpscmap.h>
26 #include <freetype/internal/psaux.h>
27 #include <freetype/internal/pshints.h>
28 #include <freetype/ftdriver.h>
33 /**************************************************************************
35 * The macro FT_COMPONENT is used in trace mode. It is an implicit
36 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
37 * messages during execution.
40 #define FT_COMPONENT cidobjs
43 /**************************************************************************
50 cid_slot_done( FT_GlyphSlot slot )
53 slot->internal->glyph_hints = NULL;
57 FT_LOCAL_DEF( FT_Error )
58 cid_slot_init( FT_GlyphSlot slot )
61 PSHinter_Service pshinter;
64 face = (CID_Face)slot->face;
65 pshinter = (PSHinter_Service)face->pshinter;
72 module = FT_Get_Module( slot->face->driver->root.library,
79 funcs = pshinter->get_t1_funcs( module );
80 slot->internal->glyph_hints = (void*)funcs;
88 /**************************************************************************
95 static PSH_Globals_Funcs
96 cid_size_get_globals_funcs( CID_Size size )
98 CID_Face face = (CID_Face)size->root.face;
99 PSHinter_Service pshinter = (PSHinter_Service)face->pshinter;
103 module = FT_Get_Module( size->root.face->driver->root.library,
105 return ( module && pshinter && pshinter->get_globals_funcs )
106 ? pshinter->get_globals_funcs( module )
112 cid_size_done( FT_Size cidsize ) /* CID_Size */
114 CID_Size size = (CID_Size)cidsize;
117 if ( cidsize->internal->module_data )
119 PSH_Globals_Funcs funcs;
122 funcs = cid_size_get_globals_funcs( size );
124 funcs->destroy( (PSH_Globals)cidsize->internal->module_data );
126 cidsize->internal->module_data = NULL;
131 FT_LOCAL_DEF( FT_Error )
132 cid_size_init( FT_Size cidsize ) /* CID_Size */
134 CID_Size size = (CID_Size)cidsize;
135 FT_Error error = FT_Err_Ok;
136 PSH_Globals_Funcs funcs = cid_size_get_globals_funcs( size );
142 CID_Face face = (CID_Face)cidsize->face;
143 CID_FaceDict dict = face->cid.font_dicts + face->root.face_index;
144 PS_Private priv = &dict->private_dict;
147 error = funcs->create( cidsize->face->memory, priv, &globals );
149 cidsize->internal->module_data = globals;
157 cid_size_request( FT_Size size,
158 FT_Size_Request req )
160 PSH_Globals_Funcs funcs;
163 FT_Request_Metrics( size->face, req );
165 funcs = cid_size_get_globals_funcs( (CID_Size)size );
168 funcs->set_scale( (PSH_Globals)size->internal->module_data,
169 size->metrics.x_scale,
170 size->metrics.y_scale,
177 /**************************************************************************
183 /**************************************************************************
189 * Finalizes a given face object.
193 * A pointer to the face object to destroy.
196 cid_face_done( FT_Face cidface ) /* CID_Face */
198 CID_Face face = (CID_Face)cidface;
208 info = &cid->font_info;
209 memory = cidface->memory;
217 for ( n = 0; n < cid->num_dicts; n++ )
219 CID_Subrs subr = face->subrs + n;
224 FT_FREE( subr->code[0] );
225 FT_FREE( subr->code );
229 FT_FREE( face->subrs );
232 /* release FontInfo strings */
233 FT_FREE( info->version );
234 FT_FREE( info->notice );
235 FT_FREE( info->full_name );
236 FT_FREE( info->family_name );
237 FT_FREE( info->weight );
239 /* release font dictionaries */
240 FT_FREE( cid->font_dicts );
243 /* release other strings */
244 FT_FREE( cid->cid_font_name );
245 FT_FREE( cid->registry );
246 FT_FREE( cid->ordering );
248 cidface->family_name = NULL;
249 cidface->style_name = NULL;
251 FT_FREE( face->binary_data );
252 FT_FREE( face->cid_stream );
256 /**************************************************************************
262 * Initializes a given CID face object.
266 * The source font stream.
269 * The index of the font face in the resource.
272 * Number of additional generic parameters. Ignored.
275 * Additional generic parameters. Ignored.
279 * The newly built face object.
282 * FreeType error code. 0 means success.
284 FT_LOCAL_DEF( FT_Error )
285 cid_face_init( FT_Stream stream,
286 FT_Face cidface, /* CID_Face */
289 FT_Parameter* params )
291 CID_Face face = (CID_Face)cidface;
294 PSHinter_Service pshinter;
296 FT_UNUSED( num_params );
301 cidface->num_faces = 1;
303 psaux = (PSAux_Service)face->psaux;
306 psaux = (PSAux_Service)FT_Get_Module_Interface(
307 FT_FACE_LIBRARY( face ), "psaux" );
311 FT_ERROR(( "cid_face_init: cannot access `psaux' module\n" ));
312 error = FT_THROW( Missing_Module );
319 pshinter = (PSHinter_Service)face->pshinter;
322 pshinter = (PSHinter_Service)FT_Get_Module_Interface(
323 FT_FACE_LIBRARY( face ), "pshinter" );
325 face->pshinter = pshinter;
328 FT_TRACE2(( "CID driver\n" ));
330 /* open the tokenizer; this will also check the font format */
331 if ( FT_STREAM_SEEK( 0 ) )
334 error = cid_face_open( face, face_index );
338 /* if we just wanted to check the format, leave successfully now */
339 if ( face_index < 0 )
342 /* check the face index */
343 /* XXX: handle CID fonts with more than a single face */
344 if ( ( face_index & 0xFFFF ) != 0 )
346 FT_ERROR(( "cid_face_init: invalid face index\n" ));
347 error = FT_THROW( Invalid_Argument );
351 /* now load the font program into the face object */
353 /* initialize the face object fields */
355 /* set up root face fields */
357 CID_FaceInfo cid = &face->cid;
358 PS_FontInfo info = &cid->font_info;
361 cidface->num_glyphs = (FT_Long)cid->cid_count;
362 cidface->num_charmaps = 0;
364 cidface->face_index = face_index & 0xFFFF;
366 cidface->face_flags |= FT_FACE_FLAG_SCALABLE | /* scalable outlines */
367 FT_FACE_FLAG_HORIZONTAL | /* horizontal data */
368 FT_FACE_FLAG_HINTER; /* has native hinter */
370 if ( info->is_fixed_pitch )
371 cidface->face_flags |= FT_FACE_FLAG_FIXED_WIDTH;
373 /* XXX: TODO: add kerning with .afm support */
375 /* get style name -- be careful, some broken fonts only */
376 /* have a /FontName dictionary entry! */
377 cidface->family_name = info->family_name;
378 /* assume "Regular" style if we don't know better */
379 cidface->style_name = (char *)"Regular";
380 if ( cidface->family_name )
382 char* full = info->full_name;
383 char* family = cidface->family_name;
390 if ( *full == *family )
397 if ( *full == ' ' || *full == '-' )
399 else if ( *family == ' ' || *family == '-' )
404 cidface->style_name = full;
413 /* do we have a `/FontName'? */
414 if ( cid->cid_font_name )
415 cidface->family_name = cid->cid_font_name;
418 /* compute style flags */
419 cidface->style_flags = 0;
420 if ( info->italic_angle )
421 cidface->style_flags |= FT_STYLE_FLAG_ITALIC;
424 if ( !ft_strcmp( info->weight, "Bold" ) ||
425 !ft_strcmp( info->weight, "Black" ) )
426 cidface->style_flags |= FT_STYLE_FLAG_BOLD;
429 /* no embedded bitmap support */
430 cidface->num_fixed_sizes = 0;
431 cidface->available_sizes = NULL;
433 cidface->bbox.xMin = cid->font_bbox.xMin >> 16;
434 cidface->bbox.yMin = cid->font_bbox.yMin >> 16;
435 /* no `U' suffix here to 0xFFFF! */
436 cidface->bbox.xMax = ( cid->font_bbox.xMax + 0xFFFF ) >> 16;
437 cidface->bbox.yMax = ( cid->font_bbox.yMax + 0xFFFF ) >> 16;
439 if ( !cidface->units_per_EM )
440 cidface->units_per_EM = 1000;
442 cidface->ascender = (FT_Short)( cidface->bbox.yMax );
443 cidface->descender = (FT_Short)( cidface->bbox.yMin );
445 cidface->height = (FT_Short)( ( cidface->units_per_EM * 12 ) / 10 );
446 if ( cidface->height < cidface->ascender - cidface->descender )
447 cidface->height = (FT_Short)( cidface->ascender - cidface->descender );
449 cidface->underline_position = (FT_Short)info->underline_position;
450 cidface->underline_thickness = (FT_Short)info->underline_thickness;
458 /**************************************************************************
464 * Initializes a given CID driver object.
468 * A handle to the target driver object.
471 * FreeType error code. 0 means success.
473 FT_LOCAL_DEF( FT_Error )
474 cid_driver_init( FT_Module module )
476 PS_Driver driver = (PS_Driver)module;
481 /* set default property values, cf. `ftt1drv.h' */
482 #ifdef T1_CONFIG_OPTION_OLD_ENGINE
483 driver->hinting_engine = FT_HINTING_FREETYPE;
485 driver->hinting_engine = FT_HINTING_ADOBE;
488 driver->no_stem_darkening = TRUE;
490 driver->darken_params[0] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X1;
491 driver->darken_params[1] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y1;
492 driver->darken_params[2] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X2;
493 driver->darken_params[3] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y2;
494 driver->darken_params[4] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X3;
495 driver->darken_params[5] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y3;
496 driver->darken_params[6] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X4;
497 driver->darken_params[7] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y4;
499 /* compute random seed from some memory addresses */
500 seed = (FT_UInt32)( (FT_Offset)(char*)&seed ^
501 (FT_Offset)(char*)&module ^
502 (FT_Offset)(char*)module->memory );
503 seed = seed ^ ( seed >> 10 ) ^ ( seed >> 20 );
505 driver->random_seed = (FT_Int32)seed;
506 if ( driver->random_seed < 0 )
507 driver->random_seed = -driver->random_seed;
508 else if ( driver->random_seed == 0 )
509 driver->random_seed = 123456789;
515 /**************************************************************************
521 * Finalizes a given CID driver.
525 * A handle to the target CID driver.
528 cid_driver_done( FT_Module driver )