1 /***************************************************************************/
5 /* PostScript Type 1 decoding routines (body). */
7 /* Copyright 2000-2014 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_CALC_H
21 #include FT_INTERNAL_DEBUG_H
22 #include FT_INTERNAL_POSTSCRIPT_HINTS_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 trace_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 */
117 /* Looks up a given glyph by its StandardEncoding charcode. Used to */
118 /* implement the SEAC Type 1 operator. */
121 /* face :: The current face object. */
123 /* charcode :: The character code to look for. */
126 /* A glyph index in the font face. Returns -1 if the corresponding */
127 /* glyph wasn't found. */
130 t1_lookup_glyph_by_stdcharcode( T1_Decoder decoder,
134 const FT_String* glyph_name;
135 FT_Service_PsCMaps psnames = decoder->psnames;
138 /* check range of standard char code */
139 if ( charcode < 0 || charcode > 255 )
142 glyph_name = psnames->adobe_std_strings(
143 psnames->adobe_std_encoding[charcode]);
145 for ( n = 0; n < decoder->num_glyphs; n++ )
147 FT_String* name = (FT_String*)decoder->glyph_names[n];
151 name[0] == glyph_name[0] &&
152 ft_strcmp( name, glyph_name ) == 0 )
160 /*************************************************************************/
163 /* t1operator_seac */
166 /* Implements the `seac' Type 1 operator for a Type 1 decoder. */
169 /* decoder :: The current CID decoder. */
171 /* asb :: The accent's side bearing. */
173 /* adx :: The horizontal offset of the accent. */
175 /* ady :: The vertical offset of the accent. */
177 /* bchar :: The base character's StandardEncoding charcode. */
179 /* achar :: The accent character's StandardEncoding charcode. */
182 /* FreeType error code. 0 means success. */
185 t1operator_seac( T1_Decoder decoder,
193 FT_Int bchar_index, achar_index;
195 FT_Int n_base_points;
196 FT_Outline* base = decoder->builder.base;
198 FT_Vector left_bearing, advance;
200 #ifdef FT_CONFIG_OPTION_INCREMENTAL
201 T1_Face face = (T1_Face)decoder->builder.face;
207 FT_ERROR(( "t1operator_seac: invalid nested seac\n" ));
208 return FT_THROW( Syntax_Error );
211 if ( decoder->builder.metrics_only )
213 FT_ERROR(( "t1operator_seac: unexpected seac\n" ));
214 return FT_THROW( Syntax_Error );
218 adx += decoder->builder.left_bearing.x;
220 /* `glyph_names' is set to 0 for CID fonts which do not */
221 /* include an encoding. How can we deal with these? */
222 #ifdef FT_CONFIG_OPTION_INCREMENTAL
223 if ( decoder->glyph_names == 0 &&
224 !face->root.internal->incremental_interface )
226 if ( decoder->glyph_names == 0 )
227 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
229 FT_ERROR(( "t1operator_seac:"
230 " glyph names table not available in this font\n" ));
231 return FT_THROW( Syntax_Error );
234 #ifdef FT_CONFIG_OPTION_INCREMENTAL
235 if ( face->root.internal->incremental_interface )
237 /* the caller must handle the font encoding also */
244 bchar_index = t1_lookup_glyph_by_stdcharcode( decoder, bchar );
245 achar_index = t1_lookup_glyph_by_stdcharcode( decoder, achar );
248 if ( bchar_index < 0 || achar_index < 0 )
250 FT_ERROR(( "t1operator_seac:"
251 " invalid seac character code arguments\n" ));
252 return FT_THROW( Syntax_Error );
255 /* if we are trying to load a composite glyph, do not load the */
256 /* accent character and return the array of subglyphs. */
257 if ( decoder->builder.no_recurse )
259 FT_GlyphSlot glyph = (FT_GlyphSlot)decoder->builder.glyph;
260 FT_GlyphLoader loader = glyph->internal->loader;
264 /* reallocate subglyph array if necessary */
265 error = FT_GlyphLoader_CheckSubGlyphs( loader, 2 );
269 subg = loader->current.subglyphs;
271 /* subglyph 0 = base character */
272 subg->index = bchar_index;
273 subg->flags = FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES |
274 FT_SUBGLYPH_FLAG_USE_MY_METRICS;
279 /* subglyph 1 = accent character */
280 subg->index = achar_index;
281 subg->flags = FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES;
282 subg->arg1 = (FT_Int)FIXED_TO_INT( adx - asb );
283 subg->arg2 = (FT_Int)FIXED_TO_INT( ady );
285 /* set up remaining glyph fields */
286 glyph->num_subglyphs = 2;
287 glyph->subglyphs = loader->base.subglyphs;
288 glyph->format = FT_GLYPH_FORMAT_COMPOSITE;
290 loader->current.num_subglyphs = 2;
294 /* First load `bchar' in builder */
295 /* now load the unscaled outline */
297 FT_GlyphLoader_Prepare( decoder->builder.loader ); /* prepare loader */
299 /* the seac operator must not be nested */
300 decoder->seac = TRUE;
301 error = t1_decoder_parse_glyph( decoder, bchar_index );
302 decoder->seac = FALSE;
306 /* save the left bearing and width of the base character */
307 /* as they will be erased by the next load. */
309 left_bearing = decoder->builder.left_bearing;
310 advance = decoder->builder.advance;
312 decoder->builder.left_bearing.x = 0;
313 decoder->builder.left_bearing.y = 0;
315 decoder->builder.pos_x = adx - asb;
316 decoder->builder.pos_y = ady;
318 /* Now load `achar' on top of */
319 /* the base outline */
321 /* the seac operator must not be nested */
322 decoder->seac = TRUE;
323 error = t1_decoder_parse_glyph( decoder, achar_index );
324 decoder->seac = FALSE;
328 /* restore the left side bearing and */
329 /* advance width of the base character */
331 decoder->builder.left_bearing = left_bearing;
332 decoder->builder.advance = advance;
334 decoder->builder.pos_x = 0;
335 decoder->builder.pos_y = 0;
342 /*************************************************************************/
345 /* t1_decoder_parse_charstrings */
348 /* Parses a given Type 1 charstrings program. */
351 /* decoder :: The current Type 1 decoder. */
353 /* charstring_base :: The base address of the charstring stream. */
355 /* charstring_len :: The length in bytes of the charstring stream. */
358 /* FreeType error code. 0 means success. */
360 FT_LOCAL_DEF( FT_Error )
361 t1_decoder_parse_charstrings( T1_Decoder decoder,
362 FT_Byte* charstring_base,
363 FT_UInt charstring_len )
366 T1_Decoder_Zone zone;
369 T1_Builder builder = &decoder->builder;
370 FT_Pos x, y, orig_x, orig_y;
371 FT_Int known_othersubr_result_cnt = 0;
372 FT_Int unknown_othersubr_result_cnt = 0;
376 T1_Hints_Funcs hinter;
378 #ifdef FT_DEBUG_LEVEL_TRACE
383 /* compute random seed from stack address of parameter */
384 seed = (FT_Fixed)( ( (FT_PtrDist)(char*)&seed ^
385 (FT_PtrDist)(char*)&decoder ^
386 (FT_PtrDist)(char*)&charstring_base ) &
388 seed = ( seed ^ ( seed >> 10 ) ^ ( seed >> 20 ) ) & 0xFFFFL;
392 /* First of all, initialize the decoder */
393 decoder->top = decoder->stack;
394 decoder->zone = decoder->zones;
395 zone = decoder->zones;
397 builder->parse_state = T1_Parse_Start;
399 hinter = (T1_Hints_Funcs)builder->hints_funcs;
401 /* a font that reads BuildCharArray without setting */
402 /* its values first is buggy, but ... */
403 FT_ASSERT( ( decoder->len_buildchar == 0 ) ==
404 ( decoder->buildchar == NULL ) );
406 if ( decoder->buildchar && decoder->len_buildchar > 0 )
407 ft_memset( &decoder->buildchar[0],
409 sizeof ( decoder->buildchar[0] ) * decoder->len_buildchar );
412 "Start charstring\n" ));
414 zone->base = charstring_base;
415 limit = zone->limit = charstring_base + charstring_len;
416 ip = zone->cursor = zone->base;
420 x = orig_x = builder->pos_x;
421 y = orig_y = builder->pos_y;
423 /* begin hints recording session, if any */
425 hinter->open( hinter->hints );
429 /* now, execute loop */
432 FT_Long* top = decoder->top;
433 T1_Operator op = op_none;
437 FT_ASSERT( known_othersubr_result_cnt == 0 ||
438 unknown_othersubr_result_cnt == 0 );
440 #ifdef FT_DEBUG_LEVEL_TRACE
443 FT_TRACE5(( " (%d)", decoder->top - decoder->stack ));
448 /*********************************************************************/
450 /* Decode operator or operand */
454 /* first of all, decompress operator or value */
496 case 15: /* undocumented, obsolete operator */
517 FT_ERROR(( "t1_decoder_parse_charstrings:"
518 " invalid escape (12+EOF)\n" ));
543 op = op_callothersubr;
549 op = op_setcurrentpoint;
553 FT_ERROR(( "t1_decoder_parse_charstrings:"
554 " invalid escape (12+%d)\n",
560 case 255: /* four bytes integer */
561 if ( ip + 4 > limit )
563 FT_ERROR(( "t1_decoder_parse_charstrings:"
564 " unexpected EOF in integer\n" ));
568 value = (FT_Int32)( ( (FT_UInt32)ip[0] << 24 ) |
569 ( (FT_UInt32)ip[1] << 16 ) |
570 ( (FT_UInt32)ip[2] << 8 ) |
574 /* According to the specification, values > 32000 or < -32000 must */
575 /* be followed by a `div' operator to make the result be in the */
576 /* range [-32000;32000]. We expect that the second argument of */
577 /* `div' is not a large number. Additionally, we don't handle */
578 /* stuff like `<large1> <large2> <num> div <num> div' or */
579 /* <large1> <large2> <num> div div'. This is probably not allowed */
581 if ( value > 32000 || value < -32000 )
585 FT_ERROR(( "t1_decoder_parse_charstrings:"
586 " no `div' after large integer\n" ));
594 value = (FT_Int32)( (FT_UInt32)value << 16 );
603 value = (FT_Int32)ip[-1] - 139;
608 FT_ERROR(( "t1_decoder_parse_charstrings:"
609 " unexpected EOF in integer\n" ));
614 value = ( ( ip[-2] - 247 ) * 256 ) + ip[-1] + 108;
616 value = -( ( ( ip[-2] - 251 ) * 256 ) + ip[-1] + 108 );
620 value = (FT_Int32)( (FT_UInt32)value << 16 );
624 FT_ERROR(( "t1_decoder_parse_charstrings:"
625 " invalid byte (%d)\n", ip[-1] ));
630 if ( unknown_othersubr_result_cnt > 0 )
641 /* all operands have been transferred by previous pops */
642 unknown_othersubr_result_cnt = 0;
647 if ( large_int && !( op == op_none || op == op_div ) )
649 FT_ERROR(( "t1_decoder_parse_charstrings:"
650 " no `div' after large integer\n" ));
655 /*********************************************************************/
657 /* Push value on stack, or process operator */
662 if ( top - decoder->stack >= T1_MAX_CHARSTRINGS_OPERANDS )
664 FT_ERROR(( "t1_decoder_parse_charstrings: stack overflow\n" ));
668 #ifdef FT_DEBUG_LEVEL_TRACE
670 FT_TRACE4(( " %ld", value ));
672 FT_TRACE4(( " %ld", Fix2Int( value ) ));
678 else if ( op == op_callothersubr ) /* callothersubr */
684 #ifdef FT_DEBUG_LEVEL_TRACE
685 FT_TRACE4(( " callothersubr\n" ));
689 if ( top - decoder->stack < 2 )
690 goto Stack_Underflow;
694 subr_no = Fix2Int( top[1] );
695 arg_cnt = Fix2Int( top[0] );
697 /***********************************************************/
699 /* remove all operands to callothersubr from the stack */
701 /* for handled othersubrs, where we know the number of */
702 /* arguments, we increase the stack by the value of */
703 /* known_othersubr_result_cnt */
705 /* for unhandled othersubrs the following pops adjust the */
706 /* stack pointer as necessary */
708 if ( arg_cnt > top - decoder->stack )
709 goto Stack_Underflow;
713 known_othersubr_result_cnt = 0;
714 unknown_othersubr_result_cnt = 0;
716 /* XXX TODO: The checks to `arg_count == <whatever>' */
717 /* might not be correct; an othersubr expects a certain */
718 /* number of operands on the PostScript stack (as opposed */
719 /* to the T1 stack) but it doesn't have to put them there */
720 /* by itself; previous othersubrs might have left the */
721 /* operands there if they were not followed by an */
722 /* appropriate number of pops */
724 /* On the other hand, Adobe Reader 7.0.8 for Linux doesn't */
725 /* accept a font that contains charstrings like */
727 /* 100 200 2 20 callothersubr */
728 /* 300 1 20 callothersubr pop */
730 /* Perhaps this is the reason why BuildCharArray exists. */
734 case 0: /* end flex feature */
736 goto Unexpected_OtherSubr;
738 if ( decoder->flex_state == 0 ||
739 decoder->num_flex_vectors != 7 )
741 FT_ERROR(( "t1_decoder_parse_charstrings:"
742 " unexpected flex end\n" ));
746 /* the two `results' are popped by the following setcurrentpoint */
749 known_othersubr_result_cnt = 2;
752 case 1: /* start flex feature */
754 goto Unexpected_OtherSubr;
756 decoder->flex_state = 1;
757 decoder->num_flex_vectors = 0;
758 if ( ( error = t1_builder_start_point( builder, x, y ) )
760 ( error = t1_builder_check_points( builder, 6 ) )
765 case 2: /* add flex vectors */
771 goto Unexpected_OtherSubr;
773 if ( decoder->flex_state == 0 )
775 FT_ERROR(( "t1_decoder_parse_charstrings:"
776 " missing flex start\n" ));
780 /* note that we should not add a point for index 0; */
781 /* this will move our current position to the flex */
782 /* point without adding any point to the outline */
783 idx = decoder->num_flex_vectors++;
784 if ( idx > 0 && idx < 7 )
785 t1_builder_add_point( builder,
788 (FT_Byte)( idx == 3 || idx == 6 ) );
792 case 3: /* change hints */
794 goto Unexpected_OtherSubr;
796 known_othersubr_result_cnt = 1;
799 hinter->reset( hinter->hints, builder->current->n_points );
804 /* counter control hints, clear stack */
805 top = decoder->stack;
812 case 18: /* multiple masters */
814 PS_Blend blend = decoder->blend;
815 FT_UInt num_points, nn, mm;
822 FT_ERROR(( "t1_decoder_parse_charstrings:"
823 " unexpected multiple masters operator\n" ));
827 num_points = (FT_UInt)subr_no - 13 + ( subr_no == 18 );
828 if ( arg_cnt != (FT_Int)( num_points * blend->num_designs ) )
830 FT_ERROR(( "t1_decoder_parse_charstrings:"
831 " incorrect number of multiple masters arguments\n" ));
835 /* We want to compute */
837 /* a0*w0 + a1*w1 + ... + ak*wk */
839 /* but we only have a0, a1-a0, a2-a0, ..., ak-a0. */
841 /* However, given that w0 + w1 + ... + wk == 1, we can */
842 /* rewrite it easily as */
844 /* a0 + (a1-a0)*w1 + (a2-a0)*w2 + ... + (ak-a0)*wk */
846 /* where k == num_designs-1. */
848 /* I guess that's why it's written in this `compact' */
851 delta = top + num_points;
853 for ( nn = 0; nn < num_points; nn++ )
855 FT_Long tmp = values[0];
858 for ( mm = 1; mm < blend->num_designs; mm++ )
859 tmp += FT_MulFix( *delta++, blend->weight_vector[mm] );
864 known_othersubr_result_cnt = num_points;
869 /* <idx> 1 19 callothersubr */
870 /* => replace elements starting from index cvi( <idx> ) */
871 /* of BuildCharArray with WeightVector */
874 PS_Blend blend = decoder->blend;
877 if ( arg_cnt != 1 || blend == NULL )
878 goto Unexpected_OtherSubr;
880 idx = Fix2Int( top[0] );
883 idx + blend->num_designs > decoder->len_buildchar )
884 goto Unexpected_OtherSubr;
886 ft_memcpy( &decoder->buildchar[idx],
887 blend->weight_vector,
889 sizeof ( blend->weight_vector[0] ) );
894 /* <arg1> <arg2> 2 20 callothersubr pop */
895 /* ==> push <arg1> + <arg2> onto T1 stack */
897 goto Unexpected_OtherSubr;
899 top[0] += top[1]; /* XXX (over|under)flow */
901 known_othersubr_result_cnt = 1;
905 /* <arg1> <arg2> 2 21 callothersubr pop */
906 /* ==> push <arg1> - <arg2> onto T1 stack */
908 goto Unexpected_OtherSubr;
910 top[0] -= top[1]; /* XXX (over|under)flow */
912 known_othersubr_result_cnt = 1;
916 /* <arg1> <arg2> 2 22 callothersubr pop */
917 /* ==> push <arg1> * <arg2> onto T1 stack */
919 goto Unexpected_OtherSubr;
921 top[0] = FT_MulFix( top[0], top[1] );
923 known_othersubr_result_cnt = 1;
927 /* <arg1> <arg2> 2 23 callothersubr pop */
928 /* ==> push <arg1> / <arg2> onto T1 stack */
929 if ( arg_cnt != 2 || top[1] == 0 )
930 goto Unexpected_OtherSubr;
932 top[0] = FT_DivFix( top[0], top[1] );
934 known_othersubr_result_cnt = 1;
938 /* <val> <idx> 2 24 callothersubr */
939 /* ==> set BuildCharArray[cvi( <idx> )] = <val> */
942 PS_Blend blend = decoder->blend;
945 if ( arg_cnt != 2 || blend == NULL )
946 goto Unexpected_OtherSubr;
948 idx = Fix2Int( top[1] );
950 if ( idx < 0 || (FT_UInt) idx >= decoder->len_buildchar )
951 goto Unexpected_OtherSubr;
953 decoder->buildchar[idx] = top[0];
958 /* <idx> 1 25 callothersubr pop */
959 /* ==> push BuildCharArray[cvi( idx )] */
963 PS_Blend blend = decoder->blend;
966 if ( arg_cnt != 1 || blend == NULL )
967 goto Unexpected_OtherSubr;
969 idx = Fix2Int( top[0] );
971 if ( idx < 0 || (FT_UInt) idx >= decoder->len_buildchar )
972 goto Unexpected_OtherSubr;
974 top[0] = decoder->buildchar[idx];
977 known_othersubr_result_cnt = 1;
982 /* <val> mark <idx> ==> set BuildCharArray[cvi( <idx> )] = <val>, */
983 /* leave mark on T1 stack */
984 /* <val> <idx> ==> set BuildCharArray[cvi( <idx> )] = <val> */
985 XXX which routine has left its mark on the (PostScript) stack?;
990 /* <res1> <res2> <val1> <val2> 4 27 callothersubr pop */
991 /* ==> push <res1> onto T1 stack if <val1> <= <val2>, */
992 /* otherwise push <res2> */
994 goto Unexpected_OtherSubr;
996 if ( top[2] > top[3] )
999 known_othersubr_result_cnt = 1;
1003 /* 0 28 callothersubr pop */
1004 /* => push random value from interval [0, 1) onto stack */
1006 goto Unexpected_OtherSubr;
1013 if ( Rand >= 0x8000L )
1018 seed = FT_MulFix( seed, 0x10000L - seed );
1023 known_othersubr_result_cnt = 1;
1027 if ( arg_cnt >= 0 && subr_no >= 0 )
1029 FT_ERROR(( "t1_decoder_parse_charstrings:"
1030 " unknown othersubr [%d %d], wish me luck\n",
1031 arg_cnt, subr_no ));
1032 unknown_othersubr_result_cnt = arg_cnt;
1037 Unexpected_OtherSubr:
1038 FT_ERROR(( "t1_decoder_parse_charstrings:"
1039 " invalid othersubr [%d %d]\n", arg_cnt, subr_no ));
1043 top += known_othersubr_result_cnt;
1047 else /* general operator */
1049 FT_Int num_args = t1_args_count[op];
1052 FT_ASSERT( num_args >= 0 );
1054 if ( top - decoder->stack < num_args )
1055 goto Stack_Underflow;
1057 /* XXX Operators usually take their operands from the */
1058 /* bottom of the stack, i.e., the operands are */
1059 /* decoder->stack[0], ..., decoder->stack[num_args - 1]; */
1060 /* only div, callsubr, and callothersubr are different. */
1061 /* In practice it doesn't matter (?). */
1063 #ifdef FT_DEBUG_LEVEL_TRACE
1069 case op_callothersubr:
1075 if ( top - decoder->stack != num_args )
1076 FT_TRACE0(( "t1_decoder_parse_charstrings:"
1077 " too much operands on the stack"
1078 " (seen %d, expected %d)\n",
1079 top - decoder->stack, num_args ));
1083 #endif /* FT_DEBUG_LEVEL_TRACE */
1090 FT_TRACE4(( " endchar\n" ));
1092 t1_builder_close_contour( builder );
1094 /* close hints recording session */
1097 if ( hinter->close( hinter->hints, builder->current->n_points ) )
1100 /* apply hints to the loaded glyph outline now */
1101 error = hinter->apply( hinter->hints,
1103 (PSH_Globals)builder->hints_globals,
1104 decoder->hint_mode );
1109 /* add current outline to the glyph slot */
1110 FT_GlyphLoader_Add( builder->loader );
1112 /* the compiler should optimize away this empty loop but ... */
1114 #ifdef FT_DEBUG_LEVEL_TRACE
1116 if ( decoder->len_buildchar > 0 )
1121 FT_TRACE4(( "BuildCharArray = [ " ));
1123 for ( i = 0; i < decoder->len_buildchar; ++i )
1124 FT_TRACE4(( "%d ", decoder->buildchar[i] ));
1126 FT_TRACE4(( "]\n" ));
1129 #endif /* FT_DEBUG_LEVEL_TRACE */
1131 FT_TRACE4(( "\n" ));
1137 FT_TRACE4(( " hsbw" ));
1139 builder->parse_state = T1_Parse_Have_Width;
1141 builder->left_bearing.x += top[0];
1142 builder->advance.x = top[1];
1143 builder->advance.y = 0;
1145 orig_x = x = builder->pos_x + top[0];
1146 orig_y = y = builder->pos_y;
1148 FT_UNUSED( orig_y );
1150 /* the `metrics_only' indicates that we only want to compute */
1151 /* the glyph's metrics (lsb + advance width), not load the */
1152 /* rest of it; so exit immediately */
1153 if ( builder->metrics_only )
1159 return t1operator_seac( decoder,
1164 Fix2Int( top[4] ) );
1167 FT_TRACE4(( " sbw" ));
1169 builder->parse_state = T1_Parse_Have_Width;
1171 builder->left_bearing.x += top[0];
1172 builder->left_bearing.y += top[1];
1173 builder->advance.x = top[2];
1174 builder->advance.y = top[3];
1176 x = builder->pos_x + top[0];
1177 y = builder->pos_y + top[1];
1179 /* the `metrics_only' indicates that we only want to compute */
1180 /* the glyph's metrics (lsb + advance width), not load the */
1181 /* rest of it; so exit immediately */
1182 if ( builder->metrics_only )
1188 FT_TRACE4(( " closepath" ));
1190 /* if there is no path, `closepath' is a no-op */
1191 if ( builder->parse_state == T1_Parse_Have_Path ||
1192 builder->parse_state == T1_Parse_Have_Moveto )
1193 t1_builder_close_contour( builder );
1195 builder->parse_state = T1_Parse_Have_Width;
1199 FT_TRACE4(( " hlineto" ));
1201 if ( ( error = t1_builder_start_point( builder, x, y ) )
1209 FT_TRACE4(( " hmoveto" ));
1212 if ( !decoder->flex_state )
1214 if ( builder->parse_state == T1_Parse_Start )
1216 builder->parse_state = T1_Parse_Have_Moveto;
1221 FT_TRACE4(( " hvcurveto" ));
1223 if ( ( error = t1_builder_start_point( builder, x, y ) )
1225 ( error = t1_builder_check_points( builder, 3 ) )
1230 t1_builder_add_point( builder, x, y, 0 );
1233 t1_builder_add_point( builder, x, y, 0 );
1235 t1_builder_add_point( builder, x, y, 1 );
1239 FT_TRACE4(( " rlineto" ));
1241 if ( ( error = t1_builder_start_point( builder, x, y ) )
1249 if ( ( error = t1_builder_add_point1( builder, x, y ) )
1255 FT_TRACE4(( " rmoveto" ));
1259 if ( !decoder->flex_state )
1261 if ( builder->parse_state == T1_Parse_Start )
1263 builder->parse_state = T1_Parse_Have_Moveto;
1268 FT_TRACE4(( " rrcurveto" ));
1270 if ( ( error = t1_builder_start_point( builder, x, y ) )
1272 ( error = t1_builder_check_points( builder, 3 ) )
1278 t1_builder_add_point( builder, x, y, 0 );
1282 t1_builder_add_point( builder, x, y, 0 );
1286 t1_builder_add_point( builder, x, y, 1 );
1290 FT_TRACE4(( " vhcurveto" ));
1292 if ( ( error = t1_builder_start_point( builder, x, y ) )
1294 ( error = t1_builder_check_points( builder, 3 ) )
1299 t1_builder_add_point( builder, x, y, 0 );
1302 t1_builder_add_point( builder, x, y, 0 );
1304 t1_builder_add_point( builder, x, y, 1 );
1308 FT_TRACE4(( " vlineto" ));
1310 if ( ( error = t1_builder_start_point( builder, x, y ) )
1318 FT_TRACE4(( " vmoveto" ));
1321 if ( !decoder->flex_state )
1323 if ( builder->parse_state == T1_Parse_Start )
1325 builder->parse_state = T1_Parse_Have_Moveto;
1330 FT_TRACE4(( " div" ));
1332 /* if `large_int' is set, we divide unscaled numbers; */
1333 /* otherwise, we divide numbers in 16.16 format -- */
1334 /* in both cases, it is the same operation */
1335 *top = FT_DivFix( top[0], top[1] );
1346 FT_TRACE4(( " callsubr" ));
1348 idx = Fix2Int( top[0] );
1349 if ( idx < 0 || idx >= (FT_Int)decoder->num_subrs )
1351 FT_ERROR(( "t1_decoder_parse_charstrings:"
1352 " invalid subrs index\n" ));
1356 if ( zone - decoder->zones >= T1_MAX_SUBRS_CALLS )
1358 FT_ERROR(( "t1_decoder_parse_charstrings:"
1359 " too many nested subrs\n" ));
1363 zone->cursor = ip; /* save current instruction pointer */
1367 /* The Type 1 driver stores subroutines without the seed bytes. */
1368 /* The CID driver stores subroutines with seed bytes. This */
1369 /* case is taken care of when decoder->subrs_len == 0. */
1370 zone->base = decoder->subrs[idx];
1372 if ( decoder->subrs_len )
1373 zone->limit = zone->base + decoder->subrs_len[idx];
1376 /* We are using subroutines from a CID font. We must adjust */
1377 /* for the seed bytes. */
1378 zone->base += ( decoder->lenIV >= 0 ? decoder->lenIV : 0 );
1379 zone->limit = decoder->subrs[idx + 1];
1382 zone->cursor = zone->base;
1386 FT_ERROR(( "t1_decoder_parse_charstrings:"
1387 " invoking empty subrs\n" ));
1391 decoder->zone = zone;
1393 limit = zone->limit;
1398 FT_TRACE4(( " pop" ));
1400 if ( known_othersubr_result_cnt > 0 )
1402 known_othersubr_result_cnt--;
1403 /* ignore, we pushed the operands ourselves */
1407 if ( unknown_othersubr_result_cnt == 0 )
1409 FT_ERROR(( "t1_decoder_parse_charstrings:"
1410 " no more operands for othersubr\n" ));
1414 unknown_othersubr_result_cnt--;
1415 top++; /* `push' the operand to callothersubr onto the stack */
1419 FT_TRACE4(( " return" ));
1421 if ( zone <= decoder->zones )
1423 FT_ERROR(( "t1_decoder_parse_charstrings:"
1424 " unexpected return\n" ));
1430 limit = zone->limit;
1431 decoder->zone = zone;
1435 FT_TRACE4(( " dotsection" ));
1440 FT_TRACE4(( " hstem" ));
1442 /* record horizontal hint */
1445 /* top[0] += builder->left_bearing.y; */
1446 hinter->stem( hinter->hints, 1, top );
1451 FT_TRACE4(( " hstem3" ));
1453 /* record horizontal counter-controlled hints */
1455 hinter->stem3( hinter->hints, 1, top );
1459 FT_TRACE4(( " vstem" ));
1461 /* record vertical hint */
1465 hinter->stem( hinter->hints, 0, top );
1470 FT_TRACE4(( " vstem3" ));
1472 /* record vertical counter-controlled hints */
1481 hinter->stem3( hinter->hints, 0, top );
1485 case op_setcurrentpoint:
1486 FT_TRACE4(( " setcurrentpoint" ));
1488 /* From the T1 specification, section 6.4: */
1490 /* The setcurrentpoint command is used only in */
1491 /* conjunction with results from OtherSubrs procedures. */
1493 /* known_othersubr_result_cnt != 0 is already handled */
1496 /* Note, however, that both Ghostscript and Adobe */
1497 /* Distiller handle this situation by silently ignoring */
1498 /* the inappropriate `setcurrentpoint' instruction. So */
1499 /* we do the same. */
1502 if ( decoder->flex_state != 1 )
1504 FT_ERROR(( "t1_decoder_parse_charstrings:"
1505 " unexpected `setcurrentpoint'\n" ));
1514 decoder->flex_state = 0;
1518 FT_TRACE4(( " opcode_15" ));
1519 /* nothing to do except to pop the two arguments */
1523 FT_ERROR(( "t1_decoder_parse_charstrings:"
1524 " unhandled opcode %d\n", op ));
1528 /* XXX Operators usually clear the operand stack; */
1529 /* only div, callsubr, callothersubr, pop, and */
1530 /* return are different. */
1531 /* In practice it doesn't matter (?). */
1535 #ifdef FT_DEBUG_LEVEL_TRACE
1536 FT_TRACE4(( "\n" ));
1540 } /* general operator processing */
1542 } /* while ip < limit */
1544 FT_TRACE4(( "..end..\n\n" ));
1550 return FT_THROW( Syntax_Error );
1553 return FT_THROW( Stack_Underflow );
1557 /* parse a single Type 1 glyph */
1558 FT_LOCAL_DEF( FT_Error )
1559 t1_decoder_parse_glyph( T1_Decoder decoder,
1562 return decoder->parse_callback( decoder, glyph );
1566 /* initialize T1 decoder */
1567 FT_LOCAL_DEF( FT_Error )
1568 t1_decoder_init( T1_Decoder decoder,
1572 FT_Byte** glyph_names,
1575 FT_Render_Mode hint_mode,
1576 T1_Decoder_Callback parse_callback )
1578 FT_MEM_ZERO( decoder, sizeof ( *decoder ) );
1580 /* retrieve PSNames interface from list of current modules */
1582 FT_Service_PsCMaps psnames = 0;
1585 FT_FACE_FIND_GLOBAL_SERVICE( face, psnames, POSTSCRIPT_CMAPS );
1588 FT_ERROR(( "t1_decoder_init:"
1589 " the `psnames' module is not available\n" ));
1590 return FT_THROW( Unimplemented_Feature );
1593 decoder->psnames = psnames;
1596 t1_builder_init( &decoder->builder, face, size, slot, hinting );
1598 /* decoder->buildchar and decoder->len_buildchar have to be */
1599 /* initialized by the caller since we cannot know the length */
1600 /* of the BuildCharArray */
1602 decoder->num_glyphs = (FT_UInt)face->num_glyphs;
1603 decoder->glyph_names = glyph_names;
1604 decoder->hint_mode = hint_mode;
1605 decoder->blend = blend;
1606 decoder->parse_callback = parse_callback;
1608 decoder->funcs = t1_decoder_funcs;
1614 /* finalize T1 decoder */
1615 FT_LOCAL_DEF( void )
1616 t1_decoder_done( T1_Decoder decoder )
1618 t1_builder_done( &decoder->builder );