1 /****************************************************************************
5 * OpenType and CFF data/program tables loader (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/ftobjs.h>
21 #include <freetype/internal/ftstream.h>
22 #include <freetype/tttags.h>
23 #include <freetype/t1tables.h>
24 #include <freetype/internal/psaux.h>
26 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
27 #include <freetype/ftmm.h>
28 #include <freetype/internal/services/svmm.h>
37 #define FT_FIXED_ONE ( (FT_Fixed)0x10000 )
42 static const FT_UShort cff_isoadobe_charset[229] =
44 0, 1, 2, 3, 4, 5, 6, 7,
45 8, 9, 10, 11, 12, 13, 14, 15,
46 16, 17, 18, 19, 20, 21, 22, 23,
47 24, 25, 26, 27, 28, 29, 30, 31,
48 32, 33, 34, 35, 36, 37, 38, 39,
49 40, 41, 42, 43, 44, 45, 46, 47,
50 48, 49, 50, 51, 52, 53, 54, 55,
51 56, 57, 58, 59, 60, 61, 62, 63,
52 64, 65, 66, 67, 68, 69, 70, 71,
53 72, 73, 74, 75, 76, 77, 78, 79,
54 80, 81, 82, 83, 84, 85, 86, 87,
55 88, 89, 90, 91, 92, 93, 94, 95,
56 96, 97, 98, 99, 100, 101, 102, 103,
57 104, 105, 106, 107, 108, 109, 110, 111,
58 112, 113, 114, 115, 116, 117, 118, 119,
59 120, 121, 122, 123, 124, 125, 126, 127,
60 128, 129, 130, 131, 132, 133, 134, 135,
61 136, 137, 138, 139, 140, 141, 142, 143,
62 144, 145, 146, 147, 148, 149, 150, 151,
63 152, 153, 154, 155, 156, 157, 158, 159,
64 160, 161, 162, 163, 164, 165, 166, 167,
65 168, 169, 170, 171, 172, 173, 174, 175,
66 176, 177, 178, 179, 180, 181, 182, 183,
67 184, 185, 186, 187, 188, 189, 190, 191,
68 192, 193, 194, 195, 196, 197, 198, 199,
69 200, 201, 202, 203, 204, 205, 206, 207,
70 208, 209, 210, 211, 212, 213, 214, 215,
71 216, 217, 218, 219, 220, 221, 222, 223,
72 224, 225, 226, 227, 228
75 static const FT_UShort cff_expert_charset[166] =
77 0, 1, 229, 230, 231, 232, 233, 234,
78 235, 236, 237, 238, 13, 14, 15, 99,
79 239, 240, 241, 242, 243, 244, 245, 246,
80 247, 248, 27, 28, 249, 250, 251, 252,
81 253, 254, 255, 256, 257, 258, 259, 260,
82 261, 262, 263, 264, 265, 266, 109, 110,
83 267, 268, 269, 270, 271, 272, 273, 274,
84 275, 276, 277, 278, 279, 280, 281, 282,
85 283, 284, 285, 286, 287, 288, 289, 290,
86 291, 292, 293, 294, 295, 296, 297, 298,
87 299, 300, 301, 302, 303, 304, 305, 306,
88 307, 308, 309, 310, 311, 312, 313, 314,
89 315, 316, 317, 318, 158, 155, 163, 319,
90 320, 321, 322, 323, 324, 325, 326, 150,
91 164, 169, 327, 328, 329, 330, 331, 332,
92 333, 334, 335, 336, 337, 338, 339, 340,
93 341, 342, 343, 344, 345, 346, 347, 348,
94 349, 350, 351, 352, 353, 354, 355, 356,
95 357, 358, 359, 360, 361, 362, 363, 364,
96 365, 366, 367, 368, 369, 370, 371, 372,
97 373, 374, 375, 376, 377, 378
100 static const FT_UShort cff_expertsubset_charset[87] =
102 0, 1, 231, 232, 235, 236, 237, 238,
103 13, 14, 15, 99, 239, 240, 241, 242,
104 243, 244, 245, 246, 247, 248, 27, 28,
105 249, 250, 251, 253, 254, 255, 256, 257,
106 258, 259, 260, 261, 262, 263, 264, 265,
107 266, 109, 110, 267, 268, 269, 270, 272,
108 300, 301, 302, 305, 314, 315, 158, 155,
109 163, 320, 321, 322, 323, 324, 325, 326,
110 150, 164, 169, 327, 328, 329, 330, 331,
111 332, 333, 334, 335, 336, 337, 338, 339,
112 340, 341, 342, 343, 344, 345, 346
115 static const FT_UShort cff_standard_encoding[256] =
117 0, 0, 0, 0, 0, 0, 0, 0,
118 0, 0, 0, 0, 0, 0, 0, 0,
119 0, 0, 0, 0, 0, 0, 0, 0,
120 0, 0, 0, 0, 0, 0, 0, 0,
121 1, 2, 3, 4, 5, 6, 7, 8,
122 9, 10, 11, 12, 13, 14, 15, 16,
123 17, 18, 19, 20, 21, 22, 23, 24,
124 25, 26, 27, 28, 29, 30, 31, 32,
125 33, 34, 35, 36, 37, 38, 39, 40,
126 41, 42, 43, 44, 45, 46, 47, 48,
127 49, 50, 51, 52, 53, 54, 55, 56,
128 57, 58, 59, 60, 61, 62, 63, 64,
129 65, 66, 67, 68, 69, 70, 71, 72,
130 73, 74, 75, 76, 77, 78, 79, 80,
131 81, 82, 83, 84, 85, 86, 87, 88,
132 89, 90, 91, 92, 93, 94, 95, 0,
133 0, 0, 0, 0, 0, 0, 0, 0,
134 0, 0, 0, 0, 0, 0, 0, 0,
135 0, 0, 0, 0, 0, 0, 0, 0,
136 0, 0, 0, 0, 0, 0, 0, 0,
137 0, 96, 97, 98, 99, 100, 101, 102,
138 103, 104, 105, 106, 107, 108, 109, 110,
139 0, 111, 112, 113, 114, 0, 115, 116,
140 117, 118, 119, 120, 121, 122, 0, 123,
141 0, 124, 125, 126, 127, 128, 129, 130,
142 131, 0, 132, 133, 0, 134, 135, 136,
143 137, 0, 0, 0, 0, 0, 0, 0,
144 0, 0, 0, 0, 0, 0, 0, 0,
145 0, 138, 0, 139, 0, 0, 0, 0,
146 140, 141, 142, 143, 0, 0, 0, 0,
147 0, 144, 0, 0, 0, 145, 0, 0,
148 146, 147, 148, 149, 0, 0, 0, 0
151 static const FT_UShort cff_expert_encoding[256] =
153 0, 0, 0, 0, 0, 0, 0, 0,
154 0, 0, 0, 0, 0, 0, 0, 0,
155 0, 0, 0, 0, 0, 0, 0, 0,
156 0, 0, 0, 0, 0, 0, 0, 0,
157 1, 229, 230, 0, 231, 232, 233, 234,
158 235, 236, 237, 238, 13, 14, 15, 99,
159 239, 240, 241, 242, 243, 244, 245, 246,
160 247, 248, 27, 28, 249, 250, 251, 252,
161 0, 253, 254, 255, 256, 257, 0, 0,
162 0, 258, 0, 0, 259, 260, 261, 262,
163 0, 0, 263, 264, 265, 0, 266, 109,
164 110, 267, 268, 269, 0, 270, 271, 272,
165 273, 274, 275, 276, 277, 278, 279, 280,
166 281, 282, 283, 284, 285, 286, 287, 288,
167 289, 290, 291, 292, 293, 294, 295, 296,
168 297, 298, 299, 300, 301, 302, 303, 0,
169 0, 0, 0, 0, 0, 0, 0, 0,
170 0, 0, 0, 0, 0, 0, 0, 0,
171 0, 0, 0, 0, 0, 0, 0, 0,
172 0, 0, 0, 0, 0, 0, 0, 0,
173 0, 304, 305, 306, 0, 0, 307, 308,
174 309, 310, 311, 0, 312, 0, 0, 312,
175 0, 0, 314, 315, 0, 0, 316, 317,
176 318, 0, 0, 0, 158, 155, 163, 319,
177 320, 321, 322, 323, 324, 325, 0, 0,
178 326, 150, 164, 169, 327, 328, 329, 330,
179 331, 332, 333, 334, 335, 336, 337, 338,
180 339, 340, 341, 342, 343, 344, 345, 346,
181 347, 348, 349, 350, 351, 352, 353, 354,
182 355, 356, 357, 358, 359, 360, 361, 362,
183 363, 364, 365, 366, 367, 368, 369, 370,
184 371, 372, 373, 374, 375, 376, 377, 378
190 FT_LOCAL_DEF( FT_UShort )
191 cff_get_standard_encoding( FT_UInt charcode )
193 return (FT_UShort)( charcode < 256 ? cff_standard_encoding[charcode]
198 /**************************************************************************
200 * The macro FT_COMPONENT is used in trace mode. It is an implicit
201 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
202 * messages during execution.
205 #define FT_COMPONENT cffload
208 /* read an offset from the index's stream current position */
210 cff_index_read_offset( CFF_Index idx,
214 FT_Stream stream = idx->stream;
219 if ( !FT_STREAM_READ( tmp, idx->off_size ) )
224 for ( nn = 0; nn < idx->off_size; nn++ )
225 result = ( result << 8 ) | tmp[nn];
234 cff_index_init( CFF_Index idx,
240 FT_Memory memory = stream->memory;
246 idx->stream = stream;
247 idx->start = FT_STREAM_POS();
251 if ( FT_READ_ULONG( count ) )
257 if ( FT_READ_USHORT( count ) )
268 /* there is at least one element; read the offset size, */
269 /* then access the offset table to compute the index's total size */
270 if ( FT_READ_BYTE( offsize ) )
273 if ( offsize < 1 || offsize > 4 )
275 error = FT_THROW( Invalid_Table );
280 idx->off_size = offsize;
281 size = (FT_ULong)( count + 1 ) * offsize;
283 idx->data_offset = idx->start + idx->hdr_size + size;
285 if ( FT_STREAM_SKIP( size - offsize ) )
288 size = cff_index_read_offset( idx, &error );
294 error = FT_THROW( Invalid_Table );
298 idx->data_size = --size;
303 if ( FT_FRAME_EXTRACT( size, idx->bytes ) )
309 if ( FT_STREAM_SKIP( size ) )
316 FT_FREE( idx->offsets );
323 cff_index_done( CFF_Index idx )
327 FT_Stream stream = idx->stream;
328 FT_Memory memory = stream->memory;
332 FT_FRAME_RELEASE( idx->bytes );
334 FT_FREE( idx->offsets );
341 cff_index_load_offsets( CFF_Index idx )
343 FT_Error error = FT_Err_Ok;
344 FT_Stream stream = idx->stream;
345 FT_Memory memory = stream->memory;
348 if ( idx->count > 0 && !idx->offsets )
350 FT_Byte offsize = idx->off_size;
357 data_size = (FT_ULong)( idx->count + 1 ) * offsize;
359 if ( FT_QNEW_ARRAY( idx->offsets, idx->count + 1 ) ||
360 FT_STREAM_SEEK( idx->start + idx->hdr_size ) ||
361 FT_FRAME_ENTER( data_size ) )
365 p = (FT_Byte*)stream->cursor;
366 p_end = p + data_size;
371 for ( ; p < p_end; p++, poff++ )
376 for ( ; p < p_end; p += 2, poff++ )
377 poff[0] = FT_PEEK_USHORT( p );
381 for ( ; p < p_end; p += 3, poff++ )
382 poff[0] = FT_PEEK_UOFF3( p );
386 for ( ; p < p_end; p += 4, poff++ )
387 poff[0] = FT_PEEK_ULONG( p );
395 FT_FREE( idx->offsets );
401 /* Allocate a table containing pointers to an index's elements. */
402 /* The `pool' argument makes this function convert the index */
403 /* entries to C-style strings (that is, null-terminated). */
405 cff_index_get_pointers( CFF_Index idx,
408 FT_ULong* pool_size )
410 FT_Error error = FT_Err_Ok;
411 FT_Memory memory = idx->stream->memory;
413 FT_Byte** tbl = NULL;
414 FT_Byte* new_bytes = NULL;
422 error = cff_index_load_offsets( idx );
427 new_size = idx->data_size + idx->count;
429 if ( idx->count > 0 &&
430 !FT_QNEW_ARRAY( tbl, idx->count + 1 ) &&
431 ( !pool || !FT_ALLOC( new_bytes, new_size ) ) )
433 FT_ULong n, cur_offset;
435 FT_Byte* org_bytes = idx->bytes;
438 /* at this point, `idx->offsets' can't be NULL */
439 cur_offset = idx->offsets[0] - 1;
442 if ( cur_offset != 0 )
444 FT_TRACE0(( "cff_index_get_pointers:"
445 " invalid first offset value %ld set to zero\n",
451 tbl[0] = org_bytes + cur_offset;
453 tbl[0] = new_bytes + cur_offset;
455 for ( n = 1; n <= idx->count; n++ )
457 FT_ULong next_offset = idx->offsets[n] - 1;
460 /* two sanity checks for invalid offset tables */
461 if ( next_offset < cur_offset )
462 next_offset = cur_offset;
463 else if ( next_offset > idx->data_size )
464 next_offset = idx->data_size;
467 tbl[n] = org_bytes + next_offset;
470 tbl[n] = new_bytes + next_offset + extra;
472 if ( next_offset != cur_offset )
474 FT_MEM_COPY( tbl[n - 1],
475 org_bytes + cur_offset,
476 tbl[n] - tbl[n - 1] );
483 cur_offset = next_offset;
490 *pool_size = new_size;
494 if ( error && new_bytes )
495 FT_FREE( new_bytes );
503 FT_LOCAL_DEF( FT_Error )
504 cff_index_access_element( CFF_Index idx,
507 FT_ULong* pbyte_len )
509 FT_Error error = FT_Err_Ok;
512 if ( idx && idx->count > element )
514 /* compute start and end offsets */
515 FT_Stream stream = idx->stream;
516 FT_ULong off1, off2 = 0;
519 /* load offsets from file or the offset table */
522 FT_ULong pos = element * idx->off_size;
525 if ( FT_STREAM_SEEK( idx->start + idx->hdr_size + pos ) )
528 off1 = cff_index_read_offset( idx, &error );
537 off2 = cff_index_read_offset( idx, &error );
539 } while ( off2 == 0 && element < idx->count );
542 else /* use offsets table */
544 off1 = idx->offsets[element];
550 off2 = idx->offsets[element];
552 } while ( off2 == 0 && element < idx->count );
556 /* XXX: should check off2 does not exceed the end of this entry; */
557 /* at present, only truncate off2 at the end of this stream */
558 if ( off2 > stream->size + 1 ||
559 idx->data_offset > stream->size - off2 + 1 )
561 FT_ERROR(( "cff_index_access_element:"
562 " offset to next entry (%ld)"
563 " exceeds the end of stream (%ld)\n",
564 off2, stream->size - idx->data_offset + 1 ));
565 off2 = stream->size - idx->data_offset + 1;
569 if ( off1 && off2 > off1 )
571 *pbyte_len = off2 - off1;
575 /* this index was completely loaded in memory, that's easy */
576 *pbytes = idx->bytes + off1 - 1;
580 /* this index is still on disk/file, access it through a frame */
581 if ( FT_STREAM_SEEK( idx->data_offset + off1 - 1 ) ||
582 FT_FRAME_EXTRACT( off2 - off1, *pbytes ) )
588 /* empty index element */
594 error = FT_THROW( Invalid_Argument );
602 cff_index_forget_element( CFF_Index idx,
605 if ( idx->bytes == 0 )
607 FT_Stream stream = idx->stream;
610 FT_FRAME_RELEASE( *pbytes );
615 /* get an entry from Name INDEX */
616 FT_LOCAL_DEF( FT_String* )
617 cff_index_get_name( CFF_Font font,
620 CFF_Index idx = &font->name_index;
625 FT_String* name = NULL;
628 if ( !idx->stream ) /* CFF2 does not include a name index */
631 memory = idx->stream->memory;
633 error = cff_index_access_element( idx, element, &bytes, &byte_len );
637 if ( !FT_QALLOC( name, byte_len + 1 ) )
639 FT_MEM_COPY( name, bytes, byte_len );
642 cff_index_forget_element( idx, &bytes );
649 /* get an entry from String INDEX */
650 FT_LOCAL_DEF( FT_String* )
651 cff_index_get_string( CFF_Font font,
654 return ( element < font->num_strings )
655 ? (FT_String*)font->strings[element]
660 FT_LOCAL_DEF( FT_String* )
661 cff_index_get_sid_string( CFF_Font font,
664 /* value 0xFFFFU indicates a missing dictionary entry */
665 if ( sid == 0xFFFFU )
668 /* if it is not a standard string, return it */
670 return cff_index_get_string( font, sid - 391 );
672 /* CID-keyed CFF fonts don't have glyph names */
673 if ( !font->psnames )
676 /* this is a standard string */
677 return (FT_String *)font->psnames->adobe_std_strings( sid );
681 /*************************************************************************/
682 /*************************************************************************/
684 /*** FD Select table support ***/
686 /*************************************************************************/
687 /*************************************************************************/
691 CFF_Done_FD_Select( CFF_FDSelect fdselect,
694 if ( fdselect->data )
695 FT_FRAME_RELEASE( fdselect->data );
697 fdselect->data_size = 0;
698 fdselect->format = 0;
699 fdselect->range_count = 0;
704 CFF_Load_FD_Select( CFF_FDSelect fdselect,
715 if ( FT_STREAM_SEEK( offset ) || FT_READ_BYTE( format ) )
718 fdselect->format = format;
719 fdselect->cache_count = 0; /* clear cache */
723 case 0: /* format 0, that's simple */
724 fdselect->data_size = num_glyphs;
727 case 3: /* format 3, a tad more complex */
728 if ( FT_READ_USHORT( num_ranges ) )
733 FT_TRACE0(( "CFF_Load_FD_Select: empty FDSelect array\n" ));
734 error = FT_THROW( Invalid_File_Format );
738 fdselect->data_size = num_ranges * 3 + 2;
741 if ( FT_FRAME_EXTRACT( fdselect->data_size, fdselect->data ) )
745 default: /* hmm... that's wrong */
746 error = FT_THROW( Invalid_File_Format );
754 FT_LOCAL_DEF( FT_Byte )
755 cff_fd_select_get( CFF_FDSelect fdselect,
756 FT_UInt glyph_index )
761 /* if there is no FDSelect, return zero */
762 /* Note: CFF2 with just one Font Dict has no FDSelect */
763 if ( !fdselect->data )
766 switch ( fdselect->format )
769 fd = fdselect->data[glyph_index];
773 /* first, compare to the cache */
774 if ( glyph_index - fdselect->cache_first < fdselect->cache_count )
776 fd = fdselect->cache_fd;
780 /* then, look up the ranges array */
782 FT_Byte* p = fdselect->data;
783 FT_Byte* p_limit = p + fdselect->data_size;
785 FT_UInt first, limit;
788 first = FT_NEXT_USHORT( p );
791 if ( glyph_index < first )
795 limit = FT_NEXT_USHORT( p );
797 if ( glyph_index < limit )
802 fdselect->cache_first = first;
803 fdselect->cache_count = limit - first;
804 fdselect->cache_fd = fd2;
809 } while ( p < p_limit );
822 /*************************************************************************/
823 /*************************************************************************/
825 /*** CFF font support ***/
827 /*************************************************************************/
828 /*************************************************************************/
831 cff_charset_compute_cids( CFF_Charset charset,
835 FT_Error error = FT_Err_Ok;
837 FT_UShort max_cid = 0;
840 if ( charset->max_cid > 0 )
843 for ( i = 0; i < num_glyphs; i++ )
845 if ( charset->sids[i] > max_cid )
846 max_cid = charset->sids[i];
849 if ( FT_NEW_ARRAY( charset->cids, (FT_ULong)max_cid + 1 ) )
852 /* When multiple GIDs map to the same CID, we choose the lowest */
853 /* GID. This is not described in any spec, but it matches the */
854 /* behaviour of recent Acroread versions. The loop stops when */
855 /* the unsigned index wraps around after reaching zero. */
856 for ( i = num_glyphs - 1; i < num_glyphs; i-- )
857 charset->cids[charset->sids[i]] = (FT_UShort)i;
859 charset->max_cid = max_cid;
860 charset->num_glyphs = num_glyphs;
867 FT_LOCAL_DEF( FT_UInt )
868 cff_charset_cid_to_gindex( CFF_Charset charset,
874 if ( cid <= charset->max_cid )
875 result = charset->cids[cid];
882 cff_charset_free_cids( CFF_Charset charset,
885 FT_FREE( charset->cids );
886 charset->max_cid = 0;
891 cff_charset_done( CFF_Charset charset,
894 FT_Memory memory = stream->memory;
897 cff_charset_free_cids( charset, memory );
899 FT_FREE( charset->sids );
906 cff_charset_load( CFF_Charset charset,
909 FT_ULong base_offset,
913 FT_Memory memory = stream->memory;
914 FT_Error error = FT_Err_Ok;
918 /* If the offset is greater than 2, we have to parse the charset */
925 charset->offset = base_offset + offset;
927 /* Get the format of the table. */
928 if ( FT_STREAM_SEEK( charset->offset ) ||
929 FT_READ_BYTE( charset->format ) )
932 /* Allocate memory for sids. */
933 if ( FT_QNEW_ARRAY( charset->sids, num_glyphs ) )
936 /* assign the .notdef glyph */
937 charset->sids[0] = 0;
939 switch ( charset->format )
942 if ( num_glyphs > 0 )
944 if ( FT_FRAME_ENTER( ( num_glyphs - 1 ) * 2 ) )
947 for ( j = 1; j < num_glyphs; j++ )
948 charset->sids[j] = FT_GET_USHORT();
963 while ( j < num_glyphs )
965 /* Read the first glyph sid of the range. */
966 if ( FT_READ_USHORT( glyph_sid ) )
969 /* Read the number of glyphs in the range. */
970 if ( charset->format == 2 )
972 if ( FT_READ_USHORT( nleft ) )
977 if ( FT_READ_BYTE( nleft ) )
981 /* try to rescue some of the SIDs if `nleft' is too large */
982 if ( glyph_sid > 0xFFFFL - nleft )
984 FT_ERROR(( "cff_charset_load: invalid SID range trimmed"
985 " nleft=%d -> %ld\n", nleft, 0xFFFFL - glyph_sid ));
986 nleft = ( FT_UInt )( 0xFFFFL - glyph_sid );
989 /* Fill in the range of sids -- `nleft + 1' glyphs. */
990 for ( i = 0; j < num_glyphs && i <= nleft; i++, j++, glyph_sid++ )
991 charset->sids[j] = glyph_sid;
997 FT_ERROR(( "cff_charset_load: invalid table format\n" ));
998 error = FT_THROW( Invalid_File_Format );
1004 /* Parse default tables corresponding to offset == 0, 1, or 2. */
1005 /* CFF specification intimates the following: */
1007 /* In order to use a predefined charset, the following must be */
1008 /* true: The charset constructed for the glyphs in the font's */
1009 /* charstrings dictionary must match the predefined charset in */
1010 /* the first num_glyphs. */
1012 charset->offset = offset; /* record charset type */
1014 switch ( (FT_UInt)offset )
1017 if ( num_glyphs > 229 )
1019 FT_ERROR(( "cff_charset_load: implicit charset larger than\n" ));
1020 FT_ERROR(( "predefined charset (Adobe ISO-Latin)\n" ));
1021 error = FT_THROW( Invalid_File_Format );
1025 /* Allocate memory for sids. */
1026 if ( FT_QNEW_ARRAY( charset->sids, num_glyphs ) )
1029 /* Copy the predefined charset into the allocated memory. */
1030 FT_ARRAY_COPY( charset->sids, cff_isoadobe_charset, num_glyphs );
1035 if ( num_glyphs > 166 )
1037 FT_ERROR(( "cff_charset_load: implicit charset larger than\n" ));
1038 FT_ERROR(( "predefined charset (Adobe Expert)\n" ));
1039 error = FT_THROW( Invalid_File_Format );
1043 /* Allocate memory for sids. */
1044 if ( FT_QNEW_ARRAY( charset->sids, num_glyphs ) )
1047 /* Copy the predefined charset into the allocated memory. */
1048 FT_ARRAY_COPY( charset->sids, cff_expert_charset, num_glyphs );
1053 if ( num_glyphs > 87 )
1055 FT_ERROR(( "cff_charset_load: implicit charset larger than\n" ));
1056 FT_ERROR(( "predefined charset (Adobe Expert Subset)\n" ));
1057 error = FT_THROW( Invalid_File_Format );
1061 /* Allocate memory for sids. */
1062 if ( FT_QNEW_ARRAY( charset->sids, num_glyphs ) )
1065 /* Copy the predefined charset into the allocated memory. */
1066 FT_ARRAY_COPY( charset->sids, cff_expertsubset_charset, num_glyphs );
1071 error = FT_THROW( Invalid_File_Format );
1076 /* we have to invert the `sids' array for subsetted CID-keyed fonts */
1078 error = cff_charset_compute_cids( charset, num_glyphs, memory );
1081 /* Clean up if there was an error. */
1084 FT_FREE( charset->sids );
1085 FT_FREE( charset->cids );
1086 charset->format = 0;
1087 charset->offset = 0;
1095 cff_vstore_done( CFF_VStoreRec* vstore,
1101 /* free regionList and axisLists */
1102 if ( vstore->varRegionList )
1104 for ( i = 0; i < vstore->regionCount; i++ )
1105 FT_FREE( vstore->varRegionList[i].axisList );
1107 FT_FREE( vstore->varRegionList );
1109 /* free varData and indices */
1110 if ( vstore->varData )
1112 for ( i = 0; i < vstore->dataCount; i++ )
1113 FT_FREE( vstore->varData[i].regionIndices );
1115 FT_FREE( vstore->varData );
1119 /* convert 2.14 to Fixed */
1120 #define FT_fdot14ToFixed( x ) ( (FT_Fixed)( (FT_ULong)(x) << 2 ) )
1124 cff_vstore_load( CFF_VStoreRec* vstore,
1126 FT_ULong base_offset,
1129 FT_Memory memory = stream->memory;
1130 FT_Error error = FT_ERR( Invalid_File_Format );
1132 FT_ULong* dataOffsetArray = NULL;
1136 /* no offset means no vstore to parse */
1142 FT_UInt regionCount;
1143 FT_ULong regionListOffset;
1146 /* we need to parse the table to determine its size; */
1147 /* skip table length */
1148 if ( FT_STREAM_SEEK( base_offset + offset ) ||
1149 FT_STREAM_SKIP( 2 ) )
1152 /* actual variation store begins after the length */
1153 vsOffset = FT_STREAM_POS();
1155 /* check the header */
1156 if ( FT_READ_USHORT( format ) )
1160 error = FT_THROW( Invalid_File_Format );
1164 /* read top level fields */
1165 if ( FT_READ_ULONG( regionListOffset ) ||
1166 FT_READ_USHORT( dataCount ) )
1169 /* make temporary copy of item variation data offsets; */
1170 /* we'll parse region list first, then come back */
1171 if ( FT_QNEW_ARRAY( dataOffsetArray, dataCount ) )
1174 for ( i = 0; i < dataCount; i++ )
1176 if ( FT_READ_ULONG( dataOffsetArray[i] ) )
1180 /* parse regionList and axisLists */
1181 if ( FT_STREAM_SEEK( vsOffset + regionListOffset ) ||
1182 FT_READ_USHORT( vstore->axisCount ) ||
1183 FT_READ_USHORT( regionCount ) )
1186 vstore->regionCount = 0;
1187 if ( FT_QNEW_ARRAY( vstore->varRegionList, regionCount ) )
1190 for ( i = 0; i < regionCount; i++ )
1192 CFF_VarRegion* region = &vstore->varRegionList[i];
1195 if ( FT_QNEW_ARRAY( region->axisList, vstore->axisCount ) )
1198 /* keep track of how many axisList to deallocate on error */
1199 vstore->regionCount++;
1201 for ( j = 0; j < vstore->axisCount; j++ )
1203 CFF_AxisCoords* axis = ®ion->axisList[j];
1205 FT_Int16 start14, peak14, end14;
1208 if ( FT_READ_SHORT( start14 ) ||
1209 FT_READ_SHORT( peak14 ) ||
1210 FT_READ_SHORT( end14 ) )
1213 axis->startCoord = FT_fdot14ToFixed( start14 );
1214 axis->peakCoord = FT_fdot14ToFixed( peak14 );
1215 axis->endCoord = FT_fdot14ToFixed( end14 );
1219 /* use dataOffsetArray now to parse varData items */
1220 vstore->dataCount = 0;
1221 if ( FT_QNEW_ARRAY( vstore->varData, dataCount ) )
1224 for ( i = 0; i < dataCount; i++ )
1226 CFF_VarData* data = &vstore->varData[i];
1229 if ( FT_STREAM_SEEK( vsOffset + dataOffsetArray[i] ) )
1232 /* ignore `itemCount' and `shortDeltaCount' */
1233 /* because CFF2 has no delta sets */
1234 if ( FT_STREAM_SKIP( 4 ) )
1237 /* Note: just record values; consistency is checked later */
1238 /* by cff_blend_build_vector when it consumes `vstore' */
1240 if ( FT_READ_USHORT( data->regionIdxCount ) )
1243 if ( FT_QNEW_ARRAY( data->regionIndices, data->regionIdxCount ) )
1246 /* keep track of how many regionIndices to deallocate on error */
1247 vstore->dataCount++;
1249 for ( j = 0; j < data->regionIdxCount; j++ )
1251 if ( FT_READ_USHORT( data->regionIndices[j] ) )
1260 FT_FREE( dataOffsetArray );
1262 cff_vstore_done( vstore, memory );
1268 /* Clear blend stack (after blend values are consumed). */
1270 /* TODO: Should do this in cff_run_parse, but subFont */
1271 /* ref is not available there. */
1273 /* Allocation is not changed when stack is cleared. */
1274 FT_LOCAL_DEF( void )
1275 cff_blend_clear( CFF_SubFont subFont )
1277 subFont->blend_top = subFont->blend_stack;
1278 subFont->blend_used = 0;
1282 /* Blend numOperands on the stack, */
1283 /* store results into the first numBlends values, */
1284 /* then pop remaining arguments. */
1286 /* This is comparable to `cf2_doBlend' but */
1287 /* the cffparse stack is different and can't be written. */
1288 /* Blended values are written to a different buffer, */
1289 /* using reserved operator 255. */
1291 /* Blend calculation is done in 16.16 fixed-point. */
1292 FT_LOCAL_DEF( FT_Error )
1293 cff_blend_doBlend( CFF_SubFont subFont,
1302 CFF_Blend blend = &subFont->blend;
1304 FT_Memory memory = subFont->blend.font->memory; /* for FT_REALLOC */
1305 FT_Error error = FT_Err_Ok; /* for FT_REALLOC */
1307 /* compute expected number of operands for this blend */
1308 FT_UInt numOperands = (FT_UInt)( numBlends * blend->lenBV );
1309 FT_UInt count = (FT_UInt)( parser->top - 1 - parser->stack );
1312 if ( numOperands > count )
1314 FT_TRACE4(( " cff_blend_doBlend: Stack underflow %d argument%s\n",
1316 count == 1 ? "" : "s" ));
1318 error = FT_THROW( Stack_Underflow );
1322 /* check whether we have room for `numBlends' values at `blend_top' */
1323 size = 5 * numBlends; /* add 5 bytes per entry */
1324 if ( subFont->blend_used + size > subFont->blend_alloc )
1326 FT_Byte* blend_stack_old = subFont->blend_stack;
1327 FT_Byte* blend_top_old = subFont->blend_top;
1330 /* increase or allocate `blend_stack' and reset `blend_top'; */
1331 /* prepare to append `numBlends' values to the buffer */
1332 if ( FT_QREALLOC( subFont->blend_stack,
1333 subFont->blend_alloc,
1334 subFont->blend_alloc + size ) )
1337 subFont->blend_top = subFont->blend_stack + subFont->blend_used;
1338 subFont->blend_alloc += size;
1340 /* iterate over the parser stack and adjust pointers */
1341 /* if the reallocated buffer has a different address */
1342 if ( blend_stack_old &&
1343 subFont->blend_stack != blend_stack_old )
1345 FT_PtrDist offset = subFont->blend_stack - blend_stack_old;
1349 for ( p = parser->stack; p < parser->top; p++ )
1351 if ( *p >= blend_stack_old && *p < blend_top_old )
1356 subFont->blend_used += size;
1358 base = count - numOperands; /* index of first blend arg */
1359 delta = base + numBlends; /* index of first delta arg */
1361 for ( i = 0; i < numBlends; i++ )
1363 const FT_Int32* weight = &blend->BV[1];
1367 /* convert inputs to 16.16 fixed point */
1368 sum = cff_parse_fixed( parser, &parser->stack[i + base] );
1370 for ( j = 1; j < blend->lenBV; j++ )
1371 sum += FT_MulFix( cff_parse_fixed( parser, &parser->stack[delta++] ),
1374 /* point parser stack to new value on blend_stack */
1375 parser->stack[i + base] = subFont->blend_top;
1377 /* Push blended result as Type 2 5-byte fixed-point number. This */
1378 /* will not conflict with actual DICTs because 255 is a reserved */
1379 /* opcode in both CFF and CFF2 DICTs. See `cff_parse_num' for */
1380 /* decode of this, which rounds to an integer. */
1381 *subFont->blend_top++ = 255;
1382 *subFont->blend_top++ = (FT_Byte)( sum >> 24 );
1383 *subFont->blend_top++ = (FT_Byte)( sum >> 16 );
1384 *subFont->blend_top++ = (FT_Byte)( sum >> 8 );
1385 *subFont->blend_top++ = (FT_Byte)sum;
1388 /* leave only numBlends results on parser stack */
1389 parser->top = &parser->stack[base + numBlends];
1396 /* Compute a blend vector from variation store index and normalized */
1397 /* vector based on pseudo-code in OpenType Font Variations Overview. */
1399 /* Note: lenNDV == 0 produces a default blend vector, (1,0,0,...). */
1400 FT_LOCAL_DEF( FT_Error )
1401 cff_blend_build_vector( CFF_Blend blend,
1406 FT_Error error = FT_Err_Ok; /* for FT_REALLOC */
1407 FT_Memory memory = blend->font->memory; /* for FT_REALLOC */
1411 CFF_VarData* varData;
1415 /* protect against malformed fonts */
1416 if ( !( lenNDV == 0 || NDV ) )
1418 FT_TRACE4(( " cff_blend_build_vector:"
1419 " Malformed Normalize Design Vector data\n" ));
1420 error = FT_THROW( Invalid_File_Format );
1424 blend->builtBV = FALSE;
1426 vs = &blend->font->vstore;
1428 /* VStore and fvar must be consistent */
1429 if ( lenNDV != 0 && lenNDV != vs->axisCount )
1431 FT_TRACE4(( " cff_blend_build_vector: Axis count mismatch\n" ));
1432 error = FT_THROW( Invalid_File_Format );
1436 if ( vsindex >= vs->dataCount )
1438 FT_TRACE4(( " cff_blend_build_vector: vsindex out of range\n" ));
1439 error = FT_THROW( Invalid_File_Format );
1443 /* select the item variation data structure */
1444 varData = &vs->varData[vsindex];
1446 /* prepare buffer for the blend vector */
1447 len = varData->regionIdxCount + 1; /* add 1 for default component */
1448 if ( FT_QRENEW_ARRAY( blend->BV, blend->lenBV, len ) )
1453 /* outer loop steps through master designs to be blended */
1454 for ( master = 0; master < len; master++ )
1458 CFF_VarRegion* varRegion;
1461 /* default factor is always one */
1464 blend->BV[master] = FT_FIXED_ONE;
1465 FT_TRACE4(( " build blend vector len %d\n", len ));
1466 FT_TRACE4(( " [ %f ", blend->BV[master] / 65536.0 ));
1470 /* VStore array does not include default master, so subtract one */
1471 idx = varData->regionIndices[master - 1];
1472 varRegion = &vs->varRegionList[idx];
1474 if ( idx >= vs->regionCount )
1476 FT_TRACE4(( " cff_blend_build_vector:"
1477 " region index out of range\n" ));
1478 error = FT_THROW( Invalid_File_Format );
1482 /* Note: `lenNDV' could be zero. */
1483 /* In that case, build default blend vector (1,0,0...). */
1486 blend->BV[master] = 0;
1490 /* In the normal case, initialize each component to 1 */
1491 /* before inner loop. */
1492 blend->BV[master] = FT_FIXED_ONE; /* default */
1494 /* inner loop steps through axes in this region */
1495 for ( j = 0; j < lenNDV; j++ )
1497 CFF_AxisCoords* axis = &varRegion->axisList[j];
1498 FT_Fixed axisScalar;
1501 /* compute the scalar contribution of this axis; */
1502 /* ignore invalid ranges */
1503 if ( axis->startCoord > axis->peakCoord ||
1504 axis->peakCoord > axis->endCoord )
1505 axisScalar = FT_FIXED_ONE;
1507 else if ( axis->startCoord < 0 &&
1508 axis->endCoord > 0 &&
1509 axis->peakCoord != 0 )
1510 axisScalar = FT_FIXED_ONE;
1512 /* peak of 0 means ignore this axis */
1513 else if ( axis->peakCoord == 0 )
1514 axisScalar = FT_FIXED_ONE;
1516 /* ignore this region if coords are out of range */
1517 else if ( NDV[j] < axis->startCoord ||
1518 NDV[j] > axis->endCoord )
1521 /* calculate a proportional factor */
1524 if ( NDV[j] == axis->peakCoord )
1525 axisScalar = FT_FIXED_ONE;
1526 else if ( NDV[j] < axis->peakCoord )
1527 axisScalar = FT_DivFix( NDV[j] - axis->startCoord,
1528 axis->peakCoord - axis->startCoord );
1530 axisScalar = FT_DivFix( axis->endCoord - NDV[j],
1531 axis->endCoord - axis->peakCoord );
1534 /* take product of all the axis scalars */
1535 blend->BV[master] = FT_MulFix( blend->BV[master], axisScalar );
1538 FT_TRACE4(( ", %f ",
1539 blend->BV[master] / 65536.0 ));
1542 FT_TRACE4(( "]\n" ));
1544 /* record the parameters used to build the blend vector */
1545 blend->lastVsindex = vsindex;
1549 /* user has set a normalized vector */
1550 if ( FT_QRENEW_ARRAY( blend->lastNDV, blend->lenNDV, lenNDV ) )
1553 FT_MEM_COPY( blend->lastNDV,
1555 lenNDV * sizeof ( *NDV ) );
1558 blend->lenNDV = lenNDV;
1559 blend->builtBV = TRUE;
1566 /* `lenNDV' is zero for default vector; */
1567 /* return TRUE if blend vector needs to be built. */
1568 FT_LOCAL_DEF( FT_Bool )
1569 cff_blend_check_vector( CFF_Blend blend,
1574 if ( !blend->builtBV ||
1575 blend->lastVsindex != vsindex ||
1576 blend->lenNDV != lenNDV ||
1580 lenNDV * sizeof ( *NDV ) ) != 0 ) )
1582 /* need to build blend vector */
1590 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1592 FT_LOCAL_DEF( FT_Error )
1593 cff_get_var_blend( FT_Face face, /* CFF_Face */
1594 FT_UInt *num_coords,
1596 FT_Fixed* *normalizedcoords,
1597 FT_MM_Var* *mm_var )
1599 CFF_Face cffface = (CFF_Face)face;
1600 FT_Service_MultiMasters mm = (FT_Service_MultiMasters)cffface->mm;
1603 return mm->get_var_blend( face,
1611 FT_LOCAL_DEF( void )
1612 cff_done_blend( FT_Face face ) /* CFF_Face */
1614 CFF_Face cffface = (CFF_Face)face;
1615 FT_Service_MultiMasters mm = (FT_Service_MultiMasters)cffface->mm;
1619 mm->done_blend( face );
1622 #endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
1626 cff_encoding_done( CFF_Encoding encoding )
1628 encoding->format = 0;
1629 encoding->offset = 0;
1630 encoding->count = 0;
1635 cff_encoding_load( CFF_Encoding encoding,
1636 CFF_Charset charset,
1639 FT_ULong base_offset,
1642 FT_Error error = FT_Err_Ok;
1645 FT_UShort glyph_sid;
1649 /* Check for charset->sids. If we do not have this, we fail. */
1650 if ( !charset->sids )
1652 error = FT_THROW( Invalid_File_Format );
1656 /* Note: The encoding table in a CFF font is indexed by glyph index; */
1657 /* the first encoded glyph index is 1. Hence, we read the character */
1658 /* code (`glyph_code') at index j and make the assignment: */
1660 /* encoding->codes[glyph_code] = j + 1 */
1662 /* We also make the assignment: */
1664 /* encoding->sids[glyph_code] = charset->sids[j + 1] */
1666 /* This gives us both a code to GID and a code to SID mapping. */
1670 /* Zero out the code to gid/sid mappings. */
1671 FT_ARRAY_ZERO( encoding->sids, 256 );
1672 FT_ARRAY_ZERO( encoding->codes, 256 );
1674 encoding->offset = base_offset + offset;
1676 /* we need to parse the table to determine its size */
1677 if ( FT_STREAM_SEEK( encoding->offset ) ||
1678 FT_READ_BYTE( encoding->format ) ||
1679 FT_READ_BYTE( count ) )
1682 switch ( encoding->format & 0x7F )
1689 /* By convention, GID 0 is always ".notdef" and is never */
1690 /* coded in the font. Hence, the number of codes found */
1691 /* in the table is `count+1'. */
1693 encoding->count = count + 1;
1695 if ( FT_FRAME_ENTER( count ) )
1698 p = (FT_Byte*)stream->cursor;
1700 for ( j = 1; j <= count; j++ )
1704 /* Make sure j is not too big. */
1705 if ( j < num_glyphs )
1707 /* Assign code to GID mapping. */
1708 encoding->codes[glyph_code] = (FT_UShort)j;
1710 /* Assign code to SID mapping. */
1711 encoding->sids[glyph_code] = charset->sids[j];
1726 encoding->count = 0;
1728 /* Parse the Format1 ranges. */
1729 for ( j = 0; j < count; j++, i += nleft )
1731 /* Read the first glyph code of the range. */
1732 if ( FT_READ_BYTE( glyph_code ) )
1735 /* Read the number of codes in the range. */
1736 if ( FT_READ_BYTE( nleft ) )
1739 /* Increment nleft, so we read `nleft + 1' codes/sids. */
1742 /* compute max number of character codes */
1743 if ( (FT_UInt)nleft > encoding->count )
1744 encoding->count = nleft;
1746 /* Fill in the range of codes/sids. */
1747 for ( k = i; k < nleft + i; k++, glyph_code++ )
1749 /* Make sure k is not too big. */
1750 if ( k < num_glyphs && glyph_code < 256 )
1752 /* Assign code to GID mapping. */
1753 encoding->codes[glyph_code] = (FT_UShort)k;
1755 /* Assign code to SID mapping. */
1756 encoding->sids[glyph_code] = charset->sids[k];
1761 /* simple check; one never knows what can be found in a font */
1762 if ( encoding->count > 256 )
1763 encoding->count = 256;
1768 FT_ERROR(( "cff_encoding_load: invalid table format\n" ));
1769 error = FT_THROW( Invalid_File_Format );
1773 /* Parse supplemental encodings, if any. */
1774 if ( encoding->format & 0x80 )
1779 /* count supplements */
1780 if ( FT_READ_BYTE( count ) )
1783 for ( j = 0; j < count; j++ )
1785 /* Read supplemental glyph code. */
1786 if ( FT_READ_BYTE( glyph_code ) )
1789 /* Read the SID associated with this glyph code. */
1790 if ( FT_READ_USHORT( glyph_sid ) )
1793 /* Assign code to SID mapping. */
1794 encoding->sids[glyph_code] = glyph_sid;
1796 /* First, look up GID which has been assigned to */
1797 /* SID glyph_sid. */
1798 for ( gindex = 0; gindex < num_glyphs; gindex++ )
1800 if ( charset->sids[gindex] == glyph_sid )
1802 encoding->codes[glyph_code] = (FT_UShort)gindex;
1811 /* We take into account the fact a CFF font can use a predefined */
1812 /* encoding without containing all of the glyphs encoded by this */
1813 /* encoding (see the note at the end of section 12 in the CFF */
1814 /* specification). */
1816 switch ( (FT_UInt)offset )
1819 /* First, copy the code to SID mapping. */
1820 FT_ARRAY_COPY( encoding->sids, cff_standard_encoding, 256 );
1824 /* First, copy the code to SID mapping. */
1825 FT_ARRAY_COPY( encoding->sids, cff_expert_encoding, 256 );
1828 /* Construct code to GID mapping from code to SID mapping */
1831 encoding->offset = offset; /* used in cff_face_init */
1832 encoding->count = 0;
1834 error = cff_charset_compute_cids( charset, num_glyphs,
1839 for ( j = 0; j < 256; j++ )
1841 FT_UInt sid = encoding->sids[j];
1846 gid = cff_charset_cid_to_gindex( charset, sid );
1850 encoding->codes[j] = (FT_UShort)gid;
1851 encoding->count = j + 1;
1855 encoding->codes[j] = 0;
1856 encoding->sids [j] = 0;
1862 FT_ERROR(( "cff_encoding_load: invalid table format\n" ));
1863 error = FT_THROW( Invalid_File_Format );
1870 /* Clean up if there was an error. */
1875 /* Parse private dictionary; first call is always from `cff_face_init', */
1876 /* so NDV has not been set for CFF2 variation. */
1878 /* `cff_slot_load' must call this function each time NDV changes. */
1879 FT_LOCAL_DEF( FT_Error )
1880 cff_load_private_dict( CFF_Font font,
1881 CFF_SubFont subfont,
1885 FT_Error error = FT_Err_Ok;
1886 CFF_ParserRec parser;
1887 CFF_FontRecDict top = &subfont->font_dict;
1888 CFF_Private priv = &subfont->private_dict;
1889 FT_Stream stream = font->stream;
1893 /* store handle needed to access memory, vstore for blend; */
1894 /* we need this for clean-up even if there is no private DICT */
1895 subfont->blend.font = font;
1896 subfont->blend.usedBV = FALSE; /* clear state */
1898 if ( !top->private_offset || !top->private_size )
1899 goto Exit2; /* no private DICT, do nothing */
1904 priv->blue_shift = 7;
1905 priv->blue_fuzz = 1;
1907 priv->expansion_factor = (FT_Fixed)( 0.06 * 0x10000L );
1908 priv->blue_scale = (FT_Fixed)( 0.039625 * 0x10000L * 1000 );
1910 /* provide inputs for blend calculations */
1911 priv->subfont = subfont;
1912 subfont->lenNDV = lenNDV;
1915 /* add 1 for the operator */
1916 stackSize = font->cff2 ? font->top_font.font_dict.maxstack + 1
1917 : CFF_MAX_STACK_DEPTH + 1;
1919 if ( cff_parser_init( &parser,
1920 font->cff2 ? CFF2_CODE_PRIVATE : CFF_CODE_PRIVATE,
1928 if ( FT_STREAM_SEEK( font->base_offset + top->private_offset ) ||
1929 FT_FRAME_ENTER( top->private_size ) )
1932 FT_TRACE4(( " private dictionary:\n" ));
1933 error = cff_parser_run( &parser,
1934 (FT_Byte*)stream->cursor,
1935 (FT_Byte*)stream->limit );
1941 /* ensure that `num_blue_values' is even */
1942 priv->num_blue_values &= ~1;
1944 /* sanitize `initialRandomSeed' to be a positive value, if necessary; */
1945 /* this is not mandated by the specification but by our implementation */
1946 if ( priv->initial_random_seed < 0 )
1947 priv->initial_random_seed = -priv->initial_random_seed;
1948 else if ( priv->initial_random_seed == 0 )
1949 priv->initial_random_seed = 987654321;
1951 /* some sanitizing to avoid overflows later on; */
1952 /* the upper limits are ad-hoc values */
1953 if ( priv->blue_shift > 1000 || priv->blue_shift < 0 )
1955 FT_TRACE2(( "cff_load_private_dict:"
1956 " setting unlikely BlueShift value %ld to default (7)\n",
1957 priv->blue_shift ));
1958 priv->blue_shift = 7;
1961 if ( priv->blue_fuzz > 1000 || priv->blue_fuzz < 0 )
1963 FT_TRACE2(( "cff_load_private_dict:"
1964 " setting unlikely BlueFuzz value %ld to default (1)\n",
1966 priv->blue_fuzz = 1;
1971 cff_blend_clear( subfont ); /* clear blend stack */
1972 cff_parser_done( &parser ); /* free parser stack */
1975 /* no clean up (parser not initialized) */
1980 /* There are 3 ways to call this function, distinguished by code. */
1982 /* . CFF_CODE_TOPDICT for either a CFF Top DICT or a CFF Font DICT */
1983 /* . CFF2_CODE_TOPDICT for CFF2 Top DICT */
1984 /* . CFF2_CODE_FONTDICT for CFF2 Font DICT */
1987 cff_subfont_load( CFF_SubFont subfont,
1991 FT_ULong base_offset,
1997 CFF_ParserRec parser;
1998 FT_Byte* dict = NULL;
2000 CFF_FontRecDict top = &subfont->font_dict;
2001 CFF_Private priv = &subfont->private_dict;
2003 PSAux_Service psaux = (PSAux_Service)face->psaux;
2005 FT_Bool cff2 = FT_BOOL( code == CFF2_CODE_TOPDICT ||
2006 code == CFF2_CODE_FONTDICT );
2007 FT_UInt stackSize = cff2 ? CFF2_DEFAULT_STACK
2008 : CFF_MAX_STACK_DEPTH;
2011 /* Note: We use default stack size for CFF2 Font DICT because */
2012 /* Top and Font DICTs are not allowed to have blend operators. */
2013 error = cff_parser_init( &parser,
2026 top->underline_position = -( 100L << 16 );
2027 top->underline_thickness = 50L << 16;
2028 top->charstring_type = 2;
2029 top->font_matrix.xx = 0x10000L;
2030 top->font_matrix.yy = 0x10000L;
2031 top->cid_count = 8720;
2033 /* we use the implementation specific SID value 0xFFFF to indicate */
2034 /* missing entries */
2035 top->version = 0xFFFFU;
2036 top->notice = 0xFFFFU;
2037 top->copyright = 0xFFFFU;
2038 top->full_name = 0xFFFFU;
2039 top->family_name = 0xFFFFU;
2040 top->weight = 0xFFFFU;
2041 top->embedded_postscript = 0xFFFFU;
2043 top->cid_registry = 0xFFFFU;
2044 top->cid_ordering = 0xFFFFU;
2045 top->cid_font_name = 0xFFFFU;
2047 /* set default stack size */
2048 top->maxstack = cff2 ? CFF2_DEFAULT_STACK : 48;
2050 if ( idx->count ) /* count is nonzero for a real index */
2051 error = cff_index_access_element( idx, font_index, &dict, &dict_len );
2054 /* CFF2 has a fake top dict index; */
2055 /* simulate `cff_index_access_element' */
2057 /* Note: macros implicitly use `stream' and set `error' */
2058 if ( FT_STREAM_SEEK( idx->data_offset ) ||
2059 FT_FRAME_EXTRACT( idx->data_size, dict ) )
2062 dict_len = idx->data_size;
2067 FT_TRACE4(( " top dictionary:\n" ));
2068 error = cff_parser_run( &parser, dict, FT_OFFSET( dict, dict_len ) );
2071 /* clean up regardless of error */
2073 cff_index_forget_element( idx, &dict );
2075 FT_FRAME_RELEASE( dict );
2080 /* if it is a CID font, we stop there */
2081 if ( top->cid_registry != 0xFFFFU )
2084 /* Parse the private dictionary, if any. */
2086 /* CFF2 does not have a private dictionary in the Top DICT */
2087 /* but may have one in a Font DICT. We need to parse */
2088 /* the latter here in order to load any local subrs. */
2089 error = cff_load_private_dict( font, subfont, 0, 0 );
2096 * Initialize the random number generator.
2098 * - If we have a face-specific seed, use it.
2099 * If non-zero, update it to a positive value.
2101 * - Otherwise, use the seed from the CFF driver.
2102 * If non-zero, update it to a positive value.
2104 * - If the random value is zero, use the seed given by the subfont's
2105 * `initialRandomSeed' value.
2108 if ( face->root.internal->random_seed == -1 )
2110 PS_Driver driver = (PS_Driver)FT_FACE_DRIVER( face );
2113 subfont->random = (FT_UInt32)driver->random_seed;
2114 if ( driver->random_seed )
2118 driver->random_seed =
2119 (FT_Int32)psaux->cff_random( (FT_UInt32)driver->random_seed );
2121 } while ( driver->random_seed < 0 );
2126 subfont->random = (FT_UInt32)face->root.internal->random_seed;
2127 if ( face->root.internal->random_seed )
2131 face->root.internal->random_seed =
2132 (FT_Int32)psaux->cff_random(
2133 (FT_UInt32)face->root.internal->random_seed );
2135 } while ( face->root.internal->random_seed < 0 );
2139 if ( !subfont->random )
2140 subfont->random = (FT_UInt32)priv->initial_random_seed;
2143 /* read the local subrs, if any */
2144 if ( priv->local_subrs_offset )
2146 if ( FT_STREAM_SEEK( base_offset + top->private_offset +
2147 priv->local_subrs_offset ) )
2150 error = cff_index_init( &subfont->local_subrs_index, stream, 1, cff2 );
2154 error = cff_index_get_pointers( &subfont->local_subrs_index,
2155 &subfont->local_subrs, NULL, NULL );
2161 cff_parser_done( &parser ); /* free parser stack */
2168 cff_subfont_done( FT_Memory memory,
2169 CFF_SubFont subfont )
2173 cff_index_done( &subfont->local_subrs_index );
2174 FT_FREE( subfont->local_subrs );
2176 FT_FREE( subfont->blend.lastNDV );
2177 FT_FREE( subfont->blend.BV );
2178 FT_FREE( subfont->blend_stack );
2183 FT_LOCAL_DEF( FT_Error )
2184 cff_font_load( FT_Library library,
2192 static const FT_Frame_Field cff_header_fields[] =
2195 #define FT_STRUCTURE CFF_FontRec
2197 FT_FRAME_START( 3 ),
2198 FT_FRAME_BYTE( version_major ),
2199 FT_FRAME_BYTE( version_minor ),
2200 FT_FRAME_BYTE( header_size ),
2205 FT_Memory memory = stream->memory;
2206 FT_ULong base_offset;
2207 CFF_FontRecDict dict;
2208 CFF_IndexRec string_index;
2209 FT_UInt subfont_index;
2213 FT_ZERO( &string_index );
2215 dict = &font->top_font.font_dict;
2216 base_offset = FT_STREAM_POS();
2218 font->library = library;
2219 font->stream = stream;
2220 font->memory = memory;
2222 font->base_offset = base_offset;
2224 /* read CFF font header */
2225 if ( FT_STREAM_READ_FIELDS( cff_header_fields, font ) )
2230 if ( font->version_major != 2 ||
2231 font->header_size < 5 )
2233 FT_TRACE2(( " not a CFF2 font header\n" ));
2234 error = FT_THROW( Unknown_File_Format );
2238 if ( FT_READ_USHORT( font->top_dict_length ) )
2243 FT_Byte absolute_offset;
2246 if ( FT_READ_BYTE( absolute_offset ) )
2249 if ( font->version_major != 1 ||
2250 font->header_size < 4 ||
2251 absolute_offset > 4 )
2253 FT_TRACE2(( " not a CFF font header\n" ));
2254 error = FT_THROW( Unknown_File_Format );
2259 /* skip the rest of the header */
2260 if ( FT_STREAM_SEEK( base_offset + font->header_size ) )
2262 /* For pure CFFs we have read only four bytes so far. Contrary to */
2263 /* other formats like SFNT those bytes doesn't define a signature; */
2264 /* it is thus possible that the font isn't a CFF at all. */
2267 FT_TRACE2(( " not a CFF file\n" ));
2268 error = FT_THROW( Unknown_File_Format );
2275 /* For CFF2, the top dict data immediately follow the header */
2276 /* and the length is stored in the header `offSize' field; */
2277 /* there is no index for it. */
2279 /* Use the `font_dict_index' to save the current position */
2280 /* and length of data, but leave count at zero as an indicator. */
2281 FT_ZERO( &font->font_dict_index );
2283 font->font_dict_index.data_offset = FT_STREAM_POS();
2284 font->font_dict_index.data_size = font->top_dict_length;
2286 /* skip the top dict data for now, we will parse it later */
2287 if ( FT_STREAM_SKIP( font->top_dict_length ) )
2290 /* next, read the global subrs index */
2291 if ( FT_SET_ERROR( cff_index_init( &font->global_subrs_index,
2292 stream, 1, cff2 ) ) )
2297 /* for CFF, read the name, top dict, string and global subrs index */
2298 if ( FT_SET_ERROR( cff_index_init( &font->name_index,
2299 stream, 0, cff2 ) ) )
2303 FT_TRACE2(( " not a CFF file\n" ));
2304 error = FT_THROW( Unknown_File_Format );
2309 /* if we have an empty font name, */
2310 /* it must be the only font in the CFF */
2311 if ( font->name_index.count > 1 &&
2312 font->name_index.data_size < font->name_index.count )
2314 /* for pure CFFs, we still haven't checked enough bytes */
2315 /* to be sure that it is a CFF at all */
2316 error = pure_cff ? FT_THROW( Unknown_File_Format )
2317 : FT_THROW( Invalid_File_Format );
2321 if ( FT_SET_ERROR( cff_index_init( &font->font_dict_index,
2322 stream, 0, cff2 ) ) ||
2323 FT_SET_ERROR( cff_index_init( &string_index,
2324 stream, 1, cff2 ) ) ||
2325 FT_SET_ERROR( cff_index_init( &font->global_subrs_index,
2326 stream, 1, cff2 ) ) ||
2327 FT_SET_ERROR( cff_index_get_pointers( &string_index,
2330 &font->string_pool_size ) ) )
2333 /* there must be a Top DICT index entry for each name index entry */
2334 if ( font->name_index.count > font->font_dict_index.count )
2336 FT_ERROR(( "cff_font_load:"
2337 " not enough entries in Top DICT index\n" ));
2338 error = FT_THROW( Invalid_File_Format );
2343 font->num_strings = string_index.count;
2347 /* well, we don't really forget the `disabled' fonts... */
2348 subfont_index = (FT_UInt)( face_index & 0xFFFF );
2350 if ( face_index > 0 && subfont_index >= font->name_index.count )
2352 FT_ERROR(( "cff_font_load:"
2353 " invalid subfont index for pure CFF font (%d)\n",
2355 error = FT_THROW( Invalid_Argument );
2359 font->num_faces = font->name_index.count;
2365 if ( font->name_index.count > 1 )
2367 FT_ERROR(( "cff_font_load:"
2368 " invalid CFF font with multiple subfonts\n" ));
2370 " in SFNT wrapper\n" ));
2371 error = FT_THROW( Invalid_File_Format );
2376 /* in case of a font format check, simply exit now */
2377 if ( face_index < 0 )
2380 /* now, parse the top-level font dictionary */
2381 FT_TRACE4(( "parsing top-level\n" ));
2382 error = cff_subfont_load( &font->top_font,
2383 &font->font_dict_index,
2387 cff2 ? CFF2_CODE_TOPDICT : CFF_CODE_TOPDICT,
2393 if ( FT_STREAM_SEEK( base_offset + dict->charstrings_offset ) )
2396 error = cff_index_init( &font->charstrings_index, stream, 0, cff2 );
2400 /* now, check for a CID or CFF2 font */
2401 if ( dict->cid_registry != 0xFFFFU ||
2404 CFF_IndexRec fd_index;
2405 CFF_SubFont sub = NULL;
2409 /* for CFF2, read the Variation Store if available; */
2410 /* this must follow the Top DICT parse and precede any Private DICT */
2411 error = cff_vstore_load( &font->vstore,
2414 dict->vstore_offset );
2418 /* this is a CID-keyed font, we must now allocate a table of */
2419 /* sub-fonts, then load each of them separately */
2420 if ( FT_STREAM_SEEK( base_offset + dict->cid_fd_array_offset ) )
2423 error = cff_index_init( &fd_index, stream, 0, cff2 );
2427 /* Font Dicts are not limited to 256 for CFF2. */
2428 /* TODO: support this for CFF2 */
2429 if ( fd_index.count > CFF_MAX_CID_FONTS )
2431 FT_TRACE0(( "cff_font_load: FD array too large in CID font\n" ));
2435 /* allocate & read each font dict independently */
2436 font->num_subfonts = fd_index.count;
2437 if ( FT_NEW_ARRAY( sub, fd_index.count ) )
2440 /* set up pointer table */
2441 for ( idx = 0; idx < fd_index.count; idx++ )
2442 font->subfonts[idx] = sub + idx;
2444 /* now load each subfont independently */
2445 for ( idx = 0; idx < fd_index.count; idx++ )
2447 sub = font->subfonts[idx];
2448 FT_TRACE4(( "parsing subfont %u\n", idx ));
2449 error = cff_subfont_load( sub,
2454 cff2 ? CFF2_CODE_FONTDICT
2462 /* now load the FD Select array; */
2463 /* CFF2 omits FDSelect if there is only one FD */
2464 if ( !cff2 || fd_index.count > 1 )
2465 error = CFF_Load_FD_Select( &font->fd_select,
2466 font->charstrings_index.count,
2468 base_offset + dict->cid_fd_select_offset );
2471 cff_index_done( &fd_index );
2477 font->num_subfonts = 0;
2479 /* read the charstrings index now */
2480 if ( dict->charstrings_offset == 0 )
2482 FT_ERROR(( "cff_font_load: no charstrings offset\n" ));
2483 error = FT_THROW( Invalid_File_Format );
2487 font->num_glyphs = font->charstrings_index.count;
2489 error = cff_index_get_pointers( &font->global_subrs_index,
2490 &font->global_subrs, NULL, NULL );
2495 /* read the Charset and Encoding tables if available */
2496 if ( !cff2 && font->num_glyphs > 0 )
2498 FT_Bool invert = FT_BOOL( dict->cid_registry != 0xFFFFU && pure_cff );
2501 error = cff_charset_load( &font->charset, font->num_glyphs, stream,
2502 base_offset, dict->charset_offset, invert );
2506 /* CID-keyed CFFs don't have an encoding */
2507 if ( dict->cid_registry == 0xFFFFU )
2509 error = cff_encoding_load( &font->encoding,
2514 dict->encoding_offset );
2520 /* get the font name (/CIDFontName for CID-keyed fonts, */
2521 /* /FontName otherwise) */
2522 font->font_name = cff_index_get_name( font, subfont_index );
2525 cff_index_done( &string_index );
2531 FT_LOCAL_DEF( void )
2532 cff_font_done( CFF_Font font )
2534 FT_Memory memory = font->memory;
2538 cff_index_done( &font->global_subrs_index );
2539 cff_index_done( &font->font_dict_index );
2540 cff_index_done( &font->name_index );
2541 cff_index_done( &font->charstrings_index );
2543 /* release font dictionaries, but only if working with */
2544 /* a CID keyed CFF font or a CFF2 font */
2545 if ( font->num_subfonts > 0 )
2547 for ( idx = 0; idx < font->num_subfonts; idx++ )
2548 cff_subfont_done( memory, font->subfonts[idx] );
2550 /* the subfonts array has been allocated as a single block */
2551 FT_FREE( font->subfonts[0] );
2554 cff_encoding_done( &font->encoding );
2555 cff_charset_done( &font->charset, font->stream );
2556 cff_vstore_done( &font->vstore, memory );
2558 cff_subfont_done( memory, &font->top_font );
2560 CFF_Done_FD_Select( &font->fd_select, font->stream );
2562 FT_FREE( font->font_info );
2564 FT_FREE( font->font_name );
2565 FT_FREE( font->global_subrs );
2566 FT_FREE( font->strings );
2567 FT_FREE( font->string_pool );
2569 if ( font->cf2_instance.finalizer )
2571 font->cf2_instance.finalizer( font->cf2_instance.data );
2572 FT_FREE( font->cf2_instance.data );
2575 FT_FREE( font->font_extra );