1 /***************************************************************************/
5 /* Auxiliary functions for PostScript fonts (body). */
7 /* Copyright 1996-2014 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 /***************************************************************************/
20 #include FT_INTERNAL_POSTSCRIPT_AUX_H
21 #include FT_INTERNAL_DEBUG_H
22 #include FT_INTERNAL_CALC_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 trace_psobjs
40 /*************************************************************************/
41 /*************************************************************************/
43 /***** PS_TABLE *****/
45 /*************************************************************************/
46 /*************************************************************************/
48 /*************************************************************************/
54 /* Initializes a PS_Table. */
57 /* table :: The address of the target table. */
60 /* count :: The table size = the maximum number of elements. */
62 /* memory :: The memory object to use for all subsequent */
66 /* FreeType error code. 0 means success. */
68 FT_LOCAL_DEF( FT_Error
)
69 ps_table_new( PS_Table table
,
76 table
->memory
= memory
;
77 if ( FT_NEW_ARRAY( table
->elements
, count
) ||
78 FT_NEW_ARRAY( table
->lengths
, count
) )
81 table
->max_elems
= count
;
82 table
->init
= 0xDEADBEEFUL
;
88 *(PS_Table_FuncsRec
*)&table
->funcs
= ps_table_funcs
;
92 FT_FREE( table
->elements
);
99 shift_elements( PS_Table table
,
102 FT_PtrDist delta
= table
->block
- old_base
;
103 FT_Byte
** offset
= table
->elements
;
104 FT_Byte
** limit
= offset
+ table
->max_elems
;
107 for ( ; offset
< limit
; offset
++ )
116 reallocate_t1_table( PS_Table table
,
119 FT_Memory memory
= table
->memory
;
120 FT_Byte
* old_base
= table
->block
;
124 /* allocate new base block */
125 if ( FT_ALLOC( table
->block
, new_size
) )
127 table
->block
= old_base
;
131 /* copy elements and shift offsets */
134 FT_MEM_COPY( table
->block
, old_base
, table
->capacity
);
135 shift_elements( table
, old_base
);
139 table
->capacity
= new_size
;
145 /*************************************************************************/
151 /* Adds an object to a PS_Table, possibly growing its memory block. */
154 /* table :: The target table. */
157 /* idx :: The index of the object in the table. */
159 /* object :: The address of the object to copy in memory. */
161 /* length :: The length in bytes of the source object. */
164 /* FreeType error code. 0 means success. An error is returned if a */
165 /* reallocation fails. */
167 FT_LOCAL_DEF( FT_Error
)
168 ps_table_add( PS_Table table
,
173 if ( idx
< 0 || idx
>= table
->max_elems
)
175 FT_ERROR(( "ps_table_add: invalid index\n" ));
176 return FT_THROW( Invalid_Argument
);
181 FT_ERROR(( "ps_table_add: invalid length\n" ));
182 return FT_THROW( Invalid_Argument
);
185 /* grow the base block if needed */
186 if ( table
->cursor
+ length
> table
->capacity
)
189 FT_Offset new_size
= table
->capacity
;
190 FT_PtrDist in_offset
;
193 in_offset
= (FT_Byte
*)object
- table
->block
;
194 if ( in_offset
< 0 || (FT_Offset
)in_offset
>= table
->capacity
)
197 while ( new_size
< table
->cursor
+ length
)
199 /* increase size by 25% and round up to the nearest multiple
201 new_size
+= ( new_size
>> 2 ) + 1;
202 new_size
= FT_PAD_CEIL( new_size
, 1024 );
205 error
= reallocate_t1_table( table
, new_size
);
209 if ( in_offset
>= 0 )
210 object
= table
->block
+ in_offset
;
213 /* add the object to the base block and adjust offset */
214 table
->elements
[idx
] = table
->block
+ table
->cursor
;
215 table
->lengths
[idx
] = length
;
216 FT_MEM_COPY( table
->block
+ table
->cursor
, object
, length
);
218 table
->cursor
+= length
;
223 /*************************************************************************/
229 /* Finalizes a PS_TableRec (i.e., reallocate it to its current */
233 /* table :: The target table. */
236 /* This function does NOT release the heap's memory block. It is up */
237 /* to the caller to clean it, or reference it in its own structures. */
240 ps_table_done( PS_Table table
)
242 FT_Memory memory
= table
->memory
;
244 FT_Byte
* old_base
= table
->block
;
247 /* should never fail, because rec.cursor <= rec.size */
251 if ( FT_ALLOC( table
->block
, table
->cursor
) )
253 FT_MEM_COPY( table
->block
, old_base
, table
->cursor
);
254 shift_elements( table
, old_base
);
256 table
->capacity
= table
->cursor
;
264 ps_table_release( PS_Table table
)
266 FT_Memory memory
= table
->memory
;
269 if ( (FT_ULong
)table
->init
== 0xDEADBEEFUL
)
271 FT_FREE( table
->block
);
272 FT_FREE( table
->elements
);
273 FT_FREE( table
->lengths
);
279 /*************************************************************************/
280 /*************************************************************************/
282 /***** T1 PARSER *****/
284 /*************************************************************************/
285 /*************************************************************************/
288 /* first character must be already part of the comment */
291 skip_comment( FT_Byte
* *acur
,
294 FT_Byte
* cur
= *acur
;
297 while ( cur
< limit
)
299 if ( IS_PS_NEWLINE( *cur
) )
309 skip_spaces( FT_Byte
* *acur
,
312 FT_Byte
* cur
= *acur
;
315 while ( cur
< limit
)
317 if ( !IS_PS_SPACE( *cur
) )
320 /* According to the PLRM, a comment is equal to a space. */
321 skip_comment( &cur
, limit
);
332 #define IS_OCTAL_DIGIT( c ) ( '0' <= (c) && (c) <= '7' )
335 /* first character must be `('; */
336 /* *acur is positioned at the character after the closing `)' */
339 skip_literal_string( FT_Byte
* *acur
,
342 FT_Byte
* cur
= *acur
;
344 FT_Error error
= FT_ERR( Invalid_File_Format
);
348 while ( cur
< limit
)
357 /* Red Book 3rd ed., section `Literal Text Strings', p. 29: */
358 /* A backslash can introduce three different types */
359 /* of escape sequences: */
360 /* - a special escaped char like \r, \n, etc. */
361 /* - a one-, two-, or three-digit octal number */
362 /* - none of the above in which case the backslash is ignored */
365 /* error (or to be ignored?) */
370 /* skip `special' escape */
383 /* skip octal escape or ignore backslash */
384 for ( i
= 0; i
< 3 && cur
< limit
; ++i
)
386 if ( !IS_OCTAL_DIGIT( *cur
) )
412 /* first character must be `<' */
415 skip_string( FT_Byte
* *acur
,
418 FT_Byte
* cur
= *acur
;
419 FT_Error err
= FT_Err_Ok
;
422 while ( ++cur
< limit
)
424 /* All whitespace characters are ignored. */
425 skip_spaces( &cur
, limit
);
429 if ( !IS_PS_XDIGIT( *cur
) )
433 if ( cur
< limit
&& *cur
!= '>' )
435 FT_ERROR(( "skip_string: missing closing delimiter `>'\n" ));
436 err
= FT_THROW( Invalid_File_Format
);
446 /* first character must be the opening brace that */
447 /* starts the procedure */
449 /* NB: [ and ] need not match: */
450 /* `/foo {[} def' is a valid PostScript fragment, */
451 /* even within a Type1 font */
454 skip_procedure( FT_Byte
* *acur
,
459 FT_Error error
= FT_Err_Ok
;
462 FT_ASSERT( **acur
== '{' );
464 for ( cur
= *acur
; cur
< limit
&& error
== FT_Err_Ok
; ++cur
)
482 error
= skip_literal_string( &cur
, limit
);
486 error
= skip_string( &cur
, limit
);
490 skip_comment( &cur
, limit
);
497 error
= FT_THROW( Invalid_File_Format
);
505 /***********************************************************************/
507 /* All exported parsing routines handle leading whitespace and stop at */
508 /* the first character which isn't part of the just handled token. */
510 /***********************************************************************/
514 ps_parser_skip_PS_token( PS_Parser parser
)
516 /* Note: PostScript allows any non-delimiting, non-whitespace */
517 /* character in a name (PS Ref Manual, 3rd ed, p31). */
518 /* PostScript delimiters are (, ), <, >, [, ], {, }, /, and %. */
520 FT_Byte
* cur
= parser
->cursor
;
521 FT_Byte
* limit
= parser
->limit
;
522 FT_Error error
= FT_Err_Ok
;
525 skip_spaces( &cur
, limit
); /* this also skips comments */
529 /* self-delimiting, single-character tokens */
530 if ( *cur
== '[' || *cur
== ']' )
536 /* skip balanced expressions (procedures and strings) */
538 if ( *cur
== '{' ) /* {...} */
540 error
= skip_procedure( &cur
, limit
);
544 if ( *cur
== '(' ) /* (...) */
546 error
= skip_literal_string( &cur
, limit
);
550 if ( *cur
== '<' ) /* <...> */
552 if ( cur
+ 1 < limit
&& *(cur
+ 1) == '<' ) /* << */
558 error
= skip_string( &cur
, limit
);
566 if ( cur
>= limit
|| *cur
!= '>' ) /* >> */
568 FT_ERROR(( "ps_parser_skip_PS_token:"
569 " unexpected closing delimiter `>'\n" ));
570 error
= FT_THROW( Invalid_File_Format
);
581 while ( cur
< limit
)
583 /* *cur might be invalid (e.g., ')' or '}'), but this */
584 /* is handled by the test `cur == parser->cursor' below */
585 if ( IS_PS_DELIM( *cur
) )
592 if ( cur
< limit
&& cur
== parser
->cursor
)
594 FT_ERROR(( "ps_parser_skip_PS_token:"
595 " current token is `%c' which is self-delimiting\n"
597 " but invalid at this point\n",
600 error
= FT_THROW( Invalid_File_Format
);
603 parser
->error
= error
;
604 parser
->cursor
= cur
;
609 ps_parser_skip_spaces( PS_Parser parser
)
611 skip_spaces( &parser
->cursor
, parser
->limit
);
615 /* `token' here means either something between balanced delimiters */
616 /* or the next token; the delimiters are not removed. */
619 ps_parser_to_token( PS_Parser parser
,
627 token
->type
= T1_TOKEN_TYPE_NONE
;
631 /* first of all, skip leading whitespace */
632 ps_parser_skip_spaces( parser
);
634 cur
= parser
->cursor
;
635 limit
= parser
->limit
;
642 /************* check for literal string *****************/
644 token
->type
= T1_TOKEN_TYPE_STRING
;
647 if ( skip_literal_string( &cur
, limit
) == FT_Err_Ok
)
651 /************* check for programs/array *****************/
653 token
->type
= T1_TOKEN_TYPE_ARRAY
;
656 if ( skip_procedure( &cur
, limit
) == FT_Err_Ok
)
660 /************* check for table/array ********************/
661 /* XXX: in theory we should also look for "<<" */
662 /* since this is semantically equivalent to "["; */
663 /* in practice it doesn't matter (?) */
665 token
->type
= T1_TOKEN_TYPE_ARRAY
;
667 token
->start
= cur
++;
669 /* we need this to catch `[ ]' */
670 parser
->cursor
= cur
;
671 ps_parser_skip_spaces( parser
);
672 cur
= parser
->cursor
;
674 while ( cur
< limit
&& !parser
->error
)
676 /* XXX: this is wrong because it does not */
677 /* skip comments, procedures, and strings */
680 else if ( *cur
== ']' )
685 token
->limit
= ++cur
;
690 parser
->cursor
= cur
;
691 ps_parser_skip_PS_token( parser
);
692 /* we need this to catch `[XXX ]' */
693 ps_parser_skip_spaces ( parser
);
694 cur
= parser
->cursor
;
698 /* ************ otherwise, it is any token **************/
701 token
->type
= ( *cur
== '/' ? T1_TOKEN_TYPE_KEY
: T1_TOKEN_TYPE_ANY
);
702 ps_parser_skip_PS_token( parser
);
703 cur
= parser
->cursor
;
704 if ( !parser
->error
)
711 token
->type
= T1_TOKEN_TYPE_NONE
;
714 parser
->cursor
= cur
;
718 /* NB: `tokens' can be NULL if we only want to count */
719 /* the number of array elements */
722 ps_parser_to_token_array( PS_Parser parser
,
725 FT_Int
* pnum_tokens
)
732 /* this also handles leading whitespace */
733 ps_parser_to_token( parser
, &master
);
735 if ( master
.type
== T1_TOKEN_TYPE_ARRAY
)
737 FT_Byte
* old_cursor
= parser
->cursor
;
738 FT_Byte
* old_limit
= parser
->limit
;
739 T1_Token cur
= tokens
;
740 T1_Token limit
= cur
+ max_tokens
;
743 /* don't include outermost delimiters */
744 parser
->cursor
= master
.start
+ 1;
745 parser
->limit
= master
.limit
- 1;
747 while ( parser
->cursor
< parser
->limit
)
752 ps_parser_to_token( parser
, &token
);
756 if ( tokens
!= NULL
&& cur
< limit
)
762 *pnum_tokens
= (FT_Int
)( cur
- tokens
);
764 parser
->cursor
= old_cursor
;
765 parser
->limit
= old_limit
;
770 /* first character must be a delimiter or a part of a number */
771 /* NB: `coords' can be NULL if we just want to skip the */
772 /* array; in this case we ignore `max_coords' */
775 ps_tocoordarray( FT_Byte
* *acur
,
780 FT_Byte
* cur
= *acur
;
788 /* check for the beginning of an array; otherwise, only one number */
801 /* now, read the coordinates */
802 while ( cur
< limit
)
808 /* skip whitespace in front of data */
809 skip_spaces( &cur
, limit
);
821 if ( coords
!= NULL
&& count
>= max_coords
)
824 /* call PS_Conv_ToFixed() even if coords == NULL */
825 /* to properly parse number at `cur' */
826 *( coords
!= NULL
? &coords
[count
] : &dummy
) =
827 (FT_Short
)( PS_Conv_ToFixed( &cur
, limit
, 0 ) >> 16 );
829 if ( old_cur
== cur
)
847 /* first character must be a delimiter or a part of a number */
848 /* NB: `values' can be NULL if we just want to skip the */
849 /* array; in this case we ignore `max_values' */
851 /* return number of successfully parsed values */
854 ps_tofixedarray( FT_Byte
* *acur
,
860 FT_Byte
* cur
= *acur
;
868 /* Check for the beginning of an array. Otherwise, only one number */
881 /* now, read the values */
882 while ( cur
< limit
)
888 /* skip whitespace in front of data */
889 skip_spaces( &cur
, limit
);
901 if ( values
!= NULL
&& count
>= max_values
)
904 /* call PS_Conv_ToFixed() even if coords == NULL */
905 /* to properly parse number at `cur' */
906 *( values
!= NULL
? &values
[count
] : &dummy
) =
907 PS_Conv_ToFixed( &cur
, limit
, power_ten
);
909 if ( old_cur
== cur
)
930 ps_tostring( FT_Byte
** cursor
,
934 FT_Byte
* cur
= *cursor
;
941 /* XXX: some stupid fonts have a `Notice' or `Copyright' string */
942 /* that simply doesn't begin with an opening parenthesis, even */
943 /* though they have a closing one! E.g. "amuncial.pfb" */
945 /* We must deal with these ill-fated cases there. Note that */
946 /* these fonts didn't work with the old Type 1 driver as the */
947 /* notice/copyright was not recognized as a valid string token */
948 /* and made the old token parser commit errors. */
950 while ( cur
< limit
&& ( *cur
== ' ' || *cur
== '\t' ) )
952 if ( cur
+ 1 >= limit
)
956 cur
++; /* skip the opening parenthesis, if there is one */
961 /* then, count its length */
962 for ( ; cur
< limit
; cur
++ )
967 else if ( *cur
== ')' )
976 if ( cur
>= limit
|| FT_ALLOC( result
, len
+ 1 ) )
979 /* now copy the string */
980 FT_MEM_COPY( result
, *cursor
, len
);
990 ps_tobool( FT_Byte
* *acur
,
993 FT_Byte
* cur
= *acur
;
997 /* return 1 if we find `true', 0 otherwise */
998 if ( cur
+ 3 < limit
&&
1007 else if ( cur
+ 4 < limit
&&
1023 /* load a simple field (i.e. non-table) into the current list of objects */
1025 FT_LOCAL_DEF( FT_Error
)
1026 ps_parser_load_field( PS_Parser parser
,
1027 const T1_Field field
,
1029 FT_UInt max_objects
,
1041 /* this also skips leading whitespace */
1042 ps_parser_to_token( parser
, &token
);
1049 limit
= token
.limit
;
1053 /* we must detect arrays in /FontBBox */
1054 if ( type
== T1_FIELD_TYPE_BBOX
)
1057 FT_Byte
* old_cur
= parser
->cursor
;
1058 FT_Byte
* old_limit
= parser
->limit
;
1061 /* don't include delimiters */
1062 parser
->cursor
= token
.start
+ 1;
1063 parser
->limit
= token
.limit
- 1;
1065 ps_parser_to_token( parser
, &token2
);
1066 parser
->cursor
= old_cur
;
1067 parser
->limit
= old_limit
;
1069 if ( token2
.type
== T1_TOKEN_TYPE_ARRAY
)
1071 type
= T1_FIELD_TYPE_MM_BBOX
;
1075 else if ( token
.type
== T1_TOKEN_TYPE_ARRAY
)
1077 count
= max_objects
;
1080 /* if this is an array and we have no blend, an error occurs */
1081 if ( max_objects
== 0 )
1086 /* don't include delimiters */
1091 for ( ; count
> 0; count
--, idx
++ )
1093 FT_Byte
* q
= (FT_Byte
*)objects
[idx
] + field
->offset
;
1098 skip_spaces( &cur
, limit
);
1102 case T1_FIELD_TYPE_BOOL
:
1103 val
= ps_tobool( &cur
, limit
);
1106 case T1_FIELD_TYPE_FIXED
:
1107 val
= PS_Conv_ToFixed( &cur
, limit
, 0 );
1110 case T1_FIELD_TYPE_FIXED_1000
:
1111 val
= PS_Conv_ToFixed( &cur
, limit
, 3 );
1114 case T1_FIELD_TYPE_INTEGER
:
1115 val
= PS_Conv_ToInt( &cur
, limit
);
1119 switch ( field
->size
)
1121 case (8 / FT_CHAR_BIT
):
1122 *(FT_Byte
*)q
= (FT_Byte
)val
;
1125 case (16 / FT_CHAR_BIT
):
1126 *(FT_UShort
*)q
= (FT_UShort
)val
;
1129 case (32 / FT_CHAR_BIT
):
1130 *(FT_UInt32
*)q
= (FT_UInt32
)val
;
1133 default: /* for 64-bit systems */
1138 case T1_FIELD_TYPE_STRING
:
1139 case T1_FIELD_TYPE_KEY
:
1141 FT_Memory memory
= parser
->memory
;
1142 FT_UInt len
= (FT_UInt
)( limit
- cur
);
1148 /* we allow both a string or a name */
1149 /* for cases like /FontName (foo) def */
1150 if ( token
.type
== T1_TOKEN_TYPE_KEY
)
1152 /* don't include leading `/' */
1156 else if ( token
.type
== T1_TOKEN_TYPE_STRING
)
1158 /* don't include delimiting parentheses */
1159 /* XXX we don't handle <<...>> here */
1160 /* XXX should we convert octal escapes? */
1161 /* if so, what encoding should we use? */
1167 FT_ERROR(( "ps_parser_load_field:"
1168 " expected a name or string\n"
1170 " but found token of type %d instead\n",
1172 error
= FT_THROW( Invalid_File_Format
);
1176 /* for this to work (FT_String**)q must have been */
1177 /* initialized to NULL */
1178 if ( *(FT_String
**)q
!= NULL
)
1180 FT_TRACE0(( "ps_parser_load_field: overwriting field %s\n",
1182 FT_FREE( *(FT_String
**)q
);
1183 *(FT_String
**)q
= NULL
;
1186 if ( FT_ALLOC( string
, len
+ 1 ) )
1189 FT_MEM_COPY( string
, cur
, len
);
1192 *(FT_String
**)q
= string
;
1196 case T1_FIELD_TYPE_BBOX
:
1199 FT_BBox
* bbox
= (FT_BBox
*)q
;
1203 result
= ps_tofixedarray( &cur
, limit
, 4, temp
, 0 );
1207 FT_ERROR(( "ps_parser_load_field:"
1208 " expected four integers in bounding box\n" ));
1209 error
= FT_THROW( Invalid_File_Format
);
1213 bbox
->xMin
= FT_RoundFix( temp
[0] );
1214 bbox
->yMin
= FT_RoundFix( temp
[1] );
1215 bbox
->xMax
= FT_RoundFix( temp
[2] );
1216 bbox
->yMax
= FT_RoundFix( temp
[3] );
1220 case T1_FIELD_TYPE_MM_BBOX
:
1222 FT_Memory memory
= parser
->memory
;
1228 if ( FT_NEW_ARRAY( temp
, max_objects
* 4 ) )
1231 for ( i
= 0; i
< 4; i
++ )
1233 result
= ps_tofixedarray( &cur
, limit
, max_objects
,
1234 temp
+ i
* max_objects
, 0 );
1235 if ( result
< 0 || (FT_UInt
)result
< max_objects
)
1237 FT_ERROR(( "ps_parser_load_field:"
1238 " expected %d integers in the %s subarray\n"
1240 " of /FontBBox in the /Blend dictionary\n",
1243 : ( i
== 1 ? "second"
1244 : ( i
== 2 ? "third"
1246 error
= FT_THROW( Invalid_File_Format
);
1250 skip_spaces( &cur
, limit
);
1253 for ( i
= 0; i
< max_objects
; i
++ )
1255 FT_BBox
* bbox
= (FT_BBox
*)objects
[i
];
1258 bbox
->xMin
= FT_RoundFix( temp
[i
] );
1259 bbox
->yMin
= FT_RoundFix( temp
[i
+ max_objects
] );
1260 bbox
->xMax
= FT_RoundFix( temp
[i
+ 2 * max_objects
] );
1261 bbox
->yMax
= FT_RoundFix( temp
[i
+ 3 * max_objects
] );
1269 /* an error occurred */
1274 #if 0 /* obsolete -- keep for reference */
1276 *pflags
|= 1L << field
->flag_bit
;
1278 FT_UNUSED( pflags
);
1287 error
= FT_THROW( Invalid_File_Format
);
1292 #define T1_MAX_TABLE_ELEMENTS 32
1295 FT_LOCAL_DEF( FT_Error
)
1296 ps_parser_load_field_table( PS_Parser parser
,
1297 const T1_Field field
,
1299 FT_UInt max_objects
,
1302 T1_TokenRec elements
[T1_MAX_TABLE_ELEMENTS
];
1304 FT_Int num_elements
;
1305 FT_Error error
= FT_Err_Ok
;
1306 FT_Byte
* old_cursor
;
1308 T1_FieldRec fieldrec
= *(T1_Field
)field
;
1311 fieldrec
.type
= T1_FIELD_TYPE_INTEGER
;
1312 if ( field
->type
== T1_FIELD_TYPE_FIXED_ARRAY
||
1313 field
->type
== T1_FIELD_TYPE_BBOX
)
1314 fieldrec
.type
= T1_FIELD_TYPE_FIXED
;
1316 ps_parser_to_token_array( parser
, elements
,
1317 T1_MAX_TABLE_ELEMENTS
, &num_elements
);
1318 if ( num_elements
< 0 )
1320 error
= FT_ERR( Ignore
);
1323 if ( (FT_UInt
)num_elements
> field
->array_max
)
1324 num_elements
= field
->array_max
;
1326 old_cursor
= parser
->cursor
;
1327 old_limit
= parser
->limit
;
1329 /* we store the elements count if necessary; */
1330 /* we further assume that `count_offset' can't be zero */
1331 if ( field
->type
!= T1_FIELD_TYPE_BBOX
&& field
->count_offset
!= 0 )
1332 *(FT_Byte
*)( (FT_Byte
*)objects
[0] + field
->count_offset
) =
1333 (FT_Byte
)num_elements
;
1335 /* we now load each element, adjusting the field.offset on each one */
1337 for ( ; num_elements
> 0; num_elements
--, token
++ )
1339 parser
->cursor
= token
->start
;
1340 parser
->limit
= token
->limit
;
1341 ps_parser_load_field( parser
, &fieldrec
, objects
, max_objects
, 0 );
1342 fieldrec
.offset
+= fieldrec
.size
;
1345 #if 0 /* obsolete -- keep for reference */
1347 *pflags
|= 1L << field
->flag_bit
;
1349 FT_UNUSED( pflags
);
1352 parser
->cursor
= old_cursor
;
1353 parser
->limit
= old_limit
;
1360 FT_LOCAL_DEF( FT_Long
)
1361 ps_parser_to_int( PS_Parser parser
)
1363 ps_parser_skip_spaces( parser
);
1364 return PS_Conv_ToInt( &parser
->cursor
, parser
->limit
);
1368 /* first character must be `<' if `delimiters' is non-zero */
1370 FT_LOCAL_DEF( FT_Error
)
1371 ps_parser_to_bytes( PS_Parser parser
,
1373 FT_Offset max_bytes
,
1374 FT_Long
* pnum_bytes
,
1375 FT_Bool delimiters
)
1377 FT_Error error
= FT_Err_Ok
;
1381 ps_parser_skip_spaces( parser
);
1382 cur
= parser
->cursor
;
1384 if ( cur
>= parser
->limit
)
1391 FT_ERROR(( "ps_parser_to_bytes: Missing starting delimiter `<'\n" ));
1392 error
= FT_THROW( Invalid_File_Format
);
1399 *pnum_bytes
= PS_Conv_ASCIIHexDecode( &cur
,
1406 if ( cur
< parser
->limit
&& *cur
!= '>' )
1408 FT_ERROR(( "ps_parser_to_bytes: Missing closing delimiter `>'\n" ));
1409 error
= FT_THROW( Invalid_File_Format
);
1416 parser
->cursor
= cur
;
1423 FT_LOCAL_DEF( FT_Fixed
)
1424 ps_parser_to_fixed( PS_Parser parser
,
1427 ps_parser_skip_spaces( parser
);
1428 return PS_Conv_ToFixed( &parser
->cursor
, parser
->limit
, power_ten
);
1432 FT_LOCAL_DEF( FT_Int
)
1433 ps_parser_to_coord_array( PS_Parser parser
,
1437 ps_parser_skip_spaces( parser
);
1438 return ps_tocoordarray( &parser
->cursor
, parser
->limit
,
1439 max_coords
, coords
);
1443 FT_LOCAL_DEF( FT_Int
)
1444 ps_parser_to_fixed_array( PS_Parser parser
,
1449 ps_parser_skip_spaces( parser
);
1450 return ps_tofixedarray( &parser
->cursor
, parser
->limit
,
1451 max_values
, values
, power_ten
);
1457 FT_LOCAL_DEF( FT_String
* )
1458 T1_ToString( PS_Parser parser
)
1460 return ps_tostring( &parser
->cursor
, parser
->limit
, parser
->memory
);
1464 FT_LOCAL_DEF( FT_Bool
)
1465 T1_ToBool( PS_Parser parser
)
1467 return ps_tobool( &parser
->cursor
, parser
->limit
);
1473 FT_LOCAL_DEF( void )
1474 ps_parser_init( PS_Parser parser
,
1479 parser
->error
= FT_Err_Ok
;
1480 parser
->base
= base
;
1481 parser
->limit
= limit
;
1482 parser
->cursor
= base
;
1483 parser
->memory
= memory
;
1484 parser
->funcs
= ps_parser_funcs
;
1488 FT_LOCAL_DEF( void )
1489 ps_parser_done( PS_Parser parser
)
1491 FT_UNUSED( parser
);
1495 /*************************************************************************/
1496 /*************************************************************************/
1498 /***** T1 BUILDER *****/
1500 /*************************************************************************/
1501 /*************************************************************************/
1503 /*************************************************************************/
1506 /* t1_builder_init */
1509 /* Initializes a given glyph builder. */
1512 /* builder :: A pointer to the glyph builder to initialize. */
1515 /* face :: The current face object. */
1517 /* size :: The current size object. */
1519 /* glyph :: The current glyph object. */
1521 /* hinting :: Whether hinting should be applied. */
1523 FT_LOCAL_DEF( void )
1524 t1_builder_init( T1_Builder builder
,
1530 builder
->parse_state
= T1_Parse_Start
;
1531 builder
->load_points
= 1;
1533 builder
->face
= face
;
1534 builder
->glyph
= glyph
;
1535 builder
->memory
= face
->memory
;
1539 FT_GlyphLoader loader
= glyph
->internal
->loader
;
1542 builder
->loader
= loader
;
1543 builder
->base
= &loader
->base
.outline
;
1544 builder
->current
= &loader
->current
.outline
;
1545 FT_GlyphLoader_Rewind( loader
);
1547 builder
->hints_globals
= size
->internal
;
1548 builder
->hints_funcs
= 0;
1551 builder
->hints_funcs
= glyph
->internal
->glyph_hints
;
1557 builder
->left_bearing
.x
= 0;
1558 builder
->left_bearing
.y
= 0;
1559 builder
->advance
.x
= 0;
1560 builder
->advance
.y
= 0;
1562 builder
->funcs
= t1_builder_funcs
;
1566 /*************************************************************************/
1569 /* t1_builder_done */
1572 /* Finalizes a given glyph builder. Its contents can still be used */
1573 /* after the call, but the function saves important information */
1574 /* within the corresponding glyph slot. */
1577 /* builder :: A pointer to the glyph builder to finalize. */
1579 FT_LOCAL_DEF( void )
1580 t1_builder_done( T1_Builder builder
)
1582 FT_GlyphSlot glyph
= builder
->glyph
;
1586 glyph
->outline
= *builder
->base
;
1590 /* check that there is enough space for `count' more points */
1591 FT_LOCAL_DEF( FT_Error
)
1592 t1_builder_check_points( T1_Builder builder
,
1595 return FT_GLYPHLOADER_CHECK_POINTS( builder
->loader
, count
, 0 );
1599 /* add a new point, do not check space */
1600 FT_LOCAL_DEF( void )
1601 t1_builder_add_point( T1_Builder builder
,
1606 FT_Outline
* outline
= builder
->current
;
1609 if ( builder
->load_points
)
1611 FT_Vector
* point
= outline
->points
+ outline
->n_points
;
1612 FT_Byte
* control
= (FT_Byte
*)outline
->tags
+ outline
->n_points
;
1615 point
->x
= FIXED_TO_INT( x
);
1616 point
->y
= FIXED_TO_INT( y
);
1617 *control
= (FT_Byte
)( flag
? FT_CURVE_TAG_ON
: FT_CURVE_TAG_CUBIC
);
1619 outline
->n_points
++;
1623 /* check space for a new on-curve point, then add it */
1624 FT_LOCAL_DEF( FT_Error
)
1625 t1_builder_add_point1( T1_Builder builder
,
1632 error
= t1_builder_check_points( builder
, 1 );
1634 t1_builder_add_point( builder
, x
, y
, 1 );
1640 /* check space for a new contour, then add it */
1641 FT_LOCAL_DEF( FT_Error
)
1642 t1_builder_add_contour( T1_Builder builder
)
1644 FT_Outline
* outline
= builder
->current
;
1648 /* this might happen in invalid fonts */
1651 FT_ERROR(( "t1_builder_add_contour: no outline to add points to\n" ));
1652 return FT_THROW( Invalid_File_Format
);
1655 if ( !builder
->load_points
)
1657 outline
->n_contours
++;
1661 error
= FT_GLYPHLOADER_CHECK_POINTS( builder
->loader
, 0, 1 );
1664 if ( outline
->n_contours
> 0 )
1665 outline
->contours
[outline
->n_contours
- 1] =
1666 (short)( outline
->n_points
- 1 );
1668 outline
->n_contours
++;
1675 /* if a path was begun, add its first on-curve point */
1676 FT_LOCAL_DEF( FT_Error
)
1677 t1_builder_start_point( T1_Builder builder
,
1681 FT_Error error
= FT_ERR( Invalid_File_Format
);
1684 /* test whether we are building a new contour */
1686 if ( builder
->parse_state
== T1_Parse_Have_Path
)
1690 builder
->parse_state
= T1_Parse_Have_Path
;
1691 error
= t1_builder_add_contour( builder
);
1693 error
= t1_builder_add_point1( builder
, x
, y
);
1700 /* close the current contour */
1701 FT_LOCAL_DEF( void )
1702 t1_builder_close_contour( T1_Builder builder
)
1704 FT_Outline
* outline
= builder
->current
;
1711 first
= outline
->n_contours
<= 1
1712 ? 0 : outline
->contours
[outline
->n_contours
- 2] + 1;
1714 /* We must not include the last point in the path if it */
1715 /* is located on the first point. */
1716 if ( outline
->n_points
> 1 )
1718 FT_Vector
* p1
= outline
->points
+ first
;
1719 FT_Vector
* p2
= outline
->points
+ outline
->n_points
- 1;
1720 FT_Byte
* control
= (FT_Byte
*)outline
->tags
+ outline
->n_points
- 1;
1723 /* `delete' last point only if it coincides with the first */
1724 /* point and it is not a control point (which can happen). */
1725 if ( p1
->x
== p2
->x
&& p1
->y
== p2
->y
)
1726 if ( *control
== FT_CURVE_TAG_ON
)
1727 outline
->n_points
--;
1730 if ( outline
->n_contours
> 0 )
1732 /* Don't add contours only consisting of one point, i.e., */
1733 /* check whether the first and the last point is the same. */
1734 if ( first
== outline
->n_points
- 1 )
1736 outline
->n_contours
--;
1737 outline
->n_points
--;
1740 outline
->contours
[outline
->n_contours
- 1] =
1741 (short)( outline
->n_points
- 1 );
1746 /*************************************************************************/
1747 /*************************************************************************/
1751 /*************************************************************************/
1752 /*************************************************************************/
1754 FT_LOCAL_DEF( void )
1755 t1_decrypt( FT_Byte
* buffer
,
1759 PS_Conv_EexecDecode( &buffer
,