1 /***************************************************************************/
5 /* CFF token stream parser (body) */
7 /* Copyright 1996-2004, 2007-2014 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
10 /* This file is part of the FreeType project, and may only be used, */
11 /* modified, and distributed under the terms of the FreeType project */
12 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
13 /* this file you indicate that you have read the license and */
14 /* understand and accept it fully. */
16 /***************************************************************************/
21 #include FT_INTERNAL_STREAM_H
22 #include FT_INTERNAL_DEBUG_H
28 /*************************************************************************/
30 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
31 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
32 /* messages during execution. */
35 #define FT_COMPONENT trace_cffparse
39 cff_parser_init( CFF_Parser parser,
44 FT_MEM_ZERO( parser, sizeof ( *parser ) );
46 parser->top = parser->stack;
47 parser->object_code = code;
48 parser->object = object;
49 parser->library = library;
55 cff_parse_integer( FT_Byte* start,
68 val = (FT_Short)( ( (FT_UShort)p[0] << 8 ) | p[1] );
75 val = (FT_Long)( ( (FT_ULong)p[0] << 24 ) |
76 ( (FT_ULong)p[1] << 16 ) |
77 ( (FT_ULong)p[2] << 8 ) |
89 val = ( v - 247 ) * 256 + p[0] + 108;
96 val = -( v - 251 ) * 256 - p[0] - 108;
104 FT_TRACE4(( "!!!END OF DATA:!!!" ));
109 static const FT_Long power_tens[] =
126 cff_parse_real( FT_Byte* start,
135 FT_Long result, number, exponent;
136 FT_Int sign = 0, exponent_sign = 0, have_overflow = 0;
137 FT_Long exponent_add, integer_length, fraction_length;
152 /* First of all, read the integer part. */
157 /* If we entered this iteration with phase == 4, we need to */
158 /* read a new byte. This also skips past the initial 0x1E. */
163 /* Make sure we don't read past the end. */
168 /* Get the nibble. */
169 nib = ( p[0] >> phase ) & 0xF;
178 /* Increase exponent if we can't add the digit. */
179 if ( number >= 0xCCCCCCCL )
181 /* Skip leading zeros. */
182 else if ( nib || number )
185 number = number * 10 + nib;
190 /* Read fraction part, if any. */
194 /* If we entered this iteration with phase == 4, we need */
195 /* to read a new byte. */
200 /* Make sure we don't read past the end. */
205 /* Get the nibble. */
206 nib = ( p[0] >> phase ) & 0xF;
211 /* Skip leading zeros if possible. */
212 if ( !nib && !number )
214 /* Only add digit if we don't overflow. */
215 else if ( number < 0xCCCCCCCL && fraction_length < 9 )
218 number = number * 10 + nib;
222 /* Read exponent, if any. */
233 /* If we entered this iteration with phase == 4, */
234 /* we need to read a new byte. */
239 /* Make sure we don't read past the end. */
244 /* Get the nibble. */
245 nib = ( p[0] >> phase ) & 0xF;
250 /* Arbitrarily limit exponent. */
251 if ( exponent > 1000 )
254 exponent = exponent * 10 + nib;
258 exponent = -exponent;
272 /* We don't check `power_ten' and `exponent_add'. */
273 exponent += power_ten + exponent_add;
277 /* Only use `fraction_length'. */
278 fraction_length += integer_length;
279 exponent += integer_length;
281 if ( fraction_length <= 5 )
283 if ( number > 0x7FFFL )
285 result = FT_DivFix( number, 10 );
286 *scaling = exponent - fraction_length + 1;
292 FT_Long new_fraction_length, shift;
295 /* Make `scaling' as small as possible. */
296 new_fraction_length = FT_MIN( exponent, 5 );
297 shift = new_fraction_length - fraction_length;
301 exponent -= new_fraction_length;
302 number *= power_tens[shift];
303 if ( number > 0x7FFFL )
310 exponent -= fraction_length;
313 exponent -= fraction_length;
315 result = (FT_Long)( (FT_ULong)number << 16 );
321 if ( ( number / power_tens[fraction_length - 5] ) > 0x7FFFL )
323 result = FT_DivFix( number, power_tens[fraction_length - 4] );
324 *scaling = exponent - 4;
328 result = FT_DivFix( number, power_tens[fraction_length - 5] );
329 *scaling = exponent - 5;
335 integer_length += exponent;
336 fraction_length -= exponent;
338 if ( integer_length > 5 )
340 if ( integer_length < -5 )
343 /* Remove non-significant digits. */
344 if ( integer_length < 0 )
346 number /= power_tens[-integer_length];
347 fraction_length += integer_length;
350 /* this can only happen if exponent was non-zero */
351 if ( fraction_length == 10 )
354 fraction_length -= 1;
357 /* Convert into 16.16 format. */
358 if ( fraction_length > 0 )
360 if ( ( number / power_tens[fraction_length] ) > 0x7FFFL )
363 result = FT_DivFix( number, power_tens[fraction_length] );
367 number *= power_tens[-fraction_length];
369 if ( number > 0x7FFFL )
372 result = (FT_Long)( (FT_ULong)number << 16 );
383 result = 0x7FFFFFFFL;
384 FT_TRACE4(( "!!!OVERFLOW:!!!" ));
389 FT_TRACE4(( "!!!UNDERFLOW:!!!" ));
394 FT_TRACE4(( "!!!END OF DATA:!!!" ));
399 /* read a number, either integer or real */
401 cff_parse_num( FT_Byte** d )
403 return **d == 30 ? ( cff_parse_real( d[0], d[1], 0, NULL ) >> 16 )
404 : cff_parse_integer( d[0], d[1] );
408 /* read a floating point number, either integer or real */
410 do_fixed( FT_Byte** d,
414 return cff_parse_real( d[0], d[1], scaling, NULL );
417 FT_Long val = cff_parse_integer( d[0], d[1] );
421 val *= power_tens[scaling];
428 else if ( val < -0x7FFF )
434 return (FT_Long)( (FT_ULong)val << 16 );
437 FT_TRACE4(( "!!!OVERFLOW:!!!" ));
443 /* read a floating point number, either integer or real */
445 cff_parse_fixed( FT_Byte** d )
447 return do_fixed( d, 0 );
451 /* read a floating point number, either integer or real, */
452 /* but return `10^scaling' times the number read in */
454 cff_parse_fixed_scaled( FT_Byte** d,
457 return do_fixed( d, scaling );
461 /* read a floating point number, either integer or real, */
462 /* and return it as precise as possible -- `scaling' returns */
463 /* the scaling factor (as a power of 10) */
465 cff_parse_fixed_dynamic( FT_Byte** d,
468 FT_ASSERT( scaling );
471 return cff_parse_real( d[0], d[1], 0, scaling );
475 FT_Int integer_length;
478 number = cff_parse_integer( d[0], d[1] );
480 if ( number > 0x7FFFL )
482 for ( integer_length = 5; integer_length < 10; integer_length++ )
483 if ( number < power_tens[integer_length] )
486 if ( ( number / power_tens[integer_length - 5] ) > 0x7FFFL )
488 *scaling = integer_length - 4;
489 return FT_DivFix( number, power_tens[integer_length - 4] );
493 *scaling = integer_length - 5;
494 return FT_DivFix( number, power_tens[integer_length - 5] );
500 return (FT_Long)( (FT_ULong)number << 16 );
507 cff_parse_font_matrix( CFF_Parser parser )
509 CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
510 FT_Matrix* matrix = &dict->font_matrix;
511 FT_Vector* offset = &dict->font_offset;
512 FT_ULong* upm = &dict->units_per_em;
513 FT_Byte** data = parser->stack;
514 FT_Error error = FT_ERR( Stack_Underflow );
517 if ( parser->top >= parser->stack + 6 )
524 dict->has_font_matrix = TRUE;
526 /* We expect a well-formed font matrix, this is, the matrix elements */
527 /* `xx' and `yy' are of approximately the same magnitude. To avoid */
528 /* loss of precision, we use the magnitude of element `xx' to scale */
529 /* all other elements. The scaling factor is then contained in the */
530 /* `units_per_em' value. */
532 matrix->xx = cff_parse_fixed_dynamic( data++, &scaling );
536 if ( scaling < 0 || scaling > 9 )
538 /* Return default matrix in case of unlikely values. */
540 FT_TRACE1(( "cff_parse_font_matrix:"
541 " strange scaling value for xx element (%d),\n"
543 " using default matrix\n", scaling ));
545 matrix->xx = 0x10000L;
548 matrix->yy = 0x10000L;
556 matrix->yx = cff_parse_fixed_scaled( data++, scaling );
557 matrix->xy = cff_parse_fixed_scaled( data++, scaling );
558 matrix->yy = cff_parse_fixed_scaled( data++, scaling );
559 offset->x = cff_parse_fixed_scaled( data++, scaling );
560 offset->y = cff_parse_fixed_scaled( data, scaling );
562 *upm = power_tens[scaling];
564 FT_TRACE4(( " [%f %f %f %f %f %f]\n",
565 (double)matrix->xx / *upm / 65536,
566 (double)matrix->xy / *upm / 65536,
567 (double)matrix->yx / *upm / 65536,
568 (double)matrix->yy / *upm / 65536,
569 (double)offset->x / *upm / 65536,
570 (double)offset->y / *upm / 65536 ));
579 cff_parse_font_bbox( CFF_Parser parser )
581 CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
582 FT_BBox* bbox = &dict->font_bbox;
583 FT_Byte** data = parser->stack;
587 error = FT_ERR( Stack_Underflow );
589 if ( parser->top >= parser->stack + 4 )
591 bbox->xMin = FT_RoundFix( cff_parse_fixed( data++ ) );
592 bbox->yMin = FT_RoundFix( cff_parse_fixed( data++ ) );
593 bbox->xMax = FT_RoundFix( cff_parse_fixed( data++ ) );
594 bbox->yMax = FT_RoundFix( cff_parse_fixed( data ) );
597 FT_TRACE4(( " [%d %d %d %d]\n",
601 bbox->yMax / 65536 ));
609 cff_parse_private_dict( CFF_Parser parser )
611 CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
612 FT_Byte** data = parser->stack;
616 error = FT_ERR( Stack_Underflow );
618 if ( parser->top >= parser->stack + 2 )
620 dict->private_size = cff_parse_num( data++ );
621 dict->private_offset = cff_parse_num( data );
622 FT_TRACE4(( " %lu %lu\n",
623 dict->private_size, dict->private_offset ));
633 cff_parse_cid_ros( CFF_Parser parser )
635 CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
636 FT_Byte** data = parser->stack;
640 error = FT_ERR( Stack_Underflow );
642 if ( parser->top >= parser->stack + 3 )
644 dict->cid_registry = (FT_UInt)cff_parse_num( data++ );
645 dict->cid_ordering = (FT_UInt)cff_parse_num( data++ );
647 FT_TRACE1(( "cff_parse_cid_ros: real supplement is rounded\n" ));
648 dict->cid_supplement = cff_parse_num( data );
649 if ( dict->cid_supplement < 0 )
650 FT_TRACE1(( "cff_parse_cid_ros: negative supplement %d is found\n",
651 dict->cid_supplement ));
654 FT_TRACE4(( " %d %d %d\n",
657 dict->cid_supplement ));
664 #define CFF_FIELD_NUM( code, name, id ) \
665 CFF_FIELD( code, name, id, cff_kind_num )
666 #define CFF_FIELD_FIXED( code, name, id ) \
667 CFF_FIELD( code, name, id, cff_kind_fixed )
668 #define CFF_FIELD_FIXED_1000( code, name, id ) \
669 CFF_FIELD( code, name, id, cff_kind_fixed_thousand )
670 #define CFF_FIELD_STRING( code, name, id ) \
671 CFF_FIELD( code, name, id, cff_kind_string )
672 #define CFF_FIELD_BOOL( code, name, id ) \
673 CFF_FIELD( code, name, id, cff_kind_bool )
675 #define CFFCODE_TOPDICT 0x1000
676 #define CFFCODE_PRIVATE 0x2000
679 #ifndef FT_CONFIG_OPTION_PIC
683 #undef CFF_FIELD_DELTA
686 #ifndef FT_DEBUG_LEVEL_TRACE
689 #define CFF_FIELD_CALLBACK( code, name, id ) \
694 cff_parse_ ## name, \
698 #define CFF_FIELD( code, name, id, kind ) \
702 FT_FIELD_OFFSET( name ), \
703 FT_FIELD_SIZE( name ), \
707 #define CFF_FIELD_DELTA( code, name, max, id ) \
711 FT_FIELD_OFFSET( name ), \
712 FT_FIELD_SIZE_DELTA( name ), \
715 FT_FIELD_OFFSET( num_ ## name ) \
718 static const CFF_Field_Handler cff_field_handlers[] =
721 #include "cfftoken.h"
723 { 0, 0, 0, 0, 0, 0, 0 }
727 #else /* FT_DEBUG_LEVEL_TRACE */
731 #define CFF_FIELD_CALLBACK( code, name, id ) \
736 cff_parse_ ## name, \
741 #define CFF_FIELD( code, name, id, kind ) \
745 FT_FIELD_OFFSET( name ), \
746 FT_FIELD_SIZE( name ), \
751 #define CFF_FIELD_DELTA( code, name, max, id ) \
755 FT_FIELD_OFFSET( name ), \
756 FT_FIELD_SIZE_DELTA( name ), \
759 FT_FIELD_OFFSET( num_ ## name ), \
763 static const CFF_Field_Handler cff_field_handlers[] =
766 #include "cfftoken.h"
768 { 0, 0, 0, 0, 0, 0, 0, 0 }
772 #endif /* FT_DEBUG_LEVEL_TRACE */
775 #else /* FT_CONFIG_OPTION_PIC */
779 FT_Destroy_Class_cff_field_handlers( FT_Library library,
780 CFF_Field_Handler* clazz )
782 FT_Memory memory = library->memory;
791 FT_Create_Class_cff_field_handlers( FT_Library library,
792 CFF_Field_Handler** output_class )
794 CFF_Field_Handler* clazz = NULL;
796 FT_Memory memory = library->memory;
802 #define CFF_FIELD( code, name, id, kind ) i++;
803 #undef CFF_FIELD_DELTA
804 #define CFF_FIELD_DELTA( code, name, max, id ) i++;
805 #undef CFF_FIELD_CALLBACK
806 #define CFF_FIELD_CALLBACK( code, name, id ) i++;
808 #include "cfftoken.h"
810 i++; /* { 0, 0, 0, 0, 0, 0, 0 } */
812 if ( FT_ALLOC( clazz, sizeof ( CFF_Field_Handler ) * i ) )
818 #ifndef FT_DEBUG_LEVEL_TRACE
821 #undef CFF_FIELD_CALLBACK
822 #define CFF_FIELD_CALLBACK( code_, name_, id_ ) \
823 clazz[i].kind = cff_kind_callback; \
824 clazz[i].code = code_ | CFFCODE; \
825 clazz[i].offset = 0; \
827 clazz[i].reader = cff_parse_ ## name_; \
828 clazz[i].array_max = 0; \
829 clazz[i].count_offset = 0; \
833 #define CFF_FIELD( code_, name_, id_, kind_ ) \
834 clazz[i].kind = kind_; \
835 clazz[i].code = code_ | CFFCODE; \
836 clazz[i].offset = FT_FIELD_OFFSET( name_ ); \
837 clazz[i].size = FT_FIELD_SIZE( name_ ); \
838 clazz[i].reader = 0; \
839 clazz[i].array_max = 0; \
840 clazz[i].count_offset = 0; \
843 #undef CFF_FIELD_DELTA
844 #define CFF_FIELD_DELTA( code_, name_, max_, id_ ) \
845 clazz[i].kind = cff_kind_delta; \
846 clazz[i].code = code_ | CFFCODE; \
847 clazz[i].offset = FT_FIELD_OFFSET( name_ ); \
848 clazz[i].size = FT_FIELD_SIZE_DELTA( name_ ); \
849 clazz[i].reader = 0; \
850 clazz[i].array_max = max_; \
851 clazz[i].count_offset = FT_FIELD_OFFSET( num_ ## name_ ); \
854 #include "cfftoken.h"
861 clazz[i].array_max = 0;
862 clazz[i].count_offset = 0;
865 #else /* FT_DEBUG_LEVEL_TRACE */
868 #undef CFF_FIELD_CALLBACK
869 #define CFF_FIELD_CALLBACK( code_, name_, id_ ) \
870 clazz[i].kind = cff_kind_callback; \
871 clazz[i].code = code_ | CFFCODE; \
872 clazz[i].offset = 0; \
874 clazz[i].reader = cff_parse_ ## name_; \
875 clazz[i].array_max = 0; \
876 clazz[i].count_offset = 0; \
881 #define CFF_FIELD( code_, name_, id_, kind_ ) \
882 clazz[i].kind = kind_; \
883 clazz[i].code = code_ | CFFCODE; \
884 clazz[i].offset = FT_FIELD_OFFSET( name_ ); \
885 clazz[i].size = FT_FIELD_SIZE( name_ ); \
886 clazz[i].reader = 0; \
887 clazz[i].array_max = 0; \
888 clazz[i].count_offset = 0; \
892 #undef CFF_FIELD_DELTA
893 #define CFF_FIELD_DELTA( code_, name_, max_, id_ ) \
894 clazz[i].kind = cff_kind_delta; \
895 clazz[i].code = code_ | CFFCODE; \
896 clazz[i].offset = FT_FIELD_OFFSET( name_ ); \
897 clazz[i].size = FT_FIELD_SIZE_DELTA( name_ ); \
898 clazz[i].reader = 0; \
899 clazz[i].array_max = max_; \
900 clazz[i].count_offset = FT_FIELD_OFFSET( num_ ## name_ ); \
904 #include "cfftoken.h"
911 clazz[i].array_max = 0;
912 clazz[i].count_offset = 0;
916 #endif /* FT_DEBUG_LEVEL_TRACE */
919 *output_class = clazz;
925 #endif /* FT_CONFIG_OPTION_PIC */
928 FT_LOCAL_DEF( FT_Error )
929 cff_parser_run( CFF_Parser parser,
934 FT_Error error = FT_Err_Ok;
935 FT_Library library = parser->library;
936 FT_UNUSED( library );
939 parser->top = parser->stack;
940 parser->start = start;
941 parser->limit = limit;
942 parser->cursor = start;
949 if ( v >= 27 && v != 31 )
951 /* it's a number; we will push its position on the stack */
952 if ( parser->top - parser->stack >= CFF_MAX_STACK_DEPTH )
960 /* skip real number */
964 /* An unterminated floating point number at the */
965 /* end of a dictionary is invalid but harmless. */
986 /* This is not a number, hence it's an operator. Compute its code */
987 /* and look for it in our current list. */
990 FT_UInt num_args = (FT_UInt)
991 ( parser->top - parser->stack );
992 const CFF_Field_Handler* field;
999 /* two byte operator */
1004 code = 0x100 | p[0];
1006 code = code | parser->object_code;
1008 for ( field = CFF_FIELD_HANDLERS_GET; field->kind; field++ )
1010 if ( field->code == (FT_Int)code )
1012 /* we found our field's handler; read it */
1014 FT_Byte* q = (FT_Byte*)parser->object + field->offset;
1017 #ifdef FT_DEBUG_LEVEL_TRACE
1018 FT_TRACE4(( " %s", field->id ));
1021 /* check that we have enough arguments -- except for */
1022 /* delta encoded arrays, which can be empty */
1023 if ( field->kind != cff_kind_delta && num_args < 1 )
1024 goto Stack_Underflow;
1026 switch ( field->kind )
1029 case cff_kind_string:
1031 val = cff_parse_num( parser->stack );
1034 case cff_kind_fixed:
1035 val = cff_parse_fixed( parser->stack );
1038 case cff_kind_fixed_thousand:
1039 val = cff_parse_fixed_scaled( parser->stack, 3 );
1042 switch ( field->size )
1044 case (8 / FT_CHAR_BIT):
1045 *(FT_Byte*)q = (FT_Byte)val;
1048 case (16 / FT_CHAR_BIT):
1049 *(FT_Short*)q = (FT_Short)val;
1052 case (32 / FT_CHAR_BIT):
1053 *(FT_Int32*)q = (FT_Int)val;
1056 default: /* for 64-bit systems */
1060 #ifdef FT_DEBUG_LEVEL_TRACE
1061 switch ( field->kind )
1064 FT_TRACE4(( " %s\n", val ? "true" : "false" ));
1067 case cff_kind_string:
1068 FT_TRACE4(( " %ld (SID)\n", val ));
1072 FT_TRACE4(( " %ld\n", val ));
1075 case cff_kind_fixed:
1076 FT_TRACE4(( " %f\n", (double)val / 65536 ));
1079 case cff_kind_fixed_thousand:
1080 FT_TRACE4(( " %f\n", (double)val / 65536 / 1000 ));
1083 ; /* never reached */
1089 case cff_kind_delta:
1091 FT_Byte* qcount = (FT_Byte*)parser->object +
1092 field->count_offset;
1094 FT_Byte** data = parser->stack;
1097 if ( num_args > field->array_max )
1098 num_args = field->array_max;
1100 FT_TRACE4(( " [" ));
1103 *qcount = (FT_Byte)num_args;
1106 while ( num_args > 0 )
1108 val += cff_parse_num( data++ );
1109 switch ( field->size )
1111 case (8 / FT_CHAR_BIT):
1112 *(FT_Byte*)q = (FT_Byte)val;
1115 case (16 / FT_CHAR_BIT):
1116 *(FT_Short*)q = (FT_Short)val;
1119 case (32 / FT_CHAR_BIT):
1120 *(FT_Int32*)q = (FT_Int)val;
1123 default: /* for 64-bit systems */
1127 FT_TRACE4(( " %ld", val ));
1133 FT_TRACE4(( "]\n" ));
1137 default: /* callback */
1138 error = field->reader( parser );
1146 /* this is an unknown operator, or it is unsupported; */
1147 /* we will ignore it for now. */
1151 parser->top = parser->stack;
1160 error = FT_THROW( Invalid_Argument );
1164 error = FT_THROW( Invalid_Argument );
1168 error = FT_THROW( Invalid_Argument );