1 /***************************************************************************/
5 /* CFF token stream parser (body) */
7 /* Copyright 1996-2004, 2007-2011 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_Int)p[0] << 8 ) | p[1] );
76 val = ( (FT_Long)p[0] << 24 ) |
77 ( (FT_Long)p[1] << 16 ) |
78 ( (FT_Long)p[2] << 8 ) |
91 val = ( v - 247 ) * 256 + p[0] + 108;
99 val = -( v - 251 ) * 256 - p[0] - 108;
112 static const FT_Long power_tens[] =
129 cff_parse_real( FT_Byte* start,
138 FT_Long result, number, exponent;
139 FT_Int sign = 0, exponent_sign = 0;
140 FT_Long exponent_add, integer_length, fraction_length;
155 /* First of all, read the integer part. */
160 /* If we entered this iteration with phase == 4, we need to */
161 /* read a new byte. This also skips past the initial 0x1E. */
166 /* Make sure we don't read past the end. */
171 /* Get the nibble. */
172 nib = ( p[0] >> phase ) & 0xF;
181 /* Increase exponent if we can't add the digit. */
182 if ( number >= 0xCCCCCCCL )
184 /* Skip leading zeros. */
185 else if ( nib || number )
188 number = number * 10 + nib;
193 /* Read fraction part, if any. */
197 /* If we entered this iteration with phase == 4, we need */
198 /* to read a new byte. */
203 /* Make sure we don't read past the end. */
208 /* Get the nibble. */
209 nib = ( p[0] >> phase ) & 0xF;
214 /* Skip leading zeros if possible. */
215 if ( !nib && !number )
217 /* Only add digit if we don't overflow. */
218 else if ( number < 0xCCCCCCCL && fraction_length < 9 )
221 number = number * 10 + nib;
225 /* Read exponent, if any. */
236 /* If we entered this iteration with phase == 4, */
237 /* we need to read a new byte. */
242 /* Make sure we don't read past the end. */
247 /* Get the nibble. */
248 nib = ( p[0] >> phase ) & 0xF;
253 exponent = exponent * 10 + nib;
255 /* Arbitrarily limit exponent. */
256 if ( exponent > 1000 )
261 exponent = -exponent;
264 /* We don't check `power_ten' and `exponent_add'. */
265 exponent += power_ten + exponent_add;
269 /* Only use `fraction_length'. */
270 fraction_length += integer_length;
271 exponent += integer_length;
273 if ( fraction_length <= 5 )
275 if ( number > 0x7FFFL )
277 result = FT_DivFix( number, 10 );
278 *scaling = exponent - fraction_length + 1;
284 FT_Long new_fraction_length, shift;
287 /* Make `scaling' as small as possible. */
288 new_fraction_length = FT_MIN( exponent, 5 );
289 exponent -= new_fraction_length;
290 shift = new_fraction_length - fraction_length;
292 number *= power_tens[shift];
293 if ( number > 0x7FFFL )
300 exponent -= fraction_length;
302 result = number << 16;
308 if ( ( number / power_tens[fraction_length - 5] ) > 0x7FFFL )
310 result = FT_DivFix( number, power_tens[fraction_length - 4] );
311 *scaling = exponent - 4;
315 result = FT_DivFix( number, power_tens[fraction_length - 5] );
316 *scaling = exponent - 5;
322 integer_length += exponent;
323 fraction_length -= exponent;
325 /* Check for overflow and underflow. */
326 if ( FT_ABS( integer_length ) > 5 )
329 /* Remove non-significant digits. */
330 if ( integer_length < 0 )
332 number /= power_tens[-integer_length];
333 fraction_length += integer_length;
336 /* this can only happen if exponent was non-zero */
337 if ( fraction_length == 10 )
340 fraction_length -= 1;
343 /* Convert into 16.16 format. */
344 if ( fraction_length > 0 )
346 if ( ( number / power_tens[fraction_length] ) > 0x7FFFL )
349 result = FT_DivFix( number, power_tens[fraction_length] );
353 number *= power_tens[-fraction_length];
355 if ( number > 0x7FFFL )
358 result = number << 16;
370 /* read a number, either integer or real */
372 cff_parse_num( FT_Byte** d )
374 return **d == 30 ? ( cff_parse_real( d[0], d[1], 0, NULL ) >> 16 )
375 : cff_parse_integer( d[0], d[1] );
379 /* read a floating point number, either integer or real */
381 cff_parse_fixed( FT_Byte** d )
383 return **d == 30 ? cff_parse_real( d[0], d[1], 0, NULL )
384 : cff_parse_integer( d[0], d[1] ) << 16;
388 /* read a floating point number, either integer or real, */
389 /* but return `10^scaling' times the number read in */
391 cff_parse_fixed_scaled( FT_Byte** d,
394 return **d == 30 ? cff_parse_real( d[0], d[1], scaling, NULL )
395 : ( cff_parse_integer( d[0], d[1] ) *
396 power_tens[scaling] ) << 16;
400 /* read a floating point number, either integer or real, */
401 /* and return it as precise as possible -- `scaling' returns */
402 /* the scaling factor (as a power of 10) */
404 cff_parse_fixed_dynamic( FT_Byte** d,
407 FT_ASSERT( scaling );
410 return cff_parse_real( d[0], d[1], 0, scaling );
414 FT_Int integer_length;
417 number = cff_parse_integer( d[0], d[1] );
419 if ( number > 0x7FFFL )
421 for ( integer_length = 5; integer_length < 10; integer_length++ )
422 if ( number < power_tens[integer_length] )
425 if ( ( number / power_tens[integer_length - 5] ) > 0x7FFFL )
427 *scaling = integer_length - 4;
428 return FT_DivFix( number, power_tens[integer_length - 4] );
432 *scaling = integer_length - 5;
433 return FT_DivFix( number, power_tens[integer_length - 5] );
446 cff_parse_font_matrix( CFF_Parser parser )
448 CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
449 FT_Matrix* matrix = &dict->font_matrix;
450 FT_Vector* offset = &dict->font_offset;
451 FT_ULong* upm = &dict->units_per_em;
452 FT_Byte** data = parser->stack;
453 FT_Error error = CFF_Err_Stack_Underflow;
456 if ( parser->top >= parser->stack + 6 )
463 dict->has_font_matrix = TRUE;
465 /* We expect a well-formed font matrix, this is, the matrix elements */
466 /* `xx' and `yy' are of approximately the same magnitude. To avoid */
467 /* loss of precision, we use the magnitude of element `xx' to scale */
468 /* all other elements. The scaling factor is then contained in the */
469 /* `units_per_em' value. */
471 matrix->xx = cff_parse_fixed_dynamic( data++, &scaling );
475 if ( scaling < 0 || scaling > 9 )
477 /* Return default matrix in case of unlikely values. */
479 FT_TRACE1(( "cff_parse_font_matrix:"
480 " strange scaling value for xx element (%d),\n"
482 " using default matrix\n", scaling ));
484 matrix->xx = 0x10000L;
487 matrix->yy = 0x10000L;
495 matrix->yx = cff_parse_fixed_scaled( data++, scaling );
496 matrix->xy = cff_parse_fixed_scaled( data++, scaling );
497 matrix->yy = cff_parse_fixed_scaled( data++, scaling );
498 offset->x = cff_parse_fixed_scaled( data++, scaling );
499 offset->y = cff_parse_fixed_scaled( data, scaling );
501 *upm = power_tens[scaling];
503 FT_TRACE4(( " [%f %f %f %f %f %f]\n",
504 (double)matrix->xx / *upm / 65536,
505 (double)matrix->xy / *upm / 65536,
506 (double)matrix->yx / *upm / 65536,
507 (double)matrix->yy / *upm / 65536,
508 (double)offset->x / *upm / 65536,
509 (double)offset->y / *upm / 65536 ));
518 cff_parse_font_bbox( CFF_Parser parser )
520 CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
521 FT_BBox* bbox = &dict->font_bbox;
522 FT_Byte** data = parser->stack;
526 error = CFF_Err_Stack_Underflow;
528 if ( parser->top >= parser->stack + 4 )
530 bbox->xMin = FT_RoundFix( cff_parse_fixed( data++ ) );
531 bbox->yMin = FT_RoundFix( cff_parse_fixed( data++ ) );
532 bbox->xMax = FT_RoundFix( cff_parse_fixed( data++ ) );
533 bbox->yMax = FT_RoundFix( cff_parse_fixed( data ) );
536 FT_TRACE4(( " [%d %d %d %d]\n",
540 bbox->yMax / 65536 ));
548 cff_parse_private_dict( CFF_Parser parser )
550 CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
551 FT_Byte** data = parser->stack;
555 error = CFF_Err_Stack_Underflow;
557 if ( parser->top >= parser->stack + 2 )
559 dict->private_size = cff_parse_num( data++ );
560 dict->private_offset = cff_parse_num( data );
561 FT_TRACE4(( " %lu %lu\n",
562 dict->private_size, dict->private_offset ));
572 cff_parse_cid_ros( CFF_Parser parser )
574 CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
575 FT_Byte** data = parser->stack;
579 error = CFF_Err_Stack_Underflow;
581 if ( parser->top >= parser->stack + 3 )
583 dict->cid_registry = (FT_UInt)cff_parse_num( data++ );
584 dict->cid_ordering = (FT_UInt)cff_parse_num( data++ );
586 FT_TRACE1(( "cff_parse_cid_ros: real supplement is rounded\n" ));
587 dict->cid_supplement = cff_parse_num( data );
588 if ( dict->cid_supplement < 0 )
589 FT_TRACE1(( "cff_parse_cid_ros: negative supplement %d is found\n",
590 dict->cid_supplement ));
593 FT_TRACE4(( " %d %d %d\n",
596 dict->cid_supplement ));
603 #define CFF_FIELD_NUM( code, name, id ) \
604 CFF_FIELD( code, name, id, cff_kind_num )
605 #define CFF_FIELD_FIXED( code, name, id ) \
606 CFF_FIELD( code, name, id, cff_kind_fixed )
607 #define CFF_FIELD_FIXED_1000( code, name, id ) \
608 CFF_FIELD( code, name, id, cff_kind_fixed_thousand )
609 #define CFF_FIELD_STRING( code, name, id ) \
610 CFF_FIELD( code, name, id, cff_kind_string )
611 #define CFF_FIELD_BOOL( code, name, id ) \
612 CFF_FIELD( code, name, id, cff_kind_bool )
614 #define CFFCODE_TOPDICT 0x1000
615 #define CFFCODE_PRIVATE 0x2000
618 #ifndef FT_CONFIG_OPTION_PIC
622 #undef CFF_FIELD_DELTA
625 #ifndef FT_DEBUG_LEVEL_TRACE
628 #define CFF_FIELD_CALLBACK( code, name, id ) \
633 cff_parse_ ## name, \
637 #define CFF_FIELD( code, name, id, kind ) \
641 FT_FIELD_OFFSET( name ), \
642 FT_FIELD_SIZE( name ), \
646 #define CFF_FIELD_DELTA( code, name, max, id ) \
650 FT_FIELD_OFFSET( name ), \
651 FT_FIELD_SIZE_DELTA( name ), \
654 FT_FIELD_OFFSET( num_ ## name ) \
657 static const CFF_Field_Handler cff_field_handlers[] =
660 #include "cfftoken.h"
662 { 0, 0, 0, 0, 0, 0, 0 }
666 #else /* FT_DEBUG_LEVEL_TRACE */
670 #define CFF_FIELD_CALLBACK( code, name, id ) \
675 cff_parse_ ## name, \
680 #define CFF_FIELD( code, name, id, kind ) \
684 FT_FIELD_OFFSET( name ), \
685 FT_FIELD_SIZE( name ), \
690 #define CFF_FIELD_DELTA( code, name, max, id ) \
694 FT_FIELD_OFFSET( name ), \
695 FT_FIELD_SIZE_DELTA( name ), \
698 FT_FIELD_OFFSET( num_ ## name ), \
702 static const CFF_Field_Handler cff_field_handlers[] =
705 #include "cfftoken.h"
707 { 0, 0, 0, 0, 0, 0, 0, 0 }
711 #endif /* FT_DEBUG_LEVEL_TRACE */
714 #else /* FT_CONFIG_OPTION_PIC */
718 FT_Destroy_Class_cff_field_handlers( FT_Library library,
719 CFF_Field_Handler* clazz )
721 FT_Memory memory = library->memory;
730 FT_Create_Class_cff_field_handlers( FT_Library library,
731 CFF_Field_Handler** output_class )
733 CFF_Field_Handler* clazz;
735 FT_Memory memory = library->memory;
741 #define CFF_FIELD( code, name, id, kind ) i++;
742 #undef CFF_FIELD_DELTA
743 #define CFF_FIELD_DELTA( code, name, max, id ) i++;
744 #undef CFF_FIELD_CALLBACK
745 #define CFF_FIELD_CALLBACK( code, name, id ) i++;
747 #include "cfftoken.h"
749 i++; /* { 0, 0, 0, 0, 0, 0, 0 } */
751 if ( FT_ALLOC( clazz, sizeof ( CFF_Field_Handler ) * i ) )
757 #ifndef FT_DEBUG_LEVEL_TRACE
760 #undef CFF_FIELD_CALLBACK
761 #define CFF_FIELD_CALLBACK( code_, name_, id_ ) \
762 clazz[i].kind = cff_kind_callback; \
763 clazz[i].code = code_ | CFFCODE; \
764 clazz[i].offset = 0; \
766 clazz[i].reader = cff_parse_ ## name_; \
767 clazz[i].array_max = 0; \
768 clazz[i].count_offset = 0; \
772 #define CFF_FIELD( code_, name_, id_, kind_ ) \
773 clazz[i].kind = kind_; \
774 clazz[i].code = code_ | CFFCODE; \
775 clazz[i].offset = FT_FIELD_OFFSET( name_ ); \
776 clazz[i].size = FT_FIELD_SIZE( name_ ); \
777 clazz[i].reader = 0; \
778 clazz[i].array_max = 0; \
779 clazz[i].count_offset = 0; \
782 #undef CFF_FIELD_DELTA
783 #define CFF_FIELD_DELTA( code_, name_, max_, id_ ) \
784 clazz[i].kind = cff_kind_delta; \
785 clazz[i].code = code_ | CFFCODE; \
786 clazz[i].offset = FT_FIELD_OFFSET( name_ ); \
787 clazz[i].size = FT_FIELD_SIZE_DELTA( name_ ); \
788 clazz[i].reader = 0; \
789 clazz[i].array_max = max_; \
790 clazz[i].count_offset = FT_FIELD_OFFSET( num_ ## name_ ); \
793 #include "cfftoken.h"
800 clazz[i].array_max = 0;
801 clazz[i].count_offset = 0;
804 #else /* FT_DEBUG_LEVEL_TRACE */
807 #undef CFF_FIELD_CALLBACK
808 #define CFF_FIELD_CALLBACK( code_, name_, id_ ) \
809 clazz[i].kind = cff_kind_callback; \
810 clazz[i].code = code_ | CFFCODE; \
811 clazz[i].offset = 0; \
813 clazz[i].reader = cff_parse_ ## name_; \
814 clazz[i].array_max = 0; \
815 clazz[i].count_offset = 0; \
820 #define CFF_FIELD( code_, name_, id_, kind_ ) \
821 clazz[i].kind = kind_; \
822 clazz[i].code = code_ | CFFCODE; \
823 clazz[i].offset = FT_FIELD_OFFSET( name_ ); \
824 clazz[i].size = FT_FIELD_SIZE( name_ ); \
825 clazz[i].reader = 0; \
826 clazz[i].array_max = 0; \
827 clazz[i].count_offset = 0; \
831 #undef CFF_FIELD_DELTA
832 #define CFF_FIELD_DELTA( code_, name_, max_, id_ ) \
833 clazz[i].kind = cff_kind_delta; \
834 clazz[i].code = code_ | CFFCODE; \
835 clazz[i].offset = FT_FIELD_OFFSET( name_ ); \
836 clazz[i].size = FT_FIELD_SIZE_DELTA( name_ ); \
837 clazz[i].reader = 0; \
838 clazz[i].array_max = max_; \
839 clazz[i].count_offset = FT_FIELD_OFFSET( num_ ## name_ ); \
843 #include "cfftoken.h"
850 clazz[i].array_max = 0;
851 clazz[i].count_offset = 0;
855 #endif /* FT_DEBUG_LEVEL_TRACE */
858 *output_class = clazz;
864 #endif /* FT_CONFIG_OPTION_PIC */
867 FT_LOCAL_DEF( FT_Error )
868 cff_parser_run( CFF_Parser parser,
873 FT_Error error = CFF_Err_Ok;
874 FT_Library library = parser->library;
875 FT_UNUSED( library );
878 parser->top = parser->stack;
879 parser->start = start;
880 parser->limit = limit;
881 parser->cursor = start;
888 if ( v >= 27 && v != 31 )
890 /* it's a number; we will push its position on the stack */
891 if ( parser->top - parser->stack >= CFF_MAX_STACK_DEPTH )
899 /* skip real number */
903 /* An unterminated floating point number at the */
904 /* end of a dictionary is invalid but harmless. */
925 /* This is not a number, hence it's an operator. Compute its code */
926 /* and look for it in our current list. */
929 FT_UInt num_args = (FT_UInt)
930 ( parser->top - parser->stack );
931 const CFF_Field_Handler* field;
938 /* two byte operator */
945 code = code | parser->object_code;
947 for ( field = FT_CFF_FIELD_HANDLERS_GET; field->kind; field++ )
949 if ( field->code == (FT_Int)code )
951 /* we found our field's handler; read it */
953 FT_Byte* q = (FT_Byte*)parser->object + field->offset;
956 #ifdef FT_DEBUG_LEVEL_TRACE
957 FT_TRACE4(( " %s", field->id ));
960 /* check that we have enough arguments -- except for */
961 /* delta encoded arrays, which can be empty */
962 if ( field->kind != cff_kind_delta && num_args < 1 )
963 goto Stack_Underflow;
965 switch ( field->kind )
968 case cff_kind_string:
970 val = cff_parse_num( parser->stack );
974 val = cff_parse_fixed( parser->stack );
977 case cff_kind_fixed_thousand:
978 val = cff_parse_fixed_scaled( parser->stack, 3 );
981 switch ( field->size )
983 case (8 / FT_CHAR_BIT):
984 *(FT_Byte*)q = (FT_Byte)val;
987 case (16 / FT_CHAR_BIT):
988 *(FT_Short*)q = (FT_Short)val;
991 case (32 / FT_CHAR_BIT):
992 *(FT_Int32*)q = (FT_Int)val;
995 default: /* for 64-bit systems */
999 #ifdef FT_DEBUG_LEVEL_TRACE
1000 switch ( field->kind )
1003 FT_TRACE4(( " %s\n", val ? "true" : "false" ));
1006 case cff_kind_string:
1007 FT_TRACE4(( " %ld (SID)\n", val ));
1011 FT_TRACE4(( " %ld\n", val ));
1014 case cff_kind_fixed:
1015 FT_TRACE4(( " %f\n", (double)val / 65536 ));
1018 case cff_kind_fixed_thousand:
1019 FT_TRACE4(( " %f\n", (double)val / 65536 / 1000 ));
1022 ; /* never reached */
1028 case cff_kind_delta:
1030 FT_Byte* qcount = (FT_Byte*)parser->object +
1031 field->count_offset;
1033 FT_Byte** data = parser->stack;
1036 if ( num_args > field->array_max )
1037 num_args = field->array_max;
1039 FT_TRACE4(( " [" ));
1042 *qcount = (FT_Byte)num_args;
1045 while ( num_args > 0 )
1047 val += cff_parse_num( data++ );
1048 switch ( field->size )
1050 case (8 / FT_CHAR_BIT):
1051 *(FT_Byte*)q = (FT_Byte)val;
1054 case (16 / FT_CHAR_BIT):
1055 *(FT_Short*)q = (FT_Short)val;
1058 case (32 / FT_CHAR_BIT):
1059 *(FT_Int32*)q = (FT_Int)val;
1062 default: /* for 64-bit systems */
1066 FT_TRACE4(( " %ld", val ));
1072 FT_TRACE4(( "]\n" ));
1076 default: /* callback */
1077 error = field->reader( parser );
1085 /* this is an unknown operator, or it is unsupported; */
1086 /* we will ignore it for now. */
1090 parser->top = parser->stack;
1099 error = CFF_Err_Invalid_Argument;
1103 error = CFF_Err_Invalid_Argument;
1107 error = CFF_Err_Invalid_Argument;