1 /***************************************************************************/
2 /*                                                                         */
3 /*  cf2intrp.c                                                             */
4 /*                                                                         */
5 /*    Adobe's CFF Interpreter (body).                                      */
6 /*                                                                         */
7 /*  Copyright 2007-2014 Adobe Systems Incorporated.                        */
8 /*                                                                         */
9 /*  This software, and all works of authorship, whether in source or       */
10 /*  object code form as indicated by the copyright notice(s) included      */
11 /*  herein (collectively, the "Work") is made available, and may only be   */
12 /*  used, modified, and distributed under the FreeType Project License,    */
13 /*  LICENSE.TXT.  Additionally, subject to the terms and conditions of the */
14 /*  FreeType Project License, each contributor to the Work hereby grants   */
15 /*  to any individual or legal entity exercising permissions granted by    */
16 /*  the FreeType Project License and this section (hereafter, "You" or     */
17 /*  "Your") a perpetual, worldwide, non-exclusive, no-charge,              */
18 /*  royalty-free, irrevocable (except as stated in this section) patent    */
19 /*  license to make, have made, use, offer to sell, sell, import, and      */
20 /*  otherwise transfer the Work, where such license applies only to those  */
21 /*  patent claims licensable by such contributor that are necessarily      */
22 /*  infringed by their contribution(s) alone or by combination of their    */
23 /*  contribution(s) with the Work to which such contribution(s) was        */
24 /*  submitted.  If You institute patent litigation against any entity      */
25 /*  (including a cross-claim or counterclaim in a lawsuit) alleging that   */
26 /*  the Work or a contribution incorporated within the Work constitutes    */
27 /*  direct or contributory patent infringement, then any patent licenses   */
28 /*  granted to You under this License for that Work shall terminate as of  */
29 /*  the date such litigation is filed.                                     */
30 /*                                                                         */
31 /*  By using, modifying, or distributing the Work you indicate that you    */
32 /*  have read and understood the terms and conditions of the               */
33 /*  FreeType Project License as well as those provided in this section,    */
34 /*  and you accept them fully.                                             */
35 /*                                                                         */
36 /***************************************************************************/
37 
38 
39 #include "cf2ft.h"
40 #include FT_INTERNAL_DEBUG_H
41 
42 #include "cf2glue.h"
43 #include "cf2font.h"
44 #include "cf2stack.h"
45 #include "cf2hints.h"
46 #include "cf2intrp.h"
47 
48 #include "cf2error.h"
49 
50 
51   /*************************************************************************/
52   /*                                                                       */
53   /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
54   /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
55   /* messages during execution.                                            */
56   /*                                                                       */
57 #undef  FT_COMPONENT
58 #define FT_COMPONENT  trace_cf2interp
59 
60 
61   /* some operators are not implemented yet */
62 #define CF2_FIXME  FT_TRACE4(( "cf2_interpT2CharString:"            \
63                                " operator not implemented yet\n" ))
64 
65 
66 
67   FT_LOCAL_DEF( void )
cf2_hintmask_init(CF2_HintMask hintmask,FT_Error * error)68   cf2_hintmask_init( CF2_HintMask  hintmask,
69                      FT_Error*     error )
70   {
71     FT_ZERO( hintmask );
72 
73     hintmask->error = error;
74   }
75 
76 
77   FT_LOCAL_DEF( FT_Bool )
cf2_hintmask_isValid(const CF2_HintMask hintmask)78   cf2_hintmask_isValid( const CF2_HintMask  hintmask )
79   {
80     return hintmask->isValid;
81   }
82 
83 
84   FT_LOCAL_DEF( FT_Bool )
cf2_hintmask_isNew(const CF2_HintMask hintmask)85   cf2_hintmask_isNew( const CF2_HintMask  hintmask )
86   {
87     return hintmask->isNew;
88   }
89 
90 
91   FT_LOCAL_DEF( void )
cf2_hintmask_setNew(CF2_HintMask hintmask,FT_Bool val)92   cf2_hintmask_setNew( CF2_HintMask  hintmask,
93                        FT_Bool       val )
94   {
95     hintmask->isNew = val;
96   }
97 
98 
99   /* clients call `getMaskPtr' in order to iterate */
100   /* through hint mask                             */
101 
102   FT_LOCAL_DEF( FT_Byte* )
cf2_hintmask_getMaskPtr(CF2_HintMask hintmask)103   cf2_hintmask_getMaskPtr( CF2_HintMask  hintmask )
104   {
105     return hintmask->mask;
106   }
107 
108 
109   static size_t
cf2_hintmask_setCounts(CF2_HintMask hintmask,size_t bitCount)110   cf2_hintmask_setCounts( CF2_HintMask  hintmask,
111                           size_t        bitCount )
112   {
113     if ( bitCount > CF2_MAX_HINTS )
114     {
115       /* total of h and v stems must be <= 96 */
116       CF2_SET_ERROR( hintmask->error, Invalid_Glyph_Format );
117       return 0;
118     }
119 
120     hintmask->bitCount  = bitCount;
121     hintmask->byteCount = ( hintmask->bitCount + 7 ) / 8;
122 
123     hintmask->isValid = TRUE;
124     hintmask->isNew   = TRUE;
125 
126     return bitCount;
127   }
128 
129 
130   /* consume the hintmask bytes from the charstring, advancing the src */
131   /* pointer                                                           */
132   static void
cf2_hintmask_read(CF2_HintMask hintmask,CF2_Buffer charstring,size_t bitCount)133   cf2_hintmask_read( CF2_HintMask  hintmask,
134                      CF2_Buffer    charstring,
135                      size_t        bitCount )
136   {
137     size_t  i;
138 
139 #ifndef CF2_NDEBUG
140     /* these are the bits in the final mask byte that should be zero  */
141     /* Note: this variable is only used in an assert expression below */
142     /* and then only if CF2_NDEBUG is not defined                     */
143     CF2_UInt  mask = ( 1 << ( -(CF2_Int)bitCount & 7 ) ) - 1;
144 #endif
145 
146 
147     /* initialize counts and isValid */
148     if ( cf2_hintmask_setCounts( hintmask, bitCount ) == 0 )
149       return;
150 
151     FT_ASSERT( hintmask->byteCount > 0 );
152 
153     FT_TRACE4(( " (maskbytes:" ));
154 
155     /* set mask and advance interpreter's charstring pointer */
156     for ( i = 0; i < hintmask->byteCount; i++ )
157     {
158       hintmask->mask[i] = (FT_Byte)cf2_buf_readByte( charstring );
159       FT_TRACE4(( " 0x%02X", hintmask->mask[i] ));
160     }
161 
162     FT_TRACE4(( ")\n" ));
163 
164     /* assert any unused bits in last byte are zero unless there's a prior */
165     /* error                                                               */
166     /* bitCount -> mask, 0 -> 0, 1 -> 7f, 2 -> 3f, ... 6 -> 3, 7 -> 1      */
167 #ifndef CF2_NDEBUG
168     FT_ASSERT( ( hintmask->mask[hintmask->byteCount - 1] & mask ) == 0 ||
169                *hintmask->error                                        );
170 #endif
171   }
172 
173 
174   FT_LOCAL_DEF( void )
cf2_hintmask_setAll(CF2_HintMask hintmask,size_t bitCount)175   cf2_hintmask_setAll( CF2_HintMask  hintmask,
176                        size_t        bitCount )
177   {
178     size_t    i;
179     CF2_UInt  mask = ( 1 << ( -(CF2_Int)bitCount & 7 ) ) - 1;
180 
181 
182     /* initialize counts and isValid */
183     if ( cf2_hintmask_setCounts( hintmask, bitCount ) == 0 )
184       return;
185 
186     FT_ASSERT( hintmask->byteCount > 0 );
187     FT_ASSERT( hintmask->byteCount <=
188                  sizeof ( hintmask->mask ) / sizeof ( hintmask->mask[0] ) );
189 
190     /* set mask to all ones */
191     for ( i = 0; i < hintmask->byteCount; i++ )
192       hintmask->mask[i] = 0xFF;
193 
194     /* clear unused bits                                              */
195     /* bitCount -> mask, 0 -> 0, 1 -> 7f, 2 -> 3f, ... 6 -> 3, 7 -> 1 */
196     hintmask->mask[hintmask->byteCount - 1] &= ~mask;
197   }
198 
199 
200   /* Type2 charstring opcodes */
201   enum
202   {
203     cf2_cmdRESERVED_0,   /* 0 */
204     cf2_cmdHSTEM,        /* 1 */
205     cf2_cmdRESERVED_2,   /* 2 */
206     cf2_cmdVSTEM,        /* 3 */
207     cf2_cmdVMOVETO,      /* 4 */
208     cf2_cmdRLINETO,      /* 5 */
209     cf2_cmdHLINETO,      /* 6 */
210     cf2_cmdVLINETO,      /* 7 */
211     cf2_cmdRRCURVETO,    /* 8 */
212     cf2_cmdRESERVED_9,   /* 9 */
213     cf2_cmdCALLSUBR,     /* 10 */
214     cf2_cmdRETURN,       /* 11 */
215     cf2_cmdESC,          /* 12 */
216     cf2_cmdRESERVED_13,  /* 13 */
217     cf2_cmdENDCHAR,      /* 14 */
218     cf2_cmdRESERVED_15,  /* 15 */
219     cf2_cmdRESERVED_16,  /* 16 */
220     cf2_cmdRESERVED_17,  /* 17 */
221     cf2_cmdHSTEMHM,      /* 18 */
222     cf2_cmdHINTMASK,     /* 19 */
223     cf2_cmdCNTRMASK,     /* 20 */
224     cf2_cmdRMOVETO,      /* 21 */
225     cf2_cmdHMOVETO,      /* 22 */
226     cf2_cmdVSTEMHM,      /* 23 */
227     cf2_cmdRCURVELINE,   /* 24 */
228     cf2_cmdRLINECURVE,   /* 25 */
229     cf2_cmdVVCURVETO,    /* 26 */
230     cf2_cmdHHCURVETO,    /* 27 */
231     cf2_cmdEXTENDEDNMBR, /* 28 */
232     cf2_cmdCALLGSUBR,    /* 29 */
233     cf2_cmdVHCURVETO,    /* 30 */
234     cf2_cmdHVCURVETO     /* 31 */
235   };
236 
237   enum
238   {
239     cf2_escDOTSECTION,   /* 0 */
240     cf2_escRESERVED_1,   /* 1 */
241     cf2_escRESERVED_2,   /* 2 */
242     cf2_escAND,          /* 3 */
243     cf2_escOR,           /* 4 */
244     cf2_escNOT,          /* 5 */
245     cf2_escRESERVED_6,   /* 6 */
246     cf2_escRESERVED_7,   /* 7 */
247     cf2_escRESERVED_8,   /* 8 */
248     cf2_escABS,          /* 9 */
249     cf2_escADD,          /* 10     like otherADD */
250     cf2_escSUB,          /* 11     like otherSUB */
251     cf2_escDIV,          /* 12 */
252     cf2_escRESERVED_13,  /* 13 */
253     cf2_escNEG,          /* 14 */
254     cf2_escEQ,           /* 15 */
255     cf2_escRESERVED_16,  /* 16 */
256     cf2_escRESERVED_17,  /* 17 */
257     cf2_escDROP,         /* 18 */
258     cf2_escRESERVED_19,  /* 19 */
259     cf2_escPUT,          /* 20     like otherPUT    */
260     cf2_escGET,          /* 21     like otherGET    */
261     cf2_escIFELSE,       /* 22     like otherIFELSE */
262     cf2_escRANDOM,       /* 23     like otherRANDOM */
263     cf2_escMUL,          /* 24     like otherMUL    */
264     cf2_escRESERVED_25,  /* 25 */
265     cf2_escSQRT,         /* 26 */
266     cf2_escDUP,          /* 27     like otherDUP    */
267     cf2_escEXCH,         /* 28     like otherEXCH   */
268     cf2_escINDEX,        /* 29 */
269     cf2_escROLL,         /* 30 */
270     cf2_escRESERVED_31,  /* 31 */
271     cf2_escRESERVED_32,  /* 32 */
272     cf2_escRESERVED_33,  /* 33 */
273     cf2_escHFLEX,        /* 34 */
274     cf2_escFLEX,         /* 35 */
275     cf2_escHFLEX1,       /* 36 */
276     cf2_escFLEX1         /* 37 */
277   };
278 
279 
280   /* `stemHintArray' does not change once we start drawing the outline. */
281   static void
cf2_doStems(const CF2_Font font,CF2_Stack opStack,CF2_ArrStack stemHintArray,CF2_Fixed * width,FT_Bool * haveWidth,CF2_Fixed hintOffset)282   cf2_doStems( const CF2_Font  font,
283                CF2_Stack       opStack,
284                CF2_ArrStack    stemHintArray,
285                CF2_Fixed*      width,
286                FT_Bool*        haveWidth,
287                CF2_Fixed       hintOffset )
288   {
289     CF2_UInt  i;
290     CF2_UInt  count       = cf2_stack_count( opStack );
291     FT_Bool   hasWidthArg = (FT_Bool)( count & 1 );
292 
293     /* variable accumulates delta values from operand stack */
294     CF2_Fixed  position = hintOffset;
295 
296 
297     if ( hasWidthArg && !*haveWidth )
298       *width = cf2_stack_getReal( opStack, 0 ) +
299                  cf2_getNominalWidthX( font->decoder );
300 
301     if ( font->decoder->width_only )
302       goto exit;
303 
304     for ( i = hasWidthArg ? 1 : 0; i < count; i += 2 )
305     {
306       /* construct a CF2_StemHint and push it onto the list */
307       CF2_StemHintRec  stemhint;
308 
309 
310       stemhint.min  =
311         position   += cf2_stack_getReal( opStack, i );
312       stemhint.max  =
313         position   += cf2_stack_getReal( opStack, i + 1 );
314 
315       stemhint.used  = FALSE;
316       stemhint.maxDS =
317       stemhint.minDS = 0;
318 
319       cf2_arrstack_push( stemHintArray, &stemhint ); /* defer error check */
320     }
321 
322     cf2_stack_clear( opStack );
323 
324   exit:
325     /* cf2_doStems must define a width (may be default) */
326     *haveWidth = TRUE;
327   }
328 
329 
330   static void
cf2_doFlex(CF2_Stack opStack,CF2_Fixed * curX,CF2_Fixed * curY,CF2_GlyphPath glyphPath,const FT_Bool * readFromStack,FT_Bool doConditionalLastRead)331   cf2_doFlex( CF2_Stack       opStack,
332               CF2_Fixed*      curX,
333               CF2_Fixed*      curY,
334               CF2_GlyphPath   glyphPath,
335               const FT_Bool*  readFromStack,
336               FT_Bool         doConditionalLastRead )
337   {
338     CF2_Fixed  vals[14];
339     CF2_UInt   index;
340     FT_Bool    isHFlex;
341     CF2_Int    top, i, j;
342 
343 
344     vals[0] = *curX;
345     vals[1] = *curY;
346     index   = 0;
347     isHFlex = readFromStack[9] == FALSE;
348     top     = isHFlex ? 9 : 10;
349 
350     for ( i = 0; i < top; i++ )
351     {
352       vals[i + 2] = vals[i];
353       if ( readFromStack[i] )
354         vals[i + 2] += cf2_stack_getReal( opStack, index++ );
355     }
356 
357     if ( isHFlex )
358       vals[9 + 2] = *curY;
359 
360     if ( doConditionalLastRead )
361     {
362       FT_Bool    lastIsX = (FT_Bool)( cf2_fixedAbs( vals[10] - *curX ) >
363                                         cf2_fixedAbs( vals[11] - *curY ) );
364       CF2_Fixed  lastVal = cf2_stack_getReal( opStack, index );
365 
366 
367       if ( lastIsX )
368       {
369         vals[12] = vals[10] + lastVal;
370         vals[13] = *curY;
371       }
372       else
373       {
374         vals[12] = *curX;
375         vals[13] = vals[11] + lastVal;
376       }
377     }
378     else
379     {
380       if ( readFromStack[10] )
381         vals[12] = vals[10] + cf2_stack_getReal( opStack, index++ );
382       else
383         vals[12] = *curX;
384 
385       if ( readFromStack[11] )
386         vals[13] = vals[11] + cf2_stack_getReal( opStack, index );
387       else
388         vals[13] = *curY;
389     }
390 
391     for ( j = 0; j < 2; j++ )
392       cf2_glyphpath_curveTo( glyphPath, vals[j * 6 + 2],
393                                         vals[j * 6 + 3],
394                                         vals[j * 6 + 4],
395                                         vals[j * 6 + 5],
396                                         vals[j * 6 + 6],
397                                         vals[j * 6 + 7] );
398 
399     cf2_stack_clear( opStack );
400 
401     *curX = vals[12];
402     *curY = vals[13];
403   }
404 
405 
406   /*
407    * `error' is a shared error code used by many objects in this
408    * routine.  Before the code continues from an error, it must check and
409    * record the error in `*error'.  The idea is that this shared
410    * error code will record the first error encountered.  If testing
411    * for an error anyway, the cost of `goto exit' is small, so we do it,
412    * even if continuing would be safe.  In this case, `lastError' is
413    * set, so the testing and storing can be done in one place, at `exit'.
414    *
415    * Continuing after an error is intended for objects which do their own
416    * testing of `*error', e.g., array stack functions.  This allows us to
417    * avoid an extra test after the call.
418    *
419    * Unimplemented opcodes are ignored.
420    *
421    */
422   FT_LOCAL_DEF( void )
cf2_interpT2CharString(CF2_Font font,CF2_Buffer buf,CF2_OutlineCallbacks callbacks,const FT_Vector * translation,FT_Bool doingSeac,CF2_Fixed curX,CF2_Fixed curY,CF2_Fixed * width)423   cf2_interpT2CharString( CF2_Font              font,
424                           CF2_Buffer            buf,
425                           CF2_OutlineCallbacks  callbacks,
426                           const FT_Vector*      translation,
427                           FT_Bool               doingSeac,
428                           CF2_Fixed             curX,
429                           CF2_Fixed             curY,
430                           CF2_Fixed*            width )
431   {
432     /* lastError is used for errors that are immediately tested */
433     FT_Error  lastError = FT_Err_Ok;
434 
435     /* pointer to parsed font object */
436     CFF_Decoder*  decoder = font->decoder;
437 
438     FT_Error*  error  = &font->error;
439     FT_Memory  memory = font->memory;
440 
441     CF2_Fixed  scaleY        = font->innerTransform.d;
442     CF2_Fixed  nominalWidthX = cf2_getNominalWidthX( decoder );
443 
444     /* save this for hinting seac accents */
445     CF2_Fixed  hintOriginY = curY;
446 
447     CF2_Stack  opStack = NULL;
448     FT_Byte    op1;                       /* first opcode byte */
449 
450     CF2_F16Dot16  storage[CF2_STORAGE_SIZE];    /* for `put' and `get' */
451 
452     /* instruction limit; 20,000,000 matches Avalon */
453     FT_UInt32  instructionLimit = 20000000UL;
454 
455     CF2_ArrStackRec  subrStack;
456 
457     FT_Bool     haveWidth;
458     CF2_Buffer  charstring = NULL;
459 
460     CF2_Int  charstringIndex = -1;       /* initialize to empty */
461 
462     /* TODO: placeholders for hint structures */
463 
464     /* objects used for hinting */
465     CF2_ArrStackRec  hStemHintArray;
466     CF2_ArrStackRec  vStemHintArray;
467 
468     CF2_HintMaskRec   hintMask;
469     CF2_GlyphPathRec  glyphPath;
470 
471 
472     /* initialize the remaining objects */
473     cf2_arrstack_init( &subrStack,
474                        memory,
475                        error,
476                        sizeof ( CF2_BufferRec ) );
477     cf2_arrstack_init( &hStemHintArray,
478                        memory,
479                        error,
480                        sizeof ( CF2_StemHintRec ) );
481     cf2_arrstack_init( &vStemHintArray,
482                        memory,
483                        error,
484                        sizeof ( CF2_StemHintRec ) );
485 
486     /* initialize CF2_StemHint arrays */
487     cf2_hintmask_init( &hintMask, error );
488 
489     /* initialize path map to manage drawing operations */
490 
491     /* Note: last 4 params are used to handle `MoveToPermissive', which */
492     /*       may need to call `hintMap.Build'                           */
493     /* TODO: MoveToPermissive is gone; are these still needed?          */
494     cf2_glyphpath_init( &glyphPath,
495                         font,
496                         callbacks,
497                         scaleY,
498                         /* hShift, */
499                         &hStemHintArray,
500                         &vStemHintArray,
501                         &hintMask,
502                         hintOriginY,
503                         &font->blues,
504                         translation );
505 
506     /*
507      * Initialize state for width parsing.  From the CFF Spec:
508      *
509      *   The first stack-clearing operator, which must be one of hstem,
510      *   hstemhm, vstem, vstemhm, cntrmask, hintmask, hmoveto, vmoveto,
511      *   rmoveto, or endchar, takes an additional argument - the width (as
512      *   described earlier), which may be expressed as zero or one numeric
513      *   argument.
514      *
515      * What we implement here uses the first validly specified width, but
516      * does not detect errors for specifying more than one width.
517      *
518      * If one of the above operators occurs without explicitly specifying
519      * a width, we assume the default width.
520      *
521      */
522     haveWidth = FALSE;
523     *width    = cf2_getDefaultWidthX( decoder );
524 
525     /*
526      * Note: at this point, all pointers to resources must be NULL
527      * and all local objects must be initialized.
528      * There must be no branches to exit: above this point.
529      *
530      */
531 
532     /* allocate an operand stack */
533     opStack = cf2_stack_init( memory, error );
534     if ( !opStack )
535     {
536       lastError = FT_THROW( Out_Of_Memory );
537       goto exit;
538     }
539 
540     /* initialize subroutine stack by placing top level charstring as */
541     /* first element (max depth plus one for the charstring)          */
542     /* Note: Caller owns and must finalize the first charstring.      */
543     /*       Our copy of it does not change that requirement.         */
544     cf2_arrstack_setCount( &subrStack, CF2_MAX_SUBR + 1 );
545 
546     charstring  = (CF2_Buffer)cf2_arrstack_getBuffer( &subrStack );
547     *charstring = *buf;    /* structure copy */
548 
549     charstringIndex = 0;       /* entry is valid now */
550 
551     /* catch errors so far */
552     if ( *error )
553       goto exit;
554 
555     /* main interpreter loop */
556     while ( 1 )
557     {
558       if ( cf2_buf_isEnd( charstring ) )
559       {
560         /* If we've reached the end of the charstring, simulate a */
561         /* cf2_cmdRETURN or cf2_cmdENDCHAR.                       */
562         if ( charstringIndex )
563           op1 = cf2_cmdRETURN;  /* end of buffer for subroutine */
564         else
565           op1 = cf2_cmdENDCHAR; /* end of buffer for top level charstring */
566       }
567       else
568         op1 = (FT_Byte)cf2_buf_readByte( charstring );
569 
570       /* check for errors once per loop */
571       if ( *error )
572         goto exit;
573 
574       instructionLimit--;
575       if ( instructionLimit == 0 )
576       {
577         lastError = FT_THROW( Invalid_Glyph_Format );
578         goto exit;
579       }
580 
581       switch( op1 )
582       {
583       case cf2_cmdRESERVED_0:
584       case cf2_cmdRESERVED_2:
585       case cf2_cmdRESERVED_9:
586       case cf2_cmdRESERVED_13:
587       case cf2_cmdRESERVED_15:
588       case cf2_cmdRESERVED_16:
589       case cf2_cmdRESERVED_17:
590         /* we may get here if we have a prior error */
591         FT_TRACE4(( " unknown op (%d)\n", op1 ));
592         break;
593 
594       case cf2_cmdHSTEMHM:
595       case cf2_cmdHSTEM:
596         FT_TRACE4(( op1 == cf2_cmdHSTEMHM ? " hstemhm\n" : " hstem\n" ));
597 
598         /* never add hints after the mask is computed */
599         if ( cf2_hintmask_isValid( &hintMask ) )
600         {
601           FT_TRACE4(( "cf2_interpT2CharString:"
602                       " invalid horizontal hint mask\n" ));
603           break;
604         }
605 
606         cf2_doStems( font,
607                      opStack,
608                      &hStemHintArray,
609                      width,
610                      &haveWidth,
611                      0 );
612 
613         if ( font->decoder->width_only )
614           goto exit;
615 
616         break;
617 
618       case cf2_cmdVSTEMHM:
619       case cf2_cmdVSTEM:
620         FT_TRACE4(( op1 == cf2_cmdVSTEMHM ? " vstemhm\n" : " vstem\n" ));
621 
622         /* never add hints after the mask is computed */
623         if ( cf2_hintmask_isValid( &hintMask ) )
624         {
625           FT_TRACE4(( "cf2_interpT2CharString:"
626                       " invalid vertical hint mask\n" ));
627           break;
628         }
629 
630         cf2_doStems( font,
631                      opStack,
632                      &vStemHintArray,
633                      width,
634                      &haveWidth,
635                      0 );
636 
637         if ( font->decoder->width_only )
638           goto exit;
639 
640         break;
641 
642       case cf2_cmdVMOVETO:
643         FT_TRACE4(( " vmoveto\n" ));
644 
645         if ( cf2_stack_count( opStack ) > 1 && !haveWidth )
646           *width = cf2_stack_getReal( opStack, 0 ) + nominalWidthX;
647 
648         /* width is defined or default after this */
649         haveWidth = TRUE;
650 
651         if ( font->decoder->width_only )
652           goto exit;
653 
654         curY += cf2_stack_popFixed( opStack );
655 
656         cf2_glyphpath_moveTo( &glyphPath, curX, curY );
657 
658         break;
659 
660       case cf2_cmdRLINETO:
661         {
662           CF2_UInt  index;
663           CF2_UInt  count = cf2_stack_count( opStack );
664 
665 
666           FT_TRACE4(( " rlineto\n" ));
667 
668           for ( index = 0; index < count; index += 2 )
669           {
670             curX += cf2_stack_getReal( opStack, index + 0 );
671             curY += cf2_stack_getReal( opStack, index + 1 );
672 
673             cf2_glyphpath_lineTo( &glyphPath, curX, curY );
674           }
675 
676           cf2_stack_clear( opStack );
677         }
678         continue; /* no need to clear stack again */
679 
680       case cf2_cmdHLINETO:
681       case cf2_cmdVLINETO:
682         {
683           CF2_UInt  index;
684           CF2_UInt  count = cf2_stack_count( opStack );
685 
686           FT_Bool  isX = op1 == cf2_cmdHLINETO;
687 
688 
689           FT_TRACE4(( isX ? " hlineto\n" : " vlineto\n" ));
690 
691           for ( index = 0; index < count; index++ )
692           {
693             CF2_Fixed  v = cf2_stack_getReal( opStack, index );
694 
695 
696             if ( isX )
697               curX += v;
698             else
699               curY += v;
700 
701             isX = !isX;
702 
703             cf2_glyphpath_lineTo( &glyphPath, curX, curY );
704           }
705 
706           cf2_stack_clear( opStack );
707         }
708         continue;
709 
710       case cf2_cmdRCURVELINE:
711       case cf2_cmdRRCURVETO:
712         {
713           CF2_UInt  count = cf2_stack_count( opStack );
714           CF2_UInt  index = 0;
715 
716 
717           FT_TRACE4(( op1 == cf2_cmdRCURVELINE ? " rcurveline\n"
718                                                : " rrcurveto\n" ));
719 
720           while ( index + 6 <= count )
721           {
722             CF2_Fixed  x1 = cf2_stack_getReal( opStack, index + 0 ) + curX;
723             CF2_Fixed  y1 = cf2_stack_getReal( opStack, index + 1 ) + curY;
724             CF2_Fixed  x2 = cf2_stack_getReal( opStack, index + 2 ) + x1;
725             CF2_Fixed  y2 = cf2_stack_getReal( opStack, index + 3 ) + y1;
726             CF2_Fixed  x3 = cf2_stack_getReal( opStack, index + 4 ) + x2;
727             CF2_Fixed  y3 = cf2_stack_getReal( opStack, index + 5 ) + y2;
728 
729 
730             cf2_glyphpath_curveTo( &glyphPath, x1, y1, x2, y2, x3, y3 );
731 
732             curX   = x3;
733             curY   = y3;
734             index += 6;
735           }
736 
737           if ( op1 == cf2_cmdRCURVELINE )
738           {
739             curX += cf2_stack_getReal( opStack, index + 0 );
740             curY += cf2_stack_getReal( opStack, index + 1 );
741 
742             cf2_glyphpath_lineTo( &glyphPath, curX, curY );
743           }
744 
745           cf2_stack_clear( opStack );
746         }
747         continue; /* no need to clear stack again */
748 
749       case cf2_cmdCALLGSUBR:
750       case cf2_cmdCALLSUBR:
751         {
752           CF2_Int  subrNum;
753 
754 
755           FT_TRACE4(( op1 == cf2_cmdCALLGSUBR ? " callgsubr"
756                                               : " callsubr" ));
757 
758           if ( charstringIndex > CF2_MAX_SUBR )
759           {
760             /* max subr plus one for charstring */
761             lastError = FT_THROW( Invalid_Glyph_Format );
762             goto exit;                      /* overflow of stack */
763           }
764 
765           /* push our current CFF charstring region on subrStack */
766           charstring = (CF2_Buffer)
767                          cf2_arrstack_getPointer(
768                            &subrStack,
769                            (size_t)charstringIndex + 1 );
770 
771           /* set up the new CFF region and pointer */
772           subrNum = cf2_stack_popInt( opStack );
773 
774           switch ( op1 )
775           {
776           case cf2_cmdCALLGSUBR:
777             FT_TRACE4(( " (idx %d, entering level %d)\n",
778                         subrNum + decoder->globals_bias,
779                         charstringIndex + 1 ));
780 
781             if ( cf2_initGlobalRegionBuffer( decoder,
782                                              subrNum,
783                                              charstring ) )
784             {
785               lastError = FT_THROW( Invalid_Glyph_Format );
786               goto exit;  /* subroutine lookup or stream error */
787             }
788             break;
789 
790           default:
791             /* cf2_cmdCALLSUBR */
792             FT_TRACE4(( " (idx %d, entering level %d)\n",
793                         subrNum + decoder->locals_bias,
794                         charstringIndex + 1 ));
795 
796             if ( cf2_initLocalRegionBuffer( decoder,
797                                             subrNum,
798                                             charstring ) )
799             {
800               lastError = FT_THROW( Invalid_Glyph_Format );
801               goto exit;  /* subroutine lookup or stream error */
802             }
803           }
804 
805           charstringIndex += 1;       /* entry is valid now */
806         }
807         continue; /* do not clear the stack */
808 
809       case cf2_cmdRETURN:
810         FT_TRACE4(( " return (leaving level %d)\n", charstringIndex ));
811 
812         if ( charstringIndex < 1 )
813         {
814           /* Note: cannot return from top charstring */
815           lastError = FT_THROW( Invalid_Glyph_Format );
816           goto exit;                      /* underflow of stack */
817         }
818 
819         /* restore position in previous charstring */
820         charstring = (CF2_Buffer)
821                        cf2_arrstack_getPointer(
822                          &subrStack,
823                          (CF2_UInt)--charstringIndex );
824         continue;     /* do not clear the stack */
825 
826       case cf2_cmdESC:
827         {
828           FT_Byte  op2 = (FT_Byte)cf2_buf_readByte( charstring );
829 
830 
831           switch ( op2 )
832           {
833           case cf2_escDOTSECTION:
834             /* something about `flip type of locking' -- ignore it */
835             FT_TRACE4(( " dotsection\n" ));
836 
837             break;
838 
839           case cf2_escAND:
840             {
841               CF2_F16Dot16  arg1;
842               CF2_F16Dot16  arg2;
843 
844 
845               FT_TRACE4(( " and\n" ));
846 
847               arg2 = cf2_stack_popFixed( opStack );
848               arg1 = cf2_stack_popFixed( opStack );
849 
850               cf2_stack_pushInt( opStack, arg1 && arg2 );
851             }
852             continue; /* do not clear the stack */
853 
854           case cf2_escOR:
855             {
856               CF2_F16Dot16  arg1;
857               CF2_F16Dot16  arg2;
858 
859 
860               FT_TRACE4(( " or\n" ));
861 
862               arg2 = cf2_stack_popFixed( opStack );
863               arg1 = cf2_stack_popFixed( opStack );
864 
865               cf2_stack_pushInt( opStack, arg1 || arg2 );
866             }
867             continue; /* do not clear the stack */
868 
869           case cf2_escNOT:
870             {
871               CF2_F16Dot16  arg;
872 
873 
874               FT_TRACE4(( " not\n" ));
875 
876               arg = cf2_stack_popFixed( opStack );
877 
878               cf2_stack_pushInt( opStack, !arg );
879             }
880             continue; /* do not clear the stack */
881 
882           case cf2_escABS:
883             {
884               CF2_F16Dot16  arg;
885 
886 
887               FT_TRACE4(( " abs\n" ));
888 
889               arg = cf2_stack_popFixed( opStack );
890 
891               cf2_stack_pushFixed( opStack, FT_ABS( arg ) );
892             }
893             continue; /* do not clear the stack */
894 
895           case cf2_escADD:
896             {
897               CF2_F16Dot16  summand1;
898               CF2_F16Dot16  summand2;
899 
900 
901               FT_TRACE4(( " add\n" ));
902 
903               summand2 = cf2_stack_popFixed( opStack );
904               summand1 = cf2_stack_popFixed( opStack );
905 
906               cf2_stack_pushFixed( opStack, summand1 + summand2 );
907             }
908             continue; /* do not clear the stack */
909 
910           case cf2_escSUB:
911             {
912               CF2_F16Dot16  minuend;
913               CF2_F16Dot16  subtrahend;
914 
915 
916               FT_TRACE4(( " sub\n" ));
917 
918               subtrahend = cf2_stack_popFixed( opStack );
919               minuend    = cf2_stack_popFixed( opStack );
920 
921               cf2_stack_pushFixed( opStack, minuend - subtrahend );
922             }
923             continue; /* do not clear the stack */
924 
925           case cf2_escDIV:
926             {
927               CF2_F16Dot16  dividend;
928               CF2_F16Dot16  divisor;
929 
930 
931               FT_TRACE4(( " div\n" ));
932 
933               divisor  = cf2_stack_popFixed( opStack );
934               dividend = cf2_stack_popFixed( opStack );
935 
936               cf2_stack_pushFixed( opStack, FT_DivFix( dividend, divisor ) );
937             }
938             continue; /* do not clear the stack */
939 
940           case cf2_escNEG:
941             {
942               CF2_F16Dot16  arg;
943 
944 
945               FT_TRACE4(( " neg\n" ));
946 
947               arg = cf2_stack_popFixed( opStack );
948 
949               cf2_stack_pushFixed( opStack, -arg );
950             }
951             continue; /* do not clear the stack */
952 
953           case cf2_escEQ:
954             {
955               CF2_F16Dot16  arg1;
956               CF2_F16Dot16  arg2;
957 
958 
959               FT_TRACE4(( " eq\n" ));
960 
961               arg2 = cf2_stack_popFixed( opStack );
962               arg1 = cf2_stack_popFixed( opStack );
963 
964               cf2_stack_pushInt( opStack, arg1 == arg2 );
965             }
966             continue; /* do not clear the stack */
967 
968           case cf2_escDROP:
969             FT_TRACE4(( " drop\n" ));
970 
971             (void)cf2_stack_popFixed( opStack );
972             continue; /* do not clear the stack */
973 
974           case cf2_escPUT:
975             {
976               CF2_F16Dot16  val;
977               CF2_Int       idx;
978 
979 
980               FT_TRACE4(( " put\n" ));
981 
982               idx = cf2_stack_popInt( opStack );
983               val = cf2_stack_popFixed( opStack );
984 
985               if ( idx >= 0 && idx < CF2_STORAGE_SIZE )
986                 storage[idx] = val;
987             }
988             continue; /* do not clear the stack */
989 
990           case cf2_escGET:
991             {
992               CF2_Int  idx;
993 
994 
995               FT_TRACE4(( " get\n" ));
996 
997               idx = cf2_stack_popInt( opStack );
998 
999               if ( idx >= 0 && idx < CF2_STORAGE_SIZE )
1000                 cf2_stack_pushFixed( opStack, storage[idx] );
1001             }
1002             continue; /* do not clear the stack */
1003 
1004           case cf2_escIFELSE:
1005             {
1006               CF2_F16Dot16  arg1;
1007               CF2_F16Dot16  arg2;
1008               CF2_F16Dot16  cond1;
1009               CF2_F16Dot16  cond2;
1010 
1011 
1012               FT_TRACE4(( " ifelse\n" ));
1013 
1014               cond2 = cf2_stack_popFixed( opStack );
1015               cond1 = cf2_stack_popFixed( opStack );
1016               arg2  = cf2_stack_popFixed( opStack );
1017               arg1  = cf2_stack_popFixed( opStack );
1018 
1019               cf2_stack_pushFixed( opStack, cond1 <= cond2 ? arg1 : arg2 );
1020             }
1021             continue; /* do not clear the stack */
1022 
1023           case cf2_escRANDOM: /* in spec */
1024             FT_TRACE4(( " random\n" ));
1025 
1026             CF2_FIXME;
1027             break;
1028 
1029           case cf2_escMUL:
1030             {
1031               CF2_F16Dot16  factor1;
1032               CF2_F16Dot16  factor2;
1033 
1034 
1035               FT_TRACE4(( " mul\n" ));
1036 
1037               factor2 = cf2_stack_popFixed( opStack );
1038               factor1 = cf2_stack_popFixed( opStack );
1039 
1040               cf2_stack_pushFixed( opStack, FT_MulFix( factor1, factor2 ) );
1041             }
1042             continue; /* do not clear the stack */
1043 
1044           case cf2_escSQRT:
1045             {
1046               CF2_F16Dot16  arg;
1047 
1048 
1049               FT_TRACE4(( " sqrt\n" ));
1050 
1051               arg = cf2_stack_popFixed( opStack );
1052               if ( arg > 0 )
1053               {
1054                 FT_Fixed  root = arg;
1055                 FT_Fixed  new_root;
1056 
1057 
1058                 /* Babylonian method */
1059                 for (;;)
1060                 {
1061                   new_root = ( root + FT_DivFix( arg, root ) + 1 ) >> 1;
1062                   if ( new_root == root )
1063                     break;
1064                   root = new_root;
1065                 }
1066                 arg = new_root;
1067               }
1068               else
1069                 arg = 0;
1070 
1071               cf2_stack_pushFixed( opStack, arg );
1072             }
1073             continue; /* do not clear the stack */
1074 
1075           case cf2_escDUP:
1076             {
1077               CF2_F16Dot16  arg;
1078 
1079 
1080               FT_TRACE4(( " dup\n" ));
1081 
1082               arg = cf2_stack_popFixed( opStack );
1083 
1084               cf2_stack_pushFixed( opStack, arg );
1085               cf2_stack_pushFixed( opStack, arg );
1086             }
1087             continue; /* do not clear the stack */
1088 
1089           case cf2_escEXCH:
1090             {
1091               CF2_F16Dot16  arg1;
1092               CF2_F16Dot16  arg2;
1093 
1094 
1095               FT_TRACE4(( " exch\n" ));
1096 
1097               arg2 = cf2_stack_popFixed( opStack );
1098               arg1 = cf2_stack_popFixed( opStack );
1099 
1100               cf2_stack_pushFixed( opStack, arg2 );
1101               cf2_stack_pushFixed( opStack, arg1 );
1102             }
1103             continue; /* do not clear the stack */
1104 
1105           case cf2_escINDEX:
1106             {
1107               CF2_Int   idx;
1108               CF2_UInt  size;
1109 
1110 
1111               FT_TRACE4(( " index\n" ));
1112 
1113               idx  = cf2_stack_popInt( opStack );
1114               size = cf2_stack_count( opStack );
1115 
1116               if ( size > 0 )
1117               {
1118                 /* for `cf2_stack_getReal', index 0 is bottom of stack */
1119                 CF2_UInt  gr_idx;
1120 
1121 
1122                 if ( idx < 0 )
1123                   gr_idx = size - 1;
1124                 else if ( (CF2_UInt)idx >= size )
1125                   gr_idx = 0;
1126                 else
1127                   gr_idx = size - 1 - (CF2_UInt)idx;
1128 
1129                 cf2_stack_pushFixed( opStack,
1130                                      cf2_stack_getReal( opStack, gr_idx ) );
1131               }
1132             }
1133             continue; /* do not clear the stack */
1134 
1135           case cf2_escROLL:
1136             {
1137               CF2_Int  idx;
1138               CF2_Int  count;
1139 
1140 
1141               FT_TRACE4(( " roll\n" ));
1142 
1143               idx   = cf2_stack_popInt( opStack );
1144               count = cf2_stack_popInt( opStack );
1145 
1146               cf2_stack_roll( opStack, count, idx );
1147             }
1148             continue; /* do not clear the stack */
1149 
1150           case cf2_escHFLEX:
1151             {
1152               static const FT_Bool  readFromStack[12] =
1153               {
1154                 TRUE /* dx1 */, FALSE /* dy1 */,
1155                 TRUE /* dx2 */, TRUE  /* dy2 */,
1156                 TRUE /* dx3 */, FALSE /* dy3 */,
1157                 TRUE /* dx4 */, FALSE /* dy4 */,
1158                 TRUE /* dx5 */, FALSE /* dy5 */,
1159                 TRUE /* dx6 */, FALSE /* dy6 */
1160               };
1161 
1162 
1163               FT_TRACE4(( " hflex\n" ));
1164 
1165               cf2_doFlex( opStack,
1166                           &curX,
1167                           &curY,
1168                           &glyphPath,
1169                           readFromStack,
1170                           FALSE /* doConditionalLastRead */ );
1171             }
1172             continue;
1173 
1174           case cf2_escFLEX:
1175             {
1176               static const FT_Bool  readFromStack[12] =
1177               {
1178                 TRUE /* dx1 */, TRUE /* dy1 */,
1179                 TRUE /* dx2 */, TRUE /* dy2 */,
1180                 TRUE /* dx3 */, TRUE /* dy3 */,
1181                 TRUE /* dx4 */, TRUE /* dy4 */,
1182                 TRUE /* dx5 */, TRUE /* dy5 */,
1183                 TRUE /* dx6 */, TRUE /* dy6 */
1184               };
1185 
1186 
1187               FT_TRACE4(( " flex\n" ));
1188 
1189               cf2_doFlex( opStack,
1190                           &curX,
1191                           &curY,
1192                           &glyphPath,
1193                           readFromStack,
1194                           FALSE /* doConditionalLastRead */ );
1195             }
1196             break;      /* TODO: why is this not a continue? */
1197 
1198           case cf2_escHFLEX1:
1199             {
1200               static const FT_Bool  readFromStack[12] =
1201               {
1202                 TRUE /* dx1 */, TRUE  /* dy1 */,
1203                 TRUE /* dx2 */, TRUE  /* dy2 */,
1204                 TRUE /* dx3 */, FALSE /* dy3 */,
1205                 TRUE /* dx4 */, FALSE /* dy4 */,
1206                 TRUE /* dx5 */, TRUE  /* dy5 */,
1207                 TRUE /* dx6 */, FALSE /* dy6 */
1208               };
1209 
1210 
1211               FT_TRACE4(( " hflex1\n" ));
1212 
1213               cf2_doFlex( opStack,
1214                           &curX,
1215                           &curY,
1216                           &glyphPath,
1217                           readFromStack,
1218                           FALSE /* doConditionalLastRead */ );
1219             }
1220             continue;
1221 
1222           case cf2_escFLEX1:
1223             {
1224               static const FT_Bool  readFromStack[12] =
1225               {
1226                 TRUE  /* dx1 */, TRUE  /* dy1 */,
1227                 TRUE  /* dx2 */, TRUE  /* dy2 */,
1228                 TRUE  /* dx3 */, TRUE  /* dy3 */,
1229                 TRUE  /* dx4 */, TRUE  /* dy4 */,
1230                 TRUE  /* dx5 */, TRUE  /* dy5 */,
1231                 FALSE /* dx6 */, FALSE /* dy6 */
1232               };
1233 
1234 
1235               FT_TRACE4(( " flex1\n" ));
1236 
1237               cf2_doFlex( opStack,
1238                           &curX,
1239                           &curY,
1240                           &glyphPath,
1241                           readFromStack,
1242                           TRUE /* doConditionalLastRead */ );
1243             }
1244             continue;
1245 
1246           case cf2_escRESERVED_1:
1247           case cf2_escRESERVED_2:
1248           case cf2_escRESERVED_6:
1249           case cf2_escRESERVED_7:
1250           case cf2_escRESERVED_8:
1251           case cf2_escRESERVED_13:
1252           case cf2_escRESERVED_16:
1253           case cf2_escRESERVED_17:
1254           case cf2_escRESERVED_19:
1255           case cf2_escRESERVED_25:
1256           case cf2_escRESERVED_31:
1257           case cf2_escRESERVED_32:
1258           case cf2_escRESERVED_33:
1259           default:
1260             FT_TRACE4(( " unknown op (12, %d)\n", op2 ));
1261 
1262           }; /* end of switch statement checking `op2' */
1263 
1264         } /* case cf2_cmdESC */
1265         break;
1266 
1267       case cf2_cmdENDCHAR:
1268         FT_TRACE4(( " endchar\n" ));
1269 
1270         if ( cf2_stack_count( opStack ) == 1 ||
1271              cf2_stack_count( opStack ) == 5 )
1272         {
1273           if ( !haveWidth )
1274             *width = cf2_stack_getReal( opStack, 0 ) + nominalWidthX;
1275         }
1276 
1277         /* width is defined or default after this */
1278         haveWidth = TRUE;
1279 
1280         if ( font->decoder->width_only )
1281           goto exit;
1282 
1283         /* close path if still open */
1284         cf2_glyphpath_closeOpenPath( &glyphPath );
1285 
1286         if ( cf2_stack_count( opStack ) > 1 )
1287         {
1288           /* must be either 4 or 5 --                       */
1289           /* this is a (deprecated) implied `seac' operator */
1290 
1291           CF2_Int        achar;
1292           CF2_Int        bchar;
1293           CF2_BufferRec  component;
1294           CF2_Fixed      dummyWidth;   /* ignore component width */
1295           FT_Error       error2;
1296 
1297 
1298           if ( doingSeac )
1299           {
1300             lastError = FT_THROW( Invalid_Glyph_Format );
1301             goto exit;      /* nested seac */
1302           }
1303 
1304           achar = cf2_stack_popInt( opStack );
1305           bchar = cf2_stack_popInt( opStack );
1306 
1307           curY = cf2_stack_popFixed( opStack );
1308           curX = cf2_stack_popFixed( opStack );
1309 
1310           error2 = cf2_getSeacComponent( decoder, achar, &component );
1311           if ( error2 )
1312           {
1313             lastError = error2;      /* pass FreeType error through */
1314             goto exit;
1315           }
1316           cf2_interpT2CharString( font,
1317                                   &component,
1318                                   callbacks,
1319                                   translation,
1320                                   TRUE,
1321                                   curX,
1322                                   curY,
1323                                   &dummyWidth );
1324           cf2_freeSeacComponent( decoder, &component );
1325 
1326           error2 = cf2_getSeacComponent( decoder, bchar, &component );
1327           if ( error2 )
1328           {
1329             lastError = error2;      /* pass FreeType error through */
1330             goto exit;
1331           }
1332           cf2_interpT2CharString( font,
1333                                   &component,
1334                                   callbacks,
1335                                   translation,
1336                                   TRUE,
1337                                   0,
1338                                   0,
1339                                   &dummyWidth );
1340           cf2_freeSeacComponent( decoder, &component );
1341         }
1342         goto exit;
1343 
1344       case cf2_cmdCNTRMASK:
1345       case cf2_cmdHINTMASK:
1346         /* the final \n in the tracing message gets added in      */
1347         /* `cf2_hintmask_read' (which also traces the mask bytes) */
1348         FT_TRACE4(( op1 == cf2_cmdCNTRMASK ? " cntrmask" : " hintmask" ));
1349 
1350         /* never add hints after the mask is computed */
1351         if ( cf2_stack_count( opStack ) > 1    &&
1352              cf2_hintmask_isValid( &hintMask ) )
1353         {
1354           FT_TRACE4(( "cf2_interpT2CharString: invalid hint mask\n" ));
1355           break;
1356         }
1357 
1358         /* if there are arguments on the stack, there this is an */
1359         /* implied cf2_cmdVSTEMHM                                */
1360         cf2_doStems( font,
1361                      opStack,
1362                      &vStemHintArray,
1363                      width,
1364                      &haveWidth,
1365                      0 );
1366 
1367         if ( font->decoder->width_only )
1368           goto exit;
1369 
1370         if ( op1 == cf2_cmdHINTMASK )
1371         {
1372           /* consume the hint mask bytes which follow the operator */
1373           cf2_hintmask_read( &hintMask,
1374                              charstring,
1375                              cf2_arrstack_size( &hStemHintArray ) +
1376                                cf2_arrstack_size( &vStemHintArray ) );
1377         }
1378         else
1379         {
1380           /*
1381            * Consume the counter mask bytes which follow the operator:
1382            * Build a temporary hint map, just to place and lock those
1383            * stems participating in the counter mask.  These are most
1384            * likely the dominant hstems, and are grouped together in a
1385            * few counter groups, not necessarily in correspondence
1386            * with the hint groups.  This reduces the chances of
1387            * conflicts between hstems that are initially placed in
1388            * separate hint groups and then brought together.  The
1389            * positions are copied back to `hStemHintArray', so we can
1390            * discard `counterMask' and `counterHintMap'.
1391            *
1392            */
1393           CF2_HintMapRec   counterHintMap;
1394           CF2_HintMaskRec  counterMask;
1395 
1396 
1397           cf2_hintmap_init( &counterHintMap,
1398                             font,
1399                             &glyphPath.initialHintMap,
1400                             &glyphPath.hintMoves,
1401                             scaleY );
1402           cf2_hintmask_init( &counterMask, error );
1403 
1404           cf2_hintmask_read( &counterMask,
1405                              charstring,
1406                              cf2_arrstack_size( &hStemHintArray ) +
1407                                cf2_arrstack_size( &vStemHintArray ) );
1408           cf2_hintmap_build( &counterHintMap,
1409                              &hStemHintArray,
1410                              &vStemHintArray,
1411                              &counterMask,
1412                              0,
1413                              FALSE );
1414         }
1415         break;
1416 
1417       case cf2_cmdRMOVETO:
1418         FT_TRACE4(( " rmoveto\n" ));
1419 
1420         if ( cf2_stack_count( opStack ) > 2 && !haveWidth )
1421           *width = cf2_stack_getReal( opStack, 0 ) + nominalWidthX;
1422 
1423         /* width is defined or default after this */
1424         haveWidth = TRUE;
1425 
1426         if ( font->decoder->width_only )
1427           goto exit;
1428 
1429         curY += cf2_stack_popFixed( opStack );
1430         curX += cf2_stack_popFixed( opStack );
1431 
1432         cf2_glyphpath_moveTo( &glyphPath, curX, curY );
1433 
1434         break;
1435 
1436       case cf2_cmdHMOVETO:
1437         FT_TRACE4(( " hmoveto\n" ));
1438 
1439         if ( cf2_stack_count( opStack ) > 1 && !haveWidth )
1440           *width = cf2_stack_getReal( opStack, 0 ) + nominalWidthX;
1441 
1442         /* width is defined or default after this */
1443         haveWidth = TRUE;
1444 
1445         if ( font->decoder->width_only )
1446           goto exit;
1447 
1448         curX += cf2_stack_popFixed( opStack );
1449 
1450         cf2_glyphpath_moveTo( &glyphPath, curX, curY );
1451 
1452         break;
1453 
1454       case cf2_cmdRLINECURVE:
1455         {
1456           CF2_UInt  count = cf2_stack_count( opStack );
1457           CF2_UInt  index = 0;
1458 
1459 
1460           FT_TRACE4(( " rlinecurve\n" ));
1461 
1462           while ( index + 6 < count )
1463           {
1464             curX += cf2_stack_getReal( opStack, index + 0 );
1465             curY += cf2_stack_getReal( opStack, index + 1 );
1466 
1467             cf2_glyphpath_lineTo( &glyphPath, curX, curY );
1468             index += 2;
1469           }
1470 
1471           while ( index < count )
1472           {
1473             CF2_Fixed  x1 = cf2_stack_getReal( opStack, index + 0 ) + curX;
1474             CF2_Fixed  y1 = cf2_stack_getReal( opStack, index + 1 ) + curY;
1475             CF2_Fixed  x2 = cf2_stack_getReal( opStack, index + 2 ) + x1;
1476             CF2_Fixed  y2 = cf2_stack_getReal( opStack, index + 3 ) + y1;
1477             CF2_Fixed  x3 = cf2_stack_getReal( opStack, index + 4 ) + x2;
1478             CF2_Fixed  y3 = cf2_stack_getReal( opStack, index + 5 ) + y2;
1479 
1480 
1481             cf2_glyphpath_curveTo( &glyphPath, x1, y1, x2, y2, x3, y3 );
1482 
1483             curX   = x3;
1484             curY   = y3;
1485             index += 6;
1486           }
1487 
1488           cf2_stack_clear( opStack );
1489         }
1490         continue; /* no need to clear stack again */
1491 
1492       case cf2_cmdVVCURVETO:
1493         {
1494           CF2_UInt  count, count1 = cf2_stack_count( opStack );
1495           CF2_UInt  index = 0;
1496 
1497 
1498           /* if `cf2_stack_count' isn't of the form 4n or 4n+1, */
1499           /* we enforce it by clearing the second bit           */
1500           /* (and sorting the stack indexing to suit)           */
1501           count  = count1 & ~2U;
1502           index += count1 - count;
1503 
1504           FT_TRACE4(( " vvcurveto\n" ));
1505 
1506           while ( index < count )
1507           {
1508             CF2_Fixed  x1, y1, x2, y2, x3, y3;
1509 
1510 
1511             if ( ( count - index ) & 1 )
1512             {
1513               x1 = cf2_stack_getReal( opStack, index ) + curX;
1514 
1515               ++index;
1516             }
1517             else
1518               x1 = curX;
1519 
1520             y1 = cf2_stack_getReal( opStack, index + 0 ) + curY;
1521             x2 = cf2_stack_getReal( opStack, index + 1 ) + x1;
1522             y2 = cf2_stack_getReal( opStack, index + 2 ) + y1;
1523             x3 = x2;
1524             y3 = cf2_stack_getReal( opStack, index + 3 ) + y2;
1525 
1526             cf2_glyphpath_curveTo( &glyphPath, x1, y1, x2, y2, x3, y3 );
1527 
1528             curX   = x3;
1529             curY   = y3;
1530             index += 4;
1531           }
1532 
1533           cf2_stack_clear( opStack );
1534         }
1535         continue; /* no need to clear stack again */
1536 
1537       case cf2_cmdHHCURVETO:
1538         {
1539           CF2_UInt  count, count1 = cf2_stack_count( opStack );
1540           CF2_UInt  index = 0;
1541 
1542 
1543           /* if `cf2_stack_count' isn't of the form 4n or 4n+1, */
1544           /* we enforce it by clearing the second bit           */
1545           /* (and sorting the stack indexing to suit)           */
1546           count  = count1 & ~2U;
1547           index += count1 - count;
1548 
1549           FT_TRACE4(( " hhcurveto\n" ));
1550 
1551           while ( index < count )
1552           {
1553             CF2_Fixed  x1, y1, x2, y2, x3, y3;
1554 
1555 
1556             if ( ( count - index ) & 1 )
1557             {
1558               y1 = cf2_stack_getReal( opStack, index ) + curY;
1559 
1560               ++index;
1561             }
1562             else
1563               y1 = curY;
1564 
1565             x1 = cf2_stack_getReal( opStack, index + 0 ) + curX;
1566             x2 = cf2_stack_getReal( opStack, index + 1 ) + x1;
1567             y2 = cf2_stack_getReal( opStack, index + 2 ) + y1;
1568             x3 = cf2_stack_getReal( opStack, index + 3 ) + x2;
1569             y3 = y2;
1570 
1571             cf2_glyphpath_curveTo( &glyphPath, x1, y1, x2, y2, x3, y3 );
1572 
1573             curX   = x3;
1574             curY   = y3;
1575             index += 4;
1576           }
1577 
1578           cf2_stack_clear( opStack );
1579         }
1580         continue; /* no need to clear stack again */
1581 
1582       case cf2_cmdVHCURVETO:
1583       case cf2_cmdHVCURVETO:
1584         {
1585           CF2_UInt  count, count1 = cf2_stack_count( opStack );
1586           CF2_UInt  index = 0;
1587 
1588           FT_Bool  alternate = op1 == cf2_cmdHVCURVETO;
1589 
1590 
1591           /* if `cf2_stack_count' isn't of the form 8n, 8n+1, */
1592           /* 8n+4, or 8n+5, we enforce it by clearing the     */
1593           /* second bit                                       */
1594           /* (and sorting the stack indexing to suit)         */
1595           count  = count1 & ~2U;
1596           index += count1 - count;
1597 
1598           FT_TRACE4(( alternate ? " hvcurveto\n" : " vhcurveto\n" ));
1599 
1600           while ( index < count )
1601           {
1602             CF2_Fixed x1, x2, x3, y1, y2, y3;
1603 
1604 
1605             if ( alternate )
1606             {
1607               x1 = cf2_stack_getReal( opStack, index + 0 ) + curX;
1608               y1 = curY;
1609               x2 = cf2_stack_getReal( opStack, index + 1 ) + x1;
1610               y2 = cf2_stack_getReal( opStack, index + 2 ) + y1;
1611               y3 = cf2_stack_getReal( opStack, index + 3 ) + y2;
1612 
1613               if ( count - index == 5 )
1614               {
1615                 x3 = cf2_stack_getReal( opStack, index + 4 ) + x2;
1616 
1617                 ++index;
1618               }
1619               else
1620                 x3 = x2;
1621 
1622               alternate = FALSE;
1623             }
1624             else
1625             {
1626               x1 = curX;
1627               y1 = cf2_stack_getReal( opStack, index + 0 ) + curY;
1628               x2 = cf2_stack_getReal( opStack, index + 1 ) + x1;
1629               y2 = cf2_stack_getReal( opStack, index + 2 ) + y1;
1630               x3 = cf2_stack_getReal( opStack, index + 3 ) + x2;
1631 
1632               if ( count - index == 5 )
1633               {
1634                 y3 = cf2_stack_getReal( opStack, index + 4 ) + y2;
1635 
1636                 ++index;
1637               }
1638               else
1639                 y3 = y2;
1640 
1641               alternate = TRUE;
1642             }
1643 
1644             cf2_glyphpath_curveTo( &glyphPath, x1, y1, x2, y2, x3, y3 );
1645 
1646             curX   = x3;
1647             curY   = y3;
1648             index += 4;
1649           }
1650 
1651           cf2_stack_clear( opStack );
1652         }
1653         continue;     /* no need to clear stack again */
1654 
1655       case cf2_cmdEXTENDEDNMBR:
1656         {
1657           CF2_Int  v;
1658 
1659           CF2_Int  byte1 = cf2_buf_readByte( charstring );
1660           CF2_Int  byte2 = cf2_buf_readByte( charstring );
1661 
1662 
1663           v = (FT_Short)( ( byte1 << 8 ) |
1664                             byte2        );
1665 
1666           FT_TRACE4(( " %d", v ));
1667 
1668           cf2_stack_pushInt( opStack, v );
1669         }
1670         continue;
1671 
1672       default:
1673         /* numbers */
1674         {
1675           if ( /* op1 >= 32 && */ op1 <= 246 )
1676           {
1677             CF2_Int  v;
1678 
1679 
1680             v = op1 - 139;
1681 
1682             FT_TRACE4(( " %d", v ));
1683 
1684             /* -107 .. 107 */
1685             cf2_stack_pushInt( opStack, v );
1686           }
1687 
1688           else if ( /* op1 >= 247 && */ op1 <= 250 )
1689           {
1690             CF2_Int  v;
1691 
1692 
1693             v  = op1;
1694             v -= 247;
1695             v *= 256;
1696             v += cf2_buf_readByte( charstring );
1697             v += 108;
1698 
1699             FT_TRACE4(( " %d", v ));
1700 
1701             /* 108 .. 1131 */
1702             cf2_stack_pushInt( opStack, v );
1703           }
1704 
1705           else if ( /* op1 >= 251 && */ op1 <= 254 )
1706           {
1707             CF2_Int  v;
1708 
1709 
1710             v  = op1;
1711             v -= 251;
1712             v *= 256;
1713             v += cf2_buf_readByte( charstring );
1714             v  = -v - 108;
1715 
1716             FT_TRACE4(( " %d", v ));
1717 
1718             /* -1131 .. -108 */
1719             cf2_stack_pushInt( opStack, v );
1720           }
1721 
1722           else /* op1 == 255 */
1723           {
1724             CF2_Fixed  v;
1725 
1726             FT_UInt32  byte1 = (FT_UInt32)cf2_buf_readByte( charstring );
1727             FT_UInt32  byte2 = (FT_UInt32)cf2_buf_readByte( charstring );
1728             FT_UInt32  byte3 = (FT_UInt32)cf2_buf_readByte( charstring );
1729             FT_UInt32  byte4 = (FT_UInt32)cf2_buf_readByte( charstring );
1730 
1731 
1732             v = (CF2_Fixed)( ( byte1 << 24 ) |
1733                              ( byte2 << 16 ) |
1734                              ( byte3 <<  8 ) |
1735                                byte4         );
1736 
1737             FT_TRACE4(( " %.2f", v / 65536.0 ));
1738 
1739             cf2_stack_pushFixed( opStack, v );
1740           }
1741         }
1742         continue;   /* don't clear stack */
1743 
1744       } /* end of switch statement checking `op1' */
1745 
1746       cf2_stack_clear( opStack );
1747 
1748     } /* end of main interpreter loop */
1749 
1750     /* we get here if the charstring ends without cf2_cmdENDCHAR */
1751     FT_TRACE4(( "cf2_interpT2CharString:"
1752                 "  charstring ends without ENDCHAR\n" ));
1753 
1754   exit:
1755     /* check whether last error seen is also the first one */
1756     cf2_setError( error, lastError );
1757 
1758     /* free resources from objects we've used */
1759     cf2_glyphpath_finalize( &glyphPath );
1760     cf2_arrstack_finalize( &vStemHintArray );
1761     cf2_arrstack_finalize( &hStemHintArray );
1762     cf2_arrstack_finalize( &subrStack );
1763     cf2_stack_free( opStack );
1764 
1765     FT_TRACE4(( "\n" ));
1766 
1767     return;
1768   }
1769 
1770 
1771 /* END */
1772