blob: 321f417a862da19e5fc94c7451213271111036c6 [file] [log] [blame]
David Turner19ed8af2000-12-08 02:42:29 +00001/***************************************************************************/
2/* */
3/* cffobjs.c */
4/* */
5/* OpenType objects manager (body). */
6/* */
7/* Copyright 1996-2000 by */
8/* David Turner, Robert Wilhelm, and Werner Lemberg. */
9/* */
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. */
15/* */
16/***************************************************************************/
17
Werner Lembergcc069be2000-12-08 16:17:16 +000018
David Turner19ed8af2000-12-08 02:42:29 +000019#include <ft2build.h>
20#include FT_INTERNAL_DEBUG_H
21#include FT_INTERNAL_CALC_H
22#include FT_INTERNAL_STREAM_H
23#include FT_ERRORS_H
24#include FT_TRUETYPE_NAMES_H
25#include FT_TRUETYPE_TAGS_H
26#include FT_INTERNAL_SFNT_H
27#include FT_INTERNAL_POSTSCRIPT_NAMES_H
28#include FT_SOURCE_FILE(cff,cffobjs.h)
29#include FT_SOURCE_FILE(cff,cffload.h)
30#include FT_INTERNAL_CFF_ERRORS_H
Werner Lembergcc069be2000-12-08 16:17:16 +000031
David Turner19ed8af2000-12-08 02:42:29 +000032#include <string.h> /* for strlen() */
33
34
35 /*************************************************************************/
36 /* */
37 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
38 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
39 /* messages during execution. */
40 /* */
41#undef FT_COMPONENT
42#define FT_COMPONENT trace_t2objs
43
44
45 /*************************************************************************/
46 /* */
47 /* FACE FUNCTIONS */
48 /* */
49 /*************************************************************************/
50
51 static
52 FT_String* T2_StrCopy( FT_Memory memory,
53 const FT_String* source )
54 {
55 FT_Error error;
56 FT_String* result = 0;
57 FT_Int len = (FT_Int)strlen( source );
58
59
60 if ( !ALLOC( result, len + 1 ) )
61 {
62 MEM_Copy( result, source, len );
63 result[len] = 0;
64 }
65 return result;
66 }
67
68
69#if 0
70
71 /* this function is used to build a Unicode charmap from the glyph names */
72 /* in a file */
73 static
74 FT_Error CFF_Build_Unicode_Charmap( T2_Face face,
75 FT_ULong base_offset,
76 PSNames_Interface* psnames )
77 {
78 CFF_Font* font = (CFF_Font*)face->extra.data;
79 FT_Memory memory = FT_FACE_MEMORY(face);
80 FT_UInt n, num_glyphs = face->root.num_glyphs;
81 const char** glyph_names;
82 FT_Error error;
83 CFF_Font_Dict* dict = &font->top_font.font_dict;
84 FT_ULong charset_offset;
85 FT_Byte format;
86 FT_Stream stream = face->root.stream;
87
88
89 charset_offset = dict->charset_offset;
90 if ( !charset_offset )
91 {
Werner Lembergcc069be2000-12-08 16:17:16 +000092 FT_ERROR(( "CFF_Build_Unicode_Charmap: charset table is missing\n" ));
David Turner19ed8af2000-12-08 02:42:29 +000093 error = T2_Err_Invalid_File_Format;
94 goto Exit;
95 }
96
97 /* allocate the charmap */
98 if ( ALLOC( face->charmap, ...
99
100 /* seek to charset table and allocate glyph names table */
101 if ( FILE_Seek( base_offset + charset_offset ) ||
102 ALLOC_ARRAY( glyph_names, num_glyphs, const char* ) )
103 goto Exit;
104
105 /* now, read each glyph name and store it in the glyph name table */
106 if ( READ_Byte( format ) )
107 goto Fail;
108
109 switch ( format )
110 {
Werner Lembergcc069be2000-12-08 16:17:16 +0000111 case 0: /* format 0 - one SID per glyph */
112 {
113 const char** gname = glyph_names;
114 const char** limit = gname + num_glyphs;
David Turner19ed8af2000-12-08 02:42:29 +0000115
Werner Lembergcc069be2000-12-08 16:17:16 +0000116
117 if ( ACCESS_Frame( num_glyphs * 2 ) )
118 goto Fail;
119
120 for ( ; gname < limit; gname++ )
121 gname[0] = T2_Get_String( &font->string_index,
122 GET_UShort(),
123 psnames );
124 FORGET_Frame();
125 break;
126 }
127
128 case 1: /* format 1 - sequential ranges */
129 case 2: /* format 2 - sequential ranges with 16-bit counts */
130 {
131 const char** gname = glyph_names;
132 const char** limit = gname + num_glyphs;
133 FT_UInt len = 3;
134
135
136 if ( format == 2 )
137 len++;
138
139 while ( gname < limit )
140 {
141 FT_UInt first;
142 FT_UInt count;
143
144
145 if ( ACCESS_Frame( len ) )
David Turner19ed8af2000-12-08 02:42:29 +0000146 goto Fail;
147
Werner Lembergcc069be2000-12-08 16:17:16 +0000148 first = GET_UShort();
149 if ( format == 3 )
150 count = GET_UShort();
151 else
152 count = GET_Byte();
153
David Turner19ed8af2000-12-08 02:42:29 +0000154 FORGET_Frame();
David Turner19ed8af2000-12-08 02:42:29 +0000155
Werner Lembergcc069be2000-12-08 16:17:16 +0000156 for ( ; count > 0; count-- )
David Turner19ed8af2000-12-08 02:42:29 +0000157 {
Werner Lembergcc069be2000-12-08 16:17:16 +0000158 gname[0] = T2_Get_String( &font->string_index,
159 first,
160 psnames );
161 gname++;
162 first++;
David Turner19ed8af2000-12-08 02:42:29 +0000163 }
David Turner19ed8af2000-12-08 02:42:29 +0000164 }
Werner Lembergcc069be2000-12-08 16:17:16 +0000165 break;
166 }
David Turner19ed8af2000-12-08 02:42:29 +0000167
Werner Lembergcc069be2000-12-08 16:17:16 +0000168 default: /* unknown charset format! */
169 FT_ERROR(( "CFF_Build_Unicode_Charmap: unknown charset format!\n" ));
170 error = T2_Err_Invalid_File_Format;
171 goto Fail;
David Turner19ed8af2000-12-08 02:42:29 +0000172 }
173
Werner Lembergcc069be2000-12-08 16:17:16 +0000174 /* all right, the glyph names were loaded; we now need to create */
175 /* the corresponding unicode charmap */
David Turner19ed8af2000-12-08 02:42:29 +0000176
177 Fail:
178 for ( n = 0; n < num_glyphs; n++ )
179 FREE( glyph_names[n] );
180
181 FREE( glyph_names );
182
183 Exit:
184 return error;
185 }
186
187#endif /* 0 */
188
189
190 static
191 FT_Encoding find_encoding( int platform_id,
192 int encoding_id )
193 {
194 typedef struct TEncoding
195 {
196 int platform_id;
197 int encoding_id;
198 FT_Encoding encoding;
199
200 } TEncoding;
201
202 static
203 const TEncoding tt_encodings[] =
204 {
205 { TT_PLATFORM_ISO, -1, ft_encoding_unicode },
206
207 { TT_PLATFORM_APPLE_UNICODE, -1, ft_encoding_unicode },
208
209 { TT_PLATFORM_MACINTOSH, TT_MAC_ID_ROMAN, ft_encoding_apple_roman },
210
211 { TT_PLATFORM_MICROSOFT, TT_MS_ID_UNICODE_CS, ft_encoding_unicode },
212 { TT_PLATFORM_MICROSOFT, TT_MS_ID_SJIS, ft_encoding_sjis },
213 { TT_PLATFORM_MICROSOFT, TT_MS_ID_GB2312, ft_encoding_gb2312 },
214 { TT_PLATFORM_MICROSOFT, TT_MS_ID_BIG_5, ft_encoding_big5 },
215 { TT_PLATFORM_MICROSOFT, TT_MS_ID_WANSUNG, ft_encoding_wansung },
216 { TT_PLATFORM_MICROSOFT, TT_MS_ID_JOHAB, ft_encoding_johab }
217 };
218
219 const TEncoding *cur, *limit;
220
221
222 cur = tt_encodings;
223 limit = cur + sizeof ( tt_encodings ) / sizeof ( tt_encodings[0] );
224
225 for ( ; cur < limit; cur++ )
226 {
227 if ( cur->platform_id == platform_id )
228 {
229 if ( cur->encoding_id == encoding_id ||
230 cur->encoding_id == -1 )
231 return cur->encoding;
232 }
233 }
234
235 return ft_encoding_none;
236 }
237
238
239 /*************************************************************************/
240 /* */
241 /* <Function> */
242 /* T2_Init_Face */
243 /* */
244 /* <Description> */
245 /* Initializes a given OpenType face object. */
246 /* */
247 /* <Input> */
248 /* stream :: The source font stream. */
249 /* */
250 /* face_index :: The index of the font face in the resource. */
251 /* */
252 /* num_params :: Number of additional generic parameters. Ignored. */
253 /* */
254 /* params :: Additional generic parameters. Ignored. */
255 /* */
256 /* <InOut> */
257 /* face :: The newly built face object. */
258 /* */
259 /* <Return> */
260 /* FreeType error code. 0 means success. */
261 /* */
262 FT_LOCAL
263 FT_Error T2_Init_Face( FT_Stream stream,
264 T2_Face face,
265 FT_Int face_index,
266 FT_Int num_params,
267 FT_Parameter* params )
268 {
269 FT_Error error;
270 SFNT_Interface* sfnt;
271 PSNames_Interface* psnames;
272 FT_Bool pure_cff = 1;
273 FT_Bool sfnt_format = 0;
274
275
276 sfnt = (SFNT_Interface*)FT_Get_Module_Interface(
277 face->root.driver->root.library, "sfnt" );
278 if ( !sfnt )
279 goto Bad_Format;
280
281 psnames = (PSNames_Interface*)FT_Get_Module_Interface(
282 face->root.driver->root.library, "psnames" );
283
284 /* create input stream from resource */
285 if ( FILE_Seek( 0 ) )
286 goto Exit;
287
288 /* check that we have a valid OpenType file */
289 error = sfnt->init_face( stream, face, face_index, num_params, params );
290 if ( !error )
291 {
292 if ( face->format_tag != 0x4F54544FL ) /* `OTTO'; OpenType/CFF font */
293 {
294 FT_TRACE2(( "[not a valid OpenType/CFF font]\n" ));
295 goto Bad_Format;
296 }
297
Werner Lembergcc069be2000-12-08 16:17:16 +0000298 /* if we are performing a simple font format check, exit immediately */
David Turner19ed8af2000-12-08 02:42:29 +0000299 if ( face_index < 0 )
300 return T2_Err_Ok;
301
302 sfnt_format = 1;
303
Werner Lembergcc069be2000-12-08 16:17:16 +0000304 /* now, the font can be either an OpenType/CFF font, or an SVG CEF */
305 /* font in the later case; it doesn't have a `head' table */
David Turner19ed8af2000-12-08 02:42:29 +0000306 error = face->goto_table( face, TTAG_head, stream, 0 );
307 if ( !error )
308 {
309 pure_cff = 0;
310
Werner Lembergcc069be2000-12-08 16:17:16 +0000311 /* load font directory */
David Turner19ed8af2000-12-08 02:42:29 +0000312 error = sfnt->load_face( stream, face,
313 face_index, num_params, params );
314 if ( error )
315 goto Exit;
316 }
317 else
318 {
319 /* load the `cmap' table by hand */
320 error = sfnt->load_charmaps( face, stream );
321 if ( error )
322 goto Exit;
323
Werner Lembergcc069be2000-12-08 16:17:16 +0000324 /* XXX: we don't load the GPOS table, as OpenType Layout */
325 /* support will be added later to a layout library on top of */
326 /* FreeType 2 */
David Turner19ed8af2000-12-08 02:42:29 +0000327 }
328
329 /* now, load the CFF part of the file */
330 error = face->goto_table( face, TTAG_CFF, stream, 0 );
331 if ( error )
332 goto Exit;
333 }
334 else
335 {
336 /* rewind to start of file; we are going to load a pure-CFF font */
337 if ( FILE_Seek( 0 ) )
338 goto Exit;
339 error = FT_Err_Ok;
340 }
341
342 /* now load and parse the CFF table in the file */
343 {
344 CFF_Font* cff;
345 FT_Memory memory = face->root.memory;
346 FT_Face root;
347 FT_UInt flags;
348 FT_ULong base_offset;
349
350
351 if ( ALLOC( cff, sizeof ( *cff ) ) )
352 goto Exit;
353
354 base_offset = FILE_Pos();
355
356 face->extra.data = cff;
357 error = CFF_Load_Font( stream, face_index, cff );
358 if ( error )
359 goto Exit;
360
361 /* Complement the root flags with some interesting information. */
Werner Lembergcc069be2000-12-08 16:17:16 +0000362 /* Note that this is only necessary for pure CFF and CEF fonts. */
David Turner19ed8af2000-12-08 02:42:29 +0000363
364 root = &face->root;
365 if ( pure_cff )
366 {
367 CFF_Font_Dict* dict = &cff->top_font.font_dict;
368
369
370 /* we need the `PSNames' module for pure-CFF and CEF formats */
371 if ( !psnames )
372 {
373 FT_ERROR(( "T2_Init_Face:" ));
374 FT_ERROR(( " cannot open CFF & CEF fonts\n" ));
375 FT_ERROR(( " " ));
376 FT_ERROR(( " without the `PSNames' module\n" ));
377 goto Bad_Format;
378 }
379
380 /* Set up num_faces. */
381 root->num_faces = cff->num_faces;
382
383 /* compute number of glyphs */
384 if ( dict->cid_registry )
385 root->num_glyphs = dict->cid_count;
386 else
387 root->num_glyphs = cff->charstrings_index.count;
388
389 /* set global bbox, as well as EM size */
390 root->units_per_EM = 1000;
391 root->bbox = dict->font_bbox;
392 root->ascender = (FT_Short)root->bbox.yMax;
393 root->descender = (FT_Short)root->bbox.yMin;
394 root->height = ( ( root->ascender - root->descender ) * 12 ) / 10;
395
396 /* retrieve font family & style name */
397 root->family_name = CFF_Get_Name( &cff->name_index, face_index );
398 if ( dict->cid_registry )
David Turner19ed8af2000-12-08 02:42:29 +0000399 root->style_name = T2_StrCopy( memory, "Regular" ); /* XXXX */
David Turner19ed8af2000-12-08 02:42:29 +0000400 else
David Turner19ed8af2000-12-08 02:42:29 +0000401 root->style_name = CFF_Get_String( &cff->string_index,
402 dict->weight,
403 psnames );
David Turner19ed8af2000-12-08 02:42:29 +0000404
405 /*******************************************************************/
406 /* */
407 /* Compute face flags. */
408 /* */
409 flags = FT_FACE_FLAG_SCALABLE | /* scalable outlines */
410 FT_FACE_FLAG_HORIZONTAL; /* horizontal data */
411
412 if ( sfnt_format )
413 flags |= FT_FACE_FLAG_SFNT;
414
415 /* fixed width font? */
416 if ( dict->is_fixed_pitch )
417 flags |= FT_FACE_FLAG_FIXED_WIDTH;
418
Werner Lembergcc069be2000-12-08 16:17:16 +0000419 /* XXX: WE DO NOT SUPPORT KERNING METRICS IN THE GPOS TABLE FOR NOW */
David Turner19ed8af2000-12-08 02:42:29 +0000420#if 0
421 /* kerning available? */
422 if ( face->kern_pairs )
423 flags |= FT_FACE_FLAG_KERNING;
424#endif
425
426 root->face_flags = flags;
427
428 /*******************************************************************/
429 /* */
430 /* Compute style flags. */
431 /* */
432 flags = 0;
433
434 if ( dict->italic_angle )
435 flags |= FT_STYLE_FLAG_ITALIC;
436
437 /* XXX: may not be correct */
438 if ( cff->top_font.private_dict.force_bold )
439 flags |= FT_STYLE_FLAG_BOLD;
440
441 root->style_flags = flags;
442
443 /* set the charmaps if any */
444 if ( sfnt_format )
445 {
446 /*****************************************************************/
447 /* */
448 /* Polish the charmaps. */
449 /* */
450 /* Try to set the charmap encoding according to the platform & */
451 /* encoding ID of each charmap. */
452 /* */
453 TT_CharMap charmap;
454 FT_Int n;
455
456
457 charmap = face->charmaps;
458 root->num_charmaps = face->num_charmaps;
459
460 /* allocate table of pointers */
461 if ( ALLOC_ARRAY( root->charmaps, root->num_charmaps, FT_CharMap ) )
462 goto Exit;
463
464 for ( n = 0; n < root->num_charmaps; n++, charmap++ )
465 {
466 FT_Int platform = charmap->cmap.platformID;
467 FT_Int encoding = charmap->cmap.platformEncodingID;
468
469
470 charmap->root.face = (FT_Face)face;
471 charmap->root.platform_id = platform;
472 charmap->root.encoding_id = encoding;
473 charmap->root.encoding = find_encoding( platform, encoding );
474
475 /* now, set root->charmap with a unicode charmap */
476 /* wherever available */
477 if ( !root->charmap &&
478 charmap->root.encoding == ft_encoding_unicode )
479 root->charmap = (FT_CharMap)charmap;
480
481 root->charmaps[n] = (FT_CharMap)charmap;
482 }
483 }
484 }
485 }
486
487 Exit:
488 return error;
489
490 Bad_Format:
491 error = FT_Err_Unknown_File_Format;
492 goto Exit;
493 }
494
495
496 /*************************************************************************/
497 /* */
498 /* <Function> */
499 /* T2_Done_Face */
500 /* */
501 /* <Description> */
502 /* Finalizes a given face object. */
503 /* */
504 /* <Input> */
505 /* face :: A pointer to the face object to destroy. */
506 /* */
507 FT_LOCAL
508 void T2_Done_Face( T2_Face face )
509 {
510 FT_Memory memory = face->root.memory;
511 SFNT_Interface* sfnt = (SFNT_Interface*)face->sfnt;
512
513
514 if ( sfnt )
515 sfnt->done_face( face );
516
517 {
518 CFF_Font* cff = (CFF_Font*)face->extra.data;
519
520
521 if ( cff )
522 {
523 CFF_Done_Font( cff );
524 FREE( face->extra.data );
525 }
526 }
527 }
528
529
530 /*************************************************************************/
531 /* */
532 /* <Function> */
533 /* T2_Init_Driver */
534 /* */
535 /* <Description> */
536 /* Initializes a given OpenType driver object. */
537 /* */
538 /* <Input> */
539 /* driver :: A handle to the target driver object. */
540 /* */
541 /* <Return> */
542 /* FreeType error code. 0 means success. */
543 /* */
544 FT_LOCAL_DEF
545 FT_Error T2_Init_Driver( T2_Driver driver )
546 {
547 /* init extension registry if needed */
548
549#ifdef TT_CONFIG_OPTION_EXTEND_ENGINE
550
551 return TT_Init_Extensions( driver );
552
553#else
554
555 FT_UNUSED( driver );
556
557 return T2_Err_Ok;
558
559#endif
560 }
561
562
563 /*************************************************************************/
564 /* */
565 /* <Function> */
566 /* T2_Done_Driver */
567 /* */
568 /* <Description> */
569 /* Finalizes a given OpenType driver. */
570 /* */
571 /* <Input> */
572 /* driver :: A handle to the target OpenType driver. */
573 /* */
574 FT_LOCAL_DEF
575 void T2_Done_Driver( T2_Driver driver )
576 {
577 /* destroy extensions registry if needed */
578
579#ifdef TT_CONFIG_OPTION_EXTEND_ENGINE
580
581 TT_Done_Extensions( driver );
582
583#else
584
585 FT_UNUSED( driver );
586
587#endif
588 }
589
590
591/* END */