26#ifdef FT_CONFIG_OPTION_USE_BROTLI
28#include <brotli/decode.h>
38#define FT_COMPONENT sfwoff2
41#define MAX_SFNT_SIZE ( 1 << 26 )
43#define READ_255USHORT( var ) FT_SET_ERROR( Read255UShort( stream, &var ) )
45#define READ_BASE128( var ) FT_SET_ERROR( ReadBase128( stream, &var ) )
48#define ROUND4( var ) ( ( var + 3 ) & ~3UL )
50#define WRITE_USHORT( p, v ) \
53 *(p)++ = (FT_Byte)( (v) >> 8 ); \
54 *(p)++ = (FT_Byte)( (v) >> 0 ); \
58#define WRITE_ULONG( p, v ) \
61 *(p)++ = (FT_Byte)( (v) >> 24 ); \
62 *(p)++ = (FT_Byte)( (v) >> 16 ); \
63 *(p)++ = (FT_Byte)( (v) >> 8 ); \
64 *(p)++ = (FT_Byte)( (v) >> 0 ); \
68#define WRITE_SHORT( p, v ) \
71 *(p)++ = (FT_Byte)( (v) >> 8 ); \
72 *(p)++ = (FT_Byte)( (v) >> 0 ); \
76#define WRITE_SFNT_BUF( buf, s ) \
77 write_buf( &sfnt, sfnt_size, &dest_offset, buf, s, memory )
79#define WRITE_SFNT_BUF_AT( offset, buf, s ) \
80 write_buf( &sfnt, sfnt_size, &offset, buf, s, memory )
82#define N_CONTOUR_STREAM 0
83#define N_POINTS_STREAM 1
86#define COMPOSITE_STREAM 4
88#define INSTRUCTION_STREAM 6
90#define HAVE_OVERLAP_SIMPLE_BITMAP 0x1
94 stream_close( FT_Stream stream )
96 FT_Memory memory = stream->memory;
99 FT_FREE( stream->base );
102 stream->close = NULL;
106 FT_COMPARE_DEF(
int )
107 compare_tags(
const void* a,
110 WOFF2_Table table1 = *(WOFF2_Table*)a;
111 WOFF2_Table table2 = *(WOFF2_Table*)b;
113 FT_Tag tag1 = table1->Tag;
114 FT_Tag tag2 = table2->Tag;
119 else if ( tag1 < tag2 )
127 Read255UShort( FT_Stream stream,
130 const FT_Byte oneMoreByteCode1 = 255;
131 const FT_Byte oneMoreByteCode2 = 254;
132 const FT_Byte wordCode = 253;
133 const FT_UShort lowestUCode = 253;
135 FT_Error error = FT_Err_Ok;
137 FT_Byte result_byte = 0;
138 FT_UShort result_short = 0;
141 if ( FT_READ_BYTE(
code ) )
143 if (
code == wordCode )
146 if ( FT_READ_USHORT( result_short ) )
148 *value = result_short;
151 else if (
code == oneMoreByteCode1 )
153 if ( FT_READ_BYTE( result_byte ) )
155 *value = result_byte + lowestUCode;
158 else if (
code == oneMoreByteCode2 )
160 if ( FT_READ_BYTE( result_byte ) )
162 *value = result_byte + lowestUCode * 2;
174 ReadBase128( FT_Stream stream,
180 FT_Error error = FT_Err_Ok;
183 for ( i = 0; i < 5; ++i )
186 if ( FT_READ_BYTE(
code ) )
190 if ( i == 0 &&
code == 0x80 )
191 return FT_THROW( Invalid_Table );
194 if ( result & 0xfe000000 )
195 return FT_THROW( Invalid_Table );
197 result = ( result << 7 ) | (
code & 0x7f );
200 if ( (
code & 0x80 ) == 0 )
208 return FT_THROW( Invalid_Table );
214 write_buf( FT_Byte** dst_bytes,
221 FT_Error error = FT_Err_Ok;
223 FT_Byte* dst = *dst_bytes;
227 if ( ( *offset + size ) > WOFF2_DEFAULT_MAX_SIZE )
228 return FT_THROW( Array_Too_Large );
231 if ( ( *offset + size ) > *dst_size )
233 FT_TRACE6((
"Reallocating %lu to %lu.\n",
234 *dst_size, (*offset + size) ));
235 if ( FT_QREALLOC( dst,
236 (FT_ULong)( *dst_size ),
237 (FT_ULong)( *offset + size ) ) )
240 *dst_size = *offset + size;
244 ft_memcpy( dst + *offset, src, size );
257 pad4( FT_Byte** sfnt_bytes,
259 FT_ULong* out_offset,
262 FT_Byte* sfnt = *sfnt_bytes;
263 FT_ULong dest_offset = *out_offset;
265 FT_Byte zeroes[] = { 0, 0, 0 };
269 if ( dest_offset + 3 < dest_offset )
270 return FT_THROW( Invalid_Table );
272 pad_bytes = ROUND4( dest_offset ) - dest_offset;
275 if ( WRITE_SFNT_BUF( &zeroes[0], pad_bytes ) )
276 return FT_THROW( Invalid_Table );
280 *out_offset = dest_offset;
287 compute_ULong_sum( FT_Byte* buf,
290 FT_ULong checksum = 0;
291 FT_ULong aligned_size = size & ~3UL;
296 for ( i = 0; i < aligned_size; i += 4 )
297 checksum += FT_NEXT_ULONG( buf );
300 for ( shift = 24; i < size; i++, shift -= 8 )
301 checksum += (FT_UInt32)FT_NEXT_BYTE( buf ) << shift;
308 woff2_decompress( FT_Byte* dst,
315 FT_Offset uncompressed_size = (FT_Offset)dst_size;
316 BrotliDecoderResult result;
319 result = BrotliDecoderDecompress( src_size,
324 if ( result != BROTLI_DECODER_RESULT_SUCCESS ||
325 uncompressed_size != dst_size )
327 FT_ERROR((
"woff2_decompress: Stream length mismatch.\n" ));
328 return FT_THROW( Invalid_Table );
331 FT_TRACE2((
"woff2_decompress: Brotli stream decompressed.\n" ));
337 find_table( WOFF2_Table* tables,
338 FT_UShort num_tables,
344 for ( i = 0; i < num_tables; i++ )
346 if ( tables[i]->Tag == tag )
355 read_num_hmetrics( FT_Stream stream,
356 FT_UShort* num_hmetrics )
358 FT_Error error = FT_Err_Ok;
359 FT_UShort num_metrics;
362 if ( FT_STREAM_SKIP( 34 ) )
363 return FT_THROW( Invalid_Table );
365 if ( FT_READ_USHORT( num_metrics ) )
366 return FT_THROW( Invalid_Table );
368 *num_hmetrics = num_metrics;
376 with_sign( FT_Byte flag,
380 return ( flag & 1 ) ? base_val : -base_val;
386 safe_int_addition( FT_Int a,
390 if ( ( ( a > 0 ) && ( b > FT_INT_MAX - a ) ) ||
391 ( ( a < 0 ) && ( b < FT_INT_MIN - a ) ) )
392 return FT_THROW( Invalid_Table );
406 triplet_decode(
const FT_Byte* flags_in,
411 FT_ULong* in_bytes_used )
419 FT_ULong triplet_index = 0;
425 if ( n_points > in_size )
426 return FT_THROW( Invalid_Table );
428 for ( i = 0; i < n_points; ++i )
430 FT_Byte flag = flags_in[i];
431 FT_Bool on_curve = !( flag >> 7 );
437 else if ( flag < 120 )
439 else if ( flag < 124 )
445 if ( triplet_index + data_bytes > in_size ||
446 triplet_index + data_bytes < triplet_index )
447 return FT_THROW( Invalid_Table );
452 dy = with_sign( flag,
453 ( ( flag & 14 ) << 7 ) + in[triplet_index] );
455 else if ( flag < 20 )
457 dx = with_sign( flag,
458 ( ( ( flag - 10 ) & 14 ) << 7 ) +
462 else if ( flag < 84 )
465 b1 = in[triplet_index];
466 dx = with_sign( flag,
467 1 + ( b0 & 0x30 ) + ( b1 >> 4 ) );
468 dy = with_sign( flag >> 1,
469 1 + ( ( b0 & 0x0c ) << 2 ) + ( b1 & 0x0f ) );
471 else if ( flag < 120 )
474 dx = with_sign( flag,
475 1 + ( ( b0 / 12 ) << 8 ) + in[triplet_index] );
476 dy = with_sign( flag >> 1,
477 1 + ( ( ( b0 % 12 ) >> 2 ) << 8 ) +
478 in[triplet_index + 1] );
480 else if ( flag < 124 )
482 b2 = in[triplet_index + 1];
483 dx = with_sign( flag,
484 ( in[triplet_index] << 4 ) + ( b2 >> 4 ) );
485 dy = with_sign( flag >> 1,
486 ( ( b2 & 0x0f ) << 8 ) + in[triplet_index + 2] );
490 dx = with_sign( flag,
491 ( in[triplet_index] << 8 ) +
492 in[triplet_index + 1] );
493 dy = with_sign( flag >> 1,
494 ( in[triplet_index + 2] << 8 ) +
495 in[triplet_index + 3] );
498 triplet_index += data_bytes;
500 if ( safe_int_addition( x, dx, &x ) )
501 return FT_THROW( Invalid_Table );
503 if ( safe_int_addition( y, dy, &y ) )
504 return FT_THROW( Invalid_Table );
508 result[i].on_curve = on_curve;
511 *in_bytes_used = triplet_index;
518 store_points( FT_ULong n_points,
519 const WOFF2_Point points,
520 FT_UShort n_contours,
521 FT_UShort instruction_len,
522 FT_Bool have_overlap,
525 FT_ULong* glyph_size )
527 FT_UInt flag_offset = 10 + ( 2 * n_contours ) + 2 + instruction_len;
528 FT_Byte last_flag = 0xFFU;
529 FT_Byte repeat_count = 0;
541 for ( i = 0; i < n_points; ++i )
543 const WOFF2_PointRec point = points[i];
545 FT_Byte flag = point.on_curve ? GLYF_ON_CURVE : 0;
546 FT_Int dx = point.x - last_x;
547 FT_Int dy = point.y - last_y;
550 if ( i == 0 && have_overlap )
551 flag |= GLYF_OVERLAP_SIMPLE;
554 flag |= GLYF_THIS_X_IS_SAME;
555 else if ( dx > -256 && dx < 256 )
557 flag |= GLYF_X_SHORT | ( dx > 0 ? GLYF_THIS_X_IS_SAME : 0 );
564 flag |= GLYF_THIS_Y_IS_SAME;
565 else if ( dy > -256 && dy < 256 )
567 flag |= GLYF_Y_SHORT | ( dy > 0 ? GLYF_THIS_Y_IS_SAME : 0 );
573 if ( flag == last_flag && repeat_count != 255 )
575 dst[flag_offset - 1] |= GLYF_REPEAT;
580 if ( repeat_count != 0 )
582 if ( flag_offset >= dst_size )
583 return FT_THROW( Invalid_Table );
585 dst[flag_offset++] = repeat_count;
587 if ( flag_offset >= dst_size )
588 return FT_THROW( Invalid_Table );
590 dst[flag_offset++] = flag;
599 if ( repeat_count != 0 )
601 if ( flag_offset >= dst_size )
602 return FT_THROW( Invalid_Table );
604 dst[flag_offset++] = repeat_count;
607 xy_bytes = x_bytes + y_bytes;
608 if ( xy_bytes < x_bytes ||
609 flag_offset + xy_bytes < flag_offset ||
610 flag_offset + xy_bytes > dst_size )
611 return FT_THROW( Invalid_Table );
613 x_offset = flag_offset;
614 y_offset = flag_offset + x_bytes;
618 for ( i = 0; i < n_points; ++i )
620 FT_Int dx = points[i].x - last_x;
621 FT_Int dy = points[i].y - last_y;
626 else if ( dx > -256 && dx < 256 )
627 dst[x_offset++] = (FT_Byte)FT_ABS( dx );
630 pointer = dst + x_offset;
631 WRITE_SHORT( pointer, dx );
639 else if ( dy > -256 && dy < 256 )
640 dst[y_offset++] = (FT_Byte)FT_ABS( dy );
643 pointer = dst + y_offset;
644 WRITE_SHORT( pointer, dy );
651 *glyph_size = y_offset;
657 compute_bbox( FT_ULong n_points,
658 const WOFF2_Point points,
660 FT_UShort* src_x_min )
681 for ( i = 1; i < n_points; ++i )
683 FT_Int x = points[i].x;
684 FT_Int y = points[i].y;
687 x_min = FT_MIN( x, x_min );
688 y_min = FT_MIN( y, y_min );
689 x_max = FT_MAX( x, x_max );
690 y_max = FT_MAX( y, y_max );
695 pointer = dst + offset;
697 WRITE_SHORT( pointer, x_min );
698 WRITE_SHORT( pointer, y_min );
699 WRITE_SHORT( pointer, x_max );
700 WRITE_SHORT( pointer, y_max );
702 *src_x_min = (FT_UShort)x_min;
707 compositeGlyph_size( FT_Stream stream,
710 FT_Bool* have_instructions )
712 FT_Error error = FT_Err_Ok;
713 FT_ULong start_offset = offset;
714 FT_Bool we_have_inst = FALSE;
715 FT_UShort flags = FLAG_MORE_COMPONENTS;
718 if ( FT_STREAM_SEEK( start_offset ) )
720 while ( flags & FLAG_MORE_COMPONENTS )
725 if ( FT_READ_USHORT( flags ) )
727 we_have_inst |= ( flags & FLAG_WE_HAVE_INSTRUCTIONS ) != 0;
730 if ( flags & FLAG_ARG_1_AND_2_ARE_WORDS )
735 if ( flags & FLAG_WE_HAVE_A_SCALE )
737 else if ( flags & FLAG_WE_HAVE_AN_X_AND_Y_SCALE )
739 else if ( flags & FLAG_WE_HAVE_A_TWO_BY_TWO )
742 if ( FT_STREAM_SKIP( arg_size ) )
746 *size = FT_STREAM_POS() - start_offset;
747 *have_instructions = we_have_inst;
756 store_loca( FT_ULong* loca_values,
757 FT_ULong loca_values_size,
758 FT_UShort index_format,
760 FT_Byte** sfnt_bytes,
762 FT_ULong* out_offset,
765 FT_Error error = FT_Err_Ok;
766 FT_Byte* sfnt = *sfnt_bytes;
767 FT_ULong dest_offset = *out_offset;
769 FT_Byte* loca_buf = NULL;
773 FT_ULong loca_buf_size;
775 const FT_ULong offset_size = index_format ? 4 : 2;
778 if ( ( loca_values_size << 2 ) >> 2 != loca_values_size )
781 loca_buf_size = loca_values_size * offset_size;
782 if ( FT_QALLOC( loca_buf, loca_buf_size ) )
786 for ( i = 0; i < loca_values_size; i++ )
788 FT_ULong value = loca_values[i];
792 WRITE_ULONG( dst, value );
794 WRITE_USHORT( dst, ( value >> 1 ) );
797 *checksum = compute_ULong_sum( loca_buf, loca_buf_size );
799 if ( WRITE_SFNT_BUF( loca_buf, loca_buf_size ) )
804 *out_offset = dest_offset;
811 error = FT_THROW( Invalid_Table );
820 reconstruct_glyf( FT_Stream stream,
821 FT_ULong* glyf_checksum,
822 FT_ULong* loca_checksum,
823 FT_Byte** sfnt_bytes,
825 FT_ULong* out_offset,
829 FT_Error error = FT_Err_Ok;
830 FT_Byte* sfnt = *sfnt_bytes;
833 const FT_ULong pos = FT_STREAM_POS();
835 FT_UInt num_substreams = 7;
837 FT_UShort option_flags;
838 FT_UShort num_glyphs;
839 FT_UShort index_format;
840 FT_ULong expected_loca_length;
843 FT_ULong points_size;
844 FT_ULong glyph_buf_size;
845 FT_ULong bbox_bitmap_offset;
846 FT_ULong bbox_bitmap_length;
847 FT_ULong overlap_bitmap_offset = 0;
848 FT_ULong overlap_bitmap_length = 0;
850 const FT_ULong glyf_start = *out_offset;
851 FT_ULong dest_offset = *out_offset;
853 WOFF2_Substream substreams = NULL;
855 FT_ULong* loca_values = NULL;
856 FT_UShort* n_points_arr = NULL;
857 FT_Byte* glyph_buf = NULL;
858 WOFF2_Point points = NULL;
861 if ( FT_QNEW_ARRAY( substreams, num_substreams ) )
864 if ( FT_STREAM_SKIP( 2 ) )
866 if ( FT_READ_USHORT( option_flags ) )
868 if ( FT_READ_USHORT( num_glyphs ) )
870 if ( FT_READ_USHORT( index_format ) )
873 FT_TRACE4((
"option_flags = %u; num_glyphs = %u; index_format = %u\n",
874 option_flags, num_glyphs, index_format ));
876 info->num_glyphs = num_glyphs;
882 expected_loca_length = ( index_format ? 4 : 2 ) *
883 ( (FT_ULong)num_glyphs + 1 );
884 if ( info->loca_table->dst_length != expected_loca_length )
887 offset = 2 + 2 + 2 + 2 + ( num_substreams * 4 );
888 if ( offset > info->glyf_table->TransformLength )
891 for ( i = 0; i < num_substreams; ++i )
893 FT_ULong substream_size;
896 if ( FT_READ_ULONG( substream_size ) )
898 if ( substream_size > info->glyf_table->TransformLength - offset )
901 substreams[i].start = pos + offset;
902 substreams[i].offset = pos + offset;
903 substreams[i].size = substream_size;
905 FT_TRACE5((
" Substream %u: offset = %lu; size = %lu;\n",
906 i, substreams[i].offset, substreams[i].size ));
907 offset += substream_size;
910 if ( option_flags & HAVE_OVERLAP_SIMPLE_BITMAP )
913 overlap_bitmap_length = ( num_glyphs + 7U ) >> 3;
914 if ( overlap_bitmap_length > info->glyf_table->TransformLength - offset )
917 overlap_bitmap_offset = pos + offset;
919 FT_TRACE5((
" Overlap bitmap: offset = %lu; size = %lu;\n",
920 overlap_bitmap_offset, overlap_bitmap_length ));
921 offset += overlap_bitmap_length;
924 if ( FT_QNEW_ARRAY( loca_values, num_glyphs + 1 ) )
928 bbox_bitmap_offset = substreams[BBOX_STREAM].offset;
931 bbox_bitmap_length = ( ( num_glyphs + 31U ) >> 5 ) << 2;
933 substreams[BBOX_STREAM].offset += bbox_bitmap_length;
935 glyph_buf_size = WOFF2_DEFAULT_GLYPH_BUF;
936 if ( FT_QALLOC( glyph_buf, glyph_buf_size ) )
939 if ( FT_QNEW_ARRAY( info->x_mins, num_glyphs ) )
942 for ( i = 0; i < num_glyphs; ++i )
944 FT_ULong glyph_size = 0;
945 FT_UShort n_contours = 0;
946 FT_Bool have_bbox = FALSE;
948 FT_ULong bbox_offset;
953 bbox_offset = bbox_bitmap_offset + ( i >> 3 );
954 if ( FT_STREAM_SEEK( bbox_offset ) ||
955 FT_READ_BYTE( bbox_bitmap ) )
957 if ( bbox_bitmap & ( 0x80 >> ( i & 7 ) ) )
961 if ( FT_STREAM_SEEK( substreams[N_CONTOUR_STREAM].offset ) ||
962 FT_READ_USHORT( n_contours ) )
964 substreams[N_CONTOUR_STREAM].offset += 2;
966 if ( n_contours == 0xffff )
969 FT_Bool have_instructions = FALSE;
970 FT_UShort instruction_size = 0;
971 FT_ULong composite_size = 0;
972 FT_ULong size_needed;
973 FT_Byte* pointer = NULL;
980 if ( compositeGlyph_size( stream,
981 substreams[COMPOSITE_STREAM].offset,
983 &have_instructions) )
986 if ( have_instructions )
988 if ( FT_STREAM_SEEK( substreams[GLYPH_STREAM].offset ) ||
989 READ_255USHORT( instruction_size ) )
991 substreams[GLYPH_STREAM].offset = FT_STREAM_POS();
994 size_needed = 12 + composite_size + instruction_size;
995 if ( glyph_buf_size < size_needed )
997 if ( FT_QREALLOC( glyph_buf, glyph_buf_size, size_needed ) )
999 glyph_buf_size = size_needed;
1002 pointer = glyph_buf + glyph_size;
1003 WRITE_USHORT( pointer, n_contours );
1007 if ( FT_STREAM_SEEK( substreams[BBOX_STREAM].offset ) ||
1008 FT_READ_USHORT( x_min ) )
1012 if ( FT_STREAM_SEEK( substreams[BBOX_STREAM].offset ) ||
1013 FT_STREAM_READ( glyph_buf + glyph_size, 8 ) )
1016 substreams[BBOX_STREAM].offset += 8;
1019 if ( FT_STREAM_SEEK( substreams[COMPOSITE_STREAM].offset ) ||
1020 FT_STREAM_READ( glyph_buf + glyph_size, composite_size ) )
1023 substreams[COMPOSITE_STREAM].offset += composite_size;
1024 glyph_size += composite_size;
1026 if ( have_instructions )
1028 pointer = glyph_buf + glyph_size;
1029 WRITE_USHORT( pointer, instruction_size );
1032 if ( FT_STREAM_SEEK( substreams[INSTRUCTION_STREAM].offset ) ||
1033 FT_STREAM_READ( glyph_buf + glyph_size, instruction_size ) )
1036 substreams[INSTRUCTION_STREAM].offset += instruction_size;
1037 glyph_size += instruction_size;
1040 else if ( n_contours > 0 )
1043 FT_ULong total_n_points = 0;
1044 FT_UShort n_points_contour;
1047 FT_ULong triplet_size;
1048 FT_ULong triplet_bytes_used;
1049 FT_Bool have_overlap = FALSE;
1050 FT_Byte overlap_bitmap;
1051 FT_ULong overlap_offset;
1052 FT_Byte* flags_buf = NULL;
1053 FT_Byte* triplet_buf = NULL;
1054 FT_UShort instruction_size;
1055 FT_ULong size_needed;
1059 FT_Byte* pointer = NULL;
1063 if ( overlap_bitmap_offset )
1065 overlap_offset = overlap_bitmap_offset + ( i >> 3 );
1066 if ( FT_STREAM_SEEK( overlap_offset ) ||
1067 FT_READ_BYTE( overlap_bitmap ) )
1069 if ( overlap_bitmap & ( 0x80 >> ( i & 7 ) ) )
1070 have_overlap = TRUE;
1073 if ( FT_QNEW_ARRAY( n_points_arr, n_contours ) )
1076 if ( FT_STREAM_SEEK( substreams[N_POINTS_STREAM].offset ) )
1079 for ( j = 0; j < n_contours; ++j )
1081 if ( READ_255USHORT( n_points_contour ) )
1083 n_points_arr[j] = n_points_contour;
1085 if ( total_n_points + n_points_contour < total_n_points )
1087 total_n_points += n_points_contour;
1089 substreams[N_POINTS_STREAM].offset = FT_STREAM_POS();
1091 flag_size = total_n_points;
1092 if ( flag_size > substreams[FLAG_STREAM].size )
1095 flags_buf = stream->base + substreams[FLAG_STREAM].offset;
1096 triplet_buf = stream->base + substreams[GLYPH_STREAM].offset;
1098 if ( substreams[GLYPH_STREAM].size <
1099 ( substreams[GLYPH_STREAM].offset -
1100 substreams[GLYPH_STREAM].start ) )
1103 triplet_size = substreams[GLYPH_STREAM].size -
1104 ( substreams[GLYPH_STREAM].offset -
1105 substreams[GLYPH_STREAM].start );
1106 triplet_bytes_used = 0;
1109 points_size = total_n_points;
1110 if ( FT_QNEW_ARRAY( points, points_size ) )
1113 if ( triplet_decode( flags_buf,
1118 &triplet_bytes_used ) )
1121 substreams[FLAG_STREAM].offset += flag_size;
1122 substreams[GLYPH_STREAM].offset += triplet_bytes_used;
1124 if ( FT_STREAM_SEEK( substreams[GLYPH_STREAM].offset ) ||
1125 READ_255USHORT( instruction_size ) )
1128 substreams[GLYPH_STREAM].offset = FT_STREAM_POS();
1130 if ( total_n_points >= ( 1 << 27 ) )
1134 ( 2 * n_contours ) +
1135 ( 5 * total_n_points ) +
1137 if ( glyph_buf_size < size_needed )
1139 if ( FT_QREALLOC( glyph_buf, glyph_buf_size, size_needed ) )
1141 glyph_buf_size = size_needed;
1144 pointer = glyph_buf + glyph_size;
1145 WRITE_USHORT( pointer, n_contours );
1151 if ( FT_STREAM_SEEK( substreams[BBOX_STREAM].offset ) ||
1152 FT_READ_USHORT( x_min ) )
1156 if ( FT_STREAM_SEEK( substreams[BBOX_STREAM].offset ) ||
1157 FT_STREAM_READ( glyph_buf + glyph_size, 8 ) )
1159 substreams[BBOX_STREAM].offset += 8;
1162 compute_bbox( total_n_points, points, glyph_buf, &x_min );
1164 glyph_size = CONTOUR_OFFSET_END_POINT;
1166 pointer = glyph_buf + glyph_size;
1169 for ( contour_ix = 0; contour_ix < n_contours; ++contour_ix )
1171 end_point += n_points_arr[contour_ix];
1172 if ( end_point >= 65536 )
1175 WRITE_SHORT( pointer, end_point );
1179 WRITE_USHORT( pointer, instruction_size );
1182 if ( FT_STREAM_SEEK( substreams[INSTRUCTION_STREAM].offset ) ||
1183 FT_STREAM_READ( glyph_buf + glyph_size, instruction_size ) )
1186 substreams[INSTRUCTION_STREAM].offset += instruction_size;
1187 glyph_size += instruction_size;
1189 if ( store_points( total_n_points,
1200 FT_FREE( n_points_arr );
1208 FT_ERROR((
"Empty glyph has a bbox.\n" ));
1213 loca_values[i] = dest_offset - glyf_start;
1215 if ( WRITE_SFNT_BUF( glyph_buf, glyph_size ) )
1218 if ( pad4( &sfnt, sfnt_size, &dest_offset, memory ) )
1221 *glyf_checksum += compute_ULong_sum( glyph_buf, glyph_size );
1224 info->x_mins[i] = (FT_Short)x_min;
1227 info->glyf_table->dst_length = dest_offset - info->glyf_table->dst_offset;
1228 info->loca_table->dst_offset = dest_offset;
1231 loca_values[num_glyphs] = info->glyf_table->dst_length;
1233 if ( store_loca( loca_values,
1243 info->loca_table->dst_length = dest_offset - info->loca_table->dst_offset;
1245 FT_TRACE4((
" loca table info:\n" ));
1246 FT_TRACE4((
" dst_offset = %lu\n", info->loca_table->dst_offset ));
1247 FT_TRACE4((
" dst_length = %lu\n", info->loca_table->dst_length ));
1248 FT_TRACE4((
" checksum = %09lx\n", *loca_checksum ));
1252 *out_offset = dest_offset;
1254 FT_FREE( substreams );
1255 FT_FREE( loca_values );
1256 FT_FREE( n_points_arr );
1257 FT_FREE( glyph_buf );
1264 error = FT_THROW( Invalid_Table );
1269 FT_FREE( substreams );
1270 FT_FREE( loca_values );
1271 FT_FREE( n_points_arr );
1272 FT_FREE( glyph_buf );
1281 get_x_mins( FT_Stream stream,
1282 WOFF2_Table* tables,
1283 FT_UShort num_tables,
1287 FT_UShort num_glyphs;
1288 FT_UShort index_format;
1289 FT_ULong glyf_offset;
1290 FT_UShort glyf_offset_short;
1291 FT_ULong loca_offset;
1293 FT_Error error = FT_Err_Ok;
1294 FT_ULong offset_size;
1297 const WOFF2_Table maxp_table = find_table( tables, num_tables,
1299 const WOFF2_Table head_table = find_table( tables, num_tables,
1305 FT_ERROR((
"`maxp' table is missing.\n" ));
1306 return FT_THROW( Invalid_Table );
1311 FT_ERROR((
"`head' table is missing.\n" ));
1312 return FT_THROW( Invalid_Table );
1315 if ( !info->loca_table )
1317 FT_ERROR((
"`loca' table is missing.\n" ));
1318 return FT_THROW( Invalid_Table );
1322 if ( FT_STREAM_SEEK( maxp_table->src_offset ) || FT_STREAM_SKIP( 8 ) )
1325 if ( FT_READ_USHORT( num_glyphs ) )
1328 info->num_glyphs = num_glyphs;
1331 if ( FT_STREAM_SEEK( head_table->src_offset ) ||
1332 FT_STREAM_SKIP( 50 ) )
1335 if ( FT_READ_USHORT( index_format ) )
1338 offset_size = index_format ? 4 : 2;
1341 if ( FT_QNEW_ARRAY( info->x_mins, num_glyphs ) )
1344 loca_offset = info->loca_table->src_offset;
1346 for ( i = 0; i < num_glyphs; ++i )
1348 if ( FT_STREAM_SEEK( loca_offset ) )
1351 loca_offset += offset_size;
1355 if ( FT_READ_ULONG( glyf_offset ) )
1360 if ( FT_READ_USHORT( glyf_offset_short ) )
1363 glyf_offset = (FT_ULong)( glyf_offset_short );
1364 glyf_offset = glyf_offset << 1;
1367 glyf_offset += info->glyf_table->src_offset;
1369 if ( FT_STREAM_SEEK( glyf_offset ) || FT_STREAM_SKIP( 2 ) )
1372 if ( FT_READ_SHORT( info->x_mins[i] ) )
1381 reconstruct_hmtx( FT_Stream stream,
1382 FT_UShort num_glyphs,
1383 FT_UShort num_hmetrics,
1386 FT_Byte** sfnt_bytes,
1387 FT_ULong* sfnt_size,
1388 FT_ULong* out_offset,
1391 FT_Error error = FT_Err_Ok;
1392 FT_Byte* sfnt = *sfnt_bytes;
1393 FT_ULong dest_offset = *out_offset;
1396 FT_Bool has_proportional_lsbs, has_monospace_lsbs;
1397 FT_ULong hmtx_table_size;
1400 FT_UShort* advance_widths = NULL;
1401 FT_Short* lsbs = NULL;
1402 FT_Byte* hmtx_table = NULL;
1403 FT_Byte* dst = NULL;
1406 if ( FT_READ_BYTE( hmtx_flags ) )
1409 has_proportional_lsbs = ( hmtx_flags & 1 ) == 0;
1410 has_monospace_lsbs = ( hmtx_flags & 2 ) == 0;
1413 if ( ( hmtx_flags & 0xFC ) != 0 )
1417 if ( has_proportional_lsbs && has_monospace_lsbs )
1421 if ( ( num_hmetrics > num_glyphs ) ||
1422 ( num_hmetrics < 1 ) )
1426 if ( num_hmetrics < 1 )
1429 if ( FT_QNEW_ARRAY( advance_widths, num_hmetrics ) ||
1430 FT_QNEW_ARRAY( lsbs, num_glyphs ) )
1434 for ( i = 0; i < num_hmetrics; i++ )
1436 FT_UShort advance_width;
1439 if ( FT_READ_USHORT( advance_width ) )
1442 advance_widths[i] = advance_width;
1446 for ( i = 0; i < num_hmetrics; i++ )
1451 if ( has_proportional_lsbs )
1453 if ( FT_READ_SHORT( lsb ) )
1463 for ( i = num_hmetrics; i < num_glyphs; i++ )
1468 if ( has_monospace_lsbs )
1470 if ( FT_READ_SHORT( lsb ) )
1480 hmtx_table_size = 2 * num_hmetrics + 2 * num_glyphs;
1481 if ( FT_QALLOC( hmtx_table, hmtx_table_size ) )
1485 FT_TRACE6((
"hmtx values: \n" ));
1486 for ( i = 0; i < num_glyphs; i++ )
1488 if ( i < num_hmetrics )
1490 WRITE_SHORT( dst, advance_widths[i] );
1491 FT_TRACE6((
"%d ", advance_widths[i] ));
1494 WRITE_SHORT( dst, lsbs[i] );
1495 FT_TRACE6((
"%d ", lsbs[i] ));
1497 FT_TRACE6((
"\n" ));
1499 *checksum = compute_ULong_sum( hmtx_table, hmtx_table_size );
1501 if ( WRITE_SFNT_BUF( hmtx_table, hmtx_table_size ) )
1506 *out_offset = dest_offset;
1508 FT_FREE( advance_widths );
1510 FT_FREE( hmtx_table );
1515 FT_FREE( advance_widths );
1517 FT_FREE( hmtx_table );
1520 error = FT_THROW( Invalid_Table );
1527 reconstruct_font( FT_Byte* transformed_buf,
1528 FT_ULong transformed_buf_size,
1529 WOFF2_Table* indices,
1532 FT_Byte** sfnt_bytes,
1533 FT_ULong* sfnt_size,
1538 FT_Error error = FT_Err_Ok;
1539 FT_Stream stream = NULL;
1540 FT_Byte* buf_cursor = NULL;
1541 FT_Byte table_entry[16];
1544 FT_Byte* sfnt = *sfnt_bytes;
1546 FT_UShort num_tables = woff2->num_tables;
1547 FT_ULong dest_offset = 12 + num_tables * 16UL;
1549 FT_ULong checksum = 0;
1550 FT_ULong loca_checksum = 0;
1552 FT_UShort num_hmetrics = 0;
1553 FT_ULong font_checksum = info->header_checksum;
1554 FT_Bool is_glyf_xform = FALSE;
1556 FT_ULong table_entry_offset = 12;
1561 info->glyf_table = find_table( indices, num_tables, TTAG_glyf );
1562 info->loca_table = find_table( indices, num_tables, TTAG_loca );
1564 if ( ( info->glyf_table == NULL ) ^ ( info->loca_table == NULL ) )
1566 FT_ERROR((
"One of `glyf'/`loca' tables missing.\n" ));
1567 return FT_THROW( Invalid_Table );
1571 if ( info->glyf_table != NULL )
1573 if ( ( info->glyf_table->flags & WOFF2_FLAGS_TRANSFORM ) !=
1574 ( info->loca_table->flags & WOFF2_FLAGS_TRANSFORM ) )
1576 FT_ERROR((
"Transformation mismatch"
1577 " between `glyf' and `loca' table." ));
1578 return FT_THROW( Invalid_Table );
1583 if ( FT_NEW( stream ) )
1585 FT_Stream_OpenMemory( stream, transformed_buf, transformed_buf_size );
1587 FT_ASSERT( FT_STREAM_POS() == 0 );
1590 for ( nn = 0; nn < num_tables; nn++ )
1592 WOFF2_TableRec table = *( indices[nn] );
1595 FT_TRACE3((
"Seeking to %lu with table size %lu.\n",
1596 table.src_offset, table.src_length ));
1597 FT_TRACE3((
"Table tag: %c%c%c%c.\n",
1598 (FT_Char)( table.Tag >> 24 ),
1599 (FT_Char)( table.Tag >> 16 ),
1600 (FT_Char)( table.Tag >> 8 ),
1601 (FT_Char)( table.Tag ) ));
1603 if ( FT_STREAM_SEEK( table.src_offset ) )
1606 if ( table.src_offset + table.src_length > transformed_buf_size )
1610 if ( table.Tag == TTAG_hhea )
1612 if ( read_num_hmetrics( stream, &num_hmetrics ) )
1616 info->num_hmetrics = num_hmetrics;
1619 if ( ( table.flags & WOFF2_FLAGS_TRANSFORM ) != WOFF2_FLAGS_TRANSFORM )
1622 if ( table.Tag == TTAG_head )
1624 if ( table.src_length < 12 )
1627 buf_cursor = transformed_buf + table.src_offset + 8;
1629 WRITE_ULONG( buf_cursor, 0 );
1632 table.dst_offset = dest_offset;
1634 checksum = compute_ULong_sum( transformed_buf + table.src_offset,
1636 FT_TRACE4((
"Checksum = %09lx.\n", checksum ));
1638 if ( WRITE_SFNT_BUF( transformed_buf + table.src_offset,
1639 table.src_length ) )
1644 FT_TRACE3((
"This table is transformed.\n" ));
1646 if ( table.Tag == TTAG_glyf )
1648 is_glyf_xform = TRUE;
1649 table.dst_offset = dest_offset;
1651 if ( reconstruct_glyf( stream,
1661 FT_TRACE4((
"Checksum = %09lx.\n", checksum ));
1664 else if ( table.Tag == TTAG_loca )
1665 checksum = loca_checksum;
1667 else if ( table.Tag == TTAG_hmtx )
1670 if ( !is_glyf_xform )
1672 if ( get_x_mins( stream, indices, num_tables, info, memory ) )
1676 table.dst_offset = dest_offset;
1678 if ( reconstruct_hmtx( stream,
1692 FT_ERROR((
"Unknown table transform.\n" ));
1697 font_checksum += checksum;
1699 buf_cursor = &table_entry[0];
1700 WRITE_ULONG( buf_cursor, table.Tag );
1701 WRITE_ULONG( buf_cursor, checksum );
1702 WRITE_ULONG( buf_cursor, table.dst_offset );
1703 WRITE_ULONG( buf_cursor, table.dst_length );
1705 WRITE_SFNT_BUF_AT( table_entry_offset, table_entry, 16 );
1708 font_checksum += compute_ULong_sum( table_entry, 16 );
1710 if ( pad4( &sfnt, sfnt_size, &dest_offset, memory ) )
1714 if ( (FT_ULong)( table.dst_offset + table.dst_length ) > dest_offset )
1716 FT_ERROR((
"Table was partially written.\n" ));
1722 info->head_table = find_table( indices, num_tables, TTAG_head );
1723 if ( !info->head_table )
1725 FT_ERROR((
"`head' table is missing.\n" ));
1729 if ( info->head_table->dst_length < 12 )
1732 buf_cursor = sfnt + info->head_table->dst_offset + 8;
1733 font_checksum = 0xB1B0AFBA - font_checksum;
1735 WRITE_ULONG( buf_cursor, font_checksum );
1737 FT_TRACE2((
"Final checksum = %09lx.\n", font_checksum ));
1739 woff2->actual_sfnt_size = dest_offset;
1744 FT_Stream_Close( stream );
1751 error = FT_THROW( Invalid_Table );
1756 FT_Stream_Close( stream );
1766 FT_LOCAL_DEF( FT_Error )
1767 woff2_open_font( FT_Stream stream,
1769 FT_Int* face_instance_index,
1770 FT_Long* num_faces )
1772 FT_Memory memory = stream->memory;
1773 FT_Error error = FT_Err_Ok;
1776 WOFF2_HeaderRec woff2;
1777 WOFF2_InfoRec info = { 0, 0, 0, NULL, NULL, NULL, NULL };
1778 WOFF2_Table tables = NULL;
1779 WOFF2_Table* indices = NULL;
1780 WOFF2_Table* temp_indices = NULL;
1781 WOFF2_Table last_table;
1786 FT_UShort xform_version;
1787 FT_ULong src_offset = 0;
1791 FT_UInt32 file_offset;
1793 FT_Byte* sfnt = NULL;
1794 FT_Stream sfnt_stream = NULL;
1797 FT_Byte* uncompressed_buf = NULL;
1799 static const FT_Frame_Field woff2_header_fields[] =
1802#define FT_STRUCTURE WOFF2_HeaderRec
1804 FT_FRAME_START( 48 ),
1805 FT_FRAME_ULONG ( signature ),
1806 FT_FRAME_ULONG ( flavor ),
1807 FT_FRAME_ULONG ( length ),
1808 FT_FRAME_USHORT ( num_tables ),
1809 FT_FRAME_SKIP_BYTES( 2 ),
1810 FT_FRAME_ULONG ( totalSfntSize ),
1811 FT_FRAME_ULONG ( totalCompressedSize ),
1812 FT_FRAME_SKIP_BYTES( 2 * 2 ),
1813 FT_FRAME_ULONG ( metaOffset ),
1814 FT_FRAME_ULONG ( metaLength ),
1815 FT_FRAME_ULONG ( metaOrigLength ),
1816 FT_FRAME_ULONG ( privOffset ),
1817 FT_FRAME_ULONG ( privLength ),
1822 FT_ASSERT( stream == face->root.stream );
1823 FT_ASSERT( FT_STREAM_POS() == 0 );
1825 face_index = FT_ABS( *face_instance_index ) & 0xFFFF;
1828 if ( FT_STREAM_READ_FIELDS( woff2_header_fields, &woff2 ) )
1831 FT_TRACE4((
"signature -> 0x%lX\n", woff2.signature ));
1832 FT_TRACE2((
"flavor -> 0x%08lx\n", woff2.flavor ));
1833 FT_TRACE4((
"length -> %lu\n", woff2.length ));
1834 FT_TRACE2((
"num_tables -> %hu\n", woff2.num_tables ));
1835 FT_TRACE4((
"totalSfntSize -> %lu\n", woff2.totalSfntSize ));
1836 FT_TRACE4((
"metaOffset -> %lu\n", woff2.metaOffset ));
1837 FT_TRACE4((
"metaLength -> %lu\n", woff2.metaLength ));
1838 FT_TRACE4((
"privOffset -> %lu\n", woff2.privOffset ));
1839 FT_TRACE4((
"privLength -> %lu\n", woff2.privLength ));
1842 if ( woff2.flavor == TTAG_wOF2 )
1843 return FT_THROW( Invalid_Table );
1846 if ( woff2.length != stream->size ||
1847 woff2.num_tables == 0 ||
1848 woff2.num_tables > 0xFFFU ||
1849 48 + woff2.num_tables * 20UL >= woff2.length ||
1850 ( woff2.metaOffset == 0 && ( woff2.metaLength != 0 ||
1851 woff2.metaOrigLength != 0 ) ) ||
1852 ( woff2.metaLength != 0 && woff2.metaOrigLength == 0 ) ||
1853 ( woff2.metaOffset >= woff2.length ) ||
1854 ( woff2.length - woff2.metaOffset < woff2.metaLength ) ||
1855 ( woff2.privOffset == 0 && woff2.privLength != 0 ) ||
1856 ( woff2.privOffset >= woff2.length ) ||
1857 ( woff2.length - woff2.privOffset < woff2.privLength ) )
1859 FT_ERROR((
"woff2_open_font: invalid WOFF2 header\n" ));
1860 return FT_THROW( Invalid_Table );
1863 FT_TRACE2((
"woff2_open_font: WOFF2 Header is valid.\n" ));
1865 woff2.ttc_fonts = NULL;
1868 if ( FT_QNEW_ARRAY( tables, woff2.num_tables ) ||
1869 FT_QNEW_ARRAY( indices, woff2.num_tables ) )
1872 FT_TRACE2((
"\n" ));
1873 FT_TRACE2((
" tag flags transform origLen transformLen offset\n" ));
1874 FT_TRACE2((
" -----------------------------------------------------------\n" ));
1877 for ( nn = 0; nn < woff2.num_tables; nn++ )
1879 WOFF2_Table table = tables + nn;
1882 if ( FT_READ_BYTE( table->FlagByte ) )
1885 if ( ( table->FlagByte & 0x3f ) == 0x3f )
1887 if ( FT_READ_ULONG( table->Tag ) )
1892 table->Tag = woff2_known_tags( table->FlagByte & 0x3f );
1895 FT_ERROR((
"woff2_open_font: Unknown table tag." ));
1896 error = FT_THROW( Invalid_Table );
1902 xform_version = ( table->FlagByte >> 6 ) & 0x03;
1905 if ( table->Tag == TTAG_glyf || table->Tag == TTAG_loca )
1907 if ( xform_version == 0 )
1908 flags |= WOFF2_FLAGS_TRANSFORM;
1910 else if ( xform_version != 0 )
1911 flags |= WOFF2_FLAGS_TRANSFORM;
1913 flags |= xform_version;
1915 if ( READ_BASE128( table->dst_length ) )
1918 table->TransformLength = table->dst_length;
1920 if ( ( flags & WOFF2_FLAGS_TRANSFORM ) != 0 )
1922 if ( READ_BASE128( table->TransformLength ) )
1925 if ( table->Tag == TTAG_loca && table->TransformLength )
1927 FT_ERROR((
"woff2_open_font: Invalid loca `transformLength'.\n" ));
1928 error = FT_THROW( Invalid_Table );
1933 if ( src_offset + table->TransformLength < src_offset )
1935 FT_ERROR((
"woff2_open_font: invalid WOFF2 table directory.\n" ));
1936 error = FT_THROW( Invalid_Table );
1940 table->flags = flags;
1941 table->src_offset = src_offset;
1942 table->src_length = table->TransformLength;
1943 src_offset += table->TransformLength;
1944 table->dst_offset = 0;
1946 FT_TRACE2((
" %c%c%c%c %08d %08d %08lu %08lu %08lu\n",
1947 (FT_Char)( table->Tag >> 24 ),
1948 (FT_Char)( table->Tag >> 16 ),
1949 (FT_Char)( table->Tag >> 8 ),
1950 (FT_Char)( table->Tag ),
1951 table->FlagByte & 0x3f,
1952 ( table->FlagByte >> 6 ) & 0x03,
1954 table->TransformLength,
1955 table->src_offset ));
1957 indices[nn] = table;
1961 last_table = indices[woff2.num_tables - 1];
1963 woff2.uncompressed_size = last_table->src_offset +
1964 last_table->src_length;
1965 if ( woff2.uncompressed_size < last_table->src_offset )
1967 error = FT_THROW( Invalid_Table );
1971 FT_TRACE2((
"Table directory parsed.\n" ));
1974 woff2.num_fonts = 1;
1975 woff2.header_version = 0;
1977 if ( woff2.flavor == TTAG_ttcf )
1979 FT_TRACE2((
"Font is a TTC, reading collection directory.\n" ));
1981 if ( FT_READ_ULONG( woff2.header_version ) )
1984 if ( woff2.header_version != 0x00010000 &&
1985 woff2.header_version != 0x00020000 )
1987 error = FT_THROW( Invalid_Table );
1991 if ( READ_255USHORT( woff2.num_fonts ) )
1994 if ( !woff2.num_fonts )
1996 error = FT_THROW( Invalid_Table );
2000 FT_TRACE4((
"Number of fonts in TTC: %d\n", woff2.num_fonts ));
2003 if ( FT_NEW_ARRAY( woff2.ttc_fonts, woff2.num_fonts ) )
2006 for ( nn = 0; nn < woff2.num_fonts; nn++ )
2008 WOFF2_TtcFont ttc_font = woff2.ttc_fonts + nn;
2011 if ( READ_255USHORT( ttc_font->num_tables ) )
2013 if ( FT_READ_ULONG( ttc_font->flavor ) )
2016 if ( FT_QNEW_ARRAY( ttc_font->table_indices, ttc_font->num_tables ) )
2019 FT_TRACE5((
"Number of tables in font %d: %d\n",
2020 nn, ttc_font->num_tables ));
2022#ifdef FT_DEBUG_LEVEL_TRACE
2023 if ( ttc_font->num_tables )
2024 FT_TRACE6((
" Indices: " ));
2030 for ( j = 0; j < ttc_font->num_tables; j++ )
2032 FT_UShort table_index;
2036 if ( READ_255USHORT( table_index ) )
2039 FT_TRACE6((
"%hu ", table_index ));
2040 if ( table_index >= woff2.num_tables )
2042 FT_ERROR((
"woff2_open_font: invalid table index\n" ));
2043 error = FT_THROW( Invalid_Table );
2047 ttc_font->table_indices[j] = table_index;
2049 table = indices[table_index];
2050 if ( table->Tag == TTAG_loca )
2051 loca_index = table_index;
2052 if ( table->Tag == TTAG_glyf )
2053 glyf_index = table_index;
2056#ifdef FT_DEBUG_LEVEL_TRACE
2057 if ( ttc_font->num_tables )
2058 FT_TRACE6((
"\n" ));
2062 if ( glyf_index > 0 || loca_index > 0 )
2064 if ( glyf_index > loca_index ||
2065 loca_index - glyf_index != 1 )
2067 error = FT_THROW( Invalid_Table );
2074 FT_TRACE2((
"WOFF2 collection directory is valid.\n" ));
2077 woff2.ttc_fonts = NULL;
2079 woff2.compressed_offset = FT_STREAM_POS();
2080 file_offset = ROUND4( woff2.compressed_offset +
2081 woff2.totalCompressedSize );
2084 if ( file_offset > woff2.length )
2086 error = FT_THROW( Invalid_Table );
2090 if ( woff2.metaOffset )
2092 if ( file_offset != woff2.metaOffset )
2094 error = FT_THROW( Invalid_Table );
2097 file_offset = ROUND4( woff2.metaOffset + woff2.metaLength );
2100 if ( woff2.privOffset )
2102 if ( file_offset != woff2.privOffset )
2104 error = FT_THROW( Invalid_Table );
2107 file_offset = ROUND4( woff2.privOffset + woff2.privLength );
2110 if ( file_offset != ( ROUND4( woff2.length ) ) )
2112 error = FT_THROW( Invalid_Table );
2117 *num_faces = woff2.num_fonts;
2119 if ( *face_instance_index < 0 && face_index > 0 )
2122 if ( face_index >= woff2.num_fonts )
2124 if ( *face_instance_index >= 0 )
2126 error = FT_THROW( Invalid_Argument );
2134 if ( woff2.header_version )
2136 WOFF2_TtcFont ttc_font = woff2.ttc_fonts + face_index;
2139 if ( ttc_font->num_tables == 0 || ttc_font->num_tables > 0xFFFU )
2141 FT_ERROR((
"woff2_open_font: invalid WOFF2 CollectionFontEntry\n" ));
2142 error = FT_THROW( Invalid_Table );
2147 if ( FT_QNEW_ARRAY( temp_indices,
2148 ttc_font->num_tables ) )
2151 FT_TRACE4((
"Storing tables for TTC face index %d.\n", face_index ));
2152 for ( nn = 0; nn < ttc_font->num_tables; nn++ )
2153 temp_indices[nn] = indices[ttc_font->table_indices[nn]];
2156 if ( FT_QRENEW_ARRAY( indices,
2158 ttc_font->num_tables ) )
2161 for ( nn = 0; nn < ttc_font->num_tables; nn++ )
2162 indices[nn] = temp_indices[nn];
2164 FT_FREE( temp_indices );
2167 woff2.flavor = ttc_font->flavor;
2168 woff2.num_tables = ttc_font->num_tables;
2172 sfnt_size = 12 + woff2.num_tables * 16UL;
2175 if ( woff2.totalSfntSize > sfnt_size )
2180 if ( ( woff2.totalSfntSize >> 6 ) > woff2.length )
2181 sfnt_size = woff2.length << 6;
2183 sfnt_size = woff2.totalSfntSize;
2185 if ( sfnt_size >= MAX_SFNT_SIZE )
2186 sfnt_size = MAX_SFNT_SIZE;
2188#ifdef FT_DEBUG_LEVEL_TRACE
2189 if ( sfnt_size != woff2.totalSfntSize )
2190 FT_TRACE4((
"adjusting estimate of uncompressed font size"
2197 if ( FT_QALLOC( sfnt, sfnt_size ) ||
2198 FT_NEW( sfnt_stream ) )
2202 FT_Byte* sfnt_header = sfnt;
2204 FT_Int entrySelector = FT_MSB( woff2.num_tables );
2205 FT_Int searchRange = ( 1 << entrySelector ) * 16;
2206 FT_Int rangeShift = woff2.num_tables * 16 - searchRange;
2209 WRITE_ULONG ( sfnt_header, woff2.flavor );
2210 WRITE_USHORT( sfnt_header, woff2.num_tables );
2211 WRITE_USHORT( sfnt_header, searchRange );
2212 WRITE_USHORT( sfnt_header, entrySelector );
2213 WRITE_USHORT( sfnt_header, rangeShift );
2216 info.header_checksum = compute_ULong_sum( sfnt, 12 );
2221 sizeof ( WOFF2_Table ),
2225 for ( nn = 1; nn < woff2.num_tables; nn++ )
2227 FT_Tag tag = indices[nn]->Tag;
2230 if ( tag == indices[nn - 1]->Tag )
2232 FT_ERROR((
"woff2_open_font:"
2233 " multiple tables with tag `%c%c%c%c'.\n",
2234 (FT_Char)( tag >> 24 ),
2235 (FT_Char)( tag >> 16 ),
2236 (FT_Char)( tag >> 8 ),
2237 (FT_Char)( tag ) ));
2238 error = FT_THROW( Invalid_Table );
2243 if ( woff2.uncompressed_size < 1 )
2245 error = FT_THROW( Invalid_Table );
2254 if ( woff2.uncompressed_size > MAX_SFNT_SIZE )
2256 FT_ERROR((
"Uncompressed font too large.\n" ));
2257 error = FT_THROW( Array_Too_Large );
2262 if ( FT_QALLOC( uncompressed_buf, woff2.uncompressed_size ) ||
2263 FT_FRAME_ENTER( woff2.totalCompressedSize ) )
2267 error = woff2_decompress( uncompressed_buf,
2268 woff2.uncompressed_size,
2270 woff2.totalCompressedSize );
2277 error = reconstruct_font( uncompressed_buf,
2278 woff2.uncompressed_size,
2290 if ( woff2.actual_sfnt_size < sfnt_size )
2292 FT_TRACE5((
"Trimming sfnt stream from %lu to %lu.\n",
2293 sfnt_size, woff2.actual_sfnt_size ));
2294 if ( FT_QREALLOC( sfnt,
2295 (FT_ULong)( sfnt_size ),
2296 (FT_ULong)( woff2.actual_sfnt_size ) ) )
2302 FT_Stream_OpenMemory( sfnt_stream, sfnt, woff2.actual_sfnt_size );
2303 sfnt_stream->memory = stream->memory;
2304 sfnt_stream->close = stream_close;
2308 ( face->root.face_flags & FT_FACE_FLAG_EXTERNAL_STREAM ) != 0 );
2310 face->root.stream = sfnt_stream;
2311 face->root.face_flags &= ~FT_FACE_FLAG_EXTERNAL_STREAM;
2314 if ( *face_instance_index >= 0 )
2315 *face_instance_index = 0;
2317 *face_instance_index = -1;
2319 FT_TRACE2((
"woff2_open_font: SFNT synthesized.\n" ));
2324 FT_FREE( uncompressed_buf );
2325 FT_FREE( info.x_mins );
2327 if ( woff2.ttc_fonts )
2329 WOFF2_TtcFont ttc_font = woff2.ttc_fonts;
2332 for ( nn = 0; nn < woff2.num_fonts; nn++ )
2334 FT_FREE( ttc_font->table_indices );
2338 FT_FREE( woff2.ttc_fonts );
2346 FT_Stream_Close( sfnt_stream );
2347 FT_FREE( sfnt_stream );
2355#undef READ_255USHORT
2361#undef WRITE_SFNT_BUF
2362#undef WRITE_SFNT_BUF_AT
2364#undef N_CONTOUR_STREAM
2365#undef N_POINTS_STREAM
2368#undef COMPOSITE_STREAM
2370#undef INSTRUCTION_STREAM
2375 typedef int sfwoff2_dummy_;