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
21 #include FT_INTERNAL_DEBUG_H
27 /*************************************************************************/
29 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
30 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
31 /* messages during execution. */
34 #define FT_COMPONENT trace_psconv
37 /* The following array is used by various functions to quickly convert */
38 /* digits (both decimal and non-decimal) into numbers. */
43 static const FT_Char ft_char_table[128] =
46 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
47 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
48 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
49 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
50 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
51 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1,
52 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
53 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1,
56 /* no character >= 0x80 can represent a valid number */
59 #endif /* 'A' == 65 */
64 static const FT_Char ft_char_table[128] =
67 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, -1, -1, -1, -1, -1, -1,
68 -1, 19, 20, 21, 22, 23, 24, 25, 26, 27, -1, -1, -1, -1, -1, -1,
69 -1, -1, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, -1,
70 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
71 -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, -1, -1, -1, -1, -1, -1,
72 -1, 19, 20, 21, 22, 23, 24, 25, 26, 27, -1, -1, -1, -1, -1, -1,
73 -1, -1, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, -1,
74 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
77 /* no character < 0x80 can represent a valid number */
80 #endif /* 'A' == 193 */
83 FT_LOCAL_DEF( FT_Long )
84 PS_Conv_Strtol( FT_Byte** cursor,
92 FT_Bool have_overflow = 0;
101 if ( base < 2 || base > 36 )
103 FT_TRACE4(( "!!!INVALID BASE:!!!" ));
107 if ( *p == '-' || *p == '+' )
109 sign = FT_BOOL( *p == '-' );
116 num_limit = 0x7FFFFFFFL / base;
117 c_limit = (FT_Char)( 0x7FFFFFFFL % base );
119 for ( ; p < limit; p++ )
124 if ( IS_PS_SPACE( *p ) || *p OP 0x80 )
127 c = ft_char_table[*p & 0x7f];
129 if ( c < 0 || c >= base )
132 if ( num > num_limit || ( num == num_limit && c > c_limit ) )
135 num = num * base + c;
143 FT_TRACE4(( "!!!OVERFLOW:!!!" ));
152 FT_TRACE4(( "!!!END OF DATA:!!!" ));
157 FT_LOCAL_DEF( FT_Long )
158 PS_Conv_ToInt( FT_Byte** cursor,
162 FT_Byte* p = *cursor;
169 num = PS_Conv_Strtol( &p, limit, 10 );
174 if ( p < limit && *p == '#' )
179 num = PS_Conv_Strtol( &p, limit, num );
191 FT_LOCAL_DEF( FT_Fixed )
192 PS_Conv_ToFixed( FT_Byte** cursor,
196 FT_Byte* p = *cursor;
199 FT_Fixed integral = 0;
204 FT_Bool have_overflow = 0;
205 FT_Bool have_underflow = 0;
211 if ( *p == '-' || *p == '+' )
213 sign = FT_BOOL( *p == '-' );
220 /* read the integer part */
224 integral = PS_Conv_ToInt( &p, limit );
229 if ( integral > 0x7FFF )
235 /* read the decimal part */
236 if ( p < limit && *p == '.' )
240 for ( ; p < limit; p++ )
245 if ( IS_PS_SPACE( *p ) || *p OP 0x80 )
248 c = ft_char_table[*p & 0x7f];
250 if ( c < 0 || c >= 10 )
253 if ( decimal < 0xCCCCCCCL )
255 decimal = decimal * 10 + c;
257 if ( !integral && power_ten > 0 )
265 /* read exponent, if any */
266 if ( p + 1 < limit && ( *p == 'e' || *p == 'E' ) )
274 exponent = PS_Conv_ToInt( &p, limit );
279 /* arbitrarily limit exponent */
280 if ( exponent > 1000 )
282 else if ( exponent < -1000 )
285 power_ten += exponent;
290 if ( !integral && !decimal )
295 if ( have_underflow )
298 while ( power_ten > 0 )
300 if ( integral >= 0xCCCCCCCL )
304 if ( decimal >= 0xCCCCCCCL )
316 while ( power_ten < 0 )
319 if ( divider < 0xCCCCCCCL )
324 if ( !integral && !decimal )
332 decimal = FT_DivFix( decimal, divider );
333 /* it's not necessary to check this addition for overflow */
334 /* due to the structure of the real number representation */
340 integral = -integral;
345 FT_TRACE4(( "!!!END OF DATA:!!!" ));
349 integral = 0x7FFFFFFFL;
350 FT_TRACE4(( "!!!OVERFLOW:!!!" ));
354 FT_TRACE4(( "!!!UNDERFLOW:!!!" ));
360 FT_LOCAL_DEF( FT_UInt )
361 PS_Conv_StringDecode( FT_Byte** cursor,
370 for ( p = *cursor; r < n && p < limit; p++ )
414 if ( IS_PS_DIGIT( *p ) )
420 if ( IS_PS_DIGIT( *p ) )
422 b = b * 8 + *p - '0';
426 if ( IS_PS_DIGIT( *p ) )
427 b = b * 8 + *p - '0';
455 FT_LOCAL_DEF( FT_UInt )
456 PS_Conv_ASCIIHexDecode( FT_Byte** cursor,
476 if ( n > (FT_UInt)( limit - p ) )
477 n = (FT_UInt)( limit - p );
479 /* we try to process two nibbles at a time to be as fast as possible */
485 if ( IS_PS_SPACE( c ) )
491 c = ft_char_table[c & 0x7F];
492 if ( (unsigned)c >= 16 )
495 pad = ( pad << 4 ) | c;
498 buffer[w++] = (FT_Byte)pad;
504 buffer[w++] = (FT_Byte)( pad << 4 );
512 for ( r = 0; r < n; r++ )
517 if ( IS_PS_SPACE( *p ) )
523 c = ft_char_table[*p & 0x7f];
525 if ( (unsigned)c >= 16 )
530 *buffer = (FT_Byte)(*buffer + c);
534 *buffer = (FT_Byte)(c << 4);
541 return ( r + 1 ) / 2;
548 FT_LOCAL_DEF( FT_UInt )
549 PS_Conv_EexecDecode( FT_Byte** cursor,
567 if ( n > (FT_UInt)(limit - p) )
568 n = (FT_UInt)(limit - p);
570 for ( r = 0; r < n; r++ )
573 FT_UInt b = ( val ^ ( s >> 8 ) );
576 s = ( (val + s)*52845U + 22719 ) & 0xFFFFU;
577 buffer[r] = (FT_Byte) b;
581 *seed = (FT_UShort)s;
585 for ( r = 0, p = *cursor; r < n && p < limit; r++, p++ )
587 FT_Byte b = (FT_Byte)( *p ^ ( s >> 8 ) );
590 s = (FT_UShort)( ( *p + s ) * 52845U + 22719 );