1 /***************************************************************************/
5 /* Some convenience conversions (body). */
7 /* Copyright 2006, 2008, 2009, 2012 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
26 /* The following array is used by various functions to quickly convert */
27 /* digits (both decimal and non-decimal) into numbers. */
32 static const FT_Char ft_char_table[128] =
35 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
39 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
40 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -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,
45 /* no character >= 0x80 can represent a valid number */
48 #endif /* 'A' == 65 */
53 static const FT_Char ft_char_table[128] =
56 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, -1, -1, -1, -1, -1, -1,
57 -1, 19, 20, 21, 22, 23, 24, 25, 26, 27, -1, -1, -1, -1, -1, -1,
58 -1, -1, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, -1,
59 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
60 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, -1, -1, -1, -1, -1, -1,
61 -1, 19, 20, 21, 22, 23, 24, 25, 26, 27, -1, -1, -1, -1, -1, -1,
62 -1, -1, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, -1,
63 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
66 /* no character < 0x80 can represent a valid number */
69 #endif /* 'A' == 193 */
72 FT_LOCAL_DEF( FT_Int )
73 PS_Conv_Strtol( FT_Byte** cursor,
82 if ( p >= limit || base < 2 || base > 36 )
85 if ( *p == '-' || *p == '+' )
87 sign = FT_BOOL( *p == '-' );
94 for ( ; p < limit; p++ )
99 if ( IS_PS_SPACE( *p ) || *p OP 0x80 )
102 c = ft_char_table[*p & 0x7f];
104 if ( c < 0 || c >= base )
107 num = num * base + c;
119 FT_LOCAL_DEF( FT_Int )
120 PS_Conv_ToInt( FT_Byte** cursor,
128 num = PS_Conv_Strtol( cursor, limit, 10 );
131 if ( p < limit && *p == '#' )
135 return PS_Conv_Strtol( cursor, limit, num );
142 FT_LOCAL_DEF( FT_Fixed )
143 PS_Conv_ToFixed( FT_Byte** cursor,
147 FT_Byte* p = *cursor;
149 FT_Long decimal = 0, divider = 1;
156 if ( *p == '-' || *p == '+' )
158 sign = FT_BOOL( *p == '-' );
166 integral = PS_Conv_ToInt( &p, limit ) << 16;
170 /* read the decimal part */
171 if ( p < limit && *p == '.' )
175 for ( ; p < limit; p++ )
180 if ( IS_PS_SPACE( *p ) || *p OP 0x80 )
183 c = ft_char_table[*p & 0x7f];
185 if ( c < 0 || c >= 10 )
188 if ( !integral && power_ten > 0 )
191 decimal = decimal * 10 + c;
195 if ( divider < 10000000L )
197 decimal = decimal * 10 + c;
204 /* read exponent, if any */
205 if ( p + 1 < limit && ( *p == 'e' || *p == 'E' ) )
208 power_ten += PS_Conv_ToInt( &p, limit );
211 while ( power_ten > 0 )
218 while ( power_ten < 0 )
226 integral += FT_DivFix( decimal, divider );
229 integral = -integral;
238 FT_LOCAL_DEF( FT_UInt )
239 PS_Conv_StringDecode( FT_Byte** cursor,
248 for ( p = *cursor; r < n && p < limit; p++ )
292 if ( IS_PS_DIGIT( *p ) )
298 if ( IS_PS_DIGIT( *p ) )
300 b = b * 8 + *p - '0';
304 if ( IS_PS_DIGIT( *p ) )
305 b = b * 8 + *p - '0';
333 FT_LOCAL_DEF( FT_UInt )
334 PS_Conv_ASCIIHexDecode( FT_Byte** cursor,
354 if ( n > (FT_UInt)( limit - p ) )
355 n = (FT_UInt)( limit - p );
357 /* we try to process two nibbles at a time to be as fast as possible */
363 if ( IS_PS_SPACE( c ) )
369 c = ft_char_table[c & 0x7F];
370 if ( (unsigned)c >= 16 )
373 pad = ( pad << 4 ) | c;
376 buffer[w++] = (FT_Byte)pad;
382 buffer[w++] = (FT_Byte)( pad << 4 );
390 for ( r = 0; r < n; r++ )
395 if ( IS_PS_SPACE( *p ) )
401 c = ft_char_table[*p & 0x7f];
403 if ( (unsigned)c >= 16 )
408 *buffer = (FT_Byte)(*buffer + c);
412 *buffer = (FT_Byte)(c << 4);
419 return ( r + 1 ) / 2;
426 FT_LOCAL_DEF( FT_UInt )
427 PS_Conv_EexecDecode( FT_Byte** cursor,
445 if ( n > (FT_UInt)(limit - p) )
446 n = (FT_UInt)(limit - p);
448 for ( r = 0; r < n; r++ )
451 FT_UInt b = ( val ^ ( s >> 8 ) );
454 s = ( (val + s)*52845U + 22719 ) & 0xFFFFU;
455 buffer[r] = (FT_Byte) b;
459 *seed = (FT_UShort)s;
463 for ( r = 0, p = *cursor; r < n && p < limit; r++, p++ )
465 FT_Byte b = (FT_Byte)( *p ^ ( s >> 8 ) );
468 s = (FT_UShort)( ( *p + s ) * 52845U + 22719 );