1 /***************************************************************************/
5 /* PostScript Type 1 decoding routines (body). */
7 /* Copyright 2000-2001 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_DEBUG_H
29 /*************************************************************************/
31 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
32 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
33 /* messages during execution. */
36 #define FT_COMPONENT trace_t1decode
39 typedef enum T1_Operator_
68 op_max /* never remove this one */
73 const FT_Int t1_args_count[op_max] =
96 -1, /* callothersubr */
100 2 /* setcurrentpoint */
104 /*************************************************************************/
107 /* t1_lookup_glyph_by_stdcharcode */
110 /* Looks up a given glyph by its StandardEncoding charcode. Used to */
111 /* implement the SEAC Type 1 operator. */
114 /* face :: The current face object. */
116 /* charcode :: The character code to look for. */
119 /* A glyph index in the font face. Returns -1 if the corresponding */
120 /* glyph wasn't found. */
123 t1_lookup_glyph_by_stdcharcode( T1_Decoder* decoder,
127 const FT_String* glyph_name;
128 PSNames_Interface* psnames = decoder->psnames;
131 /* check range of standard char code */
132 if ( charcode < 0 || charcode > 255 )
135 glyph_name = psnames->adobe_std_strings(
136 psnames->adobe_std_encoding[charcode]);
138 for ( n = 0; n < decoder->num_glyphs; n++ )
140 FT_String* name = (FT_String*)decoder->glyph_names[n];
143 if ( name && strcmp( name,glyph_name ) == 0 )
151 /*************************************************************************/
154 /* t1operator_seac */
157 /* Implements the `seac' Type 1 operator for a Type 1 decoder. */
160 /* decoder :: The current CID decoder. */
162 /* asb :: The accent's side bearing. */
164 /* adx :: The horizontal offset of the accent. */
166 /* ady :: The vertical offset of the accent. */
168 /* bchar :: The base character's StandardEncoding charcode. */
170 /* achar :: The accent character's StandardEncoding charcode. */
173 /* FreeType error code. 0 means success. */
176 t1operator_seac( T1_Decoder* decoder,
184 FT_Int bchar_index, achar_index, n_base_points;
185 FT_Outline* base = decoder->builder.base;
186 FT_Vector left_bearing, advance;
190 adx += decoder->builder.left_bearing.x;
192 /* `glyph_names' is set to 0 for CID fonts which do not */
193 /* include an encoding. How can we deal with these? */
194 if ( decoder->glyph_names == 0 )
196 FT_ERROR(( "t1operator_seac:" ));
197 FT_ERROR(( " glyph names table not available in this font!\n" ));
198 return PSaux_Err_Syntax_Error;
201 bchar_index = t1_lookup_glyph_by_stdcharcode( decoder, bchar );
202 achar_index = t1_lookup_glyph_by_stdcharcode( decoder, achar );
204 if ( bchar_index < 0 || achar_index < 0 )
206 FT_ERROR(( "t1operator_seac:" ));
207 FT_ERROR(( " invalid seac character code arguments\n" ));
208 return PSaux_Err_Syntax_Error;
211 /* if we are trying to load a composite glyph, do not load the */
212 /* accent character and return the array of subglyphs. */
213 if ( decoder->builder.no_recurse )
215 FT_GlyphSlot glyph = (FT_GlyphSlot)decoder->builder.glyph;
216 FT_GlyphLoader* loader = glyph->internal->loader;
220 /* reallocate subglyph array if necessary */
221 error = FT_GlyphLoader_Check_Subglyphs( loader, 2 );
225 subg = loader->current.subglyphs;
227 /* subglyph 0 = base character */
228 subg->index = bchar_index;
229 subg->flags = FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES |
230 FT_SUBGLYPH_FLAG_USE_MY_METRICS;
235 /* subglyph 1 = accent character */
236 subg->index = achar_index;
237 subg->flags = FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES;
238 subg->arg1 = adx - asb;
241 /* set up remaining glyph fields */
242 glyph->num_subglyphs = 2;
243 glyph->subglyphs = loader->base.subglyphs;
244 glyph->format = ft_glyph_format_composite;
246 loader->current.num_subglyphs = 2;
249 /* First load `bchar' in builder */
250 /* now load the unscaled outline */
252 FT_GlyphLoader_Prepare( decoder->builder.loader ); /* prepare loader */
254 error = T1_Decoder_Parse_Glyph( decoder, bchar_index );
258 n_base_points = base->n_points;
260 /* save the left bearing and width of the base character */
261 /* as they will be erased by the next load. */
263 left_bearing = decoder->builder.left_bearing;
264 advance = decoder->builder.advance;
266 decoder->builder.left_bearing.x = 0;
267 decoder->builder.left_bearing.y = 0;
269 /* Now load `achar' on top of */
270 /* the base outline */
271 error = T1_Decoder_Parse_Glyph( decoder, achar_index );
275 /* restore the left side bearing and */
276 /* advance width of the base character */
278 decoder->builder.left_bearing = left_bearing;
279 decoder->builder.advance = advance;
281 /* Finally, move the accent */
282 if ( decoder->builder.load_points )
287 dummy.n_points = (short)( base->n_points - n_base_points );
288 dummy.points = base->points + n_base_points;
290 FT_Outline_Translate( &dummy, adx - asb, ady );
298 /*************************************************************************/
301 /* T1_Decoder_Parse_Charstrings */
304 /* Parses a given Type 1 charstrings program. */
307 /* decoder :: The current Type 1 decoder. */
309 /* charstring_base :: The base address of the charstring stream. */
311 /* charstring_len :: The length in bytes of the charstring stream. */
314 /* FreeType error code. 0 means success. */
316 FT_LOCAL_DEF FT_Error
317 T1_Decoder_Parse_Charstrings( T1_Decoder* decoder,
318 FT_Byte* charstring_base,
319 FT_UInt charstring_len )
322 T1_Decoder_Zone* zone;
325 T1_Builder* builder = &decoder->builder;
329 /* we don't want to touch the source code -- use macro trick */
330 #define start_point T1_Builder_Start_Point
331 #define check_points T1_Builder_Check_Points
332 #define add_point T1_Builder_Add_Point
333 #define add_point1 T1_Builder_Add_Point1
334 #define add_contour T1_Builder_Add_Contour
335 #define close_contour T1_Builder_Close_Contour
337 /* First of all, initialize the decoder */
338 decoder->top = decoder->stack;
339 decoder->zone = decoder->zones;
340 zone = decoder->zones;
342 builder->path_begun = 0;
344 zone->base = charstring_base;
345 limit = zone->limit = charstring_base + charstring_len;
346 ip = zone->cursor = zone->base;
348 error = PSaux_Err_Ok;
353 /* now, execute loop */
356 FT_Long* top = decoder->top;
357 T1_Operator op = op_none;
361 /*********************************************************************/
363 /* Decode operator or operand */
367 /* first of all, decompress operator or value */
426 FT_ERROR(( "T1_Decoder_Parse_CharStrings: "
427 "invalid escape (12+EOF)\n" ));
452 op = op_callothersubr;
458 op = op_setcurrentpoint;
462 FT_ERROR(( "T1_Decoder_Parse_CharStrings: "
463 "invalid escape (12+%d)\n",
469 case 255: /* four bytes integer */
470 if ( ip + 4 > limit )
472 FT_ERROR(( "T1_Decoder_Parse_CharStrings: "
473 "unexpected EOF in integer\n" ));
477 value = (FT_Int32)( ((FT_Long)ip[0] << 24) |
478 ((FT_Long)ip[1] << 16) |
479 ((FT_Long)ip[2] << 8 ) |
488 value = (FT_Long)ip[-1] - 139;
493 FT_ERROR(( "T1_Decoder_Parse_CharStrings: " ));
494 FT_ERROR(( "unexpected EOF in integer\n" ));
499 value = ( ( (FT_Long)ip[-2] - 247 ) << 8 ) + ip[-1] + 108;
501 value = -( ( ( (FT_Long)ip[-2] - 251 ) << 8 ) + ip[-1] + 108 );
506 FT_ERROR(( "T1_Decoder_Parse_CharStrings: "
507 "invalid byte (%d)\n", ip[-1] ));
512 /*********************************************************************/
514 /* Push value on stack, or process operator */
519 if ( top - decoder->stack >= T1_MAX_CHARSTRINGS_OPERANDS )
521 FT_ERROR(( "T1_Decoder_Parse_CharStrings: stack overflow!\n" ));
525 FT_TRACE4(( " %ld", value ));
530 else if ( op == op_callothersubr ) /* callothersubr */
532 FT_TRACE4(( " callothersubr" ));
534 if ( top - decoder->stack < 2 )
535 goto Stack_Underflow;
540 case 1: /* start flex feature */
542 goto Unexpected_OtherSubr;
544 decoder->flex_state = 1;
545 decoder->num_flex_vectors = 0;
546 if ( start_point( builder, x, y ) ||
547 check_points( builder, 6 ) )
551 case 2: /* add flex vectors */
556 goto Unexpected_OtherSubr;
558 /* note that we should not add a point for index 0; */
559 /* this will move our current position to the flex */
560 /* point without adding any point to the outline */
561 index = decoder->num_flex_vectors++;
562 if ( index > 0 && index < 7 )
566 (FT_Byte)( index == 3 || index == 6 ) );
570 case 0: /* end flex feature */
572 goto Unexpected_OtherSubr;
574 if ( decoder->flex_state == 0 ||
575 decoder->num_flex_vectors != 7 )
577 FT_ERROR(( "T1_Decoder_Parse_CharStrings: "
578 "unexpected flex end\n" ));
582 /* now consume the remaining `pop pop setcurpoint' */
583 if ( ip + 6 > limit ||
584 ip[0] != 12 || ip[1] != 17 || /* pop */
585 ip[2] != 12 || ip[3] != 17 || /* pop */
586 ip[4] != 12 || ip[5] != 33 ) /* setcurpoint */
588 FT_ERROR(( "T1_Decoder_Parse_CharStrings: "
589 "invalid flex charstring\n" ));
594 decoder->flex_state = 0;
597 case 3: /* change hints */
599 goto Unexpected_OtherSubr;
601 /* eat the following `pop' */
602 if ( ip + 2 > limit )
604 FT_ERROR(( "T1_Decoder_Parse_CharStrings: "
605 "invalid escape (12+%d)\n", ip[-1] ));
609 if ( ip[0] != 12 || ip[1] != 17 )
611 FT_ERROR(( "T1_Decoder_Parse_CharStrings: " ));
612 FT_ERROR(( "`pop' expected, found (%d %d)\n", ip[0], ip[1] ));
620 /* counter control hints, clear stack */
621 top = decoder->stack;
628 case 18: /* multiple masters */
630 T1_Blend* blend = decoder->blend;
631 FT_UInt num_points, nn, mm;
638 FT_ERROR(( "T1_Decoder_Parse_CharStrings: " ));
639 FT_ERROR(( "unexpected multiple masters operator!\n" ));
643 num_points = top[1] - 13 + ( top[1] == 18 );
644 if ( top[0] != (FT_Int)( num_points * blend->num_designs ) )
646 FT_ERROR(( "T1_Decoder_Parse_CharStrings: " ));
647 FT_ERROR(( "incorrect number of mm arguments\n" ));
651 top -= blend->num_designs*num_points;
652 if ( top < decoder->stack )
653 goto Stack_Underflow;
655 /* we want to compute: */
657 /* a0*w0 + a1*w1 + ... + ak*wk */
659 /* but we only have the a0, a1-a0, a2-a0, .. ak-a0 */
660 /* however, given that w0 + w1 + ... + wk == 1, we can */
661 /* rewrite it easily as: */
663 /* a0 + (a1-a0)*w1 + (a2-a0)*w2 + .. + (ak-a0)*wk */
665 /* where k == num_designs-1 */
667 /* I guess that's why it's written in this `compact' */
670 delta = top + num_points;
672 for ( nn = 0; nn < num_points; nn++ )
674 FT_Int tmp = values[0];
677 for ( mm = 1; mm < blend->num_designs; mm++ )
678 tmp += FT_MulFix( *delta++, blend->weight_vector[mm] );
682 /* note that `top' will be incremented later by calls to `pop' */
687 Unexpected_OtherSubr:
688 FT_ERROR(( "T1_Decoder_Parse_CharStrings: "
689 "invalid othersubr [%d %d]!\n", top[0], top[1] ));
694 else /* general operator */
696 FT_Int num_args = t1_args_count[op];
699 if ( top - decoder->stack < num_args )
700 goto Stack_Underflow;
707 FT_TRACE4(( " endchar" ));
709 close_contour( builder );
711 /* add current outline to the glyph slot */
712 FT_GlyphLoader_Add( builder->loader );
715 FT_TRACE4(( "\n\n" ));
719 FT_TRACE4(( " hsbw" ));
721 builder->left_bearing.x += top[0];
722 builder->advance.x = top[1];
723 builder->advance.y = 0;
725 builder->last.x = x = top[0];
726 builder->last.y = y = 0;
728 /* the `metrics_only' indicates that we only want to compute */
729 /* the glyph's metrics (lsb + advance width), not load the */
730 /* rest of it; so exit immediately */
731 if ( builder->metrics_only )
737 /* return immediately after the processing */
738 return t1operator_seac( decoder, top[0], top[1],
739 top[2], top[3], top[4] );
742 FT_TRACE4(( " sbw" ));
744 builder->left_bearing.x += top[0];
745 builder->left_bearing.y += top[1];
746 builder->advance.x = top[2];
747 builder->advance.y = top[3];
749 builder->last.x = x = top[0];
750 builder->last.y = y = top[1];
752 /* the `metrics_only' indicates that we only want to compute */
753 /* the glyph's metrics (lsb + advance width), not load the */
754 /* rest of it; so exit immediately */
755 if ( builder->metrics_only )
761 FT_TRACE4(( " closepath" ));
763 close_contour( builder );
764 builder->path_begun = 0;
768 FT_TRACE4(( " hlineto" ));
770 if ( start_point( builder, x, y ) )
777 FT_TRACE4(( " hmoveto" ));
780 if ( !decoder->flex_state )
781 builder->path_begun = 0;
785 FT_TRACE4(( " hvcurveto" ));
787 if ( start_point( builder, x, y ) ||
788 check_points( builder, 3 ) )
792 add_point( builder, x, y, 0 );
795 add_point( builder, x, y, 0 );
797 add_point( builder, x, y, 1 );
801 FT_TRACE4(( " rlineto" ));
803 if ( start_point( builder, x, y ) )
810 if ( add_point1( builder, x, y ) )
815 FT_TRACE4(( " rmoveto" ));
819 if ( !decoder->flex_state )
820 builder->path_begun = 0;
824 FT_TRACE4(( " rcurveto" ));
826 if ( start_point( builder, x, y ) ||
827 check_points( builder, 3 ) )
832 add_point( builder, x, y, 0 );
836 add_point( builder, x, y, 0 );
840 add_point( builder, x, y, 1 );
844 FT_TRACE4(( " vhcurveto" ));
846 if ( start_point( builder, x, y ) ||
847 check_points( builder, 3 ) )
851 add_point( builder, x, y, 0 );
854 add_point( builder, x, y, 0 );
856 add_point( builder, x, y, 1 );
860 FT_TRACE4(( " vlineto" ));
862 if ( start_point( builder, x, y ) )
869 FT_TRACE4(( " vmoveto" ));
872 if ( !decoder->flex_state )
873 builder->path_begun = 0;
877 FT_TRACE4(( " div" ));
881 *top = top[0] / top[1];
886 FT_ERROR(( "T1_Decoder_Parse_CharStrings: division by 0\n" ));
896 FT_TRACE4(( " callsubr" ));
899 if ( index < 0 || index >= (FT_Int)decoder->num_subrs )
901 FT_ERROR(( "T1_Decoder_Parse_CharStrings: "
902 "invalid subrs index\n" ));
906 if ( zone - decoder->zones >= T1_MAX_SUBRS_CALLS )
908 FT_ERROR(( "T1_Decoder_Parse_CharStrings: "
909 "too many nested subrs\n" ));
913 zone->cursor = ip; /* save current instruction pointer */
917 /* The Type 1 driver stores subroutines without the seed bytes. */
918 /* The CID driver stores subroutines with seed bytes. This */
919 /* case is taken care of when decoder->subrs_len == 0. */
920 zone->base = decoder->subrs[index];
922 if ( decoder->subrs_len )
923 zone->limit = zone->base + decoder->subrs_len[index];
926 /* We are using subroutines from a CID font. We must adjust */
927 /* for the seed bytes. */
928 zone->base += ( decoder->lenIV >= 0 ? decoder->lenIV : 0 );
929 zone->limit = decoder->subrs[index + 1];
932 zone->cursor = zone->base;
936 FT_ERROR(( "T1_Decoder_Parse_CharStrings: "
937 "invoking empty subrs!\n" ));
941 decoder->zone = zone;
948 FT_TRACE4(( " pop" ));
950 /* theoretically, the arguments are already on the stack */
955 FT_TRACE4(( " return" ));
957 if ( zone <= decoder->zones )
959 FT_ERROR(( "T1_Decoder_Parse_CharStrings: unexpected return\n" ));
966 decoder->zone = zone;
970 FT_TRACE4(( " dotsection" ));
975 FT_TRACE4(( " hstem" ));
980 FT_TRACE4(( " hstem3" ));
985 FT_TRACE4(( " vstem" ));
990 FT_TRACE4(( " vstem3" ));
994 case op_setcurrentpoint:
995 FT_TRACE4(( " setcurrentpoint" ));
997 FT_ERROR(( "T1_Decoder_Parse_CharStrings: " ));
998 FT_ERROR(( "unexpected `setcurrentpoint'\n" ));
1002 FT_ERROR(( "T1_Decoder_Parse_CharStrings: "
1003 "unhandled opcode %d\n", op ));
1009 } /* general operator processing */
1011 } /* while ip < limit */
1013 FT_TRACE4(( "..end..\n\n" ));
1017 return PSaux_Err_Syntax_Error;
1020 return PSaux_Err_Stack_Underflow;
1023 return builder->error;
1027 FT_LOCAL_DEF FT_Error
1028 T1_Decoder_Parse_Glyph( T1_Decoder* decoder,
1031 return decoder->parse_callback( decoder, glyph );
1035 FT_LOCAL_DEF FT_Error
1036 T1_Decoder_Init( T1_Decoder* decoder,
1040 FT_Byte** glyph_names,
1042 T1_Decoder_Callback parse_callback )
1044 MEM_Set( decoder, 0, sizeof ( *decoder ) );
1046 /* retrieve PSNames interface from list of current modules */
1048 PSNames_Interface* psnames = 0;
1051 psnames = (PSNames_Interface*)FT_Get_Module_Interface(
1052 FT_FACE_LIBRARY(face), "psnames" );
1055 FT_ERROR(( "T1_Decoder_Init: " ));
1056 FT_ERROR(( "the `psnames' module is not available\n" ));
1057 return PSaux_Err_Unimplemented_Feature;
1060 decoder->psnames = psnames;
1062 T1_Builder_Init( &decoder->builder, face, size, slot );
1064 decoder->num_glyphs = face->num_glyphs;
1065 decoder->glyph_names = glyph_names;
1066 decoder->blend = blend;
1067 decoder->parse_callback = parse_callback;
1069 decoder->funcs = t1_decoder_funcs;
1076 T1_Decoder_Done( T1_Decoder* decoder )
1078 T1_Builder_Done( &decoder->builder );