[FREETYPE]
[reactos.git] / reactos / lib / 3rdparty / freetype / src / psaux / t1decode.c
1 /***************************************************************************/
2 /* */
3 /* t1decode.c */
4 /* */
5 /* PostScript Type 1 decoding routines (body). */
6 /* */
7 /* Copyright 2000-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
9 /* */
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. */
15 /* */
16 /***************************************************************************/
17
18
19 #include <ft2build.h>
20 #include FT_INTERNAL_CALC_H
21 #include FT_INTERNAL_DEBUG_H
22 #include FT_INTERNAL_POSTSCRIPT_HINTS_H
23 #include FT_OUTLINE_H
24
25 #include "t1decode.h"
26 #include "psobjs.h"
27
28 #include "psauxerr.h"
29
30
31 /*************************************************************************/
32 /* */
33 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
34 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
35 /* messages during execution. */
36 /* */
37 #undef FT_COMPONENT
38 #define FT_COMPONENT trace_t1decode
39
40
41 typedef enum T1_Operator_
42 {
43 op_none = 0,
44 op_endchar,
45 op_hsbw,
46 op_seac,
47 op_sbw,
48 op_closepath,
49 op_hlineto,
50 op_hmoveto,
51 op_hvcurveto,
52 op_rlineto,
53 op_rmoveto,
54 op_rrcurveto,
55 op_vhcurveto,
56 op_vlineto,
57 op_vmoveto,
58 op_dotsection,
59 op_hstem,
60 op_hstem3,
61 op_vstem,
62 op_vstem3,
63 op_div,
64 op_callothersubr,
65 op_callsubr,
66 op_pop,
67 op_return,
68 op_setcurrentpoint,
69 op_unknown15,
70
71 op_max /* never remove this one */
72
73 } T1_Operator;
74
75
76 static
77 const FT_Int t1_args_count[op_max] =
78 {
79 0, /* none */
80 0, /* endchar */
81 2, /* hsbw */
82 5, /* seac */
83 4, /* sbw */
84 0, /* closepath */
85 1, /* hlineto */
86 1, /* hmoveto */
87 4, /* hvcurveto */
88 2, /* rlineto */
89 2, /* rmoveto */
90 6, /* rrcurveto */
91 4, /* vhcurveto */
92 1, /* vlineto */
93 1, /* vmoveto */
94 0, /* dotsection */
95 2, /* hstem */
96 6, /* hstem3 */
97 2, /* vstem */
98 6, /* vstem3 */
99 2, /* div */
100 -1, /* callothersubr */
101 1, /* callsubr */
102 0, /* pop */
103 0, /* return */
104 2, /* setcurrentpoint */
105 2 /* opcode 15 (undocumented and obsolete) */
106 };
107
108
109 /*************************************************************************/
110 /* */
111 /* <Function> */
112 /* t1_lookup_glyph_by_stdcharcode */
113 /* */
114 /* <Description> */
115 /* Looks up a given glyph by its StandardEncoding charcode. Used to */
116 /* implement the SEAC Type 1 operator. */
117 /* */
118 /* <Input> */
119 /* face :: The current face object. */
120 /* */
121 /* charcode :: The character code to look for. */
122 /* */
123 /* <Return> */
124 /* A glyph index in the font face. Returns -1 if the corresponding */
125 /* glyph wasn't found. */
126 /* */
127 static FT_Int
128 t1_lookup_glyph_by_stdcharcode( T1_Decoder decoder,
129 FT_Int charcode )
130 {
131 FT_UInt n;
132 const FT_String* glyph_name;
133 FT_Service_PsCMaps psnames = decoder->psnames;
134
135
136 /* check range of standard char code */
137 if ( charcode < 0 || charcode > 255 )
138 return -1;
139
140 glyph_name = psnames->adobe_std_strings(
141 psnames->adobe_std_encoding[charcode]);
142
143 for ( n = 0; n < decoder->num_glyphs; n++ )
144 {
145 FT_String* name = (FT_String*)decoder->glyph_names[n];
146
147
148 if ( name &&
149 name[0] == glyph_name[0] &&
150 ft_strcmp( name, glyph_name ) == 0 )
151 return n;
152 }
153
154 return -1;
155 }
156
157
158 /*************************************************************************/
159 /* */
160 /* <Function> */
161 /* t1operator_seac */
162 /* */
163 /* <Description> */
164 /* Implements the `seac' Type 1 operator for a Type 1 decoder. */
165 /* */
166 /* <Input> */
167 /* decoder :: The current CID decoder. */
168 /* */
169 /* asb :: The accent's side bearing. */
170 /* */
171 /* adx :: The horizontal offset of the accent. */
172 /* */
173 /* ady :: The vertical offset of the accent. */
174 /* */
175 /* bchar :: The base character's StandardEncoding charcode. */
176 /* */
177 /* achar :: The accent character's StandardEncoding charcode. */
178 /* */
179 /* <Return> */
180 /* FreeType error code. 0 means success. */
181 /* */
182 static FT_Error
183 t1operator_seac( T1_Decoder decoder,
184 FT_Pos asb,
185 FT_Pos adx,
186 FT_Pos ady,
187 FT_Int bchar,
188 FT_Int achar )
189 {
190 FT_Error error;
191 FT_Int bchar_index, achar_index;
192 #if 0
193 FT_Int n_base_points;
194 FT_Outline* base = decoder->builder.base;
195 #endif
196 FT_Vector left_bearing, advance;
197
198 #ifdef FT_CONFIG_OPTION_INCREMENTAL
199 T1_Face face = (T1_Face)decoder->builder.face;
200 #endif
201
202
203 if ( decoder->seac )
204 {
205 FT_ERROR(( "t1operator_seac: invalid nested seac\n" ));
206 return PSaux_Err_Syntax_Error;
207 }
208
209 /* seac weirdness */
210 adx += decoder->builder.left_bearing.x;
211
212 /* `glyph_names' is set to 0 for CID fonts which do not */
213 /* include an encoding. How can we deal with these? */
214 if ( decoder->glyph_names == 0 )
215 {
216 FT_ERROR(( "t1operator_seac:"
217 " glyph names table not available in this font\n" ));
218 return PSaux_Err_Syntax_Error;
219 }
220
221 #ifdef FT_CONFIG_OPTION_INCREMENTAL
222 if ( face->root.internal->incremental_interface )
223 {
224 /* the caller must handle the font encoding also */
225 bchar_index = bchar;
226 achar_index = achar;
227 }
228 else
229 #endif
230 {
231 bchar_index = t1_lookup_glyph_by_stdcharcode( decoder, bchar );
232 achar_index = t1_lookup_glyph_by_stdcharcode( decoder, achar );
233 }
234
235 if ( bchar_index < 0 || achar_index < 0 )
236 {
237 FT_ERROR(( "t1operator_seac:"
238 " invalid seac character code arguments\n" ));
239 return PSaux_Err_Syntax_Error;
240 }
241
242 /* if we are trying to load a composite glyph, do not load the */
243 /* accent character and return the array of subglyphs. */
244 if ( decoder->builder.no_recurse )
245 {
246 FT_GlyphSlot glyph = (FT_GlyphSlot)decoder->builder.glyph;
247 FT_GlyphLoader loader = glyph->internal->loader;
248 FT_SubGlyph subg;
249
250
251 /* reallocate subglyph array if necessary */
252 error = FT_GlyphLoader_CheckSubGlyphs( loader, 2 );
253 if ( error )
254 goto Exit;
255
256 subg = loader->current.subglyphs;
257
258 /* subglyph 0 = base character */
259 subg->index = bchar_index;
260 subg->flags = FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES |
261 FT_SUBGLYPH_FLAG_USE_MY_METRICS;
262 subg->arg1 = 0;
263 subg->arg2 = 0;
264 subg++;
265
266 /* subglyph 1 = accent character */
267 subg->index = achar_index;
268 subg->flags = FT_SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES;
269 subg->arg1 = (FT_Int)FIXED_TO_INT( adx - asb );
270 subg->arg2 = (FT_Int)FIXED_TO_INT( ady );
271
272 /* set up remaining glyph fields */
273 glyph->num_subglyphs = 2;
274 glyph->subglyphs = loader->base.subglyphs;
275 glyph->format = FT_GLYPH_FORMAT_COMPOSITE;
276
277 loader->current.num_subglyphs = 2;
278 goto Exit;
279 }
280
281 /* First load `bchar' in builder */
282 /* now load the unscaled outline */
283
284 FT_GlyphLoader_Prepare( decoder->builder.loader ); /* prepare loader */
285
286 /* the seac operator must not be nested */
287 decoder->seac = TRUE;
288 error = t1_decoder_parse_glyph( decoder, bchar_index );
289 decoder->seac = FALSE;
290 if ( error )
291 goto Exit;
292
293 /* save the left bearing and width of the base character */
294 /* as they will be erased by the next load. */
295
296 left_bearing = decoder->builder.left_bearing;
297 advance = decoder->builder.advance;
298
299 decoder->builder.left_bearing.x = 0;
300 decoder->builder.left_bearing.y = 0;
301
302 decoder->builder.pos_x = adx - asb;
303 decoder->builder.pos_y = ady;
304
305 /* Now load `achar' on top of */
306 /* the base outline */
307
308 /* the seac operator must not be nested */
309 decoder->seac = TRUE;
310 error = t1_decoder_parse_glyph( decoder, achar_index );
311 decoder->seac = FALSE;
312 if ( error )
313 goto Exit;
314
315 /* restore the left side bearing and */
316 /* advance width of the base character */
317
318 decoder->builder.left_bearing = left_bearing;
319 decoder->builder.advance = advance;
320
321 decoder->builder.pos_x = 0;
322 decoder->builder.pos_y = 0;
323
324 Exit:
325 return error;
326 }
327
328
329 /*************************************************************************/
330 /* */
331 /* <Function> */
332 /* t1_decoder_parse_charstrings */
333 /* */
334 /* <Description> */
335 /* Parses a given Type 1 charstrings program. */
336 /* */
337 /* <Input> */
338 /* decoder :: The current Type 1 decoder. */
339 /* */
340 /* charstring_base :: The base address of the charstring stream. */
341 /* */
342 /* charstring_len :: The length in bytes of the charstring stream. */
343 /* */
344 /* <Return> */
345 /* FreeType error code. 0 means success. */
346 /* */
347 FT_LOCAL_DEF( FT_Error )
348 t1_decoder_parse_charstrings( T1_Decoder decoder,
349 FT_Byte* charstring_base,
350 FT_UInt charstring_len )
351 {
352 FT_Error error;
353 T1_Decoder_Zone zone;
354 FT_Byte* ip;
355 FT_Byte* limit;
356 T1_Builder builder = &decoder->builder;
357 FT_Pos x, y, orig_x, orig_y;
358 FT_Int known_othersubr_result_cnt = 0;
359 FT_Int unknown_othersubr_result_cnt = 0;
360 FT_Bool large_int;
361 FT_Fixed seed;
362
363 T1_Hints_Funcs hinter;
364
365 #ifdef FT_DEBUG_LEVEL_TRACE
366 FT_Bool bol = TRUE;
367 #endif
368
369
370 /* we don't want to touch the source code -- use macro trick */
371 #define start_point t1_builder_start_point
372 #define check_points t1_builder_check_points
373 #define add_point t1_builder_add_point
374 #define add_point1 t1_builder_add_point1
375 #define add_contour t1_builder_add_contour
376 #define close_contour t1_builder_close_contour
377
378
379 /* compute random seed from stack address of parameter */
380 seed = (FT_Fixed)( ( (FT_PtrDist)(char*)&seed ^
381 (FT_PtrDist)(char*)&decoder ^
382 (FT_PtrDist)(char*)&charstring_base ) &
383 FT_ULONG_MAX ) ;
384 seed = ( seed ^ ( seed >> 10 ) ^ ( seed >> 20 ) ) & 0xFFFFL;
385 if ( seed == 0 )
386 seed = 0x7384;
387
388 /* First of all, initialize the decoder */
389 decoder->top = decoder->stack;
390 decoder->zone = decoder->zones;
391 zone = decoder->zones;
392
393 builder->parse_state = T1_Parse_Start;
394
395 hinter = (T1_Hints_Funcs)builder->hints_funcs;
396
397 /* a font that reads BuildCharArray without setting */
398 /* its values first is buggy, but ... */
399 FT_ASSERT( ( decoder->len_buildchar == 0 ) ==
400 ( decoder->buildchar == NULL ) );
401
402 if ( decoder->len_buildchar > 0 )
403 ft_memset( &decoder->buildchar[0],
404 0,
405 sizeof( decoder->buildchar[0] ) * decoder->len_buildchar );
406
407 FT_TRACE4(( "\n"
408 "Start charstring\n" ));
409
410 zone->base = charstring_base;
411 limit = zone->limit = charstring_base + charstring_len;
412 ip = zone->cursor = zone->base;
413
414 error = PSaux_Err_Ok;
415
416 x = orig_x = builder->pos_x;
417 y = orig_y = builder->pos_y;
418
419 /* begin hints recording session, if any */
420 if ( hinter )
421 hinter->open( hinter->hints );
422
423 large_int = FALSE;
424
425 /* now, execute loop */
426 while ( ip < limit )
427 {
428 FT_Long* top = decoder->top;
429 T1_Operator op = op_none;
430 FT_Int32 value = 0;
431
432
433 FT_ASSERT( known_othersubr_result_cnt == 0 ||
434 unknown_othersubr_result_cnt == 0 );
435
436 #ifdef FT_DEBUG_LEVEL_TRACE
437 if ( bol )
438 {
439 FT_TRACE5(( " (%d)", decoder->top - decoder->stack ));
440 bol = FALSE;
441 }
442 #endif
443
444 /*********************************************************************/
445 /* */
446 /* Decode operator or operand */
447 /* */
448 /* */
449
450 /* first of all, decompress operator or value */
451 switch ( *ip++ )
452 {
453 case 1:
454 op = op_hstem;
455 break;
456
457 case 3:
458 op = op_vstem;
459 break;
460 case 4:
461 op = op_vmoveto;
462 break;
463 case 5:
464 op = op_rlineto;
465 break;
466 case 6:
467 op = op_hlineto;
468 break;
469 case 7:
470 op = op_vlineto;
471 break;
472 case 8:
473 op = op_rrcurveto;
474 break;
475 case 9:
476 op = op_closepath;
477 break;
478 case 10:
479 op = op_callsubr;
480 break;
481 case 11:
482 op = op_return;
483 break;
484
485 case 13:
486 op = op_hsbw;
487 break;
488 case 14:
489 op = op_endchar;
490 break;
491
492 case 15: /* undocumented, obsolete operator */
493 op = op_unknown15;
494 break;
495
496 case 21:
497 op = op_rmoveto;
498 break;
499 case 22:
500 op = op_hmoveto;
501 break;
502
503 case 30:
504 op = op_vhcurveto;
505 break;
506 case 31:
507 op = op_hvcurveto;
508 break;
509
510 case 12:
511 if ( ip > limit )
512 {
513 FT_ERROR(( "t1_decoder_parse_charstrings:"
514 " invalid escape (12+EOF)\n" ));
515 goto Syntax_Error;
516 }
517
518 switch ( *ip++ )
519 {
520 case 0:
521 op = op_dotsection;
522 break;
523 case 1:
524 op = op_vstem3;
525 break;
526 case 2:
527 op = op_hstem3;
528 break;
529 case 6:
530 op = op_seac;
531 break;
532 case 7:
533 op = op_sbw;
534 break;
535 case 12:
536 op = op_div;
537 break;
538 case 16:
539 op = op_callothersubr;
540 break;
541 case 17:
542 op = op_pop;
543 break;
544 case 33:
545 op = op_setcurrentpoint;
546 break;
547
548 default:
549 FT_ERROR(( "t1_decoder_parse_charstrings:"
550 " invalid escape (12+%d)\n",
551 ip[-1] ));
552 goto Syntax_Error;
553 }
554 break;
555
556 case 255: /* four bytes integer */
557 if ( ip + 4 > limit )
558 {
559 FT_ERROR(( "t1_decoder_parse_charstrings:"
560 " unexpected EOF in integer\n" ));
561 goto Syntax_Error;
562 }
563
564 value = (FT_Int32)( ( (FT_Long)ip[0] << 24 ) |
565 ( (FT_Long)ip[1] << 16 ) |
566 ( (FT_Long)ip[2] << 8 ) |
567 ip[3] );
568 ip += 4;
569
570 /* According to the specification, values > 32000 or < -32000 must */
571 /* be followed by a `div' operator to make the result be in the */
572 /* range [-32000;32000]. We expect that the second argument of */
573 /* `div' is not a large number. Additionally, we don't handle */
574 /* stuff like `<large1> <large2> <num> div <num> div' or */
575 /* <large1> <large2> <num> div div'. This is probably not allowed */
576 /* anyway. */
577 if ( value > 32000 || value < -32000 )
578 {
579 if ( large_int )
580 {
581 FT_ERROR(( "t1_decoder_parse_charstrings:"
582 " no `div' after large integer\n" ));
583 }
584 else
585 large_int = TRUE;
586 }
587 else
588 {
589 if ( !large_int )
590 value <<= 16;
591 }
592
593 break;
594
595 default:
596 if ( ip[-1] >= 32 )
597 {
598 if ( ip[-1] < 247 )
599 value = (FT_Int32)ip[-1] - 139;
600 else
601 {
602 if ( ++ip > limit )
603 {
604 FT_ERROR(( "t1_decoder_parse_charstrings:"
605 " unexpected EOF in integer\n" ));
606 goto Syntax_Error;
607 }
608
609 if ( ip[-2] < 251 )
610 value = ( ( (FT_Int32)ip[-2] - 247 ) << 8 ) + ip[-1] + 108;
611 else
612 value = -( ( ( (FT_Int32)ip[-2] - 251 ) << 8 ) + ip[-1] + 108 );
613 }
614
615 if ( !large_int )
616 value <<= 16;
617 }
618 else
619 {
620 FT_ERROR(( "t1_decoder_parse_charstrings:"
621 " invalid byte (%d)\n", ip[-1] ));
622 goto Syntax_Error;
623 }
624 }
625
626 if ( unknown_othersubr_result_cnt > 0 )
627 {
628 switch ( op )
629 {
630 case op_callsubr:
631 case op_return:
632 case op_none:
633 case op_pop:
634 break;
635
636 default:
637 /* all operands have been transferred by previous pops */
638 unknown_othersubr_result_cnt = 0;
639 break;
640 }
641 }
642
643 if ( large_int && !( op == op_none || op == op_div ) )
644 {
645 FT_ERROR(( "t1_decoder_parse_charstrings:"
646 " no `div' after large integer\n" ));
647
648 large_int = FALSE;
649 }
650
651 /*********************************************************************/
652 /* */
653 /* Push value on stack, or process operator */
654 /* */
655 /* */
656 if ( op == op_none )
657 {
658 if ( top - decoder->stack >= T1_MAX_CHARSTRINGS_OPERANDS )
659 {
660 FT_ERROR(( "t1_decoder_parse_charstrings: stack overflow\n" ));
661 goto Syntax_Error;
662 }
663
664 #ifdef FT_DEBUG_LEVEL_TRACE
665 if ( large_int )
666 FT_TRACE4(( " %ld", value ));
667 else
668 FT_TRACE4(( " %ld", (FT_Int32)( value >> 16 ) ));
669 #endif
670
671 *top++ = value;
672 decoder->top = top;
673 }
674 else if ( op == op_callothersubr ) /* callothersubr */
675 {
676 FT_Int subr_no;
677 FT_Int arg_cnt;
678
679
680 #ifdef FT_DEBUG_LEVEL_TRACE
681 FT_TRACE4(( " callothersubr\n" ));
682 bol = TRUE;
683 #endif
684
685 if ( top - decoder->stack < 2 )
686 goto Stack_Underflow;
687
688 top -= 2;
689
690 subr_no = (FT_Int)( top[1] >> 16 );
691 arg_cnt = (FT_Int)( top[0] >> 16 );
692
693 /***********************************************************/
694 /* */
695 /* remove all operands to callothersubr from the stack */
696 /* */
697 /* for handled othersubrs, where we know the number of */
698 /* arguments, we increase the stack by the value of */
699 /* known_othersubr_result_cnt */
700 /* */
701 /* for unhandled othersubrs the following pops adjust the */
702 /* stack pointer as necessary */
703
704 if ( arg_cnt > top - decoder->stack )
705 goto Stack_Underflow;
706
707 top -= arg_cnt;
708
709 known_othersubr_result_cnt = 0;
710 unknown_othersubr_result_cnt = 0;
711
712 /* XXX TODO: The checks to `arg_count == <whatever>' */
713 /* might not be correct; an othersubr expects a certain */
714 /* number of operands on the PostScript stack (as opposed */
715 /* to the T1 stack) but it doesn't have to put them there */
716 /* by itself; previous othersubrs might have left the */
717 /* operands there if they were not followed by an */
718 /* appropriate number of pops */
719 /* */
720 /* On the other hand, Adobe Reader 7.0.8 for Linux doesn't */
721 /* accept a font that contains charstrings like */
722 /* */
723 /* 100 200 2 20 callothersubr */
724 /* 300 1 20 callothersubr pop */
725 /* */
726 /* Perhaps this is the reason why BuildCharArray exists. */
727
728 switch ( subr_no )
729 {
730 case 1: /* start flex feature */
731 if ( arg_cnt != 0 )
732 goto Unexpected_OtherSubr;
733
734 decoder->flex_state = 1;
735 decoder->num_flex_vectors = 0;
736 if ( start_point( builder, x, y ) ||
737 check_points( builder, 6 ) )
738 goto Fail;
739 break;
740
741 case 2: /* add flex vectors */
742 {
743 FT_Int idx;
744
745
746 if ( arg_cnt != 0 )
747 goto Unexpected_OtherSubr;
748
749 /* note that we should not add a point for index 0; */
750 /* this will move our current position to the flex */
751 /* point without adding any point to the outline */
752 idx = decoder->num_flex_vectors++;
753 if ( idx > 0 && idx < 7 )
754 add_point( builder,
755 x,
756 y,
757 (FT_Byte)( idx == 3 || idx == 6 ) );
758 }
759 break;
760
761 case 0: /* end flex feature */
762 if ( arg_cnt != 3 )
763 goto Unexpected_OtherSubr;
764
765 if ( decoder->flex_state == 0 ||
766 decoder->num_flex_vectors != 7 )
767 {
768 FT_ERROR(( "t1_decoder_parse_charstrings:"
769 " unexpected flex end\n" ));
770 goto Syntax_Error;
771 }
772
773 /* the two `results' are popped by the following setcurrentpoint */
774 known_othersubr_result_cnt = 2;
775 break;
776
777 case 3: /* change hints */
778 if ( arg_cnt != 1 )
779 goto Unexpected_OtherSubr;
780
781 known_othersubr_result_cnt = 1;
782
783 if ( hinter )
784 hinter->reset( hinter->hints, builder->current->n_points );
785 break;
786
787 case 12:
788 case 13:
789 /* counter control hints, clear stack */
790 top = decoder->stack;
791 break;
792
793 case 14:
794 case 15:
795 case 16:
796 case 17:
797 case 18: /* multiple masters */
798 {
799 PS_Blend blend = decoder->blend;
800 FT_UInt num_points, nn, mm;
801 FT_Long* delta;
802 FT_Long* values;
803
804
805 if ( !blend )
806 {
807 FT_ERROR(( "t1_decoder_parse_charstrings:"
808 " unexpected multiple masters operator\n" ));
809 goto Syntax_Error;
810 }
811
812 num_points = (FT_UInt)subr_no - 13 + ( subr_no == 18 );
813 if ( arg_cnt != (FT_Int)( num_points * blend->num_designs ) )
814 {
815 FT_ERROR(( "t1_decoder_parse_charstrings:"
816 " incorrect number of multiple masters arguments\n" ));
817 goto Syntax_Error;
818 }
819
820 /* we want to compute: */
821 /* */
822 /* a0*w0 + a1*w1 + ... + ak*wk */
823 /* */
824 /* but we only have the a0, a1-a0, a2-a0, .. ak-a0 */
825 /* however, given that w0 + w1 + ... + wk == 1, we can */
826 /* rewrite it easily as: */
827 /* */
828 /* a0 + (a1-a0)*w1 + (a2-a0)*w2 + .. + (ak-a0)*wk */
829 /* */
830 /* where k == num_designs-1 */
831 /* */
832 /* I guess that's why it's written in this `compact' */
833 /* form. */
834 /* */
835 delta = top + num_points;
836 values = top;
837 for ( nn = 0; nn < num_points; nn++ )
838 {
839 FT_Long tmp = values[0];
840
841
842 for ( mm = 1; mm < blend->num_designs; mm++ )
843 tmp += FT_MulFix( *delta++, blend->weight_vector[mm] );
844
845 *values++ = tmp;
846 }
847
848 known_othersubr_result_cnt = num_points;
849 break;
850 }
851
852 case 19:
853 /* <idx> 1 19 callothersubr */
854 /* => replace elements starting from index cvi( <idx> ) */
855 /* of BuildCharArray with WeightVector */
856 {
857 FT_Int idx;
858 PS_Blend blend = decoder->blend;
859
860
861 if ( arg_cnt != 1 || blend == NULL )
862 goto Unexpected_OtherSubr;
863
864 idx = (FT_Int)( top[0] >> 16 );
865
866 if ( idx < 0 ||
867 idx + blend->num_designs > decoder->len_buildchar )
868 goto Unexpected_OtherSubr;
869
870 ft_memcpy( &decoder->buildchar[idx],
871 blend->weight_vector,
872 blend->num_designs *
873 sizeof( blend->weight_vector[0] ) );
874 }
875 break;
876
877 case 20:
878 /* <arg1> <arg2> 2 20 callothersubr pop */
879 /* ==> push <arg1> + <arg2> onto T1 stack */
880 if ( arg_cnt != 2 )
881 goto Unexpected_OtherSubr;
882
883 top[0] += top[1]; /* XXX (over|under)flow */
884
885 known_othersubr_result_cnt = 1;
886 break;
887
888 case 21:
889 /* <arg1> <arg2> 2 21 callothersubr pop */
890 /* ==> push <arg1> - <arg2> onto T1 stack */
891 if ( arg_cnt != 2 )
892 goto Unexpected_OtherSubr;
893
894 top[0] -= top[1]; /* XXX (over|under)flow */
895
896 known_othersubr_result_cnt = 1;
897 break;
898
899 case 22:
900 /* <arg1> <arg2> 2 22 callothersubr pop */
901 /* ==> push <arg1> * <arg2> onto T1 stack */
902 if ( arg_cnt != 2 )
903 goto Unexpected_OtherSubr;
904
905 top[0] = FT_MulFix( top[0], top[1] );
906
907 known_othersubr_result_cnt = 1;
908 break;
909
910 case 23:
911 /* <arg1> <arg2> 2 23 callothersubr pop */
912 /* ==> push <arg1> / <arg2> onto T1 stack */
913 if ( arg_cnt != 2 || top[1] == 0 )
914 goto Unexpected_OtherSubr;
915
916 top[0] = FT_DivFix( top[0], top[1] );
917
918 known_othersubr_result_cnt = 1;
919 break;
920
921 case 24:
922 /* <val> <idx> 2 24 callothersubr */
923 /* ==> set BuildCharArray[cvi( <idx> )] = <val> */
924 {
925 FT_Int idx;
926 PS_Blend blend = decoder->blend;
927
928
929 if ( arg_cnt != 2 || blend == NULL )
930 goto Unexpected_OtherSubr;
931
932 idx = (FT_Int)( top[1] >> 16 );
933
934 if ( idx < 0 || (FT_UInt) idx >= decoder->len_buildchar )
935 goto Unexpected_OtherSubr;
936
937 decoder->buildchar[idx] = top[0];
938 }
939 break;
940
941 case 25:
942 /* <idx> 1 25 callothersubr pop */
943 /* ==> push BuildCharArray[cvi( idx )] */
944 /* onto T1 stack */
945 {
946 FT_Int idx;
947 PS_Blend blend = decoder->blend;
948
949
950 if ( arg_cnt != 1 || blend == NULL )
951 goto Unexpected_OtherSubr;
952
953 idx = (FT_Int)( top[0] >> 16 );
954
955 if ( idx < 0 || (FT_UInt) idx >= decoder->len_buildchar )
956 goto Unexpected_OtherSubr;
957
958 top[0] = decoder->buildchar[idx];
959 }
960
961 known_othersubr_result_cnt = 1;
962 break;
963
964 #if 0
965 case 26:
966 /* <val> mark <idx> ==> set BuildCharArray[cvi( <idx> )] = <val>, */
967 /* leave mark on T1 stack */
968 /* <val> <idx> ==> set BuildCharArray[cvi( <idx> )] = <val> */
969 XXX which routine has left its mark on the (PostScript) stack?;
970 break;
971 #endif
972
973 case 27:
974 /* <res1> <res2> <val1> <val2> 4 27 callothersubr pop */
975 /* ==> push <res1> onto T1 stack if <val1> <= <val2>, */
976 /* otherwise push <res2> */
977 if ( arg_cnt != 4 )
978 goto Unexpected_OtherSubr;
979
980 if ( top[2] > top[3] )
981 top[0] = top[1];
982
983 known_othersubr_result_cnt = 1;
984 break;
985
986 case 28:
987 /* 0 28 callothersubr pop */
988 /* => push random value from interval [0, 1) onto stack */
989 if ( arg_cnt != 0 )
990 goto Unexpected_OtherSubr;
991
992 {
993 FT_Fixed Rand;
994
995
996 Rand = seed;
997 if ( Rand >= 0x8000L )
998 Rand++;
999
1000 top[0] = Rand;
1001
1002 seed = FT_MulFix( seed, 0x10000L - seed );
1003 if ( seed == 0 )
1004 seed += 0x2873;
1005 }
1006
1007 known_othersubr_result_cnt = 1;
1008 break;
1009
1010 default:
1011 FT_ERROR(( "t1_decoder_parse_charstrings:"
1012 " unknown othersubr [%d %d], wish me luck\n",
1013 arg_cnt, subr_no ));
1014 unknown_othersubr_result_cnt = arg_cnt;
1015 break;
1016
1017 Unexpected_OtherSubr:
1018 FT_ERROR(( "t1_decoder_parse_charstrings:"
1019 " invalid othersubr [%d %d]\n", arg_cnt, subr_no ));
1020 goto Syntax_Error;
1021 }
1022
1023 top += known_othersubr_result_cnt;
1024
1025 decoder->top = top;
1026 }
1027 else /* general operator */
1028 {
1029 FT_Int num_args = t1_args_count[op];
1030
1031
1032 FT_ASSERT( num_args >= 0 );
1033
1034 if ( top - decoder->stack < num_args )
1035 goto Stack_Underflow;
1036
1037 /* XXX Operators usually take their operands from the */
1038 /* bottom of the stack, i.e., the operands are */
1039 /* decoder->stack[0], ..., decoder->stack[num_args - 1]; */
1040 /* only div, callsubr, and callothersubr are different. */
1041 /* In practice it doesn't matter (?). */
1042
1043 #ifdef FT_DEBUG_LEVEL_TRACE
1044
1045 switch ( op )
1046 {
1047 case op_callsubr:
1048 case op_div:
1049 case op_callothersubr:
1050 case op_pop:
1051 case op_return:
1052 break;
1053
1054 default:
1055 if ( top - decoder->stack != num_args )
1056 FT_TRACE0(( "t1_decoder_parse_charstrings:"
1057 " too much operands on the stack"
1058 " (seen %d, expected %d)\n",
1059 top - decoder->stack, num_args ));
1060 break;
1061 }
1062
1063 #endif /* FT_DEBUG_LEVEL_TRACE */
1064
1065 top -= num_args;
1066
1067 switch ( op )
1068 {
1069 case op_endchar:
1070 FT_TRACE4(( " endchar\n" ));
1071
1072 close_contour( builder );
1073
1074 /* close hints recording session */
1075 if ( hinter )
1076 {
1077 if ( hinter->close( hinter->hints, builder->current->n_points ) )
1078 goto Syntax_Error;
1079
1080 /* apply hints to the loaded glyph outline now */
1081 hinter->apply( hinter->hints,
1082 builder->current,
1083 (PSH_Globals)builder->hints_globals,
1084 decoder->hint_mode );
1085 }
1086
1087 /* add current outline to the glyph slot */
1088 FT_GlyphLoader_Add( builder->loader );
1089
1090 /* the compiler should optimize away this empty loop but ... */
1091
1092 #ifdef FT_DEBUG_LEVEL_TRACE
1093
1094 if ( decoder->len_buildchar > 0 )
1095 {
1096 FT_UInt i;
1097
1098
1099 FT_TRACE4(( "BuildCharArray = [ " ));
1100
1101 for ( i = 0; i < decoder->len_buildchar; ++i )
1102 FT_TRACE4(( "%d ", decoder->buildchar[ i ] ));
1103
1104 FT_TRACE4(( "]\n" ));
1105 }
1106
1107 #endif /* FT_DEBUG_LEVEL_TRACE */
1108
1109 FT_TRACE4(( "\n" ));
1110
1111 /* return now! */
1112 return PSaux_Err_Ok;
1113
1114 case op_hsbw:
1115 FT_TRACE4(( " hsbw" ));
1116
1117 builder->parse_state = T1_Parse_Have_Width;
1118
1119 builder->left_bearing.x += top[0];
1120 builder->advance.x = top[1];
1121 builder->advance.y = 0;
1122
1123 orig_x = x = builder->pos_x + top[0];
1124 orig_y = y = builder->pos_y;
1125
1126 FT_UNUSED( orig_y );
1127
1128 /* the `metrics_only' indicates that we only want to compute */
1129 /* the glyph's metrics (lsb + advance width), not load the */
1130 /* rest of it; so exit immediately */
1131 if ( builder->metrics_only )
1132 return PSaux_Err_Ok;
1133
1134 break;
1135
1136 case op_seac:
1137 return t1operator_seac( decoder,
1138 top[0],
1139 top[1],
1140 top[2],
1141 (FT_Int)( top[3] >> 16 ),
1142 (FT_Int)( top[4] >> 16 ) );
1143
1144 case op_sbw:
1145 FT_TRACE4(( " sbw" ));
1146
1147 builder->parse_state = T1_Parse_Have_Width;
1148
1149 builder->left_bearing.x += top[0];
1150 builder->left_bearing.y += top[1];
1151 builder->advance.x = top[2];
1152 builder->advance.y = top[3];
1153
1154 x = builder->pos_x + top[0];
1155 y = builder->pos_y + top[1];
1156
1157 /* the `metrics_only' indicates that we only want to compute */
1158 /* the glyph's metrics (lsb + advance width), not load the */
1159 /* rest of it; so exit immediately */
1160 if ( builder->metrics_only )
1161 return PSaux_Err_Ok;
1162
1163 break;
1164
1165 case op_closepath:
1166 FT_TRACE4(( " closepath" ));
1167
1168 /* if there is no path, `closepath' is a no-op */
1169 if ( builder->parse_state == T1_Parse_Have_Path ||
1170 builder->parse_state == T1_Parse_Have_Moveto )
1171 close_contour( builder );
1172
1173 builder->parse_state = T1_Parse_Have_Width;
1174 break;
1175
1176 case op_hlineto:
1177 FT_TRACE4(( " hlineto" ));
1178
1179 if ( start_point( builder, x, y ) )
1180 goto Fail;
1181
1182 x += top[0];
1183 goto Add_Line;
1184
1185 case op_hmoveto:
1186 FT_TRACE4(( " hmoveto" ));
1187
1188 x += top[0];
1189 if ( !decoder->flex_state )
1190 {
1191 if ( builder->parse_state == T1_Parse_Start )
1192 goto Syntax_Error;
1193 builder->parse_state = T1_Parse_Have_Moveto;
1194 }
1195 break;
1196
1197 case op_hvcurveto:
1198 FT_TRACE4(( " hvcurveto" ));
1199
1200 if ( start_point( builder, x, y ) ||
1201 check_points( builder, 3 ) )
1202 goto Fail;
1203
1204 x += top[0];
1205 add_point( builder, x, y, 0 );
1206 x += top[1];
1207 y += top[2];
1208 add_point( builder, x, y, 0 );
1209 y += top[3];
1210 add_point( builder, x, y, 1 );
1211 break;
1212
1213 case op_rlineto:
1214 FT_TRACE4(( " rlineto" ));
1215
1216 if ( start_point( builder, x, y ) )
1217 goto Fail;
1218
1219 x += top[0];
1220 y += top[1];
1221
1222 Add_Line:
1223 if ( add_point1( builder, x, y ) )
1224 goto Fail;
1225 break;
1226
1227 case op_rmoveto:
1228 FT_TRACE4(( " rmoveto" ));
1229
1230 x += top[0];
1231 y += top[1];
1232 if ( !decoder->flex_state )
1233 {
1234 if ( builder->parse_state == T1_Parse_Start )
1235 goto Syntax_Error;
1236 builder->parse_state = T1_Parse_Have_Moveto;
1237 }
1238 break;
1239
1240 case op_rrcurveto:
1241 FT_TRACE4(( " rrcurveto" ));
1242
1243 if ( start_point( builder, x, y ) ||
1244 check_points( builder, 3 ) )
1245 goto Fail;
1246
1247 x += top[0];
1248 y += top[1];
1249 add_point( builder, x, y, 0 );
1250
1251 x += top[2];
1252 y += top[3];
1253 add_point( builder, x, y, 0 );
1254
1255 x += top[4];
1256 y += top[5];
1257 add_point( builder, x, y, 1 );
1258 break;
1259
1260 case op_vhcurveto:
1261 FT_TRACE4(( " vhcurveto" ));
1262
1263 if ( start_point( builder, x, y ) ||
1264 check_points( builder, 3 ) )
1265 goto Fail;
1266
1267 y += top[0];
1268 add_point( builder, x, y, 0 );
1269 x += top[1];
1270 y += top[2];
1271 add_point( builder, x, y, 0 );
1272 x += top[3];
1273 add_point( builder, x, y, 1 );
1274 break;
1275
1276 case op_vlineto:
1277 FT_TRACE4(( " vlineto" ));
1278
1279 if ( start_point( builder, x, y ) )
1280 goto Fail;
1281
1282 y += top[0];
1283 goto Add_Line;
1284
1285 case op_vmoveto:
1286 FT_TRACE4(( " vmoveto" ));
1287
1288 y += top[0];
1289 if ( !decoder->flex_state )
1290 {
1291 if ( builder->parse_state == T1_Parse_Start )
1292 goto Syntax_Error;
1293 builder->parse_state = T1_Parse_Have_Moveto;
1294 }
1295 break;
1296
1297 case op_div:
1298 FT_TRACE4(( " div" ));
1299
1300 /* if `large_int' is set, we divide unscaled numbers; */
1301 /* otherwise, we divide numbers in 16.16 format -- */
1302 /* in both cases, it is the same operation */
1303 *top = FT_DivFix( top[0], top[1] );
1304 ++top;
1305
1306 large_int = FALSE;
1307 break;
1308
1309 case op_callsubr:
1310 {
1311 FT_Int idx;
1312
1313
1314 FT_TRACE4(( " callsubr" ));
1315
1316 idx = (FT_Int)( top[0] >> 16 );
1317 if ( idx < 0 || idx >= (FT_Int)decoder->num_subrs )
1318 {
1319 FT_ERROR(( "t1_decoder_parse_charstrings:"
1320 " invalid subrs index\n" ));
1321 goto Syntax_Error;
1322 }
1323
1324 if ( zone - decoder->zones >= T1_MAX_SUBRS_CALLS )
1325 {
1326 FT_ERROR(( "t1_decoder_parse_charstrings:"
1327 " too many nested subrs\n" ));
1328 goto Syntax_Error;
1329 }
1330
1331 zone->cursor = ip; /* save current instruction pointer */
1332
1333 zone++;
1334
1335 /* The Type 1 driver stores subroutines without the seed bytes. */
1336 /* The CID driver stores subroutines with seed bytes. This */
1337 /* case is taken care of when decoder->subrs_len == 0. */
1338 zone->base = decoder->subrs[idx];
1339
1340 if ( decoder->subrs_len )
1341 zone->limit = zone->base + decoder->subrs_len[idx];
1342 else
1343 {
1344 /* We are using subroutines from a CID font. We must adjust */
1345 /* for the seed bytes. */
1346 zone->base += ( decoder->lenIV >= 0 ? decoder->lenIV : 0 );
1347 zone->limit = decoder->subrs[idx + 1];
1348 }
1349
1350 zone->cursor = zone->base;
1351
1352 if ( !zone->base )
1353 {
1354 FT_ERROR(( "t1_decoder_parse_charstrings:"
1355 " invoking empty subrs\n" ));
1356 goto Syntax_Error;
1357 }
1358
1359 decoder->zone = zone;
1360 ip = zone->base;
1361 limit = zone->limit;
1362 break;
1363 }
1364
1365 case op_pop:
1366 FT_TRACE4(( " pop" ));
1367
1368 if ( known_othersubr_result_cnt > 0 )
1369 {
1370 known_othersubr_result_cnt--;
1371 /* ignore, we pushed the operands ourselves */
1372 break;
1373 }
1374
1375 if ( unknown_othersubr_result_cnt == 0 )
1376 {
1377 FT_ERROR(( "t1_decoder_parse_charstrings:"
1378 " no more operands for othersubr\n" ));
1379 goto Syntax_Error;
1380 }
1381
1382 unknown_othersubr_result_cnt--;
1383 top++; /* `push' the operand to callothersubr onto the stack */
1384 break;
1385
1386 case op_return:
1387 FT_TRACE4(( " return" ));
1388
1389 if ( zone <= decoder->zones )
1390 {
1391 FT_ERROR(( "t1_decoder_parse_charstrings:"
1392 " unexpected return\n" ));
1393 goto Syntax_Error;
1394 }
1395
1396 zone--;
1397 ip = zone->cursor;
1398 limit = zone->limit;
1399 decoder->zone = zone;
1400 break;
1401
1402 case op_dotsection:
1403 FT_TRACE4(( " dotsection" ));
1404
1405 break;
1406
1407 case op_hstem:
1408 FT_TRACE4(( " hstem" ));
1409
1410 /* record horizontal hint */
1411 if ( hinter )
1412 {
1413 /* top[0] += builder->left_bearing.y; */
1414 hinter->stem( hinter->hints, 1, top );
1415 }
1416 break;
1417
1418 case op_hstem3:
1419 FT_TRACE4(( " hstem3" ));
1420
1421 /* record horizontal counter-controlled hints */
1422 if ( hinter )
1423 hinter->stem3( hinter->hints, 1, top );
1424 break;
1425
1426 case op_vstem:
1427 FT_TRACE4(( " vstem" ));
1428
1429 /* record vertical hint */
1430 if ( hinter )
1431 {
1432 top[0] += orig_x;
1433 hinter->stem( hinter->hints, 0, top );
1434 }
1435 break;
1436
1437 case op_vstem3:
1438 FT_TRACE4(( " vstem3" ));
1439
1440 /* record vertical counter-controlled hints */
1441 if ( hinter )
1442 {
1443 FT_Pos dx = orig_x;
1444
1445
1446 top[0] += dx;
1447 top[2] += dx;
1448 top[4] += dx;
1449 hinter->stem3( hinter->hints, 0, top );
1450 }
1451 break;
1452
1453 case op_setcurrentpoint:
1454 FT_TRACE4(( " setcurrentpoint" ));
1455
1456 /* From the T1 specs, section 6.4: */
1457 /* */
1458 /* The setcurrentpoint command is used only in */
1459 /* conjunction with results from OtherSubrs procedures. */
1460
1461 /* known_othersubr_result_cnt != 0 is already handled above */
1462 if ( decoder->flex_state != 1 )
1463 {
1464 FT_ERROR(( "t1_decoder_parse_charstrings:"
1465 " unexpected `setcurrentpoint'\n" ));
1466 goto Syntax_Error;
1467 }
1468 else
1469 decoder->flex_state = 0;
1470 break;
1471
1472 case op_unknown15:
1473 FT_TRACE4(( " opcode_15" ));
1474 /* nothing to do except to pop the two arguments */
1475 break;
1476
1477 default:
1478 FT_ERROR(( "t1_decoder_parse_charstrings:"
1479 " unhandled opcode %d\n", op ));
1480 goto Syntax_Error;
1481 }
1482
1483 /* XXX Operators usually clear the operand stack; */
1484 /* only div, callsubr, callothersubr, pop, and */
1485 /* return are different. */
1486 /* In practice it doesn't matter (?). */
1487
1488 decoder->top = top;
1489
1490 #ifdef FT_DEBUG_LEVEL_TRACE
1491 FT_TRACE4(( "\n" ));
1492 bol = TRUE;
1493 #endif
1494
1495 } /* general operator processing */
1496
1497 } /* while ip < limit */
1498
1499 FT_TRACE4(( "..end..\n\n" ));
1500
1501 Fail:
1502 return error;
1503
1504 Syntax_Error:
1505 return PSaux_Err_Syntax_Error;
1506
1507 Stack_Underflow:
1508 return PSaux_Err_Stack_Underflow;
1509 }
1510
1511
1512 /* parse a single Type 1 glyph */
1513 FT_LOCAL_DEF( FT_Error )
1514 t1_decoder_parse_glyph( T1_Decoder decoder,
1515 FT_UInt glyph )
1516 {
1517 return decoder->parse_callback( decoder, glyph );
1518 }
1519
1520
1521 /* initialize T1 decoder */
1522 FT_LOCAL_DEF( FT_Error )
1523 t1_decoder_init( T1_Decoder decoder,
1524 FT_Face face,
1525 FT_Size size,
1526 FT_GlyphSlot slot,
1527 FT_Byte** glyph_names,
1528 PS_Blend blend,
1529 FT_Bool hinting,
1530 FT_Render_Mode hint_mode,
1531 T1_Decoder_Callback parse_callback )
1532 {
1533 FT_MEM_ZERO( decoder, sizeof ( *decoder ) );
1534
1535 /* retrieve PSNames interface from list of current modules */
1536 {
1537 FT_Service_PsCMaps psnames = 0;
1538
1539
1540 FT_FACE_FIND_GLOBAL_SERVICE( face, psnames, POSTSCRIPT_CMAPS );
1541 if ( !psnames )
1542 {
1543 FT_ERROR(( "t1_decoder_init:"
1544 " the `psnames' module is not available\n" ));
1545 return PSaux_Err_Unimplemented_Feature;
1546 }
1547
1548 decoder->psnames = psnames;
1549 }
1550
1551 t1_builder_init( &decoder->builder, face, size, slot, hinting );
1552
1553 /* decoder->buildchar and decoder->len_buildchar have to be */
1554 /* initialized by the caller since we cannot know the length */
1555 /* of the BuildCharArray */
1556
1557 decoder->num_glyphs = (FT_UInt)face->num_glyphs;
1558 decoder->glyph_names = glyph_names;
1559 decoder->hint_mode = hint_mode;
1560 decoder->blend = blend;
1561 decoder->parse_callback = parse_callback;
1562
1563 decoder->funcs = t1_decoder_funcs;
1564
1565 return PSaux_Err_Ok;
1566 }
1567
1568
1569 /* finalize T1 decoder */
1570 FT_LOCAL_DEF( void )
1571 t1_decoder_done( T1_Decoder decoder )
1572 {
1573 t1_builder_done( &decoder->builder );
1574 }
1575
1576
1577 /* END */