1 /***************************************************************************/
5 /* OpenType objects manager (body). */
7 /* Copyright 1996-2001 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_DEBUG_H
21 #include FT_INTERNAL_CALC_H
22 #include FT_INTERNAL_STREAM_H
24 #include FT_TRUETYPE_IDS_H
25 #include FT_TRUETYPE_TAGS_H
26 #include FT_INTERNAL_SFNT_H
27 #include FT_INTERNAL_POSTSCRIPT_NAMES_H
33 #include <string.h> /* for strlen() */
36 /*************************************************************************/
38 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
39 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
40 /* messages during execution. */
43 #define FT_COMPONENT trace_cffobjs
46 /*************************************************************************/
50 /*************************************************************************/
53 CFF_StrCopy( FT_Memory memory,
54 const FT_String* source )
57 FT_String* result = 0;
58 FT_Int len = (FT_Int)strlen( source );
61 if ( !ALLOC( result, len + 1 ) )
63 MEM_Copy( result, source, len );
72 /* this function is used to build a Unicode charmap from the glyph names */
75 CFF_Build_Unicode_Charmap( CFF_Face face,
77 PSNames_Interface* psnames )
79 CFF_Font* font = (CFF_Font*)face->extra.data;
80 FT_Memory memory = FT_FACE_MEMORY(face);
81 FT_UInt n, num_glyphs = face->root.num_glyphs;
82 const char** glyph_names;
84 CFF_Font_Dict* dict = &font->top_font.font_dict;
85 FT_ULong charset_offset;
87 FT_Stream stream = face->root.stream;
90 charset_offset = dict->charset_offset;
91 if ( !charset_offset )
93 FT_ERROR(( "CFF_Build_Unicode_Charmap: charset table is missing\n" ));
94 error = CFF_Err_Invalid_File_Format;
98 /* allocate the charmap */
99 if ( ALLOC( face->charmap, ...
101 /* seek to charset table and allocate glyph names table */
102 if ( FILE_Seek( base_offset + charset_offset ) ||
103 ALLOC_ARRAY( glyph_names, num_glyphs, const char* ) )
106 /* now, read each glyph name and store it in the glyph name table */
107 if ( READ_Byte( format ) )
112 case 0: /* format 0 - one SID per glyph */
114 const char** gname = glyph_names;
115 const char** limit = gname + num_glyphs;
118 if ( ACCESS_Frame( num_glyphs * 2 ) )
121 for ( ; gname < limit; gname++ )
122 gname[0] = CFF_Get_String( &font->string_index,
129 case 1: /* format 1 - sequential ranges */
130 case 2: /* format 2 - sequential ranges with 16-bit counts */
132 const char** gname = glyph_names;
133 const char** limit = gname + num_glyphs;
140 while ( gname < limit )
146 if ( ACCESS_Frame( len ) )
149 first = GET_UShort();
151 count = GET_UShort();
157 for ( ; count > 0; count-- )
159 gname[0] = CFF_Get_String( &font->string_index,
169 default: /* unknown charset format! */
170 FT_ERROR(( "CFF_Build_Unicode_Charmap: unknown charset format!\n" ));
171 error = CFF_Err_Invalid_File_Format;
175 /* all right, the glyph names were loaded; we now need to create */
176 /* the corresponding unicode charmap */
179 for ( n = 0; n < num_glyphs; n++ )
180 FREE( glyph_names[n] );
192 find_encoding( int platform_id,
195 typedef struct TEncoding
199 FT_Encoding encoding;
204 const TEncoding tt_encodings[] =
206 { TT_PLATFORM_ISO, -1, ft_encoding_unicode },
208 { TT_PLATFORM_APPLE_UNICODE, -1, ft_encoding_unicode },
210 { TT_PLATFORM_MACINTOSH, TT_MAC_ID_ROMAN, ft_encoding_apple_roman },
212 { TT_PLATFORM_MICROSOFT, TT_MS_ID_UNICODE_CS, ft_encoding_unicode },
213 { TT_PLATFORM_MICROSOFT, TT_MS_ID_SJIS, ft_encoding_sjis },
214 { TT_PLATFORM_MICROSOFT, TT_MS_ID_GB2312, ft_encoding_gb2312 },
215 { TT_PLATFORM_MICROSOFT, TT_MS_ID_BIG_5, ft_encoding_big5 },
216 { TT_PLATFORM_MICROSOFT, TT_MS_ID_WANSUNG, ft_encoding_wansung },
217 { TT_PLATFORM_MICROSOFT, TT_MS_ID_JOHAB, ft_encoding_johab }
220 const TEncoding *cur, *limit;
224 limit = cur + sizeof ( tt_encodings ) / sizeof ( tt_encodings[0] );
226 for ( ; cur < limit; cur++ )
228 if ( cur->platform_id == platform_id )
230 if ( cur->encoding_id == encoding_id ||
231 cur->encoding_id == -1 )
232 return cur->encoding;
236 return ft_encoding_none;
240 /*************************************************************************/
246 /* Initializes a given OpenType face object. */
249 /* stream :: The source font stream. */
251 /* face_index :: The index of the font face in the resource. */
253 /* num_params :: Number of additional generic parameters. Ignored. */
255 /* params :: Additional generic parameters. Ignored. */
258 /* face :: The newly built face object. */
261 /* FreeType error code. 0 means success. */
263 FT_LOCAL_DEF FT_Error
264 CFF_Init_Face( FT_Stream stream,
268 FT_Parameter* params )
271 SFNT_Interface* sfnt;
272 PSNames_Interface* psnames;
273 FT_Bool pure_cff = 1;
274 FT_Bool sfnt_format = 0;
277 sfnt = (SFNT_Interface*)FT_Get_Module_Interface(
278 face->root.driver->root.library, "sfnt" );
282 psnames = (PSNames_Interface*)FT_Get_Module_Interface(
283 face->root.driver->root.library, "psnames" );
285 /* create input stream from resource */
286 if ( FILE_Seek( 0 ) )
289 /* check that we have a valid OpenType file */
290 error = sfnt->init_face( stream, face, face_index, num_params, params );
293 if ( face->format_tag != 0x4F54544FL ) /* `OTTO'; OpenType/CFF font */
295 FT_TRACE2(( "[not a valid OpenType/CFF font]\n" ));
299 /* if we are performing a simple font format check, exit immediately */
300 if ( face_index < 0 )
305 /* now, the font can be either an OpenType/CFF font, or an SVG CEF */
306 /* font in the later case; it doesn't have a `head' table */
307 error = face->goto_table( face, TTAG_head, stream, 0 );
312 /* load font directory */
313 error = sfnt->load_face( stream, face,
314 face_index, num_params, params );
320 /* load the `cmap' table by hand */
321 error = sfnt->load_charmaps( face, stream );
325 /* XXX: we don't load the GPOS table, as OpenType Layout */
326 /* support will be added later to a layout library on top of */
330 /* now, load the CFF part of the file */
331 error = face->goto_table( face, TTAG_CFF, stream, 0 );
337 /* rewind to start of file; we are going to load a pure-CFF font */
338 if ( FILE_Seek( 0 ) )
343 /* now load and parse the CFF table in the file */
346 FT_Memory memory = face->root.memory;
351 if ( ALLOC( cff, sizeof ( *cff ) ) )
354 face->extra.data = cff;
355 error = CFF_Load_Font( stream, face_index, cff );
359 /* Complement the root flags with some interesting information. */
360 /* Note that this is only necessary for pure CFF and CEF fonts. */
365 CFF_Font_Dict* dict = &cff->top_font.font_dict;
368 /* we need the `PSNames' module for pure-CFF and CEF formats */
371 FT_ERROR(( "CFF_Init_Face:" ));
372 FT_ERROR(( " cannot open CFF & CEF fonts\n" ));
374 FT_ERROR(( " without the `PSNames' module\n" ));
378 /* Set up num_faces. */
379 root->num_faces = cff->num_faces;
381 /* compute number of glyphs */
382 if ( dict->cid_registry )
383 root->num_glyphs = dict->cid_count;
385 root->num_glyphs = cff->charstrings_index.count;
387 /* set global bbox, as well as EM size */
388 root->bbox = dict->font_bbox;
389 root->ascender = (FT_Short)( root->bbox.yMax >> 16 );
390 root->descender = (FT_Short)( root->bbox.yMin >> 16 );
391 root->height = (FT_Short)(
392 ( ( root->ascender - root->descender ) * 12 ) / 10 );
394 if ( dict->units_per_em )
395 root->units_per_EM = dict->units_per_em;
397 root->units_per_EM = 1000;
399 /* retrieve font family & style name */
400 root->family_name = CFF_Get_Name( &cff->name_index, face_index );
401 if ( dict->cid_registry )
402 root->style_name = CFF_StrCopy( memory, "Regular" ); /* XXXX */
404 root->style_name = CFF_Get_String( &cff->string_index,
408 /*******************************************************************/
410 /* Compute face flags. */
412 flags = FT_FACE_FLAG_SCALABLE | /* scalable outlines */
413 FT_FACE_FLAG_HORIZONTAL; /* horizontal data */
416 flags |= FT_FACE_FLAG_SFNT;
418 /* fixed width font? */
419 if ( dict->is_fixed_pitch )
420 flags |= FT_FACE_FLAG_FIXED_WIDTH;
422 /* XXX: WE DO NOT SUPPORT KERNING METRICS IN THE GPOS TABLE FOR NOW */
424 /* kerning available? */
425 if ( face->kern_pairs )
426 flags |= FT_FACE_FLAG_KERNING;
429 #ifndef FT_CONFIG_OPTION_NO_GLYPH_NAMES
430 flags |= FT_FACE_FLAG_GLYPH_NAMES;
433 root->face_flags = flags;
435 /*******************************************************************/
437 /* Compute style flags. */
441 if ( dict->italic_angle )
442 flags |= FT_STYLE_FLAG_ITALIC;
444 /* XXX: may not be correct */
445 if ( cff->top_font.private_dict.force_bold )
446 flags |= FT_STYLE_FLAG_BOLD;
448 root->style_flags = flags;
450 /* set the charmaps if any */
453 /*****************************************************************/
455 /* Polish the charmaps. */
457 /* Try to set the charmap encoding according to the platform & */
458 /* encoding ID of each charmap. */
464 charmap = face->charmaps;
465 root->num_charmaps = face->num_charmaps;
467 /* allocate table of pointers */
468 if ( ALLOC_ARRAY( root->charmaps, root->num_charmaps, FT_CharMap ) )
471 for ( n = 0; n < root->num_charmaps; n++, charmap++ )
473 FT_Int platform = charmap->cmap.platformID;
474 FT_Int encoding = charmap->cmap.platformEncodingID;
477 charmap->root.face = (FT_Face)face;
478 charmap->root.platform_id = (FT_UShort)platform;
479 charmap->root.encoding_id = (FT_UShort)encoding;
480 charmap->root.encoding = find_encoding( platform, encoding );
482 /* now, set root->charmap with a unicode charmap */
483 /* wherever available */
484 if ( !root->charmap &&
485 charmap->root.encoding == ft_encoding_unicode )
486 root->charmap = (FT_CharMap)charmap;
488 root->charmaps[n] = (FT_CharMap)charmap;
498 error = CFF_Err_Unknown_File_Format;
503 /*************************************************************************/
509 /* Finalizes a given face object. */
512 /* face :: A pointer to the face object to destroy. */
515 CFF_Done_Face( CFF_Face face )
517 FT_Memory memory = face->root.memory;
518 SFNT_Interface* sfnt = (SFNT_Interface*)face->sfnt;
522 sfnt->done_face( face );
525 CFF_Font* cff = (CFF_Font*)face->extra.data;
530 CFF_Done_Font( cff );
531 FREE( face->extra.data );
537 /*************************************************************************/
540 /* CFF_Init_Driver */
543 /* Initializes a given OpenType driver object. */
546 /* driver :: A handle to the target driver object. */
549 /* FreeType error code. 0 means success. */
551 FT_LOCAL_DEF FT_Error
552 CFF_Init_Driver( CFF_Driver driver )
554 /* init extension registry if needed */
556 #ifdef TT_CONFIG_OPTION_EXTEND_ENGINE
558 return TT_Init_Extensions( driver );
570 /*************************************************************************/
573 /* CFF_Done_Driver */
576 /* Finalizes a given OpenType driver. */
579 /* driver :: A handle to the target OpenType driver. */
582 CFF_Done_Driver( CFF_Driver driver )
584 /* destroy extensions registry if needed */
586 #ifdef TT_CONFIG_OPTION_EXTEND_ENGINE
588 TT_Done_Extensions( driver );