1 /***************************************************************************/
5 /* PostScript Type 1 decoding routines (body). */
7 /* Copyright 2000-2011 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 PSaux_Err_Syntax_Error;
212 adx += decoder->builder.left_bearing.x;
214 /* `glyph_names' is set to 0 for CID fonts which do not */
215 /* include an encoding. How can we deal with these? */
216 #ifdef FT_CONFIG_OPTION_INCREMENTAL
217 if ( decoder->glyph_names == 0 &&
218 !face->root.internal->incremental_interface )
220 if ( decoder->glyph_names == 0 )
221 #endif /* FT_CONFIG_OPTION_INCREMENTAL */
223 FT_ERROR(( "t1operator_seac:"
224 " glyph names table not available in this font\n" ));
225 return PSaux_Err_Syntax_Error;
228 #ifdef FT_CONFIG_OPTION_INCREMENTAL
229 if ( face->root.internal->incremental_interface )
231 /* the caller must handle the font encoding also */
238 bchar_index = t1_lookup_glyph_by_stdcharcode( decoder, bchar );
239 achar_index = t1_lookup_glyph_by_stdcharcode( decoder, achar );
242 if ( bchar_index < 0 || achar_index < 0 )
244 FT_ERROR(( "t1operator_seac:"
245 " invalid seac character code arguments\n" ));
246 return PSaux_Err_Syntax_Error;
249 /* if we are trying to load a composite glyph, do not load the */
250 /* accent character and return the array of subglyphs. */
251 if ( decoder->builder.no_recurse )
253 FT_GlyphSlot glyph = (FT_GlyphSlot)decoder->builder.glyph;
254 FT_GlyphLoader loader = glyph->internal->loader;
258 /* reallocate subglyph array if necessary */
259 error = FT_GlyphLoader_CheckSubGlyphs( loader, 2 );
263 subg = loader->current.subglyphs;
265 /* subglyph 0 = base character */
266 subg->index = bchar_index;
267 subg->flags = FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES |
268 FT_SUBGLYPH_FLAG_USE_MY_METRICS;
273 /* subglyph 1 = accent character */
274 subg->index = achar_index;
275 subg->flags = FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES;
276 subg->arg1 = (FT_Int)FIXED_TO_INT( adx - asb );
277 subg->arg2 = (FT_Int)FIXED_TO_INT( ady );
279 /* set up remaining glyph fields */
280 glyph->num_subglyphs = 2;
281 glyph->subglyphs = loader->base.subglyphs;
282 glyph->format = FT_GLYPH_FORMAT_COMPOSITE;
284 loader->current.num_subglyphs = 2;
288 /* First load `bchar' in builder */
289 /* now load the unscaled outline */
291 FT_GlyphLoader_Prepare( decoder->builder.loader ); /* prepare loader */
293 /* the seac operator must not be nested */
294 decoder->seac = TRUE;
295 error = t1_decoder_parse_glyph( decoder, bchar_index );
296 decoder->seac = FALSE;
300 /* save the left bearing and width of the base character */
301 /* as they will be erased by the next load. */
303 left_bearing = decoder->builder.left_bearing;
304 advance = decoder->builder.advance;
306 decoder->builder.left_bearing.x = 0;
307 decoder->builder.left_bearing.y = 0;
309 decoder->builder.pos_x = adx - asb;
310 decoder->builder.pos_y = ady;
312 /* Now load `achar' on top of */
313 /* the base outline */
315 /* the seac operator must not be nested */
316 decoder->seac = TRUE;
317 error = t1_decoder_parse_glyph( decoder, achar_index );
318 decoder->seac = FALSE;
322 /* restore the left side bearing and */
323 /* advance width of the base character */
325 decoder->builder.left_bearing = left_bearing;
326 decoder->builder.advance = advance;
328 decoder->builder.pos_x = 0;
329 decoder->builder.pos_y = 0;
336 /*************************************************************************/
339 /* t1_decoder_parse_charstrings */
342 /* Parses a given Type 1 charstrings program. */
345 /* decoder :: The current Type 1 decoder. */
347 /* charstring_base :: The base address of the charstring stream. */
349 /* charstring_len :: The length in bytes of the charstring stream. */
352 /* FreeType error code. 0 means success. */
354 FT_LOCAL_DEF( FT_Error )
355 t1_decoder_parse_charstrings( T1_Decoder decoder,
356 FT_Byte* charstring_base,
357 FT_UInt charstring_len )
360 T1_Decoder_Zone zone;
363 T1_Builder builder = &decoder->builder;
364 FT_Pos x, y, orig_x, orig_y;
365 FT_Int known_othersubr_result_cnt = 0;
366 FT_Int unknown_othersubr_result_cnt = 0;
370 T1_Hints_Funcs hinter;
372 #ifdef FT_DEBUG_LEVEL_TRACE
377 /* compute random seed from stack address of parameter */
378 seed = (FT_Fixed)( ( (FT_PtrDist)(char*)&seed ^
379 (FT_PtrDist)(char*)&decoder ^
380 (FT_PtrDist)(char*)&charstring_base ) &
382 seed = ( seed ^ ( seed >> 10 ) ^ ( seed >> 20 ) ) & 0xFFFFL;
386 /* First of all, initialize the decoder */
387 decoder->top = decoder->stack;
388 decoder->zone = decoder->zones;
389 zone = decoder->zones;
391 builder->parse_state = T1_Parse_Start;
393 hinter = (T1_Hints_Funcs)builder->hints_funcs;
395 /* a font that reads BuildCharArray without setting */
396 /* its values first is buggy, but ... */
397 FT_ASSERT( ( decoder->len_buildchar == 0 ) ==
398 ( decoder->buildchar == NULL ) );
400 if ( decoder->buildchar && decoder->len_buildchar > 0 )
401 ft_memset( &decoder->buildchar[0],
403 sizeof ( decoder->buildchar[0] ) * decoder->len_buildchar );
406 "Start charstring\n" ));
408 zone->base = charstring_base;
409 limit = zone->limit = charstring_base + charstring_len;
410 ip = zone->cursor = zone->base;
412 error = PSaux_Err_Ok;
414 x = orig_x = builder->pos_x;
415 y = orig_y = builder->pos_y;
417 /* begin hints recording session, if any */
419 hinter->open( hinter->hints );
423 /* now, execute loop */
426 FT_Long* top = decoder->top;
427 T1_Operator op = op_none;
431 FT_ASSERT( known_othersubr_result_cnt == 0 ||
432 unknown_othersubr_result_cnt == 0 );
434 #ifdef FT_DEBUG_LEVEL_TRACE
437 FT_TRACE5(( " (%d)", decoder->top - decoder->stack ));
442 /*********************************************************************/
444 /* Decode operator or operand */
448 /* first of all, decompress operator or value */
490 case 15: /* undocumented, obsolete operator */
511 FT_ERROR(( "t1_decoder_parse_charstrings:"
512 " invalid escape (12+EOF)\n" ));
537 op = op_callothersubr;
543 op = op_setcurrentpoint;
547 FT_ERROR(( "t1_decoder_parse_charstrings:"
548 " invalid escape (12+%d)\n",
554 case 255: /* four bytes integer */
555 if ( ip + 4 > limit )
557 FT_ERROR(( "t1_decoder_parse_charstrings:"
558 " unexpected EOF in integer\n" ));
562 value = (FT_Int32)( ( (FT_Long)ip[0] << 24 ) |
563 ( (FT_Long)ip[1] << 16 ) |
564 ( (FT_Long)ip[2] << 8 ) |
568 /* According to the specification, values > 32000 or < -32000 must */
569 /* be followed by a `div' operator to make the result be in the */
570 /* range [-32000;32000]. We expect that the second argument of */
571 /* `div' is not a large number. Additionally, we don't handle */
572 /* stuff like `<large1> <large2> <num> div <num> div' or */
573 /* <large1> <large2> <num> div div'. This is probably not allowed */
575 if ( value > 32000 || value < -32000 )
579 FT_ERROR(( "t1_decoder_parse_charstrings:"
580 " no `div' after large integer\n" ));
597 value = (FT_Int32)ip[-1] - 139;
602 FT_ERROR(( "t1_decoder_parse_charstrings:"
603 " unexpected EOF in integer\n" ));
608 value = ( ( (FT_Int32)ip[-2] - 247 ) << 8 ) + ip[-1] + 108;
610 value = -( ( ( (FT_Int32)ip[-2] - 251 ) << 8 ) + ip[-1] + 108 );
618 FT_ERROR(( "t1_decoder_parse_charstrings:"
619 " invalid byte (%d)\n", ip[-1] ));
624 if ( unknown_othersubr_result_cnt > 0 )
635 /* all operands have been transferred by previous pops */
636 unknown_othersubr_result_cnt = 0;
641 if ( large_int && !( op == op_none || op == op_div ) )
643 FT_ERROR(( "t1_decoder_parse_charstrings:"
644 " no `div' after large integer\n" ));
649 /*********************************************************************/
651 /* Push value on stack, or process operator */
656 if ( top - decoder->stack >= T1_MAX_CHARSTRINGS_OPERANDS )
658 FT_ERROR(( "t1_decoder_parse_charstrings: stack overflow\n" ));
662 #ifdef FT_DEBUG_LEVEL_TRACE
664 FT_TRACE4(( " %ld", value ));
666 FT_TRACE4(( " %ld", Fix2Int( value ) ));
672 else if ( op == op_callothersubr ) /* callothersubr */
678 #ifdef FT_DEBUG_LEVEL_TRACE
679 FT_TRACE4(( " callothersubr\n" ));
683 if ( top - decoder->stack < 2 )
684 goto Stack_Underflow;
688 subr_no = Fix2Int( top[1] );
689 arg_cnt = Fix2Int( top[0] );
691 /***********************************************************/
693 /* remove all operands to callothersubr from the stack */
695 /* for handled othersubrs, where we know the number of */
696 /* arguments, we increase the stack by the value of */
697 /* known_othersubr_result_cnt */
699 /* for unhandled othersubrs the following pops adjust the */
700 /* stack pointer as necessary */
702 if ( arg_cnt > top - decoder->stack )
703 goto Stack_Underflow;
707 known_othersubr_result_cnt = 0;
708 unknown_othersubr_result_cnt = 0;
710 /* XXX TODO: The checks to `arg_count == <whatever>' */
711 /* might not be correct; an othersubr expects a certain */
712 /* number of operands on the PostScript stack (as opposed */
713 /* to the T1 stack) but it doesn't have to put them there */
714 /* by itself; previous othersubrs might have left the */
715 /* operands there if they were not followed by an */
716 /* appropriate number of pops */
718 /* On the other hand, Adobe Reader 7.0.8 for Linux doesn't */
719 /* accept a font that contains charstrings like */
721 /* 100 200 2 20 callothersubr */
722 /* 300 1 20 callothersubr pop */
724 /* Perhaps this is the reason why BuildCharArray exists. */
728 case 0: /* end flex feature */
730 goto Unexpected_OtherSubr;
732 if ( decoder->flex_state == 0 ||
733 decoder->num_flex_vectors != 7 )
735 FT_ERROR(( "t1_decoder_parse_charstrings:"
736 " unexpected flex end\n" ));
740 /* the two `results' are popped by the following setcurrentpoint */
743 known_othersubr_result_cnt = 2;
746 case 1: /* start flex feature */
748 goto Unexpected_OtherSubr;
750 decoder->flex_state = 1;
751 decoder->num_flex_vectors = 0;
752 if ( ( error = t1_builder_start_point( builder, x, y ) )
754 ( error = t1_builder_check_points( builder, 6 ) )
759 case 2: /* add flex vectors */
765 goto Unexpected_OtherSubr;
767 if ( decoder->flex_state == 0 )
769 FT_ERROR(( "t1_decoder_parse_charstrings:"
770 " missing flex start\n" ));
774 /* note that we should not add a point for index 0; */
775 /* this will move our current position to the flex */
776 /* point without adding any point to the outline */
777 idx = decoder->num_flex_vectors++;
778 if ( idx > 0 && idx < 7 )
779 t1_builder_add_point( builder,
782 (FT_Byte)( idx == 3 || idx == 6 ) );
786 case 3: /* change hints */
788 goto Unexpected_OtherSubr;
790 known_othersubr_result_cnt = 1;
793 hinter->reset( hinter->hints, builder->current->n_points );
798 /* counter control hints, clear stack */
799 top = decoder->stack;
806 case 18: /* multiple masters */
808 PS_Blend blend = decoder->blend;
809 FT_UInt num_points, nn, mm;
816 FT_ERROR(( "t1_decoder_parse_charstrings:"
817 " unexpected multiple masters operator\n" ));
821 num_points = (FT_UInt)subr_no - 13 + ( subr_no == 18 );
822 if ( arg_cnt != (FT_Int)( num_points * blend->num_designs ) )
824 FT_ERROR(( "t1_decoder_parse_charstrings:"
825 " incorrect number of multiple masters arguments\n" ));
829 /* We want to compute */
831 /* a0*w0 + a1*w1 + ... + ak*wk */
833 /* but we only have a0, a1-a0, a2-a0, ..., ak-a0. */
835 /* However, given that w0 + w1 + ... + wk == 1, we can */
836 /* rewrite it easily as */
838 /* a0 + (a1-a0)*w1 + (a2-a0)*w2 + ... + (ak-a0)*wk */
840 /* where k == num_designs-1. */
842 /* I guess that's why it's written in this `compact' */
845 delta = top + num_points;
847 for ( nn = 0; nn < num_points; nn++ )
849 FT_Long tmp = values[0];
852 for ( mm = 1; mm < blend->num_designs; mm++ )
853 tmp += FT_MulFix( *delta++, blend->weight_vector[mm] );
858 known_othersubr_result_cnt = num_points;
863 /* <idx> 1 19 callothersubr */
864 /* => replace elements starting from index cvi( <idx> ) */
865 /* of BuildCharArray with WeightVector */
868 PS_Blend blend = decoder->blend;
871 if ( arg_cnt != 1 || blend == NULL )
872 goto Unexpected_OtherSubr;
874 idx = Fix2Int( top[0] );
877 idx + blend->num_designs > decoder->len_buildchar )
878 goto Unexpected_OtherSubr;
880 ft_memcpy( &decoder->buildchar[idx],
881 blend->weight_vector,
883 sizeof ( blend->weight_vector[0] ) );
888 /* <arg1> <arg2> 2 20 callothersubr pop */
889 /* ==> push <arg1> + <arg2> onto T1 stack */
891 goto Unexpected_OtherSubr;
893 top[0] += top[1]; /* XXX (over|under)flow */
895 known_othersubr_result_cnt = 1;
899 /* <arg1> <arg2> 2 21 callothersubr pop */
900 /* ==> push <arg1> - <arg2> onto T1 stack */
902 goto Unexpected_OtherSubr;
904 top[0] -= top[1]; /* XXX (over|under)flow */
906 known_othersubr_result_cnt = 1;
910 /* <arg1> <arg2> 2 22 callothersubr pop */
911 /* ==> push <arg1> * <arg2> onto T1 stack */
913 goto Unexpected_OtherSubr;
915 top[0] = FT_MulFix( top[0], top[1] );
917 known_othersubr_result_cnt = 1;
921 /* <arg1> <arg2> 2 23 callothersubr pop */
922 /* ==> push <arg1> / <arg2> onto T1 stack */
923 if ( arg_cnt != 2 || top[1] == 0 )
924 goto Unexpected_OtherSubr;
926 top[0] = FT_DivFix( top[0], top[1] );
928 known_othersubr_result_cnt = 1;
932 /* <val> <idx> 2 24 callothersubr */
933 /* ==> set BuildCharArray[cvi( <idx> )] = <val> */
936 PS_Blend blend = decoder->blend;
939 if ( arg_cnt != 2 || blend == NULL )
940 goto Unexpected_OtherSubr;
942 idx = Fix2Int( top[1] );
944 if ( idx < 0 || (FT_UInt) idx >= decoder->len_buildchar )
945 goto Unexpected_OtherSubr;
947 decoder->buildchar[idx] = top[0];
952 /* <idx> 1 25 callothersubr pop */
953 /* ==> push BuildCharArray[cvi( idx )] */
957 PS_Blend blend = decoder->blend;
960 if ( arg_cnt != 1 || blend == NULL )
961 goto Unexpected_OtherSubr;
963 idx = Fix2Int( top[0] );
965 if ( idx < 0 || (FT_UInt) idx >= decoder->len_buildchar )
966 goto Unexpected_OtherSubr;
968 top[0] = decoder->buildchar[idx];
971 known_othersubr_result_cnt = 1;
976 /* <val> mark <idx> ==> set BuildCharArray[cvi( <idx> )] = <val>, */
977 /* leave mark on T1 stack */
978 /* <val> <idx> ==> set BuildCharArray[cvi( <idx> )] = <val> */
979 XXX which routine has left its mark on the (PostScript) stack?;
984 /* <res1> <res2> <val1> <val2> 4 27 callothersubr pop */
985 /* ==> push <res1> onto T1 stack if <val1> <= <val2>, */
986 /* otherwise push <res2> */
988 goto Unexpected_OtherSubr;
990 if ( top[2] > top[3] )
993 known_othersubr_result_cnt = 1;
997 /* 0 28 callothersubr pop */
998 /* => push random value from interval [0, 1) onto stack */
1000 goto Unexpected_OtherSubr;
1007 if ( Rand >= 0x8000L )
1012 seed = FT_MulFix( seed, 0x10000L - seed );
1017 known_othersubr_result_cnt = 1;
1021 if ( arg_cnt >= 0 && subr_no >= 0 )
1023 FT_ERROR(( "t1_decoder_parse_charstrings:"
1024 " unknown othersubr [%d %d], wish me luck\n",
1025 arg_cnt, subr_no ));
1026 unknown_othersubr_result_cnt = arg_cnt;
1031 Unexpected_OtherSubr:
1032 FT_ERROR(( "t1_decoder_parse_charstrings:"
1033 " invalid othersubr [%d %d]\n", arg_cnt, subr_no ));
1037 top += known_othersubr_result_cnt;
1041 else /* general operator */
1043 FT_Int num_args = t1_args_count[op];
1046 FT_ASSERT( num_args >= 0 );
1048 if ( top - decoder->stack < num_args )
1049 goto Stack_Underflow;
1051 /* XXX Operators usually take their operands from the */
1052 /* bottom of the stack, i.e., the operands are */
1053 /* decoder->stack[0], ..., decoder->stack[num_args - 1]; */
1054 /* only div, callsubr, and callothersubr are different. */
1055 /* In practice it doesn't matter (?). */
1057 #ifdef FT_DEBUG_LEVEL_TRACE
1063 case op_callothersubr:
1069 if ( top - decoder->stack != num_args )
1070 FT_TRACE0(( "t1_decoder_parse_charstrings:"
1071 " too much operands on the stack"
1072 " (seen %d, expected %d)\n",
1073 top - decoder->stack, num_args ));
1077 #endif /* FT_DEBUG_LEVEL_TRACE */
1084 FT_TRACE4(( " endchar\n" ));
1086 t1_builder_close_contour( builder );
1088 /* close hints recording session */
1091 if ( hinter->close( hinter->hints, builder->current->n_points ) )
1094 /* apply hints to the loaded glyph outline now */
1095 hinter->apply( hinter->hints,
1097 (PSH_Globals)builder->hints_globals,
1098 decoder->hint_mode );
1101 /* add current outline to the glyph slot */
1102 FT_GlyphLoader_Add( builder->loader );
1104 /* the compiler should optimize away this empty loop but ... */
1106 #ifdef FT_DEBUG_LEVEL_TRACE
1108 if ( decoder->len_buildchar > 0 )
1113 FT_TRACE4(( "BuildCharArray = [ " ));
1115 for ( i = 0; i < decoder->len_buildchar; ++i )
1116 FT_TRACE4(( "%d ", decoder->buildchar[i] ));
1118 FT_TRACE4(( "]\n" ));
1121 #endif /* FT_DEBUG_LEVEL_TRACE */
1123 FT_TRACE4(( "\n" ));
1126 return PSaux_Err_Ok;
1129 FT_TRACE4(( " hsbw" ));
1131 builder->parse_state = T1_Parse_Have_Width;
1133 builder->left_bearing.x += top[0];
1134 builder->advance.x = top[1];
1135 builder->advance.y = 0;
1137 orig_x = x = builder->pos_x + top[0];
1138 orig_y = y = builder->pos_y;
1140 FT_UNUSED( orig_y );
1142 /* the `metrics_only' indicates that we only want to compute */
1143 /* the glyph's metrics (lsb + advance width), not load the */
1144 /* rest of it; so exit immediately */
1145 if ( builder->metrics_only )
1146 return PSaux_Err_Ok;
1151 return t1operator_seac( decoder,
1156 Fix2Int( top[4] ) );
1159 FT_TRACE4(( " sbw" ));
1161 builder->parse_state = T1_Parse_Have_Width;
1163 builder->left_bearing.x += top[0];
1164 builder->left_bearing.y += top[1];
1165 builder->advance.x = top[2];
1166 builder->advance.y = top[3];
1168 x = builder->pos_x + top[0];
1169 y = builder->pos_y + top[1];
1171 /* the `metrics_only' indicates that we only want to compute */
1172 /* the glyph's metrics (lsb + advance width), not load the */
1173 /* rest of it; so exit immediately */
1174 if ( builder->metrics_only )
1175 return PSaux_Err_Ok;
1180 FT_TRACE4(( " closepath" ));
1182 /* if there is no path, `closepath' is a no-op */
1183 if ( builder->parse_state == T1_Parse_Have_Path ||
1184 builder->parse_state == T1_Parse_Have_Moveto )
1185 t1_builder_close_contour( builder );
1187 builder->parse_state = T1_Parse_Have_Width;
1191 FT_TRACE4(( " hlineto" ));
1193 if ( ( error = t1_builder_start_point( builder, x, y ) )
1201 FT_TRACE4(( " hmoveto" ));
1204 if ( !decoder->flex_state )
1206 if ( builder->parse_state == T1_Parse_Start )
1208 builder->parse_state = T1_Parse_Have_Moveto;
1213 FT_TRACE4(( " hvcurveto" ));
1215 if ( ( error = t1_builder_start_point( builder, x, y ) )
1217 ( error = t1_builder_check_points( builder, 3 ) )
1222 t1_builder_add_point( builder, x, y, 0 );
1225 t1_builder_add_point( builder, x, y, 0 );
1227 t1_builder_add_point( builder, x, y, 1 );
1231 FT_TRACE4(( " rlineto" ));
1233 if ( ( error = t1_builder_start_point( builder, x, y ) )
1241 if ( ( error = t1_builder_add_point1( builder, x, y ) )
1247 FT_TRACE4(( " rmoveto" ));
1251 if ( !decoder->flex_state )
1253 if ( builder->parse_state == T1_Parse_Start )
1255 builder->parse_state = T1_Parse_Have_Moveto;
1260 FT_TRACE4(( " rrcurveto" ));
1262 if ( ( error = t1_builder_start_point( builder, x, y ) )
1264 ( error = t1_builder_check_points( builder, 3 ) )
1270 t1_builder_add_point( builder, x, y, 0 );
1274 t1_builder_add_point( builder, x, y, 0 );
1278 t1_builder_add_point( builder, x, y, 1 );
1282 FT_TRACE4(( " vhcurveto" ));
1284 if ( ( error = t1_builder_start_point( builder, x, y ) )
1286 ( error = t1_builder_check_points( builder, 3 ) )
1291 t1_builder_add_point( builder, x, y, 0 );
1294 t1_builder_add_point( builder, x, y, 0 );
1296 t1_builder_add_point( builder, x, y, 1 );
1300 FT_TRACE4(( " vlineto" ));
1302 if ( ( error = t1_builder_start_point( builder, x, y ) )
1310 FT_TRACE4(( " vmoveto" ));
1313 if ( !decoder->flex_state )
1315 if ( builder->parse_state == T1_Parse_Start )
1317 builder->parse_state = T1_Parse_Have_Moveto;
1322 FT_TRACE4(( " div" ));
1324 /* if `large_int' is set, we divide unscaled numbers; */
1325 /* otherwise, we divide numbers in 16.16 format -- */
1326 /* in both cases, it is the same operation */
1327 *top = FT_DivFix( top[0], top[1] );
1338 FT_TRACE4(( " callsubr" ));
1340 idx = Fix2Int( top[0] );
1341 if ( idx < 0 || idx >= (FT_Int)decoder->num_subrs )
1343 FT_ERROR(( "t1_decoder_parse_charstrings:"
1344 " invalid subrs index\n" ));
1348 if ( zone - decoder->zones >= T1_MAX_SUBRS_CALLS )
1350 FT_ERROR(( "t1_decoder_parse_charstrings:"
1351 " too many nested subrs\n" ));
1355 zone->cursor = ip; /* save current instruction pointer */
1359 /* The Type 1 driver stores subroutines without the seed bytes. */
1360 /* The CID driver stores subroutines with seed bytes. This */
1361 /* case is taken care of when decoder->subrs_len == 0. */
1362 zone->base = decoder->subrs[idx];
1364 if ( decoder->subrs_len )
1365 zone->limit = zone->base + decoder->subrs_len[idx];
1368 /* We are using subroutines from a CID font. We must adjust */
1369 /* for the seed bytes. */
1370 zone->base += ( decoder->lenIV >= 0 ? decoder->lenIV : 0 );
1371 zone->limit = decoder->subrs[idx + 1];
1374 zone->cursor = zone->base;
1378 FT_ERROR(( "t1_decoder_parse_charstrings:"
1379 " invoking empty subrs\n" ));
1383 decoder->zone = zone;
1385 limit = zone->limit;
1390 FT_TRACE4(( " pop" ));
1392 if ( known_othersubr_result_cnt > 0 )
1394 known_othersubr_result_cnt--;
1395 /* ignore, we pushed the operands ourselves */
1399 if ( unknown_othersubr_result_cnt == 0 )
1401 FT_ERROR(( "t1_decoder_parse_charstrings:"
1402 " no more operands for othersubr\n" ));
1406 unknown_othersubr_result_cnt--;
1407 top++; /* `push' the operand to callothersubr onto the stack */
1411 FT_TRACE4(( " return" ));
1413 if ( zone <= decoder->zones )
1415 FT_ERROR(( "t1_decoder_parse_charstrings:"
1416 " unexpected return\n" ));
1422 limit = zone->limit;
1423 decoder->zone = zone;
1427 FT_TRACE4(( " dotsection" ));
1432 FT_TRACE4(( " hstem" ));
1434 /* record horizontal hint */
1437 /* top[0] += builder->left_bearing.y; */
1438 hinter->stem( hinter->hints, 1, top );
1443 FT_TRACE4(( " hstem3" ));
1445 /* record horizontal counter-controlled hints */
1447 hinter->stem3( hinter->hints, 1, top );
1451 FT_TRACE4(( " vstem" ));
1453 /* record vertical hint */
1457 hinter->stem( hinter->hints, 0, top );
1462 FT_TRACE4(( " vstem3" ));
1464 /* record vertical counter-controlled hints */
1473 hinter->stem3( hinter->hints, 0, top );
1477 case op_setcurrentpoint:
1478 FT_TRACE4(( " setcurrentpoint" ));
1480 /* From the T1 specification, section 6.4: */
1482 /* The setcurrentpoint command is used only in */
1483 /* conjunction with results from OtherSubrs procedures. */
1485 /* known_othersubr_result_cnt != 0 is already handled */
1488 /* Note, however, that both Ghostscript and Adobe */
1489 /* Distiller handle this situation by silently ignoring */
1490 /* the inappropriate `setcurrentpoint' instruction. So */
1491 /* we do the same. */
1494 if ( decoder->flex_state != 1 )
1496 FT_ERROR(( "t1_decoder_parse_charstrings:"
1497 " unexpected `setcurrentpoint'\n" ));
1506 decoder->flex_state = 0;
1510 FT_TRACE4(( " opcode_15" ));
1511 /* nothing to do except to pop the two arguments */
1515 FT_ERROR(( "t1_decoder_parse_charstrings:"
1516 " unhandled opcode %d\n", op ));
1520 /* XXX Operators usually clear the operand stack; */
1521 /* only div, callsubr, callothersubr, pop, and */
1522 /* return are different. */
1523 /* In practice it doesn't matter (?). */
1527 #ifdef FT_DEBUG_LEVEL_TRACE
1528 FT_TRACE4(( "\n" ));
1532 } /* general operator processing */
1534 } /* while ip < limit */
1536 FT_TRACE4(( "..end..\n\n" ));
1542 return PSaux_Err_Syntax_Error;
1545 return PSaux_Err_Stack_Underflow;
1549 /* parse a single Type 1 glyph */
1550 FT_LOCAL_DEF( FT_Error )
1551 t1_decoder_parse_glyph( T1_Decoder decoder,
1554 return decoder->parse_callback( decoder, glyph );
1558 /* initialize T1 decoder */
1559 FT_LOCAL_DEF( FT_Error )
1560 t1_decoder_init( T1_Decoder decoder,
1564 FT_Byte** glyph_names,
1567 FT_Render_Mode hint_mode,
1568 T1_Decoder_Callback parse_callback )
1570 FT_MEM_ZERO( decoder, sizeof ( *decoder ) );
1572 /* retrieve PSNames interface from list of current modules */
1574 FT_Service_PsCMaps psnames = 0;
1577 FT_FACE_FIND_GLOBAL_SERVICE( face, psnames, POSTSCRIPT_CMAPS );
1580 FT_ERROR(( "t1_decoder_init:"
1581 " the `psnames' module is not available\n" ));
1582 return PSaux_Err_Unimplemented_Feature;
1585 decoder->psnames = psnames;
1588 t1_builder_init( &decoder->builder, face, size, slot, hinting );
1590 /* decoder->buildchar and decoder->len_buildchar have to be */
1591 /* initialized by the caller since we cannot know the length */
1592 /* of the BuildCharArray */
1594 decoder->num_glyphs = (FT_UInt)face->num_glyphs;
1595 decoder->glyph_names = glyph_names;
1596 decoder->hint_mode = hint_mode;
1597 decoder->blend = blend;
1598 decoder->parse_callback = parse_callback;
1600 decoder->funcs = t1_decoder_funcs;
1602 return PSaux_Err_Ok;
1606 /* finalize T1 decoder */
1607 FT_LOCAL_DEF( void )
1608 t1_decoder_done( T1_Decoder decoder )
1610 t1_builder_done( &decoder->builder );