1 /****************************************************************************
5 * CID objects manager (body).
7 * Copyright (C) 1996-2023 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->library, "pshinter" );
78 funcs = pshinter->get_t1_funcs( module );
79 slot->internal->glyph_hints = (void*)funcs;
87 /**************************************************************************
94 static PSH_Globals_Funcs
95 cid_size_get_globals_funcs( CID_Size size )
97 CID_Face face = (CID_Face)size->root.face;
98 PSHinter_Service pshinter = (PSHinter_Service)face->pshinter;
102 module = FT_Get_Module( size->root.face->driver->root.library,
104 return ( module && pshinter && pshinter->get_globals_funcs )
105 ? pshinter->get_globals_funcs( module )
111 cid_size_done( FT_Size cidsize ) /* CID_Size */
113 CID_Size size = (CID_Size)cidsize;
116 if ( cidsize->internal->module_data )
118 PSH_Globals_Funcs funcs;
121 funcs = cid_size_get_globals_funcs( size );
123 funcs->destroy( (PSH_Globals)cidsize->internal->module_data );
125 cidsize->internal->module_data = NULL;
130 FT_LOCAL_DEF( FT_Error )
131 cid_size_init( FT_Size cidsize ) /* CID_Size */
133 CID_Size size = (CID_Size)cidsize;
134 FT_Error error = FT_Err_Ok;
135 PSH_Globals_Funcs funcs = cid_size_get_globals_funcs( size );
141 CID_Face face = (CID_Face)cidsize->face;
142 CID_FaceDict dict = face->cid.font_dicts + face->root.face_index;
143 PS_Private priv = &dict->private_dict;
146 error = funcs->create( cidsize->face->memory, priv, &globals );
148 cidsize->internal->module_data = globals;
155 FT_LOCAL_DEF( FT_Error )
156 cid_size_request( FT_Size size,
157 FT_Size_Request req )
161 PSH_Globals_Funcs funcs;
164 error = FT_Request_Metrics( size->face, req );
168 funcs = cid_size_get_globals_funcs( (CID_Size)size );
171 funcs->set_scale( (PSH_Globals)size->internal->module_data,
172 size->metrics.x_scale,
173 size->metrics.y_scale,
181 /**************************************************************************
187 /**************************************************************************
193 * Finalizes a given face object.
197 * A pointer to the face object to destroy.
200 cid_face_done( FT_Face cidface ) /* CID_Face */
202 CID_Face face = (CID_Face)cidface;
212 info = &cid->font_info;
213 memory = cidface->memory;
221 for ( n = 0; n < cid->num_dicts; n++ )
223 CID_Subrs subr = face->subrs + n;
228 FT_FREE( subr->code[0] );
229 FT_FREE( subr->code );
233 FT_FREE( face->subrs );
236 /* release FontInfo strings */
237 FT_FREE( info->version );
238 FT_FREE( info->notice );
239 FT_FREE( info->full_name );
240 FT_FREE( info->family_name );
241 FT_FREE( info->weight );
243 /* release font dictionaries */
244 FT_FREE( cid->font_dicts );
247 /* release other strings */
248 FT_FREE( cid->cid_font_name );
249 FT_FREE( cid->registry );
250 FT_FREE( cid->ordering );
252 cidface->family_name = NULL;
253 cidface->style_name = NULL;
255 FT_FREE( face->binary_data );
256 FT_FREE( face->cid_stream );
260 /**************************************************************************
266 * Initializes a given CID face object.
270 * Dummy argument for compatibility with the `FT_Face_InitFunc` API.
271 * Ignored. The stream should be passed through `face->root.stream`.
274 * The index of the font face in the resource.
277 * Number of additional generic parameters. Ignored.
280 * Additional generic parameters. Ignored.
284 * The newly built face object.
287 * FreeType error code. 0 means success.
289 FT_LOCAL_DEF( FT_Error )
290 cid_face_init( FT_Stream stream,
291 FT_Face cidface, /* CID_Face */
294 FT_Parameter* params )
296 CID_Face face = (CID_Face)cidface;
299 PSHinter_Service pshinter;
301 FT_UNUSED( num_params );
306 cidface->num_faces = 1;
308 psaux = (PSAux_Service)face->psaux;
311 psaux = (PSAux_Service)FT_Get_Module_Interface(
312 FT_FACE_LIBRARY( face ), "psaux" );
316 FT_ERROR(( "cid_face_init: cannot access `psaux' module\n" ));
317 error = FT_THROW( Missing_Module );
324 pshinter = (PSHinter_Service)face->pshinter;
327 pshinter = (PSHinter_Service)FT_Get_Module_Interface(
328 FT_FACE_LIBRARY( face ), "pshinter" );
330 face->pshinter = pshinter;
333 FT_TRACE2(( "CID driver\n" ));
335 /* open the tokenizer; this will also check the font format */
336 if ( FT_STREAM_SEEK( 0 ) )
339 error = cid_face_open( face, face_index );
343 /* if we just wanted to check the format, leave successfully now */
344 if ( face_index < 0 )
347 /* check the face index */
348 /* XXX: handle CID fonts with more than a single face */
349 if ( ( face_index & 0xFFFF ) != 0 )
351 FT_ERROR(( "cid_face_init: invalid face index\n" ));
352 error = FT_THROW( Invalid_Argument );
356 /* now load the font program into the face object */
358 /* initialize the face object fields */
360 /* set up root face fields */
362 CID_FaceInfo cid = &face->cid;
363 PS_FontInfo info = &cid->font_info;
366 cidface->num_glyphs = (FT_Long)cid->cid_count;
367 cidface->num_charmaps = 0;
369 cidface->face_index = face_index & 0xFFFF;
371 cidface->face_flags |= FT_FACE_FLAG_SCALABLE | /* scalable outlines */
372 FT_FACE_FLAG_HORIZONTAL | /* horizontal data */
373 FT_FACE_FLAG_HINTER; /* has native hinter */
375 if ( info->is_fixed_pitch )
376 cidface->face_flags |= FT_FACE_FLAG_FIXED_WIDTH;
379 * For the sfnt-wrapped CID fonts for MacOS, currently,
380 * its `cmap' tables are ignored, and the content in
381 * its `CID ' table is treated the same as naked CID-keyed
382 * font. See ft_lookup_PS_in_sfnt_stream().
384 cidface->face_flags |= FT_FACE_FLAG_CID_KEYED;
386 /* XXX: TODO: add kerning with .afm support */
388 /* get style name -- be careful, some broken fonts only */
389 /* have a /FontName dictionary entry! */
390 cidface->family_name = info->family_name;
391 /* assume "Regular" style if we don't know better */
392 cidface->style_name = (char *)"Regular";
393 if ( cidface->family_name )
395 char* full = info->full_name;
396 char* family = cidface->family_name;
403 if ( *full == *family )
410 if ( *full == ' ' || *full == '-' )
412 else if ( *family == ' ' || *family == '-' )
417 cidface->style_name = full;
426 /* do we have a `/FontName'? */
427 if ( cid->cid_font_name )
428 cidface->family_name = cid->cid_font_name;
431 /* compute style flags */
432 cidface->style_flags = 0;
433 if ( info->italic_angle )
434 cidface->style_flags |= FT_STYLE_FLAG_ITALIC;
437 if ( !ft_strcmp( info->weight, "Bold" ) ||
438 !ft_strcmp( info->weight, "Black" ) )
439 cidface->style_flags |= FT_STYLE_FLAG_BOLD;
442 /* no embedded bitmap support */
443 cidface->num_fixed_sizes = 0;
444 cidface->available_sizes = NULL;
446 cidface->bbox.xMin = cid->font_bbox.xMin >> 16;
447 cidface->bbox.yMin = cid->font_bbox.yMin >> 16;
448 /* no `U' suffix here to 0xFFFF! */
449 cidface->bbox.xMax = ( cid->font_bbox.xMax + 0xFFFF ) >> 16;
450 cidface->bbox.yMax = ( cid->font_bbox.yMax + 0xFFFF ) >> 16;
452 if ( !cidface->units_per_EM )
453 cidface->units_per_EM = 1000;
455 cidface->ascender = (FT_Short)( cidface->bbox.yMax );
456 cidface->descender = (FT_Short)( cidface->bbox.yMin );
458 cidface->height = (FT_Short)( ( cidface->units_per_EM * 12 ) / 10 );
459 if ( cidface->height < cidface->ascender - cidface->descender )
460 cidface->height = (FT_Short)( cidface->ascender - cidface->descender );
462 cidface->underline_position = (FT_Short)info->underline_position;
463 cidface->underline_thickness = (FT_Short)info->underline_thickness;
471 /**************************************************************************
477 * Initializes a given CID driver object.
481 * A handle to the target driver object.
484 * FreeType error code. 0 means success.
486 FT_LOCAL_DEF( FT_Error )
487 cid_driver_init( FT_Module module )
489 PS_Driver driver = (PS_Driver)module;
494 /* set default property values, cf. `ftt1drv.h' */
495 driver->hinting_engine = FT_HINTING_ADOBE;
497 driver->no_stem_darkening = TRUE;
499 driver->darken_params[0] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X1;
500 driver->darken_params[1] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y1;
501 driver->darken_params[2] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X2;
502 driver->darken_params[3] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y2;
503 driver->darken_params[4] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X3;
504 driver->darken_params[5] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y3;
505 driver->darken_params[6] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_X4;
506 driver->darken_params[7] = CFF_CONFIG_OPTION_DARKENING_PARAMETER_Y4;
508 /* compute random seed from some memory addresses */
509 seed = (FT_UInt32)( (FT_Offset)(char*)&seed ^
510 (FT_Offset)(char*)&module ^
511 (FT_Offset)(char*)module->memory );
512 seed = seed ^ ( seed >> 10 ) ^ ( seed >> 20 );
514 driver->random_seed = (FT_Int32)seed;
515 if ( driver->random_seed < 0 )
516 driver->random_seed = -driver->random_seed;
517 else if ( driver->random_seed == 0 )
518 driver->random_seed = 123456789;
524 /**************************************************************************
530 * Finalizes a given CID driver.
534 * A handle to the target CID driver.
537 cid_driver_done( FT_Module driver )