1 /****************************************************************************
5 * CFF token stream parser (body)
7 * Copyright (C) 1996-2020 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.
20 #include <freetype/internal/ftstream.h>
21 #include <freetype/internal/ftdebug.h>
22 #include <freetype/internal/ftcalc.h>
23 #include <freetype/internal/psaux.h>
24 #include <freetype/ftlist.h>
30 /**************************************************************************
32 * The macro FT_COMPONENT is used in trace mode. It is an implicit
33 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
34 * messages during execution.
37 #define FT_COMPONENT cffparse
40 FT_LOCAL_DEF( FT_Error )
41 cff_parser_init( CFF_Parser parser,
46 FT_UShort num_designs,
49 FT_Memory memory = library->memory; /* for FT_NEW_ARRAY */
50 FT_Error error; /* for FT_NEW_ARRAY */
56 parser->top = parser->stack;
58 parser->object_code = code;
59 parser->object = object;
60 parser->library = library;
61 parser->num_designs = num_designs;
62 parser->num_axes = num_axes;
64 /* allocate the stack buffer */
65 if ( FT_NEW_ARRAY( parser->stack, stackSize ) )
67 FT_FREE( parser->stack );
71 parser->stackSize = stackSize;
72 parser->top = parser->stack; /* empty stack */
79 #ifdef CFF_CONFIG_OPTION_OLD_ENGINE
81 finalize_t2_strings( FT_Memory memory,
85 CFF_T2_String t2 = (CFF_T2_String)data;
90 memory->free( memory, t2->start );
91 memory->free( memory, data );
93 #endif /* CFF_CONFIG_OPTION_OLD_ENGINE */
97 cff_parser_done( CFF_Parser parser )
99 FT_Memory memory = parser->library->memory; /* for FT_FREE */
102 FT_FREE( parser->stack );
104 #ifdef CFF_CONFIG_OPTION_OLD_ENGINE
105 FT_List_Finalize( &parser->t2_strings,
113 /* Assuming `first >= last'. */
116 cff_parser_within_limits( CFF_Parser parser,
120 #ifndef CFF_CONFIG_OPTION_OLD_ENGINE
122 /* Fast path for regular FreeType builds with the "new" engine; */
123 /* `first >= parser->start' can be assumed. */
127 return last < parser->limit ? FT_Err_Ok : FT_THROW( Invalid_Argument );
129 #else /* CFF_CONFIG_OPTION_OLD_ENGINE */
134 if ( first >= parser->start &&
135 last < parser->limit )
138 node = parser->t2_strings.head;
142 CFF_T2_String t2 = (CFF_T2_String)node->data;
145 if ( first >= t2->start &&
152 return FT_THROW( Invalid_Argument );
154 #endif /* CFF_CONFIG_OPTION_OLD_ENGINE */
158 /* read an integer */
160 cff_parse_integer( CFF_Parser parser,
170 if ( cff_parser_within_limits( parser, p, p + 1 ) )
173 val = (FT_Short)( ( (FT_UShort)p[0] << 8 ) | p[1] );
177 if ( cff_parser_within_limits( parser, p, p + 3 ) )
180 val = (FT_Long)( ( (FT_ULong)p[0] << 24 ) |
181 ( (FT_ULong)p[1] << 16 ) |
182 ( (FT_ULong)p[2] << 8 ) |
191 if ( cff_parser_within_limits( parser, p, p ) )
194 val = ( v - 247 ) * 256 + p[0] + 108;
198 if ( cff_parser_within_limits( parser, p, p ) )
201 val = -( v - 251 ) * 256 - p[0] - 108;
209 FT_TRACE4(( "!!!END OF DATA:!!!" ));
214 static const FT_Long power_tens[] =
228 /* maximum values allowed for multiplying */
229 /* with the corresponding `power_tens' element */
230 static const FT_Long power_ten_limits[] =
236 FT_LONG_MAX / 10000L,
237 FT_LONG_MAX / 100000L,
238 FT_LONG_MAX / 1000000L,
239 FT_LONG_MAX / 10000000L,
240 FT_LONG_MAX / 100000000L,
241 FT_LONG_MAX / 1000000000L,
247 cff_parse_real( CFF_Parser parser,
256 FT_Long result, number, exponent;
257 FT_Int sign = 0, exponent_sign = 0, have_overflow = 0;
258 FT_Long exponent_add, integer_length, fraction_length;
273 /* First of all, read the integer part. */
278 /* If we entered this iteration with phase == 4, we need to */
279 /* read a new byte. This also skips past the initial 0x1E. */
284 /* Make sure we don't read past the end. */
285 if ( cff_parser_within_limits( parser, p, p ) )
289 /* Get the nibble. */
290 nib = (FT_Int)( p[0] >> phase ) & 0xF;
299 /* Increase exponent if we can't add the digit. */
300 if ( number >= 0xCCCCCCCL )
302 /* Skip leading zeros. */
303 else if ( nib || number )
306 number = number * 10 + nib;
311 /* Read fraction part, if any. */
315 /* If we entered this iteration with phase == 4, we need */
316 /* to read a new byte. */
321 /* Make sure we don't read past the end. */
322 if ( cff_parser_within_limits( parser, p, p ) )
326 /* Get the nibble. */
327 nib = ( p[0] >> phase ) & 0xF;
332 /* Skip leading zeros if possible. */
333 if ( !nib && !number )
335 /* Only add digit if we don't overflow. */
336 else if ( number < 0xCCCCCCCL && fraction_length < 9 )
339 number = number * 10 + nib;
343 /* Read exponent, if any. */
354 /* If we entered this iteration with phase == 4, */
355 /* we need to read a new byte. */
360 /* Make sure we don't read past the end. */
361 if ( cff_parser_within_limits( parser, p, p ) )
365 /* Get the nibble. */
366 nib = ( p[0] >> phase ) & 0xF;
371 /* Arbitrarily limit exponent. */
372 if ( exponent > 1000 )
375 exponent = exponent * 10 + nib;
379 exponent = -exponent;
393 /* We don't check `power_ten' and `exponent_add'. */
394 exponent += power_ten + exponent_add;
398 /* Only use `fraction_length'. */
399 fraction_length += integer_length;
400 exponent += integer_length;
402 if ( fraction_length <= 5 )
404 if ( number > 0x7FFFL )
406 result = FT_DivFix( number, 10 );
407 *scaling = exponent - fraction_length + 1;
413 FT_Long new_fraction_length, shift;
416 /* Make `scaling' as small as possible. */
417 new_fraction_length = FT_MIN( exponent, 5 );
418 shift = new_fraction_length - fraction_length;
422 exponent -= new_fraction_length;
423 number *= power_tens[shift];
424 if ( number > 0x7FFFL )
431 exponent -= fraction_length;
434 exponent -= fraction_length;
436 result = (FT_Long)( (FT_ULong)number << 16 );
442 if ( ( number / power_tens[fraction_length - 5] ) > 0x7FFFL )
444 result = FT_DivFix( number, power_tens[fraction_length - 4] );
445 *scaling = exponent - 4;
449 result = FT_DivFix( number, power_tens[fraction_length - 5] );
450 *scaling = exponent - 5;
456 integer_length += exponent;
457 fraction_length -= exponent;
459 if ( integer_length > 5 )
461 if ( integer_length < -5 )
464 /* Remove non-significant digits. */
465 if ( integer_length < 0 )
467 number /= power_tens[-integer_length];
468 fraction_length += integer_length;
471 /* this can only happen if exponent was non-zero */
472 if ( fraction_length == 10 )
475 fraction_length -= 1;
478 /* Convert into 16.16 format. */
479 if ( fraction_length > 0 )
481 if ( ( number / power_tens[fraction_length] ) > 0x7FFFL )
484 result = FT_DivFix( number, power_tens[fraction_length] );
488 number *= power_tens[-fraction_length];
490 if ( number > 0x7FFFL )
493 result = (FT_Long)( (FT_ULong)number << 16 );
504 result = 0x7FFFFFFFL;
505 FT_TRACE4(( "!!!OVERFLOW:!!!" ));
510 FT_TRACE4(( "!!!UNDERFLOW:!!!" ));
515 FT_TRACE4(( "!!!END OF DATA:!!!" ));
520 /* read a number, either integer or real */
521 FT_LOCAL_DEF( FT_Long )
522 cff_parse_num( CFF_Parser parser,
527 /* binary-coded decimal is truncated to integer */
528 return cff_parse_real( parser, *d, 0, NULL ) >> 16;
531 else if ( **d == 255 )
533 /* 16.16 fixed point is used internally for CFF2 blend results. */
534 /* Since these are trusted values, a limit check is not needed. */
536 /* After the 255, 4 bytes give the number. */
537 /* The blend value is converted to integer, with rounding; */
538 /* due to the right-shift we don't need the lowest byte. */
541 ( ( ( (FT_UInt32)*( d[0] + 1 ) << 24 ) |
542 ( (FT_UInt32)*( d[0] + 2 ) << 16 ) |
543 ( (FT_UInt32)*( d[0] + 3 ) << 8 ) |
544 (FT_UInt32)*( d[0] + 4 ) ) + 0x8000U ) >> 16 );
547 ( ( ( (FT_UInt32)*( d[0] + 1 ) << 16 ) |
548 ( (FT_UInt32)*( d[0] + 2 ) << 8 ) |
549 (FT_UInt32)*( d[0] + 3 ) ) + 0x80U ) >> 8 );
554 return cff_parse_integer( parser, *d );
558 /* read a floating point number, either integer or real */
560 do_fixed( CFF_Parser parser,
565 return cff_parse_real( parser, *d, scaling, NULL );
568 FT_Long val = cff_parse_integer( parser, *d );
573 if ( FT_ABS( val ) > power_ten_limits[scaling] )
575 val = val > 0 ? 0x7FFFFFFFL : -0x7FFFFFFFL;
579 val *= power_tens[scaling];
587 else if ( val < -0x7FFF )
593 return (FT_Long)( (FT_ULong)val << 16 );
596 FT_TRACE4(( "!!!OVERFLOW:!!!" ));
602 /* read a floating point number, either integer or real */
604 cff_parse_fixed( CFF_Parser parser,
607 return do_fixed( parser, d, 0 );
611 /* read a floating point number, either integer or real, */
612 /* but return `10^scaling' times the number read in */
614 cff_parse_fixed_scaled( CFF_Parser parser,
618 return do_fixed( parser, d, scaling );
622 /* read a floating point number, either integer or real, */
623 /* and return it as precise as possible -- `scaling' returns */
624 /* the scaling factor (as a power of 10) */
626 cff_parse_fixed_dynamic( CFF_Parser parser,
630 FT_ASSERT( scaling );
633 return cff_parse_real( parser, *d, 0, scaling );
637 FT_Int integer_length;
640 number = cff_parse_integer( parser, d[0] );
642 if ( number > 0x7FFFL )
644 for ( integer_length = 5; integer_length < 10; integer_length++ )
645 if ( number < power_tens[integer_length] )
648 if ( ( number / power_tens[integer_length - 5] ) > 0x7FFFL )
650 *scaling = integer_length - 4;
651 return FT_DivFix( number, power_tens[integer_length - 4] );
655 *scaling = integer_length - 5;
656 return FT_DivFix( number, power_tens[integer_length - 5] );
662 return (FT_Long)( (FT_ULong)number << 16 );
669 cff_parse_font_matrix( CFF_Parser parser )
671 CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
672 FT_Matrix* matrix = &dict->font_matrix;
673 FT_Vector* offset = &dict->font_offset;
674 FT_ULong* upm = &dict->units_per_em;
675 FT_Byte** data = parser->stack;
678 if ( parser->top >= parser->stack + 6 )
683 FT_Long min_scaling, max_scaling;
687 dict->has_font_matrix = TRUE;
689 /* We expect a well-formed font matrix, this is, the matrix elements */
690 /* `xx' and `yy' are of approximately the same magnitude. To avoid */
691 /* loss of precision, we use the magnitude of the largest matrix */
692 /* element to scale all other elements. The scaling factor is then */
693 /* contained in the `units_per_em' value. */
695 max_scaling = FT_LONG_MIN;
696 min_scaling = FT_LONG_MAX;
698 for ( i = 0; i < 6; i++ )
700 values[i] = cff_parse_fixed_dynamic( parser, data++, &scalings[i] );
703 if ( scalings[i] > max_scaling )
704 max_scaling = scalings[i];
705 if ( scalings[i] < min_scaling )
706 min_scaling = scalings[i];
710 if ( max_scaling < -9 ||
712 ( max_scaling - min_scaling ) < 0 ||
713 ( max_scaling - min_scaling ) > 9 )
715 FT_TRACE1(( "cff_parse_font_matrix:"
716 " strange scaling values (minimum %ld, maximum %ld),\n"
718 " using default matrix\n", min_scaling, max_scaling ));
722 for ( i = 0; i < 6; i++ )
724 FT_Fixed value = values[i];
725 FT_Long divisor, half_divisor;
731 divisor = power_tens[max_scaling - scalings[i]];
732 half_divisor = divisor >> 1;
736 if ( FT_LONG_MIN + half_divisor < value )
737 values[i] = ( value - half_divisor ) / divisor;
739 values[i] = FT_LONG_MIN / divisor;
743 if ( FT_LONG_MAX - half_divisor > value )
744 values[i] = ( value + half_divisor ) / divisor;
746 values[i] = FT_LONG_MAX / divisor;
750 matrix->xx = values[0];
751 matrix->yx = values[1];
752 matrix->xy = values[2];
753 matrix->yy = values[3];
754 offset->x = values[4];
755 offset->y = values[5];
757 *upm = (FT_ULong)power_tens[-max_scaling];
759 FT_TRACE4(( " [%f %f %f %f %f %f]\n",
760 (double)matrix->xx / *upm / 65536,
761 (double)matrix->xy / *upm / 65536,
762 (double)matrix->yx / *upm / 65536,
763 (double)matrix->yy / *upm / 65536,
764 (double)offset->x / *upm / 65536,
765 (double)offset->y / *upm / 65536 ));
767 if ( !FT_Matrix_Check( matrix ) )
769 FT_TRACE1(( "cff_parse_font_matrix:"
770 " degenerate values, using default matrix\n" ));
777 return FT_THROW( Stack_Underflow );
780 /* Return default matrix in case of unlikely values. */
782 matrix->xx = 0x10000L;
785 matrix->yy = 0x10000L;
795 cff_parse_font_bbox( CFF_Parser parser )
797 CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
798 FT_BBox* bbox = &dict->font_bbox;
799 FT_Byte** data = parser->stack;
803 error = FT_ERR( Stack_Underflow );
805 if ( parser->top >= parser->stack + 4 )
807 bbox->xMin = FT_RoundFix( cff_parse_fixed( parser, data++ ) );
808 bbox->yMin = FT_RoundFix( cff_parse_fixed( parser, data++ ) );
809 bbox->xMax = FT_RoundFix( cff_parse_fixed( parser, data++ ) );
810 bbox->yMax = FT_RoundFix( cff_parse_fixed( parser, data ) );
813 FT_TRACE4(( " [%ld %ld %ld %ld]\n",
817 bbox->yMax / 65536 ));
825 cff_parse_private_dict( CFF_Parser parser )
827 CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
828 FT_Byte** data = parser->stack;
832 error = FT_ERR( Stack_Underflow );
834 if ( parser->top >= parser->stack + 2 )
839 tmp = cff_parse_num( parser, data++ );
842 FT_ERROR(( "cff_parse_private_dict: Invalid dictionary size\n" ));
843 error = FT_THROW( Invalid_File_Format );
846 dict->private_size = (FT_ULong)tmp;
848 tmp = cff_parse_num( parser, data );
851 FT_ERROR(( "cff_parse_private_dict: Invalid dictionary offset\n" ));
852 error = FT_THROW( Invalid_File_Format );
855 dict->private_offset = (FT_ULong)tmp;
857 FT_TRACE4(( " %lu %lu\n",
858 dict->private_size, dict->private_offset ));
868 /* The `MultipleMaster' operator comes before any */
869 /* top DICT operators that contain T2 charstrings. */
872 cff_parse_multiple_master( CFF_Parser parser )
874 CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
878 #ifdef FT_DEBUG_LEVEL_TRACE
879 /* beautify tracing message */
880 if ( ft_trace_levels[FT_TRACE_COMP( FT_COMPONENT )] < 4 )
881 FT_TRACE1(( "Multiple Master CFFs not supported yet,"
882 " handling first master design only\n" ));
884 FT_TRACE1(( " (not supported yet,"
885 " handling first master design only)\n" ));
888 error = FT_ERR( Stack_Underflow );
890 /* currently, we handle only the first argument */
891 if ( parser->top >= parser->stack + 5 )
893 FT_Long num_designs = cff_parse_num( parser, parser->stack );
896 if ( num_designs > 16 || num_designs < 2 )
898 FT_ERROR(( "cff_parse_multiple_master:"
899 " Invalid number of designs\n" ));
900 error = FT_THROW( Invalid_File_Format );
904 dict->num_designs = (FT_UShort)num_designs;
905 dict->num_axes = (FT_UShort)( parser->top - parser->stack - 4 );
907 parser->num_designs = dict->num_designs;
908 parser->num_axes = dict->num_axes;
919 cff_parse_cid_ros( CFF_Parser parser )
921 CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
922 FT_Byte** data = parser->stack;
926 error = FT_ERR( Stack_Underflow );
928 if ( parser->top >= parser->stack + 3 )
930 dict->cid_registry = (FT_UInt)cff_parse_num( parser, data++ );
931 dict->cid_ordering = (FT_UInt)cff_parse_num( parser, data++ );
933 FT_TRACE1(( "cff_parse_cid_ros: real supplement is rounded\n" ));
934 dict->cid_supplement = cff_parse_num( parser, data );
935 if ( dict->cid_supplement < 0 )
936 FT_TRACE1(( "cff_parse_cid_ros: negative supplement %ld is found\n",
937 dict->cid_supplement ));
940 FT_TRACE4(( " %d %d %ld\n",
943 dict->cid_supplement ));
951 cff_parse_vsindex( CFF_Parser parser )
953 /* vsindex operator can only be used in a Private DICT */
954 CFF_Private priv = (CFF_Private)parser->object;
955 FT_Byte** data = parser->stack;
960 if ( !priv || !priv->subfont )
962 error = FT_THROW( Invalid_File_Format );
966 blend = &priv->subfont->blend;
970 FT_ERROR(( " cff_parse_vsindex: vsindex not allowed after blend\n" ));
971 error = FT_THROW( Syntax_Error );
975 priv->vsindex = (FT_UInt)cff_parse_num( parser, data++ );
977 FT_TRACE4(( " %d\n", priv->vsindex ));
987 cff_parse_blend( CFF_Parser parser )
989 /* blend operator can only be used in a Private DICT */
990 CFF_Private priv = (CFF_Private)parser->object;
997 if ( !priv || !priv->subfont )
999 error = FT_THROW( Invalid_File_Format );
1003 subFont = priv->subfont;
1004 blend = &subFont->blend;
1006 if ( cff_blend_check_vector( blend,
1011 error = cff_blend_build_vector( blend,
1019 numBlends = (FT_UInt)cff_parse_num( parser, parser->top - 1 );
1020 if ( numBlends > parser->stackSize )
1022 FT_ERROR(( "cff_parse_blend: Invalid number of blends\n" ));
1023 error = FT_THROW( Invalid_File_Format );
1027 FT_TRACE4(( " %d value%s blended\n",
1029 numBlends == 1 ? "" : "s" ));
1031 error = cff_blend_doBlend( subFont, parser, numBlends );
1033 blend->usedBV = TRUE;
1040 /* maxstack operator increases parser and operand stacks for CFF2 */
1042 cff_parse_maxstack( CFF_Parser parser )
1044 /* maxstack operator can only be used in a Top DICT */
1045 CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
1046 FT_Byte** data = parser->stack;
1047 FT_Error error = FT_Err_Ok;
1052 error = FT_THROW( Invalid_File_Format );
1056 dict->maxstack = (FT_UInt)cff_parse_num( parser, data++ );
1057 if ( dict->maxstack > CFF2_MAX_STACK )
1058 dict->maxstack = CFF2_MAX_STACK;
1059 if ( dict->maxstack < CFF2_DEFAULT_STACK )
1060 dict->maxstack = CFF2_DEFAULT_STACK;
1062 FT_TRACE4(( " %d\n", dict->maxstack ));
1069 #define CFF_FIELD_NUM( code, name, id ) \
1070 CFF_FIELD( code, name, id, cff_kind_num )
1071 #define CFF_FIELD_FIXED( code, name, id ) \
1072 CFF_FIELD( code, name, id, cff_kind_fixed )
1073 #define CFF_FIELD_FIXED_1000( code, name, id ) \
1074 CFF_FIELD( code, name, id, cff_kind_fixed_thousand )
1075 #define CFF_FIELD_STRING( code, name, id ) \
1076 CFF_FIELD( code, name, id, cff_kind_string )
1077 #define CFF_FIELD_BOOL( code, name, id ) \
1078 CFF_FIELD( code, name, id, cff_kind_bool )
1082 #undef CFF_FIELD_DELTA
1085 #ifndef FT_DEBUG_LEVEL_TRACE
1088 #define CFF_FIELD_CALLBACK( code, name, id ) \
1090 cff_kind_callback, \
1093 cff_parse_ ## name, \
1097 #define CFF_FIELD_BLEND( code, id ) \
1106 #define CFF_FIELD( code, name, id, kind ) \
1110 FT_FIELD_OFFSET( name ), \
1111 FT_FIELD_SIZE( name ), \
1115 #define CFF_FIELD_DELTA( code, name, max, id ) \
1119 FT_FIELD_OFFSET( name ), \
1120 FT_FIELD_SIZE_DELTA( name ), \
1123 FT_FIELD_OFFSET( num_ ## name ) \
1126 static const CFF_Field_Handler cff_field_handlers[] =
1129 #include "cfftoken.h"
1131 { 0, 0, 0, 0, 0, 0, 0 }
1135 #else /* FT_DEBUG_LEVEL_TRACE */
1139 #define CFF_FIELD_CALLBACK( code, name, id ) \
1141 cff_kind_callback, \
1144 cff_parse_ ## name, \
1149 #define CFF_FIELD_BLEND( code, id ) \
1159 #define CFF_FIELD( code, name, id, kind ) \
1163 FT_FIELD_OFFSET( name ), \
1164 FT_FIELD_SIZE( name ), \
1169 #define CFF_FIELD_DELTA( code, name, max, id ) \
1173 FT_FIELD_OFFSET( name ), \
1174 FT_FIELD_SIZE_DELTA( name ), \
1177 FT_FIELD_OFFSET( num_ ## name ), \
1181 static const CFF_Field_Handler cff_field_handlers[] =
1184 #include "cfftoken.h"
1186 { 0, 0, 0, 0, 0, 0, 0, 0 }
1190 #endif /* FT_DEBUG_LEVEL_TRACE */
1193 FT_LOCAL_DEF( FT_Error )
1194 cff_parser_run( CFF_Parser parser,
1199 FT_Error error = FT_Err_Ok;
1201 #ifdef CFF_CONFIG_OPTION_OLD_ENGINE
1202 PSAux_Service psaux;
1204 FT_Library library = parser->library;
1205 FT_Memory memory = library->memory;
1208 parser->top = parser->stack;
1209 parser->start = start;
1210 parser->limit = limit;
1211 parser->cursor = start;
1218 /* Opcode 31 is legacy MM T2 operator, not a number. */
1219 /* Opcode 255 is reserved and should not appear in fonts; */
1220 /* it is used internally for CFF2 blends. */
1221 if ( v >= 27 && v != 31 && v != 255 )
1223 /* it's a number; we will push its position on the stack */
1224 if ( (FT_UInt)( parser->top - parser->stack ) >= parser->stackSize )
1225 goto Stack_Overflow;
1232 /* skip real number */
1236 /* An unterminated floating point number at the */
1237 /* end of a dictionary is invalid but harmless. */
1256 #ifdef CFF_CONFIG_OPTION_OLD_ENGINE
1259 /* a Type 2 charstring */
1261 CFF_Decoder decoder;
1262 CFF_FontRec cff_rec;
1263 FT_Byte* charstring_base;
1264 FT_ULong charstring_len;
1273 charstring_base = ++p;
1275 /* search `endchar' operator */
1285 charstring_len = (FT_ULong)( p - charstring_base ) + 1;
1287 /* construct CFF_Decoder object */
1288 FT_ZERO( &decoder );
1289 FT_ZERO( &cff_rec );
1291 cff_rec.top_font.font_dict.num_designs = parser->num_designs;
1292 cff_rec.top_font.font_dict.num_axes = parser->num_axes;
1293 decoder.cff = &cff_rec;
1295 psaux = (PSAux_Service)FT_Get_Module_Interface( library, "psaux" );
1298 FT_ERROR(( "cff_parser_run: cannot access `psaux' module\n" ));
1299 error = FT_THROW( Missing_Module );
1303 error = psaux->cff_decoder_funcs->parse_charstrings_old(
1304 &decoder, charstring_base, charstring_len, 1 );
1308 /* Now copy the stack data in the temporary decoder object, */
1309 /* converting it back to charstring number representations */
1310 /* (this is ugly, I know). */
1312 node = (FT_ListNode)memory->alloc( memory,
1313 sizeof ( FT_ListNodeRec ) );
1315 goto Out_Of_Memory_Error;
1317 FT_List_Add( &parser->t2_strings, node );
1319 t2 = (CFF_T2_String)memory->alloc( memory,
1320 sizeof ( CFF_T2_StringRec ) );
1322 goto Out_Of_Memory_Error;
1326 /* `5' is the conservative upper bound of required bytes per stack */
1329 t2_size = 5 * ( decoder.top - decoder.stack );
1331 q = (FT_Byte*)memory->alloc( memory, t2_size );
1333 goto Out_Of_Memory_Error;
1336 t2->limit = q + t2_size;
1338 stack = decoder.stack;
1340 while ( stack < decoder.top )
1346 if ( (FT_UInt)( parser->top - parser->stack ) >= parser->stackSize )
1347 goto Stack_Overflow;
1353 num = (FT_ULong)NEG_LONG( *stack );
1358 num = (FT_ULong)*stack;
1362 if ( num & 0xFFFFU )
1365 num = (FT_ULong)-num;
1368 *q++ = ( num & 0xFF000000U ) >> 24;
1369 *q++ = ( num & 0x00FF0000U ) >> 16;
1370 *q++ = ( num & 0x0000FF00U ) >> 8;
1371 *q++ = num & 0x000000FFU;
1380 *q++ = (FT_Byte)( 139 - num );
1381 else if ( num <= 1131 )
1383 *q++ = (FT_Byte)( ( ( num - 108 ) >> 8 ) + 251 );
1384 *q++ = (FT_Byte)( ( num - 108 ) & 0xFF );
1388 num = (FT_ULong)-num;
1391 *q++ = (FT_Byte)( num >> 8 );
1392 *q++ = (FT_Byte)( num & 0xFF );
1398 *q++ = (FT_Byte)( num + 139 );
1399 else if ( num <= 1131 )
1401 *q++ = (FT_Byte)( ( ( num - 108 ) >> 8 ) + 247 );
1402 *q++ = (FT_Byte)( ( num - 108 ) & 0xFF );
1407 *q++ = (FT_Byte)( num >> 8 );
1408 *q++ = (FT_Byte)( num & 0xFF );
1416 #endif /* CFF_CONFIG_OPTION_OLD_ENGINE */
1419 /* This is not a number, hence it's an operator. Compute its code */
1420 /* and look for it in our current list. */
1424 const CFF_Field_Handler* field;
1427 if ( (FT_UInt)( parser->top - parser->stack ) >= parser->stackSize )
1428 goto Stack_Overflow;
1430 num_args = (FT_UInt)( parser->top - parser->stack );
1436 /* two byte operator */
1441 code = 0x100 | p[0];
1443 code = code | parser->object_code;
1445 for ( field = cff_field_handlers; field->kind; field++ )
1447 if ( field->code == (FT_Int)code )
1449 /* we found our field's handler; read it */
1451 FT_Byte* q = (FT_Byte*)parser->object + field->offset;
1454 #ifdef FT_DEBUG_LEVEL_TRACE
1455 FT_TRACE4(( " %s", field->id ));
1458 /* check that we have enough arguments -- except for */
1459 /* delta encoded arrays, which can be empty */
1460 if ( field->kind != cff_kind_delta && num_args < 1 )
1461 goto Stack_Underflow;
1463 switch ( field->kind )
1466 case cff_kind_string:
1468 val = cff_parse_num( parser, parser->stack );
1471 case cff_kind_fixed:
1472 val = cff_parse_fixed( parser, parser->stack );
1475 case cff_kind_fixed_thousand:
1476 val = cff_parse_fixed_scaled( parser, parser->stack, 3 );
1479 switch ( field->size )
1481 case (8 / FT_CHAR_BIT):
1482 *(FT_Byte*)q = (FT_Byte)val;
1485 case (16 / FT_CHAR_BIT):
1486 *(FT_Short*)q = (FT_Short)val;
1489 case (32 / FT_CHAR_BIT):
1490 *(FT_Int32*)q = (FT_Int)val;
1493 default: /* for 64-bit systems */
1497 #ifdef FT_DEBUG_LEVEL_TRACE
1498 switch ( field->kind )
1501 FT_TRACE4(( " %s\n", val ? "true" : "false" ));
1504 case cff_kind_string:
1505 FT_TRACE4(( " %ld (SID)\n", val ));
1509 FT_TRACE4(( " %ld\n", val ));
1512 case cff_kind_fixed:
1513 FT_TRACE4(( " %f\n", (double)val / 65536 ));
1516 case cff_kind_fixed_thousand:
1517 FT_TRACE4(( " %f\n", (double)val / 65536 / 1000 ));
1520 ; /* never reached */
1526 case cff_kind_delta:
1528 FT_Byte* qcount = (FT_Byte*)parser->object +
1529 field->count_offset;
1531 FT_Byte** data = parser->stack;
1534 if ( num_args > field->array_max )
1535 num_args = field->array_max;
1537 FT_TRACE4(( " [" ));
1540 *qcount = (FT_Byte)num_args;
1543 while ( num_args > 0 )
1545 val = ADD_LONG( val, cff_parse_num( parser, data++ ) );
1546 switch ( field->size )
1548 case (8 / FT_CHAR_BIT):
1549 *(FT_Byte*)q = (FT_Byte)val;
1552 case (16 / FT_CHAR_BIT):
1553 *(FT_Short*)q = (FT_Short)val;
1556 case (32 / FT_CHAR_BIT):
1557 *(FT_Int32*)q = (FT_Int)val;
1560 default: /* for 64-bit systems */
1564 FT_TRACE4(( " %ld", val ));
1570 FT_TRACE4(( "]\n" ));
1574 default: /* callback or blend */
1575 error = field->reader( parser );
1583 /* this is an unknown operator, or it is unsupported; */
1584 /* we will ignore it for now. */
1588 /* TODO: could clear blend stack here, */
1589 /* but we don't have access to subFont */
1590 if ( field->kind != cff_kind_blend )
1591 parser->top = parser->stack;
1594 } /* while ( p < limit ) */
1599 #ifdef CFF_CONFIG_OPTION_OLD_ENGINE
1600 Out_Of_Memory_Error:
1601 error = FT_THROW( Out_Of_Memory );
1606 error = FT_THROW( Invalid_Argument );
1610 error = FT_THROW( Invalid_Argument );
1614 error = FT_THROW( Invalid_Argument );