1 /***************************************************************************/
5 /* Auxiliary functions for PostScript fonts (body). */
7 /* Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, */
9 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
11 /* This file is part of the FreeType project, and may only be used, */
12 /* modified, and distributed under the terms of the FreeType project */
13 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
14 /* this file you indicate that you have read the license and */
15 /* understand and accept it fully. */
17 /***************************************************************************/
21 #include FT_INTERNAL_POSTSCRIPT_AUX_H
22 #include FT_INTERNAL_DEBUG_H
23 #include FT_INTERNAL_CALC_H
31 /*************************************************************************/
33 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
34 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
35 /* messages during execution. */
38 #define FT_COMPONENT trace_psobjs
41 /*************************************************************************/
42 /*************************************************************************/
44 /***** PS_TABLE *****/
46 /*************************************************************************/
47 /*************************************************************************/
49 /*************************************************************************/
55 /* Initializes a PS_Table. */
58 /* table :: The address of the target table. */
61 /* count :: The table size = the maximum number of elements. */
63 /* memory :: The memory object to use for all subsequent */
67 /* FreeType error code. 0 means success. */
69 FT_LOCAL_DEF( FT_Error
)
70 ps_table_new( PS_Table table
,
77 table
->memory
= memory
;
78 if ( FT_NEW_ARRAY( table
->elements
, count
) ||
79 FT_NEW_ARRAY( table
->lengths
, count
) )
82 table
->max_elems
= count
;
83 table
->init
= 0xDEADBEEFUL
;
89 *(PS_Table_FuncsRec
*)&table
->funcs
= ps_table_funcs
;
93 FT_FREE( table
->elements
);
100 shift_elements( PS_Table table
,
103 FT_PtrDist delta
= table
->block
- old_base
;
104 FT_Byte
** offset
= table
->elements
;
105 FT_Byte
** limit
= offset
+ table
->max_elems
;
108 for ( ; offset
< limit
; offset
++ )
117 reallocate_t1_table( PS_Table table
,
120 FT_Memory memory
= table
->memory
;
121 FT_Byte
* old_base
= table
->block
;
125 /* allocate new base block */
126 if ( FT_ALLOC( table
->block
, new_size
) )
128 table
->block
= old_base
;
132 /* copy elements and shift offsets */
135 FT_MEM_COPY( table
->block
, old_base
, table
->capacity
);
136 shift_elements( table
, old_base
);
140 table
->capacity
= new_size
;
146 /*************************************************************************/
152 /* Adds an object to a PS_Table, possibly growing its memory block. */
155 /* table :: The target table. */
158 /* idx :: The index of the object in the table. */
160 /* object :: The address of the object to copy in memory. */
162 /* length :: The length in bytes of the source object. */
165 /* FreeType error code. 0 means success. An error is returned if a */
166 /* reallocation fails. */
168 FT_LOCAL_DEF( FT_Error
)
169 ps_table_add( PS_Table table
,
174 if ( idx
< 0 || idx
>= table
->max_elems
)
176 FT_ERROR(( "ps_table_add: invalid index\n" ));
177 return PSaux_Err_Invalid_Argument
;
182 FT_ERROR(( "ps_table_add: invalid length\n" ));
183 return PSaux_Err_Invalid_Argument
;
186 /* grow the base block if needed */
187 if ( table
->cursor
+ length
> table
->capacity
)
190 FT_Offset new_size
= table
->capacity
;
194 in_offset
= (FT_Long
)((FT_Byte
*)object
- table
->block
);
195 if ( (FT_ULong
)in_offset
>= table
->capacity
)
198 while ( new_size
< table
->cursor
+ length
)
200 /* increase size by 25% and round up to the nearest multiple
202 new_size
+= ( new_size
>> 2 ) + 1;
203 new_size
= FT_PAD_CEIL( new_size
, 1024 );
206 error
= reallocate_t1_table( table
, new_size
);
210 if ( in_offset
>= 0 )
211 object
= table
->block
+ in_offset
;
214 /* add the object to the base block and adjust offset */
215 table
->elements
[idx
] = table
->block
+ table
->cursor
;
216 table
->lengths
[idx
] = length
;
217 FT_MEM_COPY( table
->block
+ table
->cursor
, object
, length
);
219 table
->cursor
+= length
;
224 /*************************************************************************/
230 /* Finalizes a PS_TableRec (i.e., reallocate it to its current */
234 /* table :: The target table. */
237 /* This function does NOT release the heap's memory block. It is up */
238 /* to the caller to clean it, or reference it in its own structures. */
241 ps_table_done( PS_Table table
)
243 FT_Memory memory
= table
->memory
;
245 FT_Byte
* old_base
= table
->block
;
248 /* should never fail, because rec.cursor <= rec.size */
252 if ( FT_ALLOC( table
->block
, table
->cursor
) )
254 FT_MEM_COPY( table
->block
, old_base
, table
->cursor
);
255 shift_elements( table
, old_base
);
257 table
->capacity
= table
->cursor
;
265 ps_table_release( PS_Table table
)
267 FT_Memory memory
= table
->memory
;
270 if ( (FT_ULong
)table
->init
== 0xDEADBEEFUL
)
272 FT_FREE( table
->block
);
273 FT_FREE( table
->elements
);
274 FT_FREE( table
->lengths
);
280 /*************************************************************************/
281 /*************************************************************************/
283 /***** T1 PARSER *****/
285 /*************************************************************************/
286 /*************************************************************************/
289 /* first character must be already part of the comment */
292 skip_comment( FT_Byte
* *acur
,
295 FT_Byte
* cur
= *acur
;
298 while ( cur
< limit
)
300 if ( IS_PS_NEWLINE( *cur
) )
310 skip_spaces( FT_Byte
* *acur
,
313 FT_Byte
* cur
= *acur
;
316 while ( cur
< limit
)
318 if ( !IS_PS_SPACE( *cur
) )
321 /* According to the PLRM, a comment is equal to a space. */
322 skip_comment( &cur
, limit
);
333 #define IS_OCTAL_DIGIT( c ) ( '0' <= (c) && (c) <= '7' )
336 /* first character must be `('; */
337 /* *acur is positioned at the character after the closing `)' */
340 skip_literal_string( FT_Byte
* *acur
,
343 FT_Byte
* cur
= *acur
;
345 FT_Error error
= PSaux_Err_Invalid_File_Format
;
349 while ( cur
< limit
)
358 /* Red Book 3rd ed., section `Literal Text Strings', p. 29: */
359 /* A backslash can introduce three different types */
360 /* of escape sequences: */
361 /* - a special escaped char like \r, \n, etc. */
362 /* - a one-, two-, or three-digit octal number */
363 /* - none of the above in which case the backslash is ignored */
366 /* error (or to be ignored?) */
371 /* skip `special' escape */
384 /* skip octal escape or ignore backslash */
385 for ( i
= 0; i
< 3 && cur
< limit
; ++i
)
387 if ( !IS_OCTAL_DIGIT( *cur
) )
401 error
= PSaux_Err_Ok
;
413 /* first character must be `<' */
416 skip_string( FT_Byte
* *acur
,
419 FT_Byte
* cur
= *acur
;
420 FT_Error err
= PSaux_Err_Ok
;
423 while ( ++cur
< limit
)
425 /* All whitespace characters are ignored. */
426 skip_spaces( &cur
, limit
);
430 if ( !IS_PS_XDIGIT( *cur
) )
434 if ( cur
< limit
&& *cur
!= '>' )
436 FT_ERROR(( "skip_string: missing closing delimiter `>'\n" ));
437 err
= PSaux_Err_Invalid_File_Format
;
447 /* first character must be the opening brace that */
448 /* starts the procedure */
450 /* NB: [ and ] need not match: */
451 /* `/foo {[} def' is a valid PostScript fragment, */
452 /* even within a Type1 font */
455 skip_procedure( FT_Byte
* *acur
,
460 FT_Error error
= PSaux_Err_Ok
;
463 FT_ASSERT( **acur
== '{' );
465 for ( cur
= *acur
; cur
< limit
&& error
== PSaux_Err_Ok
; ++cur
)
483 error
= skip_literal_string( &cur
, limit
);
487 error
= skip_string( &cur
, limit
);
491 skip_comment( &cur
, limit
);
498 error
= PSaux_Err_Invalid_File_Format
;
506 /***********************************************************************/
508 /* All exported parsing routines handle leading whitespace and stop at */
509 /* the first character which isn't part of the just handled token. */
511 /***********************************************************************/
515 ps_parser_skip_PS_token( PS_Parser parser
)
517 /* Note: PostScript allows any non-delimiting, non-whitespace */
518 /* character in a name (PS Ref Manual, 3rd ed, p31). */
519 /* PostScript delimiters are (, ), <, >, [, ], {, }, /, and %. */
521 FT_Byte
* cur
= parser
->cursor
;
522 FT_Byte
* limit
= parser
->limit
;
523 FT_Error error
= PSaux_Err_Ok
;
526 skip_spaces( &cur
, limit
); /* this also skips comments */
530 /* self-delimiting, single-character tokens */
531 if ( *cur
== '[' || *cur
== ']' )
537 /* skip balanced expressions (procedures and strings) */
539 if ( *cur
== '{' ) /* {...} */
541 error
= skip_procedure( &cur
, limit
);
545 if ( *cur
== '(' ) /* (...) */
547 error
= skip_literal_string( &cur
, limit
);
551 if ( *cur
== '<' ) /* <...> */
553 if ( cur
+ 1 < limit
&& *(cur
+ 1) == '<' ) /* << */
559 error
= skip_string( &cur
, limit
);
567 if ( cur
>= limit
|| *cur
!= '>' ) /* >> */
569 FT_ERROR(( "ps_parser_skip_PS_token:"
570 " unexpected closing delimiter `>'\n" ));
571 error
= PSaux_Err_Invalid_File_Format
;
582 while ( cur
< limit
)
584 /* *cur might be invalid (e.g., ')' or '}'), but this */
585 /* is handled by the test `cur == parser->cursor' below */
586 if ( IS_PS_DELIM( *cur
) )
593 if ( cur
== parser
->cursor
)
595 FT_ERROR(( "ps_parser_skip_PS_token:"
596 " current token is `%c' which is self-delimiting\n"
598 " but invalid at this point\n",
601 error
= PSaux_Err_Invalid_File_Format
;
604 parser
->error
= error
;
605 parser
->cursor
= cur
;
610 ps_parser_skip_spaces( PS_Parser parser
)
612 skip_spaces( &parser
->cursor
, parser
->limit
);
616 /* `token' here means either something between balanced delimiters */
617 /* or the next token; the delimiters are not removed. */
620 ps_parser_to_token( PS_Parser parser
,
628 token
->type
= T1_TOKEN_TYPE_NONE
;
632 /* first of all, skip leading whitespace */
633 ps_parser_skip_spaces( parser
);
635 cur
= parser
->cursor
;
636 limit
= parser
->limit
;
643 /************* check for literal string *****************/
645 token
->type
= T1_TOKEN_TYPE_STRING
;
648 if ( skip_literal_string( &cur
, limit
) == PSaux_Err_Ok
)
652 /************* check for programs/array *****************/
654 token
->type
= T1_TOKEN_TYPE_ARRAY
;
657 if ( skip_procedure( &cur
, limit
) == PSaux_Err_Ok
)
661 /************* check for table/array ********************/
662 /* XXX: in theory we should also look for "<<" */
663 /* since this is semantically equivalent to "["; */
664 /* in practice it doesn't matter (?) */
666 token
->type
= T1_TOKEN_TYPE_ARRAY
;
668 token
->start
= cur
++;
670 /* we need this to catch `[ ]' */
671 parser
->cursor
= cur
;
672 ps_parser_skip_spaces( parser
);
673 cur
= parser
->cursor
;
675 while ( cur
< limit
&& !parser
->error
)
677 /* XXX: this is wrong because it does not */
678 /* skip comments, procedures, and strings */
681 else if ( *cur
== ']' )
686 token
->limit
= ++cur
;
691 parser
->cursor
= cur
;
692 ps_parser_skip_PS_token( parser
);
693 /* we need this to catch `[XXX ]' */
694 ps_parser_skip_spaces ( parser
);
695 cur
= parser
->cursor
;
699 /* ************ otherwise, it is any token **************/
702 token
->type
= ( *cur
== '/' ? T1_TOKEN_TYPE_KEY
: T1_TOKEN_TYPE_ANY
);
703 ps_parser_skip_PS_token( parser
);
704 cur
= parser
->cursor
;
705 if ( !parser
->error
)
712 token
->type
= T1_TOKEN_TYPE_NONE
;
715 parser
->cursor
= cur
;
719 /* NB: `tokens' can be NULL if we only want to count */
720 /* the number of array elements */
723 ps_parser_to_token_array( PS_Parser parser
,
726 FT_Int
* pnum_tokens
)
733 /* this also handles leading whitespace */
734 ps_parser_to_token( parser
, &master
);
736 if ( master
.type
== T1_TOKEN_TYPE_ARRAY
)
738 FT_Byte
* old_cursor
= parser
->cursor
;
739 FT_Byte
* old_limit
= parser
->limit
;
740 T1_Token cur
= tokens
;
741 T1_Token limit
= cur
+ max_tokens
;
744 /* don't include outermost delimiters */
745 parser
->cursor
= master
.start
+ 1;
746 parser
->limit
= master
.limit
- 1;
748 while ( parser
->cursor
< parser
->limit
)
753 ps_parser_to_token( parser
, &token
);
757 if ( tokens
!= NULL
&& cur
< limit
)
763 *pnum_tokens
= (FT_Int
)( cur
- tokens
);
765 parser
->cursor
= old_cursor
;
766 parser
->limit
= old_limit
;
771 /* first character must be a delimiter or a part of a number */
772 /* NB: `coords' can be NULL if we just want to skip the */
773 /* array; in this case we ignore `max_coords' */
776 ps_tocoordarray( FT_Byte
* *acur
,
781 FT_Byte
* cur
= *acur
;
789 /* check for the beginning of an array; otherwise, only one number */
802 /* now, read the coordinates */
803 while ( cur
< limit
)
809 /* skip whitespace in front of data */
810 skip_spaces( &cur
, limit
);
822 if ( coords
!= NULL
&& count
>= max_coords
)
825 /* call PS_Conv_ToFixed() even if coords == NULL */
826 /* to properly parse number at `cur' */
827 *( coords
!= NULL
? &coords
[count
] : &dummy
) =
828 (FT_Short
)( PS_Conv_ToFixed( &cur
, limit
, 0 ) >> 16 );
830 if ( old_cur
== cur
)
848 /* first character must be a delimiter or a part of a number */
849 /* NB: `values' can be NULL if we just want to skip the */
850 /* array; in this case we ignore `max_values' */
853 ps_tofixedarray( FT_Byte
* *acur
,
859 FT_Byte
* cur
= *acur
;
867 /* Check for the beginning of an array. Otherwise, only one number */
880 /* now, read the values */
881 while ( cur
< limit
)
887 /* skip whitespace in front of data */
888 skip_spaces( &cur
, limit
);
900 if ( values
!= NULL
&& count
>= max_values
)
903 /* call PS_Conv_ToFixed() even if coords == NULL */
904 /* to properly parse number at `cur' */
905 *( values
!= NULL
? &values
[count
] : &dummy
) =
906 PS_Conv_ToFixed( &cur
, limit
, power_ten
);
908 if ( old_cur
== cur
)
929 ps_tostring( FT_Byte
** cursor
,
933 FT_Byte
* cur
= *cursor
;
940 /* XXX: some stupid fonts have a `Notice' or `Copyright' string */
941 /* that simply doesn't begin with an opening parenthesis, even */
942 /* though they have a closing one! E.g. "amuncial.pfb" */
944 /* We must deal with these ill-fated cases there. Note that */
945 /* these fonts didn't work with the old Type 1 driver as the */
946 /* notice/copyright was not recognized as a valid string token */
947 /* and made the old token parser commit errors. */
949 while ( cur
< limit
&& ( *cur
== ' ' || *cur
== '\t' ) )
951 if ( cur
+ 1 >= limit
)
955 cur
++; /* skip the opening parenthesis, if there is one */
960 /* then, count its length */
961 for ( ; cur
< limit
; cur
++ )
966 else if ( *cur
== ')' )
975 if ( cur
>= limit
|| FT_ALLOC( result
, len
+ 1 ) )
978 /* now copy the string */
979 FT_MEM_COPY( result
, *cursor
, len
);
989 ps_tobool( FT_Byte
* *acur
,
992 FT_Byte
* cur
= *acur
;
996 /* return 1 if we find `true', 0 otherwise */
997 if ( cur
+ 3 < limit
&&
1006 else if ( cur
+ 4 < limit
&&
1022 /* load a simple field (i.e. non-table) into the current list of objects */
1024 FT_LOCAL_DEF( FT_Error
)
1025 ps_parser_load_field( PS_Parser parser
,
1026 const T1_Field field
,
1028 FT_UInt max_objects
,
1039 /* this also skips leading whitespace */
1040 ps_parser_to_token( parser
, &token
);
1047 limit
= token
.limit
;
1049 /* we must detect arrays in /FontBBox */
1050 if ( field
->type
== T1_FIELD_TYPE_BBOX
)
1053 FT_Byte
* old_cur
= parser
->cursor
;
1054 FT_Byte
* old_limit
= parser
->limit
;
1057 /* don't include delimiters */
1058 parser
->cursor
= token
.start
+ 1;
1059 parser
->limit
= token
.limit
- 1;
1061 ps_parser_to_token( parser
, &token2
);
1062 parser
->cursor
= old_cur
;
1063 parser
->limit
= old_limit
;
1065 if ( token2
.type
== T1_TOKEN_TYPE_ARRAY
)
1068 else if ( token
.type
== T1_TOKEN_TYPE_ARRAY
)
1071 /* if this is an array and we have no blend, an error occurs */
1072 if ( max_objects
== 0 )
1075 count
= max_objects
;
1078 /* don't include delimiters */
1083 for ( ; count
> 0; count
--, idx
++ )
1085 FT_Byte
* q
= (FT_Byte
*)objects
[idx
] + field
->offset
;
1090 skip_spaces( &cur
, limit
);
1092 switch ( field
->type
)
1094 case T1_FIELD_TYPE_BOOL
:
1095 val
= ps_tobool( &cur
, limit
);
1098 case T1_FIELD_TYPE_FIXED
:
1099 val
= PS_Conv_ToFixed( &cur
, limit
, 0 );
1102 case T1_FIELD_TYPE_FIXED_1000
:
1103 val
= PS_Conv_ToFixed( &cur
, limit
, 3 );
1106 case T1_FIELD_TYPE_INTEGER
:
1107 val
= PS_Conv_ToInt( &cur
, limit
);
1111 switch ( field
->size
)
1113 case (8 / FT_CHAR_BIT
):
1114 *(FT_Byte
*)q
= (FT_Byte
)val
;
1117 case (16 / FT_CHAR_BIT
):
1118 *(FT_UShort
*)q
= (FT_UShort
)val
;
1121 case (32 / FT_CHAR_BIT
):
1122 *(FT_UInt32
*)q
= (FT_UInt32
)val
;
1125 default: /* for 64-bit systems */
1130 case T1_FIELD_TYPE_STRING
:
1131 case T1_FIELD_TYPE_KEY
:
1133 FT_Memory memory
= parser
->memory
;
1134 FT_UInt len
= (FT_UInt
)( limit
- cur
);
1140 /* we allow both a string or a name */
1141 /* for cases like /FontName (foo) def */
1142 if ( token
.type
== T1_TOKEN_TYPE_KEY
)
1144 /* don't include leading `/' */
1148 else if ( token
.type
== T1_TOKEN_TYPE_STRING
)
1150 /* don't include delimiting parentheses */
1151 /* XXX we don't handle <<...>> here */
1152 /* XXX should we convert octal escapes? */
1153 /* if so, what encoding should we use? */
1159 FT_ERROR(( "ps_parser_load_field:"
1160 " expected a name or string\n"
1162 " but found token of type %d instead\n",
1164 error
= PSaux_Err_Invalid_File_Format
;
1168 /* for this to work (FT_String**)q must have been */
1169 /* initialized to NULL */
1170 if ( *(FT_String
**)q
!= NULL
)
1172 FT_TRACE0(( "ps_parser_load_field: overwriting field %s\n",
1174 FT_FREE( *(FT_String
**)q
);
1175 *(FT_String
**)q
= NULL
;
1178 if ( FT_ALLOC( string
, len
+ 1 ) )
1181 FT_MEM_COPY( string
, cur
, len
);
1184 *(FT_String
**)q
= string
;
1188 case T1_FIELD_TYPE_BBOX
:
1191 FT_BBox
* bbox
= (FT_BBox
*)q
;
1195 result
= ps_tofixedarray( &cur
, limit
, 4, temp
, 0 );
1199 FT_ERROR(( "ps_parser_load_field:"
1200 " expected four integers in bounding box\n" ));
1201 error
= PSaux_Err_Invalid_File_Format
;
1205 bbox
->xMin
= FT_RoundFix( temp
[0] );
1206 bbox
->yMin
= FT_RoundFix( temp
[1] );
1207 bbox
->xMax
= FT_RoundFix( temp
[2] );
1208 bbox
->yMax
= FT_RoundFix( temp
[3] );
1213 /* an error occurred */
1218 #if 0 /* obsolete -- keep for reference */
1220 *pflags
|= 1L << field
->flag_bit
;
1222 FT_UNUSED( pflags
);
1225 error
= PSaux_Err_Ok
;
1231 error
= PSaux_Err_Invalid_File_Format
;
1236 #define T1_MAX_TABLE_ELEMENTS 32
1239 FT_LOCAL_DEF( FT_Error
)
1240 ps_parser_load_field_table( PS_Parser parser
,
1241 const T1_Field field
,
1243 FT_UInt max_objects
,
1246 T1_TokenRec elements
[T1_MAX_TABLE_ELEMENTS
];
1248 FT_Int num_elements
;
1249 FT_Error error
= PSaux_Err_Ok
;
1250 FT_Byte
* old_cursor
;
1252 T1_FieldRec fieldrec
= *(T1_Field
)field
;
1255 fieldrec
.type
= T1_FIELD_TYPE_INTEGER
;
1256 if ( field
->type
== T1_FIELD_TYPE_FIXED_ARRAY
||
1257 field
->type
== T1_FIELD_TYPE_BBOX
)
1258 fieldrec
.type
= T1_FIELD_TYPE_FIXED
;
1260 ps_parser_to_token_array( parser
, elements
,
1261 T1_MAX_TABLE_ELEMENTS
, &num_elements
);
1262 if ( num_elements
< 0 )
1264 error
= PSaux_Err_Ignore
;
1267 if ( (FT_UInt
)num_elements
> field
->array_max
)
1268 num_elements
= field
->array_max
;
1270 old_cursor
= parser
->cursor
;
1271 old_limit
= parser
->limit
;
1273 /* we store the elements count if necessary; */
1274 /* we further assume that `count_offset' can't be zero */
1275 if ( field
->type
!= T1_FIELD_TYPE_BBOX
&& field
->count_offset
!= 0 )
1276 *(FT_Byte
*)( (FT_Byte
*)objects
[0] + field
->count_offset
) =
1277 (FT_Byte
)num_elements
;
1279 /* we now load each element, adjusting the field.offset on each one */
1281 for ( ; num_elements
> 0; num_elements
--, token
++ )
1283 parser
->cursor
= token
->start
;
1284 parser
->limit
= token
->limit
;
1285 ps_parser_load_field( parser
, &fieldrec
, objects
, max_objects
, 0 );
1286 fieldrec
.offset
+= fieldrec
.size
;
1289 #if 0 /* obsolete -- keep for reference */
1291 *pflags
|= 1L << field
->flag_bit
;
1293 FT_UNUSED( pflags
);
1296 parser
->cursor
= old_cursor
;
1297 parser
->limit
= old_limit
;
1304 FT_LOCAL_DEF( FT_Long
)
1305 ps_parser_to_int( PS_Parser parser
)
1307 ps_parser_skip_spaces( parser
);
1308 return PS_Conv_ToInt( &parser
->cursor
, parser
->limit
);
1312 /* first character must be `<' if `delimiters' is non-zero */
1314 FT_LOCAL_DEF( FT_Error
)
1315 ps_parser_to_bytes( PS_Parser parser
,
1317 FT_Offset max_bytes
,
1318 FT_Long
* pnum_bytes
,
1319 FT_Bool delimiters
)
1321 FT_Error error
= PSaux_Err_Ok
;
1325 ps_parser_skip_spaces( parser
);
1326 cur
= parser
->cursor
;
1328 if ( cur
>= parser
->limit
)
1335 FT_ERROR(( "ps_parser_to_bytes: Missing starting delimiter `<'\n" ));
1336 error
= PSaux_Err_Invalid_File_Format
;
1343 *pnum_bytes
= PS_Conv_ASCIIHexDecode( &cur
,
1350 if ( cur
< parser
->limit
&& *cur
!= '>' )
1352 FT_ERROR(( "ps_parser_to_bytes: Missing closing delimiter `>'\n" ));
1353 error
= PSaux_Err_Invalid_File_Format
;
1360 parser
->cursor
= cur
;
1367 FT_LOCAL_DEF( FT_Fixed
)
1368 ps_parser_to_fixed( PS_Parser parser
,
1371 ps_parser_skip_spaces( parser
);
1372 return PS_Conv_ToFixed( &parser
->cursor
, parser
->limit
, power_ten
);
1376 FT_LOCAL_DEF( FT_Int
)
1377 ps_parser_to_coord_array( PS_Parser parser
,
1381 ps_parser_skip_spaces( parser
);
1382 return ps_tocoordarray( &parser
->cursor
, parser
->limit
,
1383 max_coords
, coords
);
1387 FT_LOCAL_DEF( FT_Int
)
1388 ps_parser_to_fixed_array( PS_Parser parser
,
1393 ps_parser_skip_spaces( parser
);
1394 return ps_tofixedarray( &parser
->cursor
, parser
->limit
,
1395 max_values
, values
, power_ten
);
1401 FT_LOCAL_DEF( FT_String
* )
1402 T1_ToString( PS_Parser parser
)
1404 return ps_tostring( &parser
->cursor
, parser
->limit
, parser
->memory
);
1408 FT_LOCAL_DEF( FT_Bool
)
1409 T1_ToBool( PS_Parser parser
)
1411 return ps_tobool( &parser
->cursor
, parser
->limit
);
1417 FT_LOCAL_DEF( void )
1418 ps_parser_init( PS_Parser parser
,
1423 parser
->error
= PSaux_Err_Ok
;
1424 parser
->base
= base
;
1425 parser
->limit
= limit
;
1426 parser
->cursor
= base
;
1427 parser
->memory
= memory
;
1428 parser
->funcs
= ps_parser_funcs
;
1432 FT_LOCAL_DEF( void )
1433 ps_parser_done( PS_Parser parser
)
1435 FT_UNUSED( parser
);
1439 /*************************************************************************/
1440 /*************************************************************************/
1442 /***** T1 BUILDER *****/
1444 /*************************************************************************/
1445 /*************************************************************************/
1447 /*************************************************************************/
1450 /* t1_builder_init */
1453 /* Initializes a given glyph builder. */
1456 /* builder :: A pointer to the glyph builder to initialize. */
1459 /* face :: The current face object. */
1461 /* size :: The current size object. */
1463 /* glyph :: The current glyph object. */
1465 /* hinting :: Whether hinting should be applied. */
1467 FT_LOCAL_DEF( void )
1468 t1_builder_init( T1_Builder builder
,
1474 builder
->parse_state
= T1_Parse_Start
;
1475 builder
->load_points
= 1;
1477 builder
->face
= face
;
1478 builder
->glyph
= glyph
;
1479 builder
->memory
= face
->memory
;
1483 FT_GlyphLoader loader
= glyph
->internal
->loader
;
1486 builder
->loader
= loader
;
1487 builder
->base
= &loader
->base
.outline
;
1488 builder
->current
= &loader
->current
.outline
;
1489 FT_GlyphLoader_Rewind( loader
);
1491 builder
->hints_globals
= size
->internal
;
1492 builder
->hints_funcs
= 0;
1495 builder
->hints_funcs
= glyph
->internal
->glyph_hints
;
1501 builder
->left_bearing
.x
= 0;
1502 builder
->left_bearing
.y
= 0;
1503 builder
->advance
.x
= 0;
1504 builder
->advance
.y
= 0;
1506 builder
->funcs
= t1_builder_funcs
;
1510 /*************************************************************************/
1513 /* t1_builder_done */
1516 /* Finalizes a given glyph builder. Its contents can still be used */
1517 /* after the call, but the function saves important information */
1518 /* within the corresponding glyph slot. */
1521 /* builder :: A pointer to the glyph builder to finalize. */
1523 FT_LOCAL_DEF( void )
1524 t1_builder_done( T1_Builder builder
)
1526 FT_GlyphSlot glyph
= builder
->glyph
;
1530 glyph
->outline
= *builder
->base
;
1534 /* check that there is enough space for `count' more points */
1535 FT_LOCAL_DEF( FT_Error
)
1536 t1_builder_check_points( T1_Builder builder
,
1539 return FT_GLYPHLOADER_CHECK_POINTS( builder
->loader
, count
, 0 );
1543 /* add a new point, do not check space */
1544 FT_LOCAL_DEF( void )
1545 t1_builder_add_point( T1_Builder builder
,
1550 FT_Outline
* outline
= builder
->current
;
1553 if ( builder
->load_points
)
1555 FT_Vector
* point
= outline
->points
+ outline
->n_points
;
1556 FT_Byte
* control
= (FT_Byte
*)outline
->tags
+ outline
->n_points
;
1559 point
->x
= FIXED_TO_INT( x
);
1560 point
->y
= FIXED_TO_INT( y
);
1561 *control
= (FT_Byte
)( flag
? FT_CURVE_TAG_ON
: FT_CURVE_TAG_CUBIC
);
1563 outline
->n_points
++;
1567 /* check space for a new on-curve point, then add it */
1568 FT_LOCAL_DEF( FT_Error
)
1569 t1_builder_add_point1( T1_Builder builder
,
1576 error
= t1_builder_check_points( builder
, 1 );
1578 t1_builder_add_point( builder
, x
, y
, 1 );
1584 /* check space for a new contour, then add it */
1585 FT_LOCAL_DEF( FT_Error
)
1586 t1_builder_add_contour( T1_Builder builder
)
1588 FT_Outline
* outline
= builder
->current
;
1592 /* this might happen in invalid fonts */
1595 FT_ERROR(( "t1_builder_add_contour: no outline to add points to\n" ));
1596 return PSaux_Err_Invalid_File_Format
;
1599 if ( !builder
->load_points
)
1601 outline
->n_contours
++;
1602 return PSaux_Err_Ok
;
1605 error
= FT_GLYPHLOADER_CHECK_POINTS( builder
->loader
, 0, 1 );
1608 if ( outline
->n_contours
> 0 )
1609 outline
->contours
[outline
->n_contours
- 1] =
1610 (short)( outline
->n_points
- 1 );
1612 outline
->n_contours
++;
1619 /* if a path was begun, add its first on-curve point */
1620 FT_LOCAL_DEF( FT_Error
)
1621 t1_builder_start_point( T1_Builder builder
,
1625 FT_Error error
= PSaux_Err_Invalid_File_Format
;
1628 /* test whether we are building a new contour */
1630 if ( builder
->parse_state
== T1_Parse_Have_Path
)
1631 error
= PSaux_Err_Ok
;
1634 builder
->parse_state
= T1_Parse_Have_Path
;
1635 error
= t1_builder_add_contour( builder
);
1637 error
= t1_builder_add_point1( builder
, x
, y
);
1644 /* close the current contour */
1645 FT_LOCAL_DEF( void )
1646 t1_builder_close_contour( T1_Builder builder
)
1648 FT_Outline
* outline
= builder
->current
;
1655 first
= outline
->n_contours
<= 1
1656 ? 0 : outline
->contours
[outline
->n_contours
- 2] + 1;
1658 /* We must not include the last point in the path if it */
1659 /* is located on the first point. */
1660 if ( outline
->n_points
> 1 )
1662 FT_Vector
* p1
= outline
->points
+ first
;
1663 FT_Vector
* p2
= outline
->points
+ outline
->n_points
- 1;
1664 FT_Byte
* control
= (FT_Byte
*)outline
->tags
+ outline
->n_points
- 1;
1667 /* `delete' last point only if it coincides with the first */
1668 /* point and it is not a control point (which can happen). */
1669 if ( p1
->x
== p2
->x
&& p1
->y
== p2
->y
)
1670 if ( *control
== FT_CURVE_TAG_ON
)
1671 outline
->n_points
--;
1674 if ( outline
->n_contours
> 0 )
1676 /* Don't add contours only consisting of one point, i.e., */
1677 /* check whether the first and the last point is the same. */
1678 if ( first
== outline
->n_points
- 1 )
1680 outline
->n_contours
--;
1681 outline
->n_points
--;
1684 outline
->contours
[outline
->n_contours
- 1] =
1685 (short)( outline
->n_points
- 1 );
1690 /*************************************************************************/
1691 /*************************************************************************/
1695 /*************************************************************************/
1696 /*************************************************************************/
1698 FT_LOCAL_DEF( void )
1699 t1_decrypt( FT_Byte
* buffer
,
1703 PS_Conv_EexecDecode( &buffer
,