1 /***************************************************************************/
5 /* OpenType parser (body). */
7 /* Copyright 1996-2000 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 /***************************************************************************/
19 #ifdef FT_FLAT_COMPILE
25 #include <cff/t2parse.h>
30 #include <freetype/internal/t2errors.h>
33 /*************************************************************************/
35 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
36 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
37 /* messages during execution. */
40 #define FT_COMPONENT trace_t2parse
43 #define T2_Err_Stack_Underflow FT_Err_Invalid_Argument
44 #define T2_Err_Syntax_Error FT_Err_Invalid_Argument
57 t2_kind_max
/* do not remove */
61 /* now generate handlers for the most simple fields */
62 typedef FT_Error (*T2_Field_Reader
)( T2_Parser
* parser
);
64 typedef struct T2_Field_Handler_
70 T2_Field_Reader reader
;
78 void T2_Parser_Init( T2_Parser
* parser
,
82 MEM_Set( parser
, 0, sizeof ( *parser
) );
84 parser
->top
= parser
->stack
;
85 parser
->object_code
= code
;
86 parser
->object
= object
;
90 /* reads an integer */
92 FT_Long
parse_t2_integer( FT_Byte
* start
,
105 val
= (FT_Short
)( ( (FT_Int
)p
[0] << 8 ) | p
[1] );
113 val
= ( (FT_Long
)p
[0] << 24 ) |
114 ( (FT_Long
)p
[1] << 16 ) |
115 ( (FT_Long
)p
[2] << 8 ) |
128 val
= ( v
- 247 ) * 256 + p
[0] + 108;
136 val
= -( v
- 251 ) * 256 - p
[0] - 108;
151 FT_Fixed
parse_t2_real( FT_Byte
* start
,
156 FT_Long num
, divider
, result
, exp
;
157 FT_Int sign
= 0, exp_sign
= 0;
166 /* first of all, read the integer part */
172 /* read one nibble at a time */
173 if ( phase
&& ++p
>= limit
)
176 nib
= ( p
[0] >> phase
) & 0xF;
184 result
= result
* 10 + nib
;
187 /* read decimal part, if any */
191 /* read one nibble at a time */
192 if ( !phase
&& ++p
>= limit
)
196 nib
= ( p
[0] >> phase
) & 0xF;
201 if (divider
< 10000000L)
203 num
= num
* 10 + nib
;
208 /* read exponent, if any */
221 /* read one nibble at a time */
222 if ( !phase
&& ++p
>= limit
)
226 nib
= ( p
[0] >> phase
) & 0xF;
231 exp
= exp
* 10 + nib
;
240 /* raise to power of ten if needed */
241 while ( power_ten
> 0 )
243 result
= result
* 10;
249 while ( power_ten
< 0 )
251 result
= result
/ 10;
252 divider
= divider
* 10;
258 result
+= FT_DivFix( num
, divider
);
272 /* read a number, either integer or real */
274 FT_Long
t2_parse_num( FT_Byte
** d
)
276 return ( **d
== 30 ? ( parse_t2_real( d
[0], d
[1], 0 ) >> 16 )
277 : parse_t2_integer( d
[0], d
[1] ) );
281 /* reads a floating point number, either integer or real */
283 FT_Fixed
t2_parse_fixed( FT_Byte
** d
)
285 return ( **d
== 30 ? parse_t2_real( d
[0], d
[1], 0 )
286 : parse_t2_integer( d
[0], d
[1] ) << 16 );
291 FT_Error
parse_font_matrix( T2_Parser
* parser
)
293 CFF_Font_Dict
* dict
= (CFF_Font_Dict
*)parser
->object
;
294 FT_Matrix
* matrix
= &dict
->font_matrix
;
295 FT_Byte
** data
= parser
->stack
;
299 error
= T2_Err_Stack_Underflow
;
301 if ( parser
->top
>= parser
->stack
+ 4 )
303 matrix
->xx
= t2_parse_fixed( data
++ );
304 matrix
->yx
= t2_parse_fixed( data
++ );
305 matrix
->xy
= t2_parse_fixed( data
++ );
306 matrix
->yy
= t2_parse_fixed( data
);
315 FT_Error
parse_font_bbox( T2_Parser
* parser
)
317 CFF_Font_Dict
* dict
= (CFF_Font_Dict
*)parser
->object
;
318 FT_BBox
* bbox
= &dict
->font_bbox
;
319 FT_Byte
** data
= parser
->stack
;
323 error
= T2_Err_Stack_Underflow
;
325 if ( parser
->top
>= parser
->stack
+ 4 )
327 bbox
->xMin
= t2_parse_num( data
++ );
328 bbox
->yMin
= t2_parse_num( data
++ );
329 bbox
->xMax
= t2_parse_num( data
++ );
330 bbox
->yMax
= t2_parse_num( data
);
339 FT_Error
parse_private_dict( T2_Parser
* parser
)
341 CFF_Font_Dict
* dict
= (CFF_Font_Dict
*)parser
->object
;
342 FT_Byte
** data
= parser
->stack
;
346 error
= T2_Err_Stack_Underflow
;
348 if ( parser
->top
>= parser
->stack
+ 2 )
350 dict
->private_size
= t2_parse_num( data
++ );
351 dict
->private_offset
= t2_parse_num( data
);
360 FT_Error
parse_cid_ros( T2_Parser
* parser
)
362 CFF_Font_Dict
* dict
= (CFF_Font_Dict
*)parser
->object
;
363 FT_Byte
** data
= parser
->stack
;
367 error
= T2_Err_Stack_Underflow
;
369 if ( parser
->top
>= parser
->stack
+ 3 )
371 dict
->cid_registry
= (FT_UInt
)t2_parse_num( data
++ );
372 dict
->cid_ordering
= (FT_UInt
)t2_parse_num( data
++ );
373 dict
->cid_supplement
= (FT_ULong
)t2_parse_num( data
);
381 #define T2_FIELD_NUM( code, name ) \
382 T2_FIELD( code, name, t2_kind_num )
383 #define T2_FIELD_FIXED( code, name ) \
384 T2_FIELD( code, name, t2_kind_fixed )
385 #define T2_FIELD_STRING( code, name ) \
386 T2_FIELD( code, name, t2_kind_string )
387 #define T2_FIELD_BOOL( code, name ) \
388 T2_FIELD( code, name, t2_kind_bool )
389 #define T2_FIELD_DELTA( code, name,max ) \
390 T2_FIELD( code, name, t2_kind_delta )
392 #define T2_REF( s, f ) ( ((s*)0)->f )
394 #define T2_FIELD_CALLBACK( code, name ) \
404 #define T2_FIELD( code, name, kind ) \
408 (FT_UInt)(char*)&T2_REF( T2TYPE, name ), \
409 sizeof( T2_REF( T2TYPE, name ) ), \
413 #undef T2_FIELD_DELTA
414 #define T2_FIELD_DELTA( code, name, max ) \
418 (FT_UInt)(char*)&T2_REF( T2TYPE, name ), \
419 sizeof( T2_REF( T2TYPE, name )[0] ), \
422 (FT_UInt)(char*)&T2_REF( T2TYPE, num_ ## name ) \
425 #define T2CODE_TOPDICT 0x1000
426 #define T2CODE_PRIVATE 0x2000
428 static const T2_Field_Handler t2_field_handlers
[] =
431 #ifdef FT_FLAT_COMPILE
433 #include "t2tokens.h"
437 #include <cff/t2tokens.h>
441 { 0, 0, 0, 0, 0, 0, 0 }
446 FT_Error
T2_Parser_Run( T2_Parser
* parser
,
451 FT_Error error
= T2_Err_Ok
;
454 parser
->top
= parser
->stack
;
455 parser
->start
= start
;
456 parser
->limit
= limit
;
457 parser
->cursor
= start
;
464 if ( v
>= 27 && v
!= 31 )
466 /* it's a number; we will push its position on the stack */
467 if ( parser
->top
- parser
->stack
>= T2_MAX_STACK_DEPTH
)
475 /* skip real number */
499 /* This is not a number, hence it's an operator. Compute its code */
500 /* and look for it in our current list. */
503 FT_UInt num_args
= (FT_UInt
)
504 ( parser
->top
- parser
->stack
);
505 const T2_Field_Handler
* field
;
508 /* first of all, a trivial check */
510 goto Stack_Underflow
;
516 /* two byte operator */
520 code
= code
| parser
->object_code
;
522 for ( field
= t2_field_handlers
; field
->kind
; field
++ )
524 if ( field
->code
== (FT_Int
)code
)
526 /* we found our field's handler; read it */
528 FT_Byte
* q
= (FT_Byte
*)parser
->object
+ field
->offset
;
531 switch ( field
->kind
)
536 val
= t2_parse_num( parser
->stack
);
540 val
= t2_parse_fixed( parser
->stack
);
543 switch ( field
->size
)
546 *(FT_Byte
*)q
= (FT_Byte
)val
;
550 *(FT_Short
*)q
= (FT_Short
)val
;
554 *(FT_Int32
*)q
= (FT_Int
)val
;
557 default: /* for 64-bit systems where long is 8 bytes */
564 FT_Byte
* qcount
= (FT_Byte
*)parser
->object
+
568 FT_Byte
** data
= parser
->stack
;
571 if ( num_args
> field
->array_max
)
572 num_args
= field
->array_max
;
575 *qcount
= (FT_Byte
)num_args
;
578 while ( num_args
> 0 )
580 val
+= t2_parse_num( data
++ );
581 switch ( field
->size
)
584 *(FT_Byte
*)q
= (FT_Byte
)val
;
588 *(FT_Short
*)q
= (FT_Short
)val
;
592 *(FT_Int32
*)q
= (FT_Int
)val
;
595 default: /* for 64-bit systems */
605 default: /* callback */
606 error
= field
->reader( parser
);
614 /* this is an unknown operator, or it is unsupported; */
615 /* we will ignore it for now. */
619 parser
->top
= parser
->stack
;
628 error
= T2_Err_Invalid_Argument
;
632 error
= T2_Err_Invalid_Argument
;
636 error
= T2_Err_Invalid_Argument
;