1 /***************************************************************************/
5 /* Some convenience conversions (body). */
7 /* Copyright 2006 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
28 /* The following array is used by various functions to quickly convert */
29 /* digits (both decimal and non-decimal) into numbers. */
34 static const FT_Char ft_char_table
[128] =
37 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
39 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
40 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
41 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
42 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1,
43 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
44 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1,
47 /* no character >= 0x80 can represent a valid number */
50 #endif /* 'A' == 65 */
55 static const FT_Char ft_char_table
[128] =
58 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, -1, -1, -1, -1, -1, -1,
59 -1, 19, 20, 21, 22, 23, 24, 25, 26, 27, -1, -1, -1, -1, -1, -1,
60 -1, -1, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, -1,
61 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
62 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, -1, -1, -1, -1, -1, -1,
63 -1, 19, 20, 21, 22, 23, 24, 25, 26, 27, -1, -1, -1, -1, -1, -1,
64 -1, -1, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, -1,
65 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
68 /* no character < 0x80 can represent a valid number */
71 #endif /* 'A' == 193 */
74 FT_LOCAL_DEF( FT_Int
)
75 PS_Conv_Strtol( FT_Byte
** cursor
,
84 if ( p
== limit
|| base
< 2 || base
> 36 )
87 if ( *p
== '-' || *p
== '+' )
89 sign
= FT_BOOL( *p
== '-' );
96 for ( ; p
< limit
; p
++ )
101 if ( IS_PS_SPACE( *p
) || *p OP
0x80 )
104 c
= ft_char_table
[*p
& 0x7f];
106 if ( c
< 0 || c
>= base
)
109 num
= num
* base
+ c
;
121 FT_LOCAL_DEF( FT_Int
)
122 PS_Conv_ToInt( FT_Byte
** cursor
,
130 num
= PS_Conv_Strtol( cursor
, limit
, 10 );
133 if ( p
< limit
&& *p
== '#' )
137 return PS_Conv_Strtol( cursor
, limit
, num
);
144 FT_LOCAL_DEF( FT_Fixed
)
145 PS_Conv_ToFixed( FT_Byte
** cursor
,
149 FT_Byte
* p
= *cursor
;
151 FT_Long decimal
= 0, divider
= 1;
158 if ( *p
== '-' || *p
== '+' )
160 sign
= FT_BOOL( *p
== '-' );
168 integral
= PS_Conv_ToInt( &p
, limit
) << 16;
172 /* read the decimal part */
173 if ( p
< limit
&& *p
== '.' )
177 for ( ; p
< limit
; p
++ )
182 if ( IS_PS_SPACE( *p
) || *p OP
0x80 )
185 c
= ft_char_table
[*p
& 0x7f];
187 if ( c
< 0 || c
>= 10 )
190 if ( divider
< 10000000L )
192 decimal
= decimal
* 10 + c
;
198 /* read exponent, if any */
199 if ( p
+ 1 < limit
&& ( *p
== 'e' || *p
== 'E' ) )
202 power_ten
+= PS_Conv_ToInt( &p
, limit
);
205 while ( power_ten
> 0 )
212 while ( power_ten
< 0 )
220 integral
+= FT_DivFix( decimal
, divider
);
223 integral
= -integral
;
232 FT_LOCAL_DEF( FT_UInt
)
233 PS_Conv_StringDecode( FT_Byte
** cursor
,
242 for ( p
= *cursor
; r
< n
&& p
< limit
; p
++ )
286 if ( IS_PS_DIGIT( *p
) )
292 if ( IS_PS_DIGIT( *p
) )
294 b
= b
* 8 + *p
- '0';
298 if ( IS_PS_DIGIT( *p
) )
299 b
= b
* 8 + *p
- '0';
327 FT_LOCAL_DEF( FT_UInt
)
328 PS_Conv_ASCIIHexDecode( FT_Byte
** cursor
,
344 if ( n
> (FT_UInt
)( limit
- p
) )
345 n
= (FT_UInt
)( limit
- p
);
347 /* we try to process two nibbles at a time to be as fast as possible */
353 if ( IS_PS_SPACE( c
) )
359 c
= ft_char_table
[c
& 0x7F];
360 if ( (unsigned)c
>= 16 )
363 pad
= ( pad
<< 4 ) | c
;
366 buffer
[w
++] = (FT_Byte
)pad
;
372 buffer
[w
++] = (FT_Byte
)( pad
<< 4 );
380 for ( r
= 0; r
< n
; r
++ )
385 if ( IS_PS_SPACE( *p
) )
391 c
= ft_char_table
[*p
& 0x7f];
393 if ( (unsigned)c
>= 16 )
398 *buffer
= (FT_Byte
)(*buffer
+ c
);
402 *buffer
= (FT_Byte
)(c
<< 4);
409 return ( r
+ 1 ) / 2;
416 FT_LOCAL_DEF( FT_UInt
)
417 PS_Conv_EexecDecode( FT_Byte
** cursor
,
431 if ( n
> (FT_UInt
)(limit
- p
) )
432 n
= (FT_UInt
)(limit
- p
);
434 for ( r
= 0; r
< n
; r
++ )
437 FT_UInt b
= ( val
^ ( s
>> 8 ) );
440 s
= ( (val
+ s
)*52845U + 22719 ) & 0xFFFFU
;
441 buffer
[r
] = (FT_Byte
) b
;
445 *seed
= (FT_UShort
)s
;
449 for ( r
= 0, p
= *cursor
; r
< n
&& p
< limit
; r
++, p
++ )
451 FT_Byte b
= (FT_Byte
)( *p
^ ( s
>> 8 ) );
454 s
= (FT_UShort
)( ( *p
+ s
) * 52845U + 22719 );