1 /***************************************************************************/
5 /* CFF token stream parser (body) */
7 /* Copyright 1996-2001, 2002, 2003, 2004, 2007, 2008, 2009 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
41 cff_parser_init( CFF_Parser parser
,
46 FT_MEM_ZERO( parser
, sizeof ( *parser
) );
48 parser
->top
= parser
->stack
;
49 parser
->object_code
= code
;
50 parser
->object
= object
;
51 parser
->library
= library
;
57 cff_parse_integer( FT_Byte
* start
,
70 val
= (FT_Short
)( ( (FT_Int
)p
[0] << 8 ) | p
[1] );
78 val
= ( (FT_Long
)p
[0] << 24 ) |
79 ( (FT_Long
)p
[1] << 16 ) |
80 ( (FT_Long
)p
[2] << 8 ) |
93 val
= ( v
- 247 ) * 256 + p
[0] + 108;
101 val
= -( v
- 251 ) * 256 - p
[0] - 108;
114 static const FT_Long power_tens
[] =
131 cff_parse_real( FT_Byte
* start
,
140 FT_Long result
, number
, rest
, exponent
;
141 FT_Int sign
= 0, exponent_sign
= 0;
142 FT_Long exponent_add
, integer_length
, fraction_length
;
160 /* First of all, read the integer part. */
165 /* If we entered this iteration with phase == 4, we need to */
166 /* read a new byte. This also skips past the initial 0x1E. */
171 /* Make sure we don't read past the end. */
176 /* Get the nibble. */
177 nib
= ( p
[0] >> phase
) & 0xF;
186 /* Increase exponent if we can't add the digit. */
187 if ( number
>= 0xCCCCCCCL
)
189 /* Skip leading zeros. */
190 else if ( nib
|| number
)
193 number
= number
* 10 + nib
;
198 /* Read fraction part, if any. */
202 /* If we entered this iteration with phase == 4, we need */
203 /* to read a new byte. */
208 /* Make sure we don't read past the end. */
213 /* Get the nibble. */
214 nib
= ( p
[0] >> phase
) & 0xF;
219 /* Skip leading zeros if possible. */
220 if ( !nib
&& !number
)
222 /* Only add digit if we don't overflow. */
223 else if ( number
< 0xCCCCCCCL
&& fraction_length
< 9 )
226 number
= number
* 10 + nib
;
230 /* Read exponent, if any. */
241 /* If we entered this iteration with phase == 4, */
242 /* we need to read a new byte. */
247 /* Make sure we don't read past the end. */
252 /* Get the nibble. */
253 nib
= ( p
[0] >> phase
) & 0xF;
258 exponent
= exponent
* 10 + nib
;
260 /* Arbitrarily limit exponent. */
261 if ( exponent
> 1000 )
266 exponent
= -exponent
;
269 /* We don't check `power_ten' and `exponent_add'. */
270 exponent
+= power_ten
+ exponent_add
;
274 /* Only use `fraction_length'. */
275 fraction_length
+= integer_length
;
276 exponent
+= integer_length
;
278 if ( fraction_length
<= 5 )
280 if ( number
> 0x7FFFL
)
282 result
= FT_DivFix( number
, 10 );
283 *scaling
= exponent
- fraction_length
+ 1;
289 FT_Long new_fraction_length
, shift
;
292 /* Make `scaling' as small as possible. */
293 new_fraction_length
= FT_MIN( exponent
, 5 );
294 exponent
-= new_fraction_length
;
295 shift
= new_fraction_length
- fraction_length
;
297 number
*= power_tens
[shift
];
298 if ( number
> 0x7FFFL
)
305 exponent
-= fraction_length
;
307 result
= number
<< 16;
313 if ( ( number
/ power_tens
[fraction_length
- 5] ) > 0x7FFFL
)
315 result
= FT_DivFix( number
, power_tens
[fraction_length
- 4] );
316 *scaling
= exponent
- 4;
320 result
= FT_DivFix( number
, power_tens
[fraction_length
- 5] );
321 *scaling
= exponent
- 5;
327 integer_length
+= exponent
;
328 fraction_length
-= exponent
;
330 /* Check for overflow and underflow. */
331 if ( FT_ABS( integer_length
) > 5 )
334 /* Remove non-significant digits. */
335 if ( integer_length
< 0 ) {
336 number
/= power_tens
[-integer_length
];
337 fraction_length
+= integer_length
;
340 /* Convert into 16.16 format. */
341 if ( fraction_length
> 0 )
343 if ( ( number
/ power_tens
[fraction_length
] ) > 0x7FFFL
)
346 result
= FT_DivFix( number
, power_tens
[fraction_length
] );
350 number
*= power_tens
[-fraction_length
];
352 if ( number
> 0x7FFFL
)
355 result
= number
<< 16;
367 /* read a number, either integer or real */
369 cff_parse_num( FT_Byte
** d
)
371 return **d
== 30 ? ( cff_parse_real( d
[0], d
[1], 0, NULL
) >> 16 )
372 : cff_parse_integer( d
[0], d
[1] );
376 /* read a floating point number, either integer or real */
378 cff_parse_fixed( FT_Byte
** d
)
380 return **d
== 30 ? cff_parse_real( d
[0], d
[1], 0, NULL
)
381 : cff_parse_integer( d
[0], d
[1] ) << 16;
385 /* read a floating point number, either integer or real, */
386 /* but return `10^scaling' times the number read in */
388 cff_parse_fixed_scaled( FT_Byte
** d
,
391 return **d
== 30 ? cff_parse_real( d
[0], d
[1], scaling
, NULL
)
392 : ( cff_parse_integer( d
[0], d
[1] ) *
393 power_tens
[scaling
] ) << 16;
397 /* read a floating point number, either integer or real, */
398 /* and return it as precise as possible -- `scaling' returns */
399 /* the scaling factor (as a power of 10) */
401 cff_parse_fixed_dynamic( FT_Byte
** d
,
404 FT_ASSERT( scaling
);
407 return cff_parse_real( d
[0], d
[1], 0, scaling
);
411 FT_Int integer_length
;
414 number
= cff_parse_integer( d
[0], d
[1] );
416 if ( number
> 0x7FFFL
)
418 for ( integer_length
= 5; integer_length
< 10; integer_length
++ )
419 if ( number
< power_tens
[integer_length
] )
422 if ( ( number
/ power_tens
[integer_length
- 5] ) > 0x7FFFL
)
424 *scaling
= integer_length
- 4;
425 return FT_DivFix( number
, power_tens
[integer_length
- 4] );
429 *scaling
= integer_length
- 5;
430 return FT_DivFix( number
, power_tens
[integer_length
- 5] );
443 cff_parse_font_matrix( CFF_Parser parser
)
445 CFF_FontRecDict dict
= (CFF_FontRecDict
)parser
->object
;
446 FT_Matrix
* matrix
= &dict
->font_matrix
;
447 FT_Vector
* offset
= &dict
->font_offset
;
448 FT_ULong
* upm
= &dict
->units_per_em
;
449 FT_Byte
** data
= parser
->stack
;
450 FT_Error error
= CFF_Err_Stack_Underflow
;
453 if ( parser
->top
>= parser
->stack
+ 6 )
460 /* We expect a well-formed font matrix, this is, the matrix elements */
461 /* `xx' and `yy' are of approximately the same magnitude. To avoid */
462 /* loss of precision, we use the magnitude of element `xx' to scale */
463 /* all other elements. The scaling factor is then contained in the */
464 /* `units_per_em' value. */
466 matrix
->xx
= cff_parse_fixed_dynamic( data
++, &scaling
);
470 if ( scaling
< 0 || scaling
> 9 )
472 /* Return default matrix in case of unlikely values. */
473 matrix
->xx
= 0x10000L
;
476 matrix
->yy
= 0x10000L
;
484 matrix
->yx
= cff_parse_fixed_scaled( data
++, scaling
);
485 matrix
->xy
= cff_parse_fixed_scaled( data
++, scaling
);
486 matrix
->yy
= cff_parse_fixed_scaled( data
++, scaling
);
487 offset
->x
= cff_parse_fixed_scaled( data
++, scaling
);
488 offset
->y
= cff_parse_fixed_scaled( data
, scaling
);
490 *upm
= power_tens
[scaling
];
499 cff_parse_font_bbox( CFF_Parser parser
)
501 CFF_FontRecDict dict
= (CFF_FontRecDict
)parser
->object
;
502 FT_BBox
* bbox
= &dict
->font_bbox
;
503 FT_Byte
** data
= parser
->stack
;
507 error
= CFF_Err_Stack_Underflow
;
509 if ( parser
->top
>= parser
->stack
+ 4 )
511 bbox
->xMin
= FT_RoundFix( cff_parse_fixed( data
++ ) );
512 bbox
->yMin
= FT_RoundFix( cff_parse_fixed( data
++ ) );
513 bbox
->xMax
= FT_RoundFix( cff_parse_fixed( data
++ ) );
514 bbox
->yMax
= FT_RoundFix( cff_parse_fixed( data
) );
523 cff_parse_private_dict( CFF_Parser parser
)
525 CFF_FontRecDict dict
= (CFF_FontRecDict
)parser
->object
;
526 FT_Byte
** data
= parser
->stack
;
530 error
= CFF_Err_Stack_Underflow
;
532 if ( parser
->top
>= parser
->stack
+ 2 )
534 dict
->private_size
= cff_parse_num( data
++ );
535 dict
->private_offset
= cff_parse_num( data
);
544 cff_parse_cid_ros( CFF_Parser parser
)
546 CFF_FontRecDict dict
= (CFF_FontRecDict
)parser
->object
;
547 FT_Byte
** data
= parser
->stack
;
551 error
= CFF_Err_Stack_Underflow
;
553 if ( parser
->top
>= parser
->stack
+ 3 )
555 dict
->cid_registry
= (FT_UInt
)cff_parse_num ( data
++ );
556 dict
->cid_ordering
= (FT_UInt
)cff_parse_num ( data
++ );
558 FT_TRACE1(( "cff_parse_cid_ros: real supplement is rounded\n" ));
559 dict
->cid_supplement
= cff_parse_num( data
);
560 if ( dict
->cid_supplement
< 0 )
561 FT_TRACE1(( "cff_parse_cid_ros: negative supplement %d is found\n",
562 dict
->cid_supplement
));
570 #define CFF_FIELD_NUM( code, name ) \
571 CFF_FIELD( code, name, cff_kind_num )
572 #define CFF_FIELD_FIXED( code, name ) \
573 CFF_FIELD( code, name, cff_kind_fixed )
574 #define CFF_FIELD_FIXED_1000( code, name ) \
575 CFF_FIELD( code, name, cff_kind_fixed_thousand )
576 #define CFF_FIELD_STRING( code, name ) \
577 CFF_FIELD( code, name, cff_kind_string )
578 #define CFF_FIELD_BOOL( code, name ) \
579 CFF_FIELD( code, name, cff_kind_bool )
580 #define CFF_FIELD_DELTA( code, name, max ) \
581 CFF_FIELD( code, name, cff_kind_delta )
583 #define CFFCODE_TOPDICT 0x1000
584 #define CFFCODE_PRIVATE 0x2000
586 #ifndef FT_CONFIG_OPTION_PIC
588 #define CFF_FIELD_CALLBACK( code, name ) \
593 cff_parse_ ## name, \
598 #define CFF_FIELD( code, name, kind ) \
602 FT_FIELD_OFFSET( name ), \
603 FT_FIELD_SIZE( name ), \
607 #undef CFF_FIELD_DELTA
608 #define CFF_FIELD_DELTA( code, name, max ) \
612 FT_FIELD_OFFSET( name ), \
613 FT_FIELD_SIZE_DELTA( name ), \
616 FT_FIELD_OFFSET( num_ ## name ) \
619 static const CFF_Field_Handler cff_field_handlers
[] =
622 #include "cfftoken.h"
624 { 0, 0, 0, 0, 0, 0, 0 }
628 #else /* FT_CONFIG_OPTION_PIC */
630 void FT_Destroy_Class_cff_field_handlers(FT_Library library
, CFF_Field_Handler
* clazz
)
632 FT_Memory memory
= library
->memory
;
637 FT_Error
FT_Create_Class_cff_field_handlers(FT_Library library
, CFF_Field_Handler
** output_class
)
639 CFF_Field_Handler
* clazz
;
641 FT_Memory memory
= library
->memory
;
645 #undef CFF_FIELD_DELTA
646 #undef CFF_FIELD_CALLBACK
647 #define CFF_FIELD_CALLBACK( code, name ) i++;
648 #define CFF_FIELD( code, name, kind ) i++;
649 #define CFF_FIELD_DELTA( code, name, max ) i++;
651 #include "cfftoken.h"
652 i
++;/*{ 0, 0, 0, 0, 0, 0, 0 }*/
654 if ( FT_ALLOC( clazz
, sizeof(CFF_Field_Handler
)*i
) )
659 #undef CFF_FIELD_DELTA
660 #undef CFF_FIELD_CALLBACK
662 #define CFF_FIELD_CALLBACK( code_, name_ ) \
663 clazz[i].kind = cff_kind_callback; \
664 clazz[i].code = code_ | CFFCODE; \
665 clazz[i].offset = 0; \
667 clazz[i].reader = cff_parse_ ## name_; \
668 clazz[i].array_max = 0; \
669 clazz[i].count_offset = 0; \
673 #define CFF_FIELD( code_, name_, kind_ ) \
674 clazz[i].kind = kind_; \
675 clazz[i].code = code_ | CFFCODE; \
676 clazz[i].offset = FT_FIELD_OFFSET( name_ ); \
677 clazz[i].size = FT_FIELD_SIZE( name_ ); \
678 clazz[i].reader = 0; \
679 clazz[i].array_max = 0; \
680 clazz[i].count_offset = 0; \
683 #undef CFF_FIELD_DELTA
684 #define CFF_FIELD_DELTA( code_, name_, max_ ) \
685 clazz[i].kind = cff_kind_delta; \
686 clazz[i].code = code_ | CFFCODE; \
687 clazz[i].offset = FT_FIELD_OFFSET( name_ ); \
688 clazz[i].size = FT_FIELD_SIZE_DELTA( name_ ); \
689 clazz[i].reader = 0; \
690 clazz[i].array_max = max_; \
691 clazz[i].count_offset = FT_FIELD_OFFSET( num_ ## name_ ); \
694 #include "cfftoken.h"
701 clazz
[i
].array_max
= 0;
702 clazz
[i
].count_offset
= 0;
704 *output_class
= clazz
;
709 #endif /* FT_CONFIG_OPTION_PIC */
712 FT_LOCAL_DEF( FT_Error
)
713 cff_parser_run( CFF_Parser parser
,
718 FT_Error error
= CFF_Err_Ok
;
719 FT_Library library
= parser
->library
;
723 parser
->top
= parser
->stack
;
724 parser
->start
= start
;
725 parser
->limit
= limit
;
726 parser
->cursor
= start
;
733 if ( v
>= 27 && v
!= 31 )
735 /* it's a number; we will push its position on the stack */
736 if ( parser
->top
- parser
->stack
>= CFF_MAX_STACK_DEPTH
)
744 /* skip real number */
748 /* An unterminated floating point number at the */
749 /* end of a dictionary is invalid but harmless. */
770 /* This is not a number, hence it's an operator. Compute its code */
771 /* and look for it in our current list. */
774 FT_UInt num_args
= (FT_UInt
)
775 ( parser
->top
- parser
->stack
);
776 const CFF_Field_Handler
* field
;
783 /* two byte operator */
790 code
= code
| parser
->object_code
;
792 for ( field
= FT_CFF_FIELD_HANDLERS_GET
; field
->kind
; field
++ )
794 if ( field
->code
== (FT_Int
)code
)
796 /* we found our field's handler; read it */
798 FT_Byte
* q
= (FT_Byte
*)parser
->object
+ field
->offset
;
801 /* check that we have enough arguments -- except for */
802 /* delta encoded arrays, which can be empty */
803 if ( field
->kind
!= cff_kind_delta
&& num_args
< 1 )
804 goto Stack_Underflow
;
806 switch ( field
->kind
)
809 case cff_kind_string
:
811 val
= cff_parse_num( parser
->stack
);
815 val
= cff_parse_fixed( parser
->stack
);
818 case cff_kind_fixed_thousand
:
819 val
= cff_parse_fixed_scaled( parser
->stack
, 3 );
822 switch ( field
->size
)
824 case (8 / FT_CHAR_BIT
):
825 *(FT_Byte
*)q
= (FT_Byte
)val
;
828 case (16 / FT_CHAR_BIT
):
829 *(FT_Short
*)q
= (FT_Short
)val
;
832 case (32 / FT_CHAR_BIT
):
833 *(FT_Int32
*)q
= (FT_Int
)val
;
836 default: /* for 64-bit systems */
843 FT_Byte
* qcount
= (FT_Byte
*)parser
->object
+
846 FT_Byte
** data
= parser
->stack
;
849 if ( num_args
> field
->array_max
)
850 num_args
= field
->array_max
;
853 *qcount
= (FT_Byte
)num_args
;
856 while ( num_args
> 0 )
858 val
+= cff_parse_num( data
++ );
859 switch ( field
->size
)
861 case (8 / FT_CHAR_BIT
):
862 *(FT_Byte
*)q
= (FT_Byte
)val
;
865 case (16 / FT_CHAR_BIT
):
866 *(FT_Short
*)q
= (FT_Short
)val
;
869 case (32 / FT_CHAR_BIT
):
870 *(FT_Int32
*)q
= (FT_Int
)val
;
873 default: /* for 64-bit systems */
883 default: /* callback */
884 error
= field
->reader( parser
);
892 /* this is an unknown operator, or it is unsupported; */
893 /* we will ignore it for now. */
897 parser
->top
= parser
->stack
;
906 error
= CFF_Err_Invalid_Argument
;
910 error
= CFF_Err_Invalid_Argument
;
914 error
= CFF_Err_Invalid_Argument
;