1 /***************************************************************************/
5 /* CFF token stream parser (body) */
7 /* Copyright 1996-2001 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
46 cff_kind_max /* do not remove */
50 /* now generate handlers for the most simple fields */
51 typedef FT_Error (*CFF_Field_Reader)( CFF_Parser* parser );
53 typedef struct CFF_Field_Handler_
59 CFF_Field_Reader reader;
67 CFF_Parser_Init( CFF_Parser* parser,
71 MEM_Set( parser, 0, sizeof ( *parser ) );
73 parser->top = parser->stack;
74 parser->object_code = code;
75 parser->object = object;
81 cff_parse_integer( FT_Byte* start,
94 val = (FT_Short)( ( (FT_Int)p[0] << 8 ) | p[1] );
102 val = ( (FT_Long)p[0] << 24 ) |
103 ( (FT_Long)p[1] << 16 ) |
104 ( (FT_Long)p[2] << 8 ) |
117 val = ( v - 247 ) * 256 + p[0] + 108;
125 val = -( v - 251 ) * 256 - p[0] - 108;
140 cff_parse_real( FT_Byte* start,
145 FT_Long num, divider, result, exp;
146 FT_Int sign = 0, exp_sign = 0;
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 intial 0x1E. */
166 /* Make sure we don't read past the end. */
171 /* Get the nibble. */
172 nib = ( p[0] >> phase ) & 0xF;
180 result = result * 10 + nib;
183 /* read decimal part, if any */
187 /* If we entered this iteration with phase == 4, we need */
188 /* to read a new byte. */
193 /* Make sure we don't read past the end. */
198 /* Get the nibble. */
199 nib = ( p[0] >> phase ) & 0xF;
204 if ( divider < 10000000L )
206 num = num * 10 + nib;
211 /* read exponent, if any */
224 /* If we entered this iteration with phase == 4, we need */
225 /* to read a new byte. */
230 /* Make sure we don't read past the end. */
235 /* Get the nibble. */
236 nib = ( p[0] >> phase ) & 0xF;
241 exp = exp * 10 + nib;
250 /* raise to power of ten if needed */
251 while ( power_ten > 0 )
253 result = result * 10;
259 while ( power_ten < 0 )
261 result = result / 10;
262 divider = divider * 10;
267 /* Move the integer part into the high 16 bits. */
270 /* Place the decimal part into the low 16 bits. */
272 result |= FT_DivFix( num, divider );
286 /* read a number, either integer or real */
288 cff_parse_num( FT_Byte** d )
290 return ( **d == 30 ? ( cff_parse_real ( d[0], d[1], 0 ) >> 16 )
291 : cff_parse_integer( d[0], d[1] ) );
295 /* read a floating point number, either integer or real */
297 cff_parse_fixed( FT_Byte** d )
299 return ( **d == 30 ? cff_parse_real ( d[0], d[1], 0 )
300 : cff_parse_integer( d[0], d[1] ) << 16 );
303 /* read a floating point number, either integer or real, */
304 /* but return 1000 times the number read in. */
306 cff_parse_fixed_thousand( FT_Byte** d )
309 30 ? cff_parse_real ( d[0], d[1], 3 )
310 : (FT_Fixed)FT_MulFix( cff_parse_integer( d[0], d[1] ) << 16, 1000 );
314 cff_parse_font_matrix( CFF_Parser* parser )
316 CFF_Font_Dict* dict = (CFF_Font_Dict*)parser->object;
317 FT_Matrix* matrix = &dict->font_matrix;
318 FT_Vector* offset = &dict->font_offset;
319 FT_UShort* upm = &dict->units_per_em;
320 FT_Byte** data = parser->stack;
325 error = CFF_Err_Stack_Underflow;
327 if ( parser->top >= parser->stack + 6 )
329 matrix->xx = cff_parse_fixed_thousand( data++ );
330 matrix->yx = cff_parse_fixed_thousand( data++ );
331 matrix->xy = cff_parse_fixed_thousand( data++ );
332 matrix->yy = cff_parse_fixed_thousand( data++ );
333 offset->x = cff_parse_fixed_thousand( data++ );
334 offset->y = cff_parse_fixed_thousand( data );
336 temp = ABS( matrix->yy );
338 *upm = (FT_UShort)FT_DivFix( 0x10000L, FT_DivFix( temp, 1000 ) );
340 if ( temp != 0x10000L )
342 matrix->xx = FT_DivFix( matrix->xx, temp );
343 matrix->yx = FT_DivFix( matrix->yx, temp );
344 matrix->xy = FT_DivFix( matrix->xy, temp );
345 matrix->yy = FT_DivFix( matrix->yy, temp );
346 offset->x = FT_DivFix( offset->x, temp );
347 offset->y = FT_DivFix( offset->y, temp );
350 /* note that the offsets must be expressed in integer font units */
362 cff_parse_font_bbox( CFF_Parser* parser )
364 CFF_Font_Dict* dict = (CFF_Font_Dict*)parser->object;
365 FT_BBox* bbox = &dict->font_bbox;
366 FT_Byte** data = parser->stack;
370 error = CFF_Err_Stack_Underflow;
372 if ( parser->top >= parser->stack + 4 )
374 bbox->xMin = FT_RoundFix( cff_parse_fixed( data++ ) );
375 bbox->yMin = FT_RoundFix( cff_parse_fixed( data++ ) );
376 bbox->xMax = FT_RoundFix( cff_parse_fixed( data++ ) );
377 bbox->yMax = FT_RoundFix( cff_parse_fixed( data ) );
386 cff_parse_private_dict( CFF_Parser* parser )
388 CFF_Font_Dict* dict = (CFF_Font_Dict*)parser->object;
389 FT_Byte** data = parser->stack;
393 error = CFF_Err_Stack_Underflow;
395 if ( parser->top >= parser->stack + 2 )
397 dict->private_size = cff_parse_num( data++ );
398 dict->private_offset = cff_parse_num( data );
407 cff_parse_cid_ros( CFF_Parser* parser )
409 CFF_Font_Dict* dict = (CFF_Font_Dict*)parser->object;
410 FT_Byte** data = parser->stack;
414 error = CFF_Err_Stack_Underflow;
416 if ( parser->top >= parser->stack + 3 )
418 dict->cid_registry = (FT_UInt)cff_parse_num ( data++ );
419 dict->cid_ordering = (FT_UInt)cff_parse_num ( data++ );
420 dict->cid_supplement = (FT_ULong)cff_parse_num( data );
428 #define CFF_FIELD_NUM( code, name ) \
429 CFF_FIELD( code, name, cff_kind_num )
430 #define CFF_FIELD_FIXED( code, name ) \
431 CFF_FIELD( code, name, cff_kind_fixed )
432 #define CFF_FIELD_STRING( code, name ) \
433 CFF_FIELD( code, name, cff_kind_string )
434 #define CFF_FIELD_BOOL( code, name ) \
435 CFF_FIELD( code, name, cff_kind_bool )
436 #define CFF_FIELD_DELTA( code, name, max ) \
437 CFF_FIELD( code, name, cff_kind_delta )
439 #define CFF_FIELD_CALLBACK( code, name ) \
444 cff_parse_ ## name, \
449 #define CFF_FIELD( code, name, kind ) \
453 FT_FIELD_OFFSET( name ), \
454 FT_FIELD_SIZE( name ), \
458 #undef CFF_FIELD_DELTA
459 #define CFF_FIELD_DELTA( code, name, max ) \
463 FT_FIELD_OFFSET( name ), \
464 FT_FIELD_SIZE_DELTA( name ), \
467 FT_FIELD_OFFSET( num_ ## name ) \
470 #define CFFCODE_TOPDICT 0x1000
471 #define CFFCODE_PRIVATE 0x2000
473 static const CFF_Field_Handler cff_field_handlers[] =
476 #include "cfftoken.h"
478 { 0, 0, 0, 0, 0, 0, 0 }
482 FT_LOCAL_DEF FT_Error
483 CFF_Parser_Run( CFF_Parser* parser,
488 FT_Error error = CFF_Err_Ok;
491 parser->top = parser->stack;
492 parser->start = start;
493 parser->limit = limit;
494 parser->cursor = start;
501 if ( v >= 27 && v != 31 )
503 /* it's a number; we will push its position on the stack */
504 if ( parser->top - parser->stack >= CFF_MAX_STACK_DEPTH )
512 /* skip real number */
536 /* This is not a number, hence it's an operator. Compute its code */
537 /* and look for it in our current list. */
540 FT_UInt num_args = (FT_UInt)
541 ( parser->top - parser->stack );
542 const CFF_Field_Handler* field;
545 /* first of all, a trivial check */
547 goto Stack_Underflow;
553 /* two byte operator */
557 code = code | parser->object_code;
559 for ( field = cff_field_handlers; field->kind; field++ )
561 if ( field->code == (FT_Int)code )
563 /* we found our field's handler; read it */
565 FT_Byte* q = (FT_Byte*)parser->object + field->offset;
568 switch ( field->kind )
571 case cff_kind_string:
573 val = cff_parse_num( parser->stack );
577 val = cff_parse_fixed( parser->stack );
580 switch ( field->size )
583 *(FT_Byte*)q = (FT_Byte)val;
587 *(FT_Short*)q = (FT_Short)val;
591 *(FT_Int32*)q = (FT_Int)val;
594 default: /* for 64-bit systems where long is 8 bytes */
601 FT_Byte* qcount = (FT_Byte*)parser->object +
604 FT_Byte** data = parser->stack;
607 if ( num_args > field->array_max )
608 num_args = field->array_max;
611 *qcount = (FT_Byte)num_args;
614 while ( num_args > 0 )
616 val += cff_parse_num( data++ );
617 switch ( field->size )
620 *(FT_Byte*)q = (FT_Byte)val;
624 *(FT_Short*)q = (FT_Short)val;
628 *(FT_Int32*)q = (FT_Int)val;
631 default: /* for 64-bit systems */
641 default: /* callback */
642 error = field->reader( parser );
650 /* this is an unknown operator, or it is unsupported; */
651 /* we will ignore it for now. */
655 parser->top = parser->stack;
664 error = CFF_Err_Invalid_Argument;
668 error = CFF_Err_Invalid_Argument;
672 error = CFF_Err_Invalid_Argument;