1 /***************************************************************************/
5 /* CFF token stream parser (body) */
7 /* Copyright 1996-2001, 2002, 2003, 2004, 2007 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
26 /*************************************************************************/
28 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
29 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
30 /* messages during execution. */
33 #define FT_COMPONENT trace_cffparse
41 cff_kind_fixed_thousand
,
47 cff_kind_max
/* do not remove */
51 /* now generate handlers for the most simple fields */
52 typedef FT_Error (*CFF_Field_Reader
)( CFF_Parser parser
);
54 typedef struct CFF_Field_Handler_
60 CFF_Field_Reader reader
;
68 cff_parser_init( CFF_Parser parser
,
72 FT_MEM_ZERO( parser
, sizeof ( *parser
) );
74 parser
->top
= parser
->stack
;
75 parser
->object_code
= code
;
76 parser
->object
= object
;
82 cff_parse_integer( FT_Byte
* start
,
95 val
= (FT_Short
)( ( (FT_Int
)p
[0] << 8 ) | p
[1] );
103 val
= ( (FT_Long
)p
[0] << 24 ) |
104 ( (FT_Long
)p
[1] << 16 ) |
105 ( (FT_Long
)p
[2] << 8 ) |
118 val
= ( v
- 247 ) * 256 + p
[0] + 108;
126 val
= -( v
- 251 ) * 256 - p
[0] - 108;
141 cff_parse_real( FT_Byte
* start
,
146 FT_Long num
, divider
, result
, exponent
;
147 FT_Int sign
= 0, exponent_sign
= 0;
156 /* first of all, read the integer part */
161 /* If we entered this iteration with phase == 4, we need to */
162 /* read a new byte. This also skips past the initial 0x1E. */
167 /* Make sure we don't read past the end. */
172 /* Get the nibble. */
173 nib
= ( p
[0] >> phase
) & 0xF;
181 result
= result
* 10 + nib
;
184 /* read decimal part, if any */
188 /* If we entered this iteration with phase == 4, we need */
189 /* to read a new byte. */
194 /* Make sure we don't read past the end. */
199 /* Get the nibble. */
200 nib
= ( p
[0] >> phase
) & 0xF;
205 if ( divider
< 10000000L )
207 num
= num
* 10 + nib
;
212 /* read exponent, if any */
225 /* If we entered this iteration with phase == 4, we need */
226 /* to read a new byte. */
231 /* Make sure we don't read past the end. */
236 /* Get the nibble. */
237 nib
= ( p
[0] >> phase
) & 0xF;
242 exponent
= exponent
* 10 + nib
;
246 exponent
= -exponent
;
248 power_ten
+= (FT_Int
)exponent
;
251 /* raise to power of ten if needed */
252 while ( power_ten
> 0 )
254 result
= result
* 10;
260 while ( power_ten
< 0 )
262 result
= result
/ 10;
263 divider
= divider
* 10;
268 /* Move the integer part into the high 16 bits. */
271 /* Place the decimal part into the low 16 bits. */
273 result
|= FT_DivFix( num
, divider
);
287 /* read a number, either integer or real */
289 cff_parse_num( FT_Byte
** d
)
291 return ( **d
== 30 ? ( cff_parse_real ( d
[0], d
[1], 0 ) >> 16 )
292 : cff_parse_integer( d
[0], d
[1] ) );
296 /* read a floating point number, either integer or real */
298 cff_parse_fixed( FT_Byte
** d
)
300 return ( **d
== 30 ? cff_parse_real ( d
[0], d
[1], 0 )
301 : cff_parse_integer( d
[0], d
[1] ) << 16 );
304 /* read a floating point number, either integer or real, */
305 /* but return 1000 times the number read in. */
307 cff_parse_fixed_thousand( FT_Byte
** d
)
310 30 ? cff_parse_real ( d
[0], d
[1], 3 )
311 : (FT_Fixed
)FT_MulFix( cff_parse_integer( d
[0], d
[1] ) << 16, 1000 );
315 cff_parse_font_matrix( CFF_Parser parser
)
317 CFF_FontRecDict dict
= (CFF_FontRecDict
)parser
->object
;
318 FT_Matrix
* matrix
= &dict
->font_matrix
;
319 FT_Vector
* offset
= &dict
->font_offset
;
320 FT_UShort
* upm
= &dict
->units_per_em
;
321 FT_Byte
** data
= parser
->stack
;
326 error
= CFF_Err_Stack_Underflow
;
328 if ( parser
->top
>= parser
->stack
+ 6 )
330 matrix
->xx
= cff_parse_fixed_thousand( data
++ );
331 matrix
->yx
= cff_parse_fixed_thousand( data
++ );
332 matrix
->xy
= cff_parse_fixed_thousand( data
++ );
333 matrix
->yy
= cff_parse_fixed_thousand( data
++ );
334 offset
->x
= cff_parse_fixed_thousand( data
++ );
335 offset
->y
= cff_parse_fixed_thousand( data
);
337 temp
= FT_ABS( matrix
->yy
);
339 *upm
= (FT_UShort
)FT_DivFix( 0x10000L
, FT_DivFix( temp
, 1000 ) );
341 if ( temp
!= 0x10000L
)
343 matrix
->xx
= FT_DivFix( matrix
->xx
, temp
);
344 matrix
->yx
= FT_DivFix( matrix
->yx
, temp
);
345 matrix
->xy
= FT_DivFix( matrix
->xy
, temp
);
346 matrix
->yy
= FT_DivFix( matrix
->yy
, temp
);
347 offset
->x
= FT_DivFix( offset
->x
, temp
);
348 offset
->y
= FT_DivFix( offset
->y
, temp
);
351 /* note that the offsets must be expressed in integer font units */
363 cff_parse_font_bbox( CFF_Parser parser
)
365 CFF_FontRecDict dict
= (CFF_FontRecDict
)parser
->object
;
366 FT_BBox
* bbox
= &dict
->font_bbox
;
367 FT_Byte
** data
= parser
->stack
;
371 error
= CFF_Err_Stack_Underflow
;
373 if ( parser
->top
>= parser
->stack
+ 4 )
375 bbox
->xMin
= FT_RoundFix( cff_parse_fixed( data
++ ) );
376 bbox
->yMin
= FT_RoundFix( cff_parse_fixed( data
++ ) );
377 bbox
->xMax
= FT_RoundFix( cff_parse_fixed( data
++ ) );
378 bbox
->yMax
= FT_RoundFix( cff_parse_fixed( data
) );
387 cff_parse_private_dict( CFF_Parser parser
)
389 CFF_FontRecDict dict
= (CFF_FontRecDict
)parser
->object
;
390 FT_Byte
** data
= parser
->stack
;
394 error
= CFF_Err_Stack_Underflow
;
396 if ( parser
->top
>= parser
->stack
+ 2 )
398 dict
->private_size
= cff_parse_num( data
++ );
399 dict
->private_offset
= cff_parse_num( data
);
408 cff_parse_cid_ros( CFF_Parser parser
)
410 CFF_FontRecDict dict
= (CFF_FontRecDict
)parser
->object
;
411 FT_Byte
** data
= parser
->stack
;
415 error
= CFF_Err_Stack_Underflow
;
417 if ( parser
->top
>= parser
->stack
+ 3 )
419 dict
->cid_registry
= (FT_UInt
)cff_parse_num ( data
++ );
420 dict
->cid_ordering
= (FT_UInt
)cff_parse_num ( data
++ );
421 dict
->cid_supplement
= (FT_ULong
)cff_parse_num( data
);
429 #define CFF_FIELD_NUM( code, name ) \
430 CFF_FIELD( code, name, cff_kind_num )
431 #define CFF_FIELD_FIXED( code, name ) \
432 CFF_FIELD( code, name, cff_kind_fixed )
433 #define CFF_FIELD_FIXED_1000( code, name ) \
434 CFF_FIELD( code, name, cff_kind_fixed_thousand )
435 #define CFF_FIELD_STRING( code, name ) \
436 CFF_FIELD( code, name, cff_kind_string )
437 #define CFF_FIELD_BOOL( code, name ) \
438 CFF_FIELD( code, name, cff_kind_bool )
439 #define CFF_FIELD_DELTA( code, name, max ) \
440 CFF_FIELD( code, name, cff_kind_delta )
442 #define CFF_FIELD_CALLBACK( code, name ) \
447 cff_parse_ ## name, \
452 #define CFF_FIELD( code, name, kind ) \
456 FT_FIELD_OFFSET( name ), \
457 FT_FIELD_SIZE( name ), \
461 #undef CFF_FIELD_DELTA
462 #define CFF_FIELD_DELTA( code, name, max ) \
466 FT_FIELD_OFFSET( name ), \
467 FT_FIELD_SIZE_DELTA( name ), \
470 FT_FIELD_OFFSET( num_ ## name ) \
473 #define CFFCODE_TOPDICT 0x1000
474 #define CFFCODE_PRIVATE 0x2000
476 static const CFF_Field_Handler cff_field_handlers
[] =
479 #include "cfftoken.h"
481 { 0, 0, 0, 0, 0, 0, 0 }
485 FT_LOCAL_DEF( FT_Error
)
486 cff_parser_run( CFF_Parser parser
,
491 FT_Error error
= CFF_Err_Ok
;
494 parser
->top
= parser
->stack
;
495 parser
->start
= start
;
496 parser
->limit
= limit
;
497 parser
->cursor
= start
;
504 if ( v
>= 27 && v
!= 31 )
506 /* it's a number; we will push its position on the stack */
507 if ( parser
->top
- parser
->stack
>= CFF_MAX_STACK_DEPTH
)
515 /* skip real number */
539 /* This is not a number, hence it's an operator. Compute its code */
540 /* and look for it in our current list. */
543 FT_UInt num_args
= (FT_UInt
)
544 ( parser
->top
- parser
->stack
);
545 const CFF_Field_Handler
* field
;
552 /* two byte operator */
559 code
= code
| parser
->object_code
;
561 for ( field
= cff_field_handlers
; field
->kind
; field
++ )
563 if ( field
->code
== (FT_Int
)code
)
565 /* we found our field's handler; read it */
567 FT_Byte
* q
= (FT_Byte
*)parser
->object
+ field
->offset
;
570 /* check that we have enough arguments -- except for */
571 /* delta encoded arrays, which can be empty */
572 if ( field
->kind
!= cff_kind_delta
&& num_args
< 1 )
573 goto Stack_Underflow
;
575 switch ( field
->kind
)
578 case cff_kind_string
:
580 val
= cff_parse_num( parser
->stack
);
584 val
= cff_parse_fixed( parser
->stack
);
587 case cff_kind_fixed_thousand
:
588 val
= cff_parse_fixed_thousand( parser
->stack
);
591 switch ( field
->size
)
593 case (8 / FT_CHAR_BIT
):
594 *(FT_Byte
*)q
= (FT_Byte
)val
;
597 case (16 / FT_CHAR_BIT
):
598 *(FT_Short
*)q
= (FT_Short
)val
;
601 case (32 / FT_CHAR_BIT
):
602 *(FT_Int32
*)q
= (FT_Int
)val
;
605 default: /* for 64-bit systems */
612 FT_Byte
* qcount
= (FT_Byte
*)parser
->object
+
615 FT_Byte
** data
= parser
->stack
;
618 if ( num_args
> field
->array_max
)
619 num_args
= field
->array_max
;
622 *qcount
= (FT_Byte
)num_args
;
625 while ( num_args
> 0 )
627 val
+= cff_parse_num( data
++ );
628 switch ( field
->size
)
630 case (8 / FT_CHAR_BIT
):
631 *(FT_Byte
*)q
= (FT_Byte
)val
;
634 case (16 / FT_CHAR_BIT
):
635 *(FT_Short
*)q
= (FT_Short
)val
;
638 case (32 / FT_CHAR_BIT
):
639 *(FT_Int32
*)q
= (FT_Int
)val
;
642 default: /* for 64-bit systems */
652 default: /* callback */
653 error
= field
->reader( parser
);
661 /* this is an unknown operator, or it is unsupported; */
662 /* we will ignore it for now. */
666 parser
->top
= parser
->stack
;
675 error
= CFF_Err_Invalid_Argument
;
679 error
= CFF_Err_Invalid_Argument
;
683 error
= CFF_Err_Invalid_Argument
;