1 /****************************************************************************
5 * PostScript Type 1 decoding routines (body).
7 * Copyright (C) 2000-2020 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.
19 #include <freetype/internal/ftcalc.h>
20 #include <freetype/internal/ftdebug.h>
21 #include <freetype/internal/pshints.h>
22 #include <freetype/internal/fthash.h>
23 #include <freetype/ftoutln.h>
30 /* ensure proper sign extension */
31 #define Fix2Int( f ) ( (FT_Int)(FT_Short)( (f) >> 16 ) )
33 /**************************************************************************
35 * The macro FT_COMPONENT is used in trace mode. It is an implicit
36 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
37 * messages during execution.
40 #define FT_COMPONENT t1decode
43 typedef enum T1_Operator_
73 op_max /* never remove this one */
79 const FT_Int t1_args_count[op_max] =
102 -1, /* callothersubr */
106 2, /* setcurrentpoint */
107 2 /* opcode 15 (undocumented and obsolete) */
111 /**************************************************************************
114 * t1_lookup_glyph_by_stdcharcode_ps
117 * Looks up a given glyph by its StandardEncoding charcode. Used to
118 * implement the SEAC Type 1 operator in the Adobe engine
122 * The current face object.
125 * The character code to look for.
128 * A glyph index in the font face. Returns -1 if the corresponding
129 * glyph wasn't found.
131 FT_LOCAL_DEF( FT_Int )
132 t1_lookup_glyph_by_stdcharcode_ps( PS_Decoder* decoder,
136 const FT_String* glyph_name;
137 FT_Service_PsCMaps psnames = decoder->psnames;
140 /* check range of standard char code */
141 if ( charcode < 0 || charcode > 255 )
144 glyph_name = psnames->adobe_std_strings(
145 psnames->adobe_std_encoding[charcode]);
147 for ( n = 0; n < decoder->num_glyphs; n++ )
149 FT_String* name = (FT_String*)decoder->glyph_names[n];
153 name[0] == glyph_name[0] &&
154 ft_strcmp( name, glyph_name ) == 0 )
162 #ifdef T1_CONFIG_OPTION_OLD_ENGINE
164 /**************************************************************************
167 * t1_lookup_glyph_by_stdcharcode
170 * Looks up a given glyph by its StandardEncoding charcode. Used to
171 * implement the SEAC Type 1 operator.
175 * The current face object.
178 * The character code to look for.
181 * A glyph index in the font face. Returns -1 if the corresponding
182 * glyph wasn't found.
185 t1_lookup_glyph_by_stdcharcode( T1_Decoder decoder,
189 const FT_String* glyph_name;
190 FT_Service_PsCMaps psnames = decoder->psnames;
193 /* check range of standard char code */
194 if ( charcode < 0 || charcode > 255 )
197 glyph_name = psnames->adobe_std_strings(
198 psnames->adobe_std_encoding[charcode]);
200 for ( n = 0; n < decoder->num_glyphs; n++ )
202 FT_String* name = (FT_String*)decoder->glyph_names[n];
206 name[0] == glyph_name[0] &&
207 ft_strcmp( name, glyph_name ) == 0 )
215 /* parse a single Type 1 glyph */
216 FT_LOCAL_DEF( FT_Error )
217 t1_decoder_parse_glyph( T1_Decoder decoder,
220 return decoder->parse_callback( decoder, glyph );
224 /**************************************************************************
230 * Implements the `seac' Type 1 operator for a Type 1 decoder.
234 * The current CID decoder.
237 * The accent's side bearing.
240 * The horizontal offset of the accent.
243 * The vertical offset of the accent.
246 * The base character's StandardEncoding charcode.
249 * The accent character's StandardEncoding charcode.
252 * FreeType error code. 0 means success.
255 t1operator_seac( T1_Decoder decoder,
263 FT_Int bchar_index, achar_index;
265 FT_Int n_base_points;
266 FT_Outline* base = decoder->builder.base;
268 FT_Vector left_bearing, advance;
270 #ifdef FT_CONFIG_OPTION_INCREMENTAL
271 T1_Face face = (T1_Face)decoder->builder.face;
277 FT_ERROR(( "t1operator_seac: invalid nested seac\n" ));
278 return FT_THROW( Syntax_Error );
281 if ( decoder->builder.metrics_only )
283 FT_ERROR(( "t1operator_seac: unexpected seac\n" ));
284 return FT_THROW( Syntax_Error );
288 adx += decoder->builder.left_bearing.x;
290 /* `glyph_names' is set to 0 for CID fonts which do not */
291 /* include an encoding. How can we deal with these? */
292 #ifdef FT_CONFIG_OPTION_INCREMENTAL
293 if ( decoder->glyph_names == 0 &&
294 !face->root.internal->incremental_interface )
296 if ( decoder->glyph_names == 0 )
297 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
299 FT_ERROR(( "t1operator_seac:"
300 " glyph names table not available in this font\n" ));
301 return FT_THROW( Syntax_Error );
304 #ifdef FT_CONFIG_OPTION_INCREMENTAL
305 if ( face->root.internal->incremental_interface )
307 /* the caller must handle the font encoding also */
314 bchar_index = t1_lookup_glyph_by_stdcharcode( decoder, bchar );
315 achar_index = t1_lookup_glyph_by_stdcharcode( decoder, achar );
318 if ( bchar_index < 0 || achar_index < 0 )
320 FT_ERROR(( "t1operator_seac:"
321 " invalid seac character code arguments\n" ));
322 return FT_THROW( Syntax_Error );
325 /* if we are trying to load a composite glyph, do not load the */
326 /* accent character and return the array of subglyphs. */
327 if ( decoder->builder.no_recurse )
329 FT_GlyphSlot glyph = (FT_GlyphSlot)decoder->builder.glyph;
330 FT_GlyphLoader loader = glyph->internal->loader;
334 /* reallocate subglyph array if necessary */
335 error = FT_GlyphLoader_CheckSubGlyphs( loader, 2 );
339 subg = loader->current.subglyphs;
341 /* subglyph 0 = base character */
342 subg->index = bchar_index;
343 subg->flags = FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES |
344 FT_SUBGLYPH_FLAG_USE_MY_METRICS;
349 /* subglyph 1 = accent character */
350 subg->index = achar_index;
351 subg->flags = FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES;
352 subg->arg1 = (FT_Int)FIXED_TO_INT( adx - asb );
353 subg->arg2 = (FT_Int)FIXED_TO_INT( ady );
355 /* set up remaining glyph fields */
356 glyph->num_subglyphs = 2;
357 glyph->subglyphs = loader->base.subglyphs;
358 glyph->format = FT_GLYPH_FORMAT_COMPOSITE;
360 loader->current.num_subglyphs = 2;
364 /* First load `bchar' in builder */
365 /* now load the unscaled outline */
367 FT_GlyphLoader_Prepare( decoder->builder.loader ); /* prepare loader */
369 /* save the left bearing and width of the SEAC */
370 /* glyph as they will be erased by the next load */
372 left_bearing = decoder->builder.left_bearing;
373 advance = decoder->builder.advance;
375 /* the seac operator must not be nested */
376 decoder->seac = TRUE;
377 error = t1_decoder_parse_glyph( decoder, (FT_UInt)bchar_index );
378 decoder->seac = FALSE;
382 /* If the SEAC glyph doesn't have a (H)SBW of its */
383 /* own use the values from the base glyph. */
385 if ( decoder->builder.parse_state != T1_Parse_Have_Width )
387 left_bearing = decoder->builder.left_bearing;
388 advance = decoder->builder.advance;
391 decoder->builder.left_bearing.x = 0;
392 decoder->builder.left_bearing.y = 0;
394 decoder->builder.pos_x = adx - asb;
395 decoder->builder.pos_y = ady;
397 /* Now load `achar' on top of */
398 /* the base outline */
400 /* the seac operator must not be nested */
401 decoder->seac = TRUE;
402 error = t1_decoder_parse_glyph( decoder, (FT_UInt)achar_index );
403 decoder->seac = FALSE;
407 /* restore the left side bearing and advance width */
408 /* of the SEAC glyph or base character (saved above) */
410 decoder->builder.left_bearing = left_bearing;
411 decoder->builder.advance = advance;
413 decoder->builder.pos_x = 0;
414 decoder->builder.pos_y = 0;
421 /**************************************************************************
424 * t1_decoder_parse_charstrings
427 * Parses a given Type 1 charstrings program.
431 * The current Type 1 decoder.
434 * The base address of the charstring stream.
437 * The length in bytes of the charstring stream.
440 * FreeType error code. 0 means success.
442 FT_LOCAL_DEF( FT_Error )
443 t1_decoder_parse_charstrings( T1_Decoder decoder,
444 FT_Byte* charstring_base,
445 FT_UInt charstring_len )
448 T1_Decoder_Zone zone;
451 T1_Builder builder = &decoder->builder;
452 FT_Pos x, y, orig_x, orig_y;
453 FT_Int known_othersubr_result_cnt = 0;
454 FT_Int unknown_othersubr_result_cnt = 0;
458 T1_Hints_Funcs hinter;
460 #ifdef FT_DEBUG_LEVEL_TRACE
465 /* compute random seed from stack address of parameter */
466 seed = (FT_Fixed)( ( (FT_Offset)(char*)&seed ^
467 (FT_Offset)(char*)&decoder ^
468 (FT_Offset)(char*)&charstring_base ) &
470 seed = ( seed ^ ( seed >> 10 ) ^ ( seed >> 20 ) ) & 0xFFFFL;
474 /* First of all, initialize the decoder */
475 decoder->top = decoder->stack;
476 decoder->zone = decoder->zones;
477 zone = decoder->zones;
479 builder->parse_state = T1_Parse_Start;
481 hinter = (T1_Hints_Funcs)builder->hints_funcs;
483 /* a font that reads BuildCharArray without setting */
484 /* its values first is buggy, but ... */
485 FT_ASSERT( ( decoder->len_buildchar == 0 ) ==
486 ( decoder->buildchar == NULL ) );
488 if ( decoder->buildchar && decoder->len_buildchar > 0 )
489 FT_ARRAY_ZERO( decoder->buildchar, decoder->len_buildchar );
491 zone->base = charstring_base;
492 limit = zone->limit = charstring_base + charstring_len;
493 ip = zone->cursor = zone->base;
497 x = orig_x = builder->pos_x;
498 y = orig_y = builder->pos_y;
500 /* begin hints recording session, if any */
502 hinter->open( hinter->hints );
506 /* now, execute loop */
509 FT_Long* top = decoder->top;
510 T1_Operator op = op_none;
514 FT_ASSERT( known_othersubr_result_cnt == 0 ||
515 unknown_othersubr_result_cnt == 0 );
517 #ifdef FT_DEBUG_LEVEL_TRACE
520 FT_TRACE5(( " (%d)", decoder->top - decoder->stack ));
525 /**********************************************************************
527 * Decode operator or operand
531 /* first of all, decompress operator or value */
573 case 15: /* undocumented, obsolete operator */
594 FT_ERROR(( "t1_decoder_parse_charstrings:"
595 " invalid escape (12+EOF)\n" ));
620 op = op_callothersubr;
626 op = op_setcurrentpoint;
630 FT_ERROR(( "t1_decoder_parse_charstrings:"
631 " invalid escape (12+%d)\n",
637 case 255: /* four bytes integer */
638 if ( ip + 4 > limit )
640 FT_ERROR(( "t1_decoder_parse_charstrings:"
641 " unexpected EOF in integer\n" ));
645 value = (FT_Int32)( ( (FT_UInt32)ip[0] << 24 ) |
646 ( (FT_UInt32)ip[1] << 16 ) |
647 ( (FT_UInt32)ip[2] << 8 ) |
651 /* According to the specification, values > 32000 or < -32000 must */
652 /* be followed by a `div' operator to make the result be in the */
653 /* range [-32000;32000]. We expect that the second argument of */
654 /* `div' is not a large number. Additionally, we don't handle */
655 /* stuff like `<large1> <large2> <num> div <num> div' or */
656 /* <large1> <large2> <num> div div'. This is probably not allowed */
658 if ( value > 32000 || value < -32000 )
661 FT_ERROR(( "t1_decoder_parse_charstrings:"
662 " no `div' after large integer\n" ));
669 value = (FT_Int32)( (FT_UInt32)value << 16 );
678 value = (FT_Int32)ip[-1] - 139;
683 FT_ERROR(( "t1_decoder_parse_charstrings:"
684 " unexpected EOF in integer\n" ));
689 value = ( ( ip[-2] - 247 ) * 256 ) + ip[-1] + 108;
691 value = -( ( ( ip[-2] - 251 ) * 256 ) + ip[-1] + 108 );
695 value = (FT_Int32)( (FT_UInt32)value << 16 );
699 FT_ERROR(( "t1_decoder_parse_charstrings:"
700 " invalid byte (%d)\n", ip[-1] ));
705 if ( unknown_othersubr_result_cnt > 0 )
716 /* all operands have been transferred by previous pops */
717 unknown_othersubr_result_cnt = 0;
722 if ( large_int && !( op == op_none || op == op_div ) )
724 FT_ERROR(( "t1_decoder_parse_charstrings:"
725 " no `div' after large integer\n" ));
730 /**********************************************************************
732 * Push value on stack, or process operator
737 if ( top - decoder->stack >= T1_MAX_CHARSTRINGS_OPERANDS )
739 FT_ERROR(( "t1_decoder_parse_charstrings: stack overflow\n" ));
743 #ifdef FT_DEBUG_LEVEL_TRACE
745 FT_TRACE4(( " %d", value ));
747 FT_TRACE4(( " %d", value / 65536 ));
753 else if ( op == op_callothersubr ) /* callothersubr */
759 #ifdef FT_DEBUG_LEVEL_TRACE
760 FT_TRACE4(( " callothersubr\n" ));
764 if ( top - decoder->stack < 2 )
765 goto Stack_Underflow;
769 subr_no = Fix2Int( top[1] );
770 arg_cnt = Fix2Int( top[0] );
772 /************************************************************
774 * remove all operands to callothersubr from the stack
776 * for handled othersubrs, where we know the number of
777 * arguments, we increase the stack by the value of
778 * known_othersubr_result_cnt
780 * for unhandled othersubrs the following pops adjust the
781 * stack pointer as necessary
784 if ( arg_cnt > top - decoder->stack )
785 goto Stack_Underflow;
789 known_othersubr_result_cnt = 0;
790 unknown_othersubr_result_cnt = 0;
792 /* XXX TODO: The checks to `arg_count == <whatever>' */
793 /* might not be correct; an othersubr expects a certain */
794 /* number of operands on the PostScript stack (as opposed */
795 /* to the T1 stack) but it doesn't have to put them there */
796 /* by itself; previous othersubrs might have left the */
797 /* operands there if they were not followed by an */
798 /* appropriate number of pops */
800 /* On the other hand, Adobe Reader 7.0.8 for Linux doesn't */
801 /* accept a font that contains charstrings like */
803 /* 100 200 2 20 callothersubr */
804 /* 300 1 20 callothersubr pop */
806 /* Perhaps this is the reason why BuildCharArray exists. */
810 case 0: /* end flex feature */
812 goto Unexpected_OtherSubr;
814 if ( !decoder->flex_state ||
815 decoder->num_flex_vectors != 7 )
817 FT_ERROR(( "t1_decoder_parse_charstrings:"
818 " unexpected flex end\n" ));
822 /* the two `results' are popped by the following setcurrentpoint */
825 known_othersubr_result_cnt = 2;
828 case 1: /* start flex feature */
830 goto Unexpected_OtherSubr;
832 if ( FT_SET_ERROR( t1_builder_start_point( builder, x, y ) ) ||
833 FT_SET_ERROR( t1_builder_check_points( builder, 6 ) ) )
836 decoder->flex_state = 1;
837 decoder->num_flex_vectors = 0;
840 case 2: /* add flex vectors */
846 goto Unexpected_OtherSubr;
848 if ( !decoder->flex_state )
850 FT_ERROR(( "t1_decoder_parse_charstrings:"
851 " missing flex start\n" ));
855 /* note that we should not add a point for index 0; */
856 /* this will move our current position to the flex */
857 /* point without adding any point to the outline */
858 idx = decoder->num_flex_vectors++;
859 if ( idx > 0 && idx < 7 )
861 /* in malformed fonts it is possible to have other */
862 /* opcodes in the middle of a flex (which don't */
863 /* increase `num_flex_vectors'); we thus have to */
864 /* check whether we can add a point */
865 if ( FT_SET_ERROR( t1_builder_check_points( builder, 1 ) ) )
868 t1_builder_add_point( builder,
871 (FT_Byte)( idx == 3 || idx == 6 ) );
876 case 3: /* change hints */
878 goto Unexpected_OtherSubr;
880 known_othersubr_result_cnt = 1;
883 hinter->reset( hinter->hints,
884 (FT_UInt)builder->current->n_points );
889 /* counter control hints, clear stack */
890 top = decoder->stack;
897 case 18: /* multiple masters */
899 PS_Blend blend = decoder->blend;
900 FT_UInt num_points, nn, mm;
907 FT_ERROR(( "t1_decoder_parse_charstrings:"
908 " unexpected multiple masters operator\n" ));
912 num_points = (FT_UInt)subr_no - 13 + ( subr_no == 18 );
913 if ( arg_cnt != (FT_Int)( num_points * blend->num_designs ) )
915 FT_ERROR(( "t1_decoder_parse_charstrings:"
916 " incorrect number of multiple masters arguments\n" ));
920 /* We want to compute */
922 /* a0*w0 + a1*w1 + ... + ak*wk */
924 /* but we only have a0, a1-a0, a2-a0, ..., ak-a0. */
926 /* However, given that w0 + w1 + ... + wk == 1, we can */
927 /* rewrite it easily as */
929 /* a0 + (a1-a0)*w1 + (a2-a0)*w2 + ... + (ak-a0)*wk */
931 /* where k == num_designs-1. */
933 /* I guess that's why it's written in this `compact' */
936 delta = top + num_points;
938 for ( nn = 0; nn < num_points; nn++ )
940 FT_Long tmp = values[0];
943 for ( mm = 1; mm < blend->num_designs; mm++ )
946 blend->weight_vector[mm] ) );
951 known_othersubr_result_cnt = (FT_Int)num_points;
956 /* <idx> 1 19 callothersubr */
957 /* => replace elements starting from index cvi( <idx> ) */
958 /* of BuildCharArray with WeightVector */
961 PS_Blend blend = decoder->blend;
964 if ( arg_cnt != 1 || !blend )
965 goto Unexpected_OtherSubr;
967 idx = Fix2Int( top[0] );
970 (FT_UInt)idx + blend->num_designs > decoder->len_buildchar )
971 goto Unexpected_OtherSubr;
973 ft_memcpy( &decoder->buildchar[idx],
974 blend->weight_vector,
976 sizeof ( blend->weight_vector[0] ) );
981 /* <arg1> <arg2> 2 20 callothersubr pop */
982 /* ==> push <arg1> + <arg2> onto T1 stack */
984 goto Unexpected_OtherSubr;
986 top[0] = ADD_LONG( top[0], top[1] );
988 known_othersubr_result_cnt = 1;
992 /* <arg1> <arg2> 2 21 callothersubr pop */
993 /* ==> push <arg1> - <arg2> onto T1 stack */
995 goto Unexpected_OtherSubr;
997 top[0] = SUB_LONG( top[0], top[1] );
999 known_othersubr_result_cnt = 1;
1003 /* <arg1> <arg2> 2 22 callothersubr pop */
1004 /* ==> push <arg1> * <arg2> onto T1 stack */
1006 goto Unexpected_OtherSubr;
1008 top[0] = FT_MulFix( top[0], top[1] );
1010 known_othersubr_result_cnt = 1;
1014 /* <arg1> <arg2> 2 23 callothersubr pop */
1015 /* ==> push <arg1> / <arg2> onto T1 stack */
1016 if ( arg_cnt != 2 || top[1] == 0 )
1017 goto Unexpected_OtherSubr;
1019 top[0] = FT_DivFix( top[0], top[1] );
1021 known_othersubr_result_cnt = 1;
1025 /* <val> <idx> 2 24 callothersubr */
1026 /* ==> set BuildCharArray[cvi( <idx> )] = <val> */
1029 PS_Blend blend = decoder->blend;
1032 if ( arg_cnt != 2 || !blend )
1033 goto Unexpected_OtherSubr;
1035 idx = Fix2Int( top[1] );
1037 if ( idx < 0 || (FT_UInt) idx >= decoder->len_buildchar )
1038 goto Unexpected_OtherSubr;
1040 decoder->buildchar[idx] = top[0];
1045 /* <idx> 1 25 callothersubr pop */
1046 /* ==> push BuildCharArray[cvi( idx )] */
1050 PS_Blend blend = decoder->blend;
1053 if ( arg_cnt != 1 || !blend )
1054 goto Unexpected_OtherSubr;
1056 idx = Fix2Int( top[0] );
1058 if ( idx < 0 || (FT_UInt) idx >= decoder->len_buildchar )
1059 goto Unexpected_OtherSubr;
1061 top[0] = decoder->buildchar[idx];
1064 known_othersubr_result_cnt = 1;
1069 /* <val> mark <idx> ==> set BuildCharArray[cvi( <idx> )] = <val>, */
1070 /* leave mark on T1 stack */
1071 /* <val> <idx> ==> set BuildCharArray[cvi( <idx> )] = <val> */
1072 XXX which routine has left its mark on the (PostScript) stack?;
1077 /* <res1> <res2> <val1> <val2> 4 27 callothersubr pop */
1078 /* ==> push <res1> onto T1 stack if <val1> <= <val2>, */
1079 /* otherwise push <res2> */
1081 goto Unexpected_OtherSubr;
1083 if ( top[2] > top[3] )
1086 known_othersubr_result_cnt = 1;
1090 /* 0 28 callothersubr pop */
1091 /* => push random value from interval [0, 1) onto stack */
1093 goto Unexpected_OtherSubr;
1100 if ( Rand >= 0x8000L )
1105 seed = FT_MulFix( seed, 0x10000L - seed );
1110 known_othersubr_result_cnt = 1;
1114 if ( arg_cnt >= 0 && subr_no >= 0 )
1116 FT_ERROR(( "t1_decoder_parse_charstrings:"
1117 " unknown othersubr [%d %d], wish me luck\n",
1118 arg_cnt, subr_no ));
1119 unknown_othersubr_result_cnt = arg_cnt;
1124 Unexpected_OtherSubr:
1125 FT_ERROR(( "t1_decoder_parse_charstrings:"
1126 " invalid othersubr [%d %d]\n", arg_cnt, subr_no ));
1130 top += known_othersubr_result_cnt;
1134 else /* general operator */
1136 FT_Int num_args = t1_args_count[op];
1139 FT_ASSERT( num_args >= 0 );
1141 if ( top - decoder->stack < num_args )
1142 goto Stack_Underflow;
1144 /* XXX Operators usually take their operands from the */
1145 /* bottom of the stack, i.e., the operands are */
1146 /* decoder->stack[0], ..., decoder->stack[num_args - 1]; */
1147 /* only div, callsubr, and callothersubr are different. */
1148 /* In practice it doesn't matter (?). */
1150 #ifdef FT_DEBUG_LEVEL_TRACE
1156 case op_callothersubr:
1162 if ( top - decoder->stack != num_args )
1163 FT_TRACE0(( "t1_decoder_parse_charstrings:"
1164 " too much operands on the stack"
1165 " (seen %d, expected %d)\n",
1166 top - decoder->stack, num_args ));
1170 #endif /* FT_DEBUG_LEVEL_TRACE */
1177 FT_TRACE4(( " endchar\n" ));
1179 t1_builder_close_contour( builder );
1181 /* close hints recording session */
1184 if ( hinter->close( hinter->hints,
1185 (FT_UInt)builder->current->n_points ) )
1188 /* apply hints to the loaded glyph outline now */
1189 error = hinter->apply( hinter->hints,
1191 (PSH_Globals)builder->hints_globals,
1192 decoder->hint_mode );
1197 /* add current outline to the glyph slot */
1198 FT_GlyphLoader_Add( builder->loader );
1200 /* the compiler should optimize away this empty loop but ... */
1202 #ifdef FT_DEBUG_LEVEL_TRACE
1204 if ( decoder->len_buildchar > 0 )
1209 FT_TRACE4(( "BuildCharArray = [ " ));
1211 for ( i = 0; i < decoder->len_buildchar; i++ )
1212 FT_TRACE4(( "%d ", decoder->buildchar[i] ));
1214 FT_TRACE4(( "]\n" ));
1217 #endif /* FT_DEBUG_LEVEL_TRACE */
1219 FT_TRACE4(( "\n" ));
1225 FT_TRACE4(( " hsbw" ));
1227 builder->parse_state = T1_Parse_Have_Width;
1229 builder->left_bearing.x = ADD_LONG( builder->left_bearing.x,
1232 builder->advance.x = top[1];
1233 builder->advance.y = 0;
1235 orig_x = x = ADD_LONG( builder->pos_x, top[0] );
1236 orig_y = y = builder->pos_y;
1238 FT_UNUSED( orig_y );
1240 /* `metrics_only' indicates that we only want to compute the */
1241 /* glyph's metrics (lsb + advance width) without loading the */
1242 /* rest of it; so exit immediately */
1243 if ( builder->metrics_only )
1245 FT_TRACE4(( "\n" ));
1252 return t1operator_seac( decoder,
1257 Fix2Int( top[4] ) );
1260 FT_TRACE4(( " sbw" ));
1262 builder->parse_state = T1_Parse_Have_Width;
1264 builder->left_bearing.x = ADD_LONG( builder->left_bearing.x,
1266 builder->left_bearing.y = ADD_LONG( builder->left_bearing.y,
1269 builder->advance.x = top[2];
1270 builder->advance.y = top[3];
1272 x = ADD_LONG( builder->pos_x, top[0] );
1273 y = ADD_LONG( builder->pos_y, top[1] );
1275 /* `metrics_only' indicates that we only want to compute the */
1276 /* glyph's metrics (lsb + advance width) without loading the */
1277 /* rest of it; so exit immediately */
1278 if ( builder->metrics_only )
1280 FT_TRACE4(( "\n" ));
1287 FT_TRACE4(( " closepath" ));
1289 /* if there is no path, `closepath' is a no-op */
1290 if ( builder->parse_state == T1_Parse_Have_Path ||
1291 builder->parse_state == T1_Parse_Have_Moveto )
1292 t1_builder_close_contour( builder );
1294 builder->parse_state = T1_Parse_Have_Width;
1298 FT_TRACE4(( " hlineto" ));
1300 if ( FT_SET_ERROR( t1_builder_start_point( builder, x, y ) ) )
1303 x = ADD_LONG( x, top[0] );
1307 FT_TRACE4(( " hmoveto" ));
1309 x = ADD_LONG( x, top[0] );
1311 if ( !decoder->flex_state )
1313 if ( builder->parse_state == T1_Parse_Start )
1315 builder->parse_state = T1_Parse_Have_Moveto;
1320 FT_TRACE4(( " hvcurveto" ));
1322 if ( FT_SET_ERROR( t1_builder_start_point( builder, x, y ) ) ||
1323 FT_SET_ERROR( t1_builder_check_points( builder, 3 ) ) )
1326 x = ADD_LONG( x, top[0] );
1327 t1_builder_add_point( builder, x, y, 0 );
1329 x = ADD_LONG( x, top[1] );
1330 y = ADD_LONG( y, top[2] );
1331 t1_builder_add_point( builder, x, y, 0 );
1333 y = ADD_LONG( y, top[3] );
1334 t1_builder_add_point( builder, x, y, 1 );
1338 FT_TRACE4(( " rlineto" ));
1340 if ( FT_SET_ERROR( t1_builder_start_point( builder, x, y ) ) )
1343 x = ADD_LONG( x, top[0] );
1344 y = ADD_LONG( y, top[1] );
1347 if ( FT_SET_ERROR( t1_builder_add_point1( builder, x, y ) ) )
1352 FT_TRACE4(( " rmoveto" ));
1354 x = ADD_LONG( x, top[0] );
1355 y = ADD_LONG( y, top[1] );
1357 if ( !decoder->flex_state )
1359 if ( builder->parse_state == T1_Parse_Start )
1361 builder->parse_state = T1_Parse_Have_Moveto;
1366 FT_TRACE4(( " rrcurveto" ));
1368 if ( FT_SET_ERROR( t1_builder_start_point( builder, x, y ) ) ||
1369 FT_SET_ERROR( t1_builder_check_points( builder, 3 ) ) )
1372 x = ADD_LONG( x, top[0] );
1373 y = ADD_LONG( y, top[1] );
1374 t1_builder_add_point( builder, x, y, 0 );
1376 x = ADD_LONG( x, top[2] );
1377 y = ADD_LONG( y, top[3] );
1378 t1_builder_add_point( builder, x, y, 0 );
1380 x = ADD_LONG( x, top[4] );
1381 y = ADD_LONG( y, top[5] );
1382 t1_builder_add_point( builder, x, y, 1 );
1386 FT_TRACE4(( " vhcurveto" ));
1388 if ( FT_SET_ERROR( t1_builder_start_point( builder, x, y ) ) ||
1389 FT_SET_ERROR( t1_builder_check_points( builder, 3 ) ) )
1392 y = ADD_LONG( y, top[0] );
1393 t1_builder_add_point( builder, x, y, 0 );
1395 x = ADD_LONG( x, top[1] );
1396 y = ADD_LONG( y, top[2] );
1397 t1_builder_add_point( builder, x, y, 0 );
1399 x = ADD_LONG( x, top[3] );
1400 t1_builder_add_point( builder, x, y, 1 );
1404 FT_TRACE4(( " vlineto" ));
1406 if ( FT_SET_ERROR( t1_builder_start_point( builder, x, y ) ) )
1409 y = ADD_LONG( y, top[0] );
1413 FT_TRACE4(( " vmoveto" ));
1415 y = ADD_LONG( y, top[0] );
1417 if ( !decoder->flex_state )
1419 if ( builder->parse_state == T1_Parse_Start )
1421 builder->parse_state = T1_Parse_Have_Moveto;
1426 FT_TRACE4(( " div" ));
1428 /* if `large_int' is set, we divide unscaled numbers; */
1429 /* otherwise, we divide numbers in 16.16 format -- */
1430 /* in both cases, it is the same operation */
1431 *top = FT_DivFix( top[0], top[1] );
1442 FT_TRACE4(( " callsubr" ));
1444 idx = Fix2Int( top[0] );
1446 if ( decoder->subrs_hash )
1448 size_t* val = ft_hash_num_lookup( idx,
1449 decoder->subrs_hash );
1458 if ( idx < 0 || idx >= decoder->num_subrs )
1460 FT_ERROR(( "t1_decoder_parse_charstrings:"
1461 " invalid subrs index\n" ));
1465 if ( zone - decoder->zones >= T1_MAX_SUBRS_CALLS )
1467 FT_ERROR(( "t1_decoder_parse_charstrings:"
1468 " too many nested subrs\n" ));
1472 zone->cursor = ip; /* save current instruction pointer */
1476 /* The Type 1 driver stores subroutines without the seed bytes. */
1477 /* The CID driver stores subroutines with seed bytes. This */
1478 /* case is taken care of when decoder->subrs_len == 0. */
1479 zone->base = decoder->subrs[idx];
1481 if ( decoder->subrs_len )
1482 zone->limit = zone->base + decoder->subrs_len[idx];
1485 /* We are using subroutines from a CID font. We must adjust */
1486 /* for the seed bytes. */
1487 zone->base += ( decoder->lenIV >= 0 ? decoder->lenIV : 0 );
1488 zone->limit = decoder->subrs[idx + 1];
1491 zone->cursor = zone->base;
1495 FT_ERROR(( "t1_decoder_parse_charstrings:"
1496 " invoking empty subrs\n" ));
1500 decoder->zone = zone;
1502 limit = zone->limit;
1507 FT_TRACE4(( " pop" ));
1509 if ( known_othersubr_result_cnt > 0 )
1511 known_othersubr_result_cnt--;
1512 /* ignore, we pushed the operands ourselves */
1516 if ( unknown_othersubr_result_cnt == 0 )
1518 FT_ERROR(( "t1_decoder_parse_charstrings:"
1519 " no more operands for othersubr\n" ));
1523 unknown_othersubr_result_cnt--;
1524 top++; /* `push' the operand to callothersubr onto the stack */
1528 FT_TRACE4(( " return" ));
1530 if ( zone <= decoder->zones )
1532 FT_ERROR(( "t1_decoder_parse_charstrings:"
1533 " unexpected return\n" ));
1539 limit = zone->limit;
1540 decoder->zone = zone;
1544 FT_TRACE4(( " dotsection" ));
1549 FT_TRACE4(( " hstem" ));
1551 /* record horizontal hint */
1554 /* top[0] += builder->left_bearing.y; */
1555 hinter->stem( hinter->hints, 1, top );
1560 FT_TRACE4(( " hstem3" ));
1562 /* record horizontal counter-controlled hints */
1564 hinter->stem3( hinter->hints, 1, top );
1568 FT_TRACE4(( " vstem" ));
1570 /* record vertical hint */
1573 top[0] = ADD_LONG( top[0], orig_x );
1574 hinter->stem( hinter->hints, 0, top );
1579 FT_TRACE4(( " vstem3" ));
1581 /* record vertical counter-controlled hints */
1587 top[0] = ADD_LONG( top[0], dx );
1588 top[2] = ADD_LONG( top[2], dx );
1589 top[4] = ADD_LONG( top[4], dx );
1590 hinter->stem3( hinter->hints, 0, top );
1594 case op_setcurrentpoint:
1595 FT_TRACE4(( " setcurrentpoint" ));
1597 /* From the T1 specification, section 6.4: */
1599 /* The setcurrentpoint command is used only in */
1600 /* conjunction with results from OtherSubrs procedures. */
1602 /* known_othersubr_result_cnt != 0 is already handled */
1605 /* Note, however, that both Ghostscript and Adobe */
1606 /* Distiller handle this situation by silently ignoring */
1607 /* the inappropriate `setcurrentpoint' instruction. So */
1608 /* we do the same. */
1611 if ( decoder->flex_state != 1 )
1613 FT_ERROR(( "t1_decoder_parse_charstrings:"
1614 " unexpected `setcurrentpoint'\n" ));
1623 decoder->flex_state = 0;
1627 FT_TRACE4(( " opcode_15" ));
1628 /* nothing to do except to pop the two arguments */
1632 FT_ERROR(( "t1_decoder_parse_charstrings:"
1633 " unhandled opcode %d\n", op ));
1637 /* XXX Operators usually clear the operand stack; */
1638 /* only div, callsubr, callothersubr, pop, and */
1639 /* return are different. */
1640 /* In practice it doesn't matter (?). */
1644 #ifdef FT_DEBUG_LEVEL_TRACE
1645 FT_TRACE4(( "\n" ));
1649 } /* general operator processing */
1651 } /* while ip < limit */
1653 FT_TRACE4(( "..end..\n\n" ));
1659 return FT_THROW( Syntax_Error );
1662 return FT_THROW( Stack_Underflow );
1666 #else /* !T1_CONFIG_OPTION_OLD_ENGINE */
1669 /**************************************************************************
1672 * t1_decoder_parse_metrics
1675 * Parses a given Type 1 charstrings program to extract width
1679 * The current Type 1 decoder.
1681 * charstring_base ::
1682 * The base address of the charstring stream.
1685 * The length in bytes of the charstring stream.
1688 * FreeType error code. 0 means success.
1690 FT_LOCAL_DEF( FT_Error )
1691 t1_decoder_parse_metrics( T1_Decoder decoder,
1692 FT_Byte* charstring_base,
1693 FT_UInt charstring_len )
1695 T1_Decoder_Zone zone;
1698 T1_Builder builder = &decoder->builder;
1701 #ifdef FT_DEBUG_LEVEL_TRACE
1706 /* First of all, initialize the decoder */
1707 decoder->top = decoder->stack;
1708 decoder->zone = decoder->zones;
1709 zone = decoder->zones;
1711 builder->parse_state = T1_Parse_Start;
1713 zone->base = charstring_base;
1714 limit = zone->limit = charstring_base + charstring_len;
1715 ip = zone->cursor = zone->base;
1719 /* now, execute loop */
1720 while ( ip < limit )
1722 FT_Long* top = decoder->top;
1723 T1_Operator op = op_none;
1727 #ifdef FT_DEBUG_LEVEL_TRACE
1730 FT_TRACE5(( " (%ld)", decoder->top - decoder->stack ));
1735 /**********************************************************************
1737 * Decode operator or operand
1741 /* first of all, decompress operator or value */
1774 FT_ERROR(( "t1_decoder_parse_metrics:"
1775 " invalid escape (12+EOF)\n" ));
1793 case 255: /* four bytes integer */
1794 if ( ip + 4 > limit )
1796 FT_ERROR(( "t1_decoder_parse_metrics:"
1797 " unexpected EOF in integer\n" ));
1801 value = (FT_Int32)( ( (FT_UInt32)ip[0] << 24 ) |
1802 ( (FT_UInt32)ip[1] << 16 ) |
1803 ( (FT_UInt32)ip[2] << 8 ) |
1807 /* According to the specification, values > 32000 or < -32000 must */
1808 /* be followed by a `div' operator to make the result be in the */
1809 /* range [-32000;32000]. We expect that the second argument of */
1810 /* `div' is not a large number. Additionally, we don't handle */
1811 /* stuff like `<large1> <large2> <num> div <num> div' or */
1812 /* <large1> <large2> <num> div div'. This is probably not allowed */
1814 if ( value > 32000 || value < -32000 )
1818 FT_ERROR(( "t1_decoder_parse_metrics:"
1819 " no `div' after large integer\n" ));
1828 value = (FT_Int32)( (FT_UInt32)value << 16 );
1837 value = (FT_Int32)ip[-1] - 139;
1842 FT_ERROR(( "t1_decoder_parse_metrics:"
1843 " unexpected EOF in integer\n" ));
1848 value = ( ( ip[-2] - 247 ) * 256 ) + ip[-1] + 108;
1850 value = -( ( ( ip[-2] - 251 ) * 256 ) + ip[-1] + 108 );
1854 value = (FT_Int32)( (FT_UInt32)value << 16 );
1858 FT_ERROR(( "t1_decoder_parse_metrics:"
1859 " invalid byte (%d)\n", ip[-1] ));
1864 if ( large_int && !( op == op_none || op == op_div ) )
1866 FT_ERROR(( "t1_decoder_parse_metrics:"
1867 " no `div' after large integer\n" ));
1871 /**********************************************************************
1873 * Push value on stack, or process operator
1876 if ( op == op_none )
1878 if ( top - decoder->stack >= T1_MAX_CHARSTRINGS_OPERANDS )
1880 FT_ERROR(( "t1_decoder_parse_metrics: stack overflow\n" ));
1884 #ifdef FT_DEBUG_LEVEL_TRACE
1886 FT_TRACE4(( " %d", value ));
1888 FT_TRACE4(( " %d", value / 65536 ));
1894 else /* general operator */
1896 FT_Int num_args = t1_args_count[op];
1899 FT_ASSERT( num_args >= 0 );
1901 if ( top - decoder->stack < num_args )
1902 goto Stack_Underflow;
1904 #ifdef FT_DEBUG_LEVEL_TRACE
1914 if ( top - decoder->stack != num_args )
1915 FT_TRACE0(( "t1_decoder_parse_metrics:"
1916 " too much operands on the stack"
1917 " (seen %ld, expected %d)\n",
1918 top - decoder->stack, num_args ));
1922 #endif /* FT_DEBUG_LEVEL_TRACE */
1929 FT_TRACE4(( " hsbw" ));
1931 builder->parse_state = T1_Parse_Have_Width;
1933 builder->left_bearing.x = ADD_LONG( builder->left_bearing.x,
1936 builder->advance.x = top[1];
1937 builder->advance.y = 0;
1939 /* we only want to compute the glyph's metrics */
1940 /* (lsb + advance width) without loading the */
1941 /* rest of it; so exit immediately */
1942 FT_TRACE4(( "\n" ));
1946 FT_TRACE4(( " sbw" ));
1948 builder->parse_state = T1_Parse_Have_Width;
1950 builder->left_bearing.x = ADD_LONG( builder->left_bearing.x,
1952 builder->left_bearing.y = ADD_LONG( builder->left_bearing.y,
1955 builder->advance.x = top[2];
1956 builder->advance.y = top[3];
1958 /* we only want to compute the glyph's metrics */
1959 /* (lsb + advance width), without loading the */
1960 /* rest of it; so exit immediately */
1961 FT_TRACE4(( "\n" ));
1965 FT_TRACE4(( " div" ));
1967 /* if `large_int' is set, we divide unscaled numbers; */
1968 /* otherwise, we divide numbers in 16.16 format -- */
1969 /* in both cases, it is the same operation */
1970 *top = FT_DivFix( top[0], top[1] );
1981 FT_TRACE4(( " callsubr" ));
1983 idx = Fix2Int( top[0] );
1985 if ( decoder->subrs_hash )
1987 size_t* val = ft_hash_num_lookup( idx,
1988 decoder->subrs_hash );
1997 if ( idx < 0 || idx >= decoder->num_subrs )
1999 FT_ERROR(( "t1_decoder_parse_metrics:"
2000 " invalid subrs index\n" ));
2004 if ( zone - decoder->zones >= T1_MAX_SUBRS_CALLS )
2006 FT_ERROR(( "t1_decoder_parse_metrics:"
2007 " too many nested subrs\n" ));
2011 zone->cursor = ip; /* save current instruction pointer */
2015 /* The Type 1 driver stores subroutines without the seed bytes. */
2016 /* The CID driver stores subroutines with seed bytes. This */
2017 /* case is taken care of when decoder->subrs_len == 0. */
2018 zone->base = decoder->subrs[idx];
2020 if ( decoder->subrs_len )
2021 zone->limit = zone->base + decoder->subrs_len[idx];
2024 /* We are using subroutines from a CID font. We must adjust */
2025 /* for the seed bytes. */
2026 zone->base += ( decoder->lenIV >= 0 ? decoder->lenIV : 0 );
2027 zone->limit = decoder->subrs[idx + 1];
2030 zone->cursor = zone->base;
2034 FT_ERROR(( "t1_decoder_parse_metrics:"
2035 " invoking empty subrs\n" ));
2039 decoder->zone = zone;
2041 limit = zone->limit;
2046 FT_TRACE4(( " return" ));
2048 if ( zone <= decoder->zones )
2050 FT_ERROR(( "t1_decoder_parse_metrics:"
2051 " unexpected return\n" ));
2057 limit = zone->limit;
2058 decoder->zone = zone;
2062 FT_ERROR(( "t1_decoder_parse_metrics:"
2063 " unhandled opcode %d\n", op ));
2069 } /* general operator processing */
2071 } /* while ip < limit */
2073 FT_TRACE4(( "..end..\n\n" ));
2076 FT_ERROR(( "t1_decoder_parse_metrics:"
2077 " no width, found op %d instead\n",
2080 return FT_THROW( Syntax_Error );
2083 return FT_THROW( Stack_Underflow );
2086 #endif /* !T1_CONFIG_OPTION_OLD_ENGINE */
2089 /* initialize T1 decoder */
2090 FT_LOCAL_DEF( FT_Error )
2091 t1_decoder_init( T1_Decoder decoder,
2095 FT_Byte** glyph_names,
2098 FT_Render_Mode hint_mode,
2099 T1_Decoder_Callback parse_callback )
2103 /* retrieve `psnames' interface from list of current modules */
2105 FT_Service_PsCMaps psnames;
2108 FT_FACE_FIND_GLOBAL_SERVICE( face, psnames, POSTSCRIPT_CMAPS );
2111 FT_ERROR(( "t1_decoder_init:"
2112 " the `psnames' module is not available\n" ));
2113 return FT_THROW( Unimplemented_Feature );
2116 decoder->psnames = psnames;
2119 t1_builder_init( &decoder->builder, face, size, slot, hinting );
2121 /* decoder->buildchar and decoder->len_buildchar have to be */
2122 /* initialized by the caller since we cannot know the length */
2123 /* of the BuildCharArray */
2125 decoder->num_glyphs = (FT_UInt)face->num_glyphs;
2126 decoder->glyph_names = glyph_names;
2127 decoder->hint_mode = hint_mode;
2128 decoder->blend = blend;
2129 decoder->parse_callback = parse_callback;
2131 decoder->funcs = t1_decoder_funcs;
2137 /* finalize T1 decoder */
2138 FT_LOCAL_DEF( void )
2139 t1_decoder_done( T1_Decoder decoder )
2141 FT_Memory memory = decoder->builder.memory;
2144 t1_builder_done( &decoder->builder );
2146 if ( decoder->cf2_instance.finalizer )
2148 decoder->cf2_instance.finalizer( decoder->cf2_instance.data );
2149 FT_FREE( decoder->cf2_instance.data );