1 /***************************************************************************/
5 /* Adobe's CFF Interpreter (body). */
7 /* Copyright 2007-2014 Adobe Systems Incorporated. */
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. */
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. */
36 /***************************************************************************/
40 #include FT_INTERNAL_DEBUG_H
51 /*************************************************************************/
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. */
58 #define FT_COMPONENT trace_cf2interp
61 /* some operators are not implemented yet */
62 #define CF2_FIXME FT_TRACE4(( "cf2_interpT2CharString:" \
63 " operator not implemented yet\n" ))
68 cf2_hintmask_init( CF2_HintMask hintmask
,
73 hintmask
->error
= error
;
77 FT_LOCAL_DEF( FT_Bool
)
78 cf2_hintmask_isValid( const CF2_HintMask hintmask
)
80 return hintmask
->isValid
;
84 FT_LOCAL_DEF( FT_Bool
)
85 cf2_hintmask_isNew( const CF2_HintMask hintmask
)
87 return hintmask
->isNew
;
92 cf2_hintmask_setNew( CF2_HintMask hintmask
,
95 hintmask
->isNew
= val
;
99 /* clients call `getMaskPtr' in order to iterate */
100 /* through hint mask */
102 FT_LOCAL_DEF( FT_Byte
* )
103 cf2_hintmask_getMaskPtr( CF2_HintMask hintmask
)
105 return hintmask
->mask
;
110 cf2_hintmask_setCounts( CF2_HintMask hintmask
,
113 if ( bitCount
> CF2_MAX_HINTS
)
115 /* total of h and v stems must be <= 96 */
116 CF2_SET_ERROR( hintmask
->error
, Invalid_Glyph_Format
);
120 hintmask
->bitCount
= bitCount
;
121 hintmask
->byteCount
= ( hintmask
->bitCount
+ 7 ) / 8;
123 hintmask
->isValid
= TRUE
;
124 hintmask
->isNew
= TRUE
;
130 /* consume the hintmask bytes from the charstring, advancing the src */
133 cf2_hintmask_read( CF2_HintMask hintmask
,
134 CF2_Buffer charstring
,
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;
147 /* initialize counts and isValid */
148 if ( cf2_hintmask_setCounts( hintmask
, bitCount
) == 0 )
151 FT_ASSERT( hintmask
->byteCount
> 0 );
153 FT_TRACE4(( " (maskbytes:" ));
155 /* set mask and advance interpreter's charstring pointer */
156 for ( i
= 0; i
< hintmask
->byteCount
; i
++ )
158 hintmask
->mask
[i
] = (FT_Byte
)cf2_buf_readByte( charstring
);
159 FT_TRACE4(( " 0x%02X", hintmask
->mask
[i
] ));
162 FT_TRACE4(( ")\n" ));
164 /* assert any unused bits in last byte are zero unless there's a prior */
166 /* bitCount -> mask, 0 -> 0, 1 -> 7f, 2 -> 3f, ... 6 -> 3, 7 -> 1 */
168 FT_ASSERT( ( hintmask
->mask
[hintmask
->byteCount
- 1] & mask
) == 0 ||
175 cf2_hintmask_setAll( CF2_HintMask hintmask
,
179 CF2_UInt mask
= ( 1 << ( -(CF2_Int
)bitCount
& 7 ) ) - 1;
182 /* initialize counts and isValid */
183 if ( cf2_hintmask_setCounts( hintmask
, bitCount
) == 0 )
186 FT_ASSERT( hintmask
->byteCount
> 0 );
187 FT_ASSERT( hintmask
->byteCount
<=
188 sizeof ( hintmask
->mask
) / sizeof ( hintmask
->mask
[0] ) );
190 /* set mask to all ones */
191 for ( i
= 0; i
< hintmask
->byteCount
; i
++ )
192 hintmask
->mask
[i
] = 0xFF;
194 /* clear unused bits */
195 /* bitCount -> mask, 0 -> 0, 1 -> 7f, 2 -> 3f, ... 6 -> 3, 7 -> 1 */
196 hintmask
->mask
[hintmask
->byteCount
- 1] &= ~mask
;
200 /* Type2 charstring opcodes */
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 */
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 */
239 cf2_escDOTSECTION
, /* 0 */
240 cf2_escRESERVED_1
, /* 1 */
241 cf2_escRESERVED_2
, /* 2 */
245 cf2_escRESERVED_6
, /* 6 */
246 cf2_escRESERVED_7
, /* 7 */
247 cf2_escRESERVED_8
, /* 8 */
249 cf2_escADD
, /* 10 like otherADD */
250 cf2_escSUB
, /* 11 like otherSUB */
252 cf2_escRESERVED_13
, /* 13 */
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 */
280 /* `stemHintArray' does not change once we start drawing the outline. */
282 cf2_doStems( const CF2_Font font
,
284 CF2_ArrStack stemHintArray
,
287 CF2_Fixed hintOffset
)
290 CF2_UInt count
= cf2_stack_count( opStack
);
291 FT_Bool hasWidthArg
= (FT_Bool
)( count
& 1 );
293 /* variable accumulates delta values from operand stack */
294 CF2_Fixed position
= hintOffset
;
297 if ( hasWidthArg
&& !*haveWidth
)
298 *width
= cf2_stack_getReal( opStack
, 0 ) +
299 cf2_getNominalWidthX( font
->decoder
);
301 if ( font
->decoder
->width_only
)
304 for ( i
= hasWidthArg
? 1 : 0; i
< count
; i
+= 2 )
306 /* construct a CF2_StemHint and push it onto the list */
307 CF2_StemHintRec stemhint
;
311 position
+= cf2_stack_getReal( opStack
, i
);
313 position
+= cf2_stack_getReal( opStack
, i
+ 1 );
315 stemhint
.used
= FALSE
;
319 cf2_arrstack_push( stemHintArray
, &stemhint
); /* defer error check */
322 cf2_stack_clear( opStack
);
325 /* cf2_doStems must define a width (may be default) */
331 cf2_doFlex( CF2_Stack opStack
,
334 CF2_GlyphPath glyphPath
,
335 const FT_Bool
* readFromStack
,
336 FT_Bool doConditionalLastRead
)
347 isHFlex
= readFromStack
[9] == FALSE
;
348 top
= isHFlex
? 9 : 10;
350 for ( i
= 0; i
< top
; i
++ )
352 vals
[i
+ 2] = vals
[i
];
353 if ( readFromStack
[i
] )
354 vals
[i
+ 2] += cf2_stack_getReal( opStack
, index
++ );
360 if ( doConditionalLastRead
)
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
);
369 vals
[12] = vals
[10] + lastVal
;
375 vals
[13] = vals
[11] + lastVal
;
380 if ( readFromStack
[10] )
381 vals
[12] = vals
[10] + cf2_stack_getReal( opStack
, index
++ );
385 if ( readFromStack
[11] )
386 vals
[13] = vals
[11] + cf2_stack_getReal( opStack
, index
);
391 for ( j
= 0; j
< 2; j
++ )
392 cf2_glyphpath_curveTo( glyphPath
, vals
[j
* 6 + 2],
399 cf2_stack_clear( opStack
);
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'.
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.
419 * Unimplemented opcodes are ignored.
423 cf2_interpT2CharString( CF2_Font font
,
425 CF2_OutlineCallbacks callbacks
,
426 const FT_Vector
* translation
,
432 /* lastError is used for errors that are immediately tested */
433 FT_Error lastError
= FT_Err_Ok
;
435 /* pointer to parsed font object */
436 CFF_Decoder
* decoder
= font
->decoder
;
438 FT_Error
* error
= &font
->error
;
439 FT_Memory memory
= font
->memory
;
441 CF2_Fixed scaleY
= font
->innerTransform
.d
;
442 CF2_Fixed nominalWidthX
= cf2_getNominalWidthX( decoder
);
444 /* save this for hinting seac accents */
445 CF2_Fixed hintOriginY
= curY
;
447 CF2_Stack opStack
= NULL
;
448 FT_Byte op1
; /* first opcode byte */
450 CF2_F16Dot16 storage
[CF2_STORAGE_SIZE
]; /* for `put' and `get' */
452 /* instruction limit; 20,000,000 matches Avalon */
453 FT_UInt32 instructionLimit
= 20000000UL;
455 CF2_ArrStackRec subrStack
;
458 CF2_Buffer charstring
= NULL
;
460 CF2_Int charstringIndex
= -1; /* initialize to empty */
462 /* TODO: placeholders for hint structures */
464 /* objects used for hinting */
465 CF2_ArrStackRec hStemHintArray
;
466 CF2_ArrStackRec vStemHintArray
;
468 CF2_HintMaskRec hintMask
;
470 CF2_GlyphPathRec
*glyphPath
= malloc(sizeof(CF2_GlyphPathRec
));
471 /* Ugly but it allows us to reduce the diff */
472 #define glyphPath (*glyphPath)
474 CF2_GlyphPathRec glyphPath
;
478 /* initialize the remaining objects */
479 cf2_arrstack_init( &subrStack
,
482 sizeof ( CF2_BufferRec
) );
483 cf2_arrstack_init( &hStemHintArray
,
486 sizeof ( CF2_StemHintRec
) );
487 cf2_arrstack_init( &vStemHintArray
,
490 sizeof ( CF2_StemHintRec
) );
492 /* initialize CF2_StemHint arrays */
493 cf2_hintmask_init( &hintMask
, error
);
495 /* initialize path map to manage drawing operations */
497 /* Note: last 4 params are used to handle `MoveToPermissive', which */
498 /* may need to call `hintMap.Build' */
499 /* TODO: MoveToPermissive is gone; are these still needed? */
500 cf2_glyphpath_init( &glyphPath
,
513 * Initialize state for width parsing. From the CFF Spec:
515 * The first stack-clearing operator, which must be one of hstem,
516 * hstemhm, vstem, vstemhm, cntrmask, hintmask, hmoveto, vmoveto,
517 * rmoveto, or endchar, takes an additional argument - the width (as
518 * described earlier), which may be expressed as zero or one numeric
521 * What we implement here uses the first validly specified width, but
522 * does not detect errors for specifying more than one width.
524 * If one of the above operators occurs without explicitly specifying
525 * a width, we assume the default width.
529 *width
= cf2_getDefaultWidthX( decoder
);
532 * Note: at this point, all pointers to resources must be NULL
533 * and all local objects must be initialized.
534 * There must be no branches to exit: above this point.
538 /* allocate an operand stack */
539 opStack
= cf2_stack_init( memory
, error
);
542 lastError
= FT_THROW( Out_Of_Memory
);
546 /* initialize subroutine stack by placing top level charstring as */
547 /* first element (max depth plus one for the charstring) */
548 /* Note: Caller owns and must finalize the first charstring. */
549 /* Our copy of it does not change that requirement. */
550 cf2_arrstack_setCount( &subrStack
, CF2_MAX_SUBR
+ 1 );
552 charstring
= (CF2_Buffer
)cf2_arrstack_getBuffer( &subrStack
);
553 *charstring
= *buf
; /* structure copy */
555 charstringIndex
= 0; /* entry is valid now */
557 /* catch errors so far */
561 /* main interpreter loop */
564 if ( cf2_buf_isEnd( charstring
) )
566 /* If we've reached the end of the charstring, simulate a */
567 /* cf2_cmdRETURN or cf2_cmdENDCHAR. */
568 if ( charstringIndex
)
569 op1
= cf2_cmdRETURN
; /* end of buffer for subroutine */
571 op1
= cf2_cmdENDCHAR
; /* end of buffer for top level charstring */
574 op1
= (FT_Byte
)cf2_buf_readByte( charstring
);
576 /* check for errors once per loop */
581 if ( instructionLimit
== 0 )
583 lastError
= FT_THROW( Invalid_Glyph_Format
);
589 case cf2_cmdRESERVED_0
:
590 case cf2_cmdRESERVED_2
:
591 case cf2_cmdRESERVED_9
:
592 case cf2_cmdRESERVED_13
:
593 case cf2_cmdRESERVED_15
:
594 case cf2_cmdRESERVED_16
:
595 case cf2_cmdRESERVED_17
:
596 /* we may get here if we have a prior error */
597 FT_TRACE4(( " unknown op (%d)\n", op1
));
602 FT_TRACE4(( op1
== cf2_cmdHSTEMHM
? " hstemhm\n" : " hstem\n" ));
604 /* never add hints after the mask is computed */
605 if ( cf2_hintmask_isValid( &hintMask
) )
607 FT_TRACE4(( "cf2_interpT2CharString:"
608 " invalid horizontal hint mask\n" ));
619 if ( font
->decoder
->width_only
)
626 FT_TRACE4(( op1
== cf2_cmdVSTEMHM
? " vstemhm\n" : " vstem\n" ));
628 /* never add hints after the mask is computed */
629 if ( cf2_hintmask_isValid( &hintMask
) )
631 FT_TRACE4(( "cf2_interpT2CharString:"
632 " invalid vertical hint mask\n" ));
643 if ( font
->decoder
->width_only
)
649 FT_TRACE4(( " vmoveto\n" ));
651 if ( cf2_stack_count( opStack
) > 1 && !haveWidth
)
652 *width
= cf2_stack_getReal( opStack
, 0 ) + nominalWidthX
;
654 /* width is defined or default after this */
657 if ( font
->decoder
->width_only
)
660 curY
+= cf2_stack_popFixed( opStack
);
662 cf2_glyphpath_moveTo( &glyphPath
, curX
, curY
);
669 CF2_UInt count
= cf2_stack_count( opStack
);
672 FT_TRACE4(( " rlineto\n" ));
674 for ( index
= 0; index
< count
; index
+= 2 )
676 curX
+= cf2_stack_getReal( opStack
, index
+ 0 );
677 curY
+= cf2_stack_getReal( opStack
, index
+ 1 );
679 cf2_glyphpath_lineTo( &glyphPath
, curX
, curY
);
682 cf2_stack_clear( opStack
);
684 continue; /* no need to clear stack again */
690 CF2_UInt count
= cf2_stack_count( opStack
);
692 FT_Bool isX
= op1
== cf2_cmdHLINETO
;
695 FT_TRACE4(( isX
? " hlineto\n" : " vlineto\n" ));
697 for ( index
= 0; index
< count
; index
++ )
699 CF2_Fixed v
= cf2_stack_getReal( opStack
, index
);
709 cf2_glyphpath_lineTo( &glyphPath
, curX
, curY
);
712 cf2_stack_clear( opStack
);
716 case cf2_cmdRCURVELINE
:
717 case cf2_cmdRRCURVETO
:
719 CF2_UInt count
= cf2_stack_count( opStack
);
723 FT_TRACE4(( op1
== cf2_cmdRCURVELINE
? " rcurveline\n"
726 while ( index
+ 6 <= count
)
728 CF2_Fixed x1
= cf2_stack_getReal( opStack
, index
+ 0 ) + curX
;
729 CF2_Fixed y1
= cf2_stack_getReal( opStack
, index
+ 1 ) + curY
;
730 CF2_Fixed x2
= cf2_stack_getReal( opStack
, index
+ 2 ) + x1
;
731 CF2_Fixed y2
= cf2_stack_getReal( opStack
, index
+ 3 ) + y1
;
732 CF2_Fixed x3
= cf2_stack_getReal( opStack
, index
+ 4 ) + x2
;
733 CF2_Fixed y3
= cf2_stack_getReal( opStack
, index
+ 5 ) + y2
;
736 cf2_glyphpath_curveTo( &glyphPath
, x1
, y1
, x2
, y2
, x3
, y3
);
743 if ( op1
== cf2_cmdRCURVELINE
)
745 curX
+= cf2_stack_getReal( opStack
, index
+ 0 );
746 curY
+= cf2_stack_getReal( opStack
, index
+ 1 );
748 cf2_glyphpath_lineTo( &glyphPath
, curX
, curY
);
751 cf2_stack_clear( opStack
);
753 continue; /* no need to clear stack again */
755 case cf2_cmdCALLGSUBR
:
756 case cf2_cmdCALLSUBR
:
761 FT_TRACE4(( op1
== cf2_cmdCALLGSUBR
? " callgsubr"
764 if ( charstringIndex
> CF2_MAX_SUBR
)
766 /* max subr plus one for charstring */
767 lastError
= FT_THROW( Invalid_Glyph_Format
);
768 goto exit
; /* overflow of stack */
771 /* push our current CFF charstring region on subrStack */
772 charstring
= (CF2_Buffer
)
773 cf2_arrstack_getPointer(
775 (size_t)charstringIndex
+ 1 );
777 /* set up the new CFF region and pointer */
778 subrNum
= cf2_stack_popInt( opStack
);
782 case cf2_cmdCALLGSUBR
:
783 FT_TRACE4(( " (idx %d, entering level %d)\n",
784 subrNum
+ decoder
->globals_bias
,
785 charstringIndex
+ 1 ));
787 if ( cf2_initGlobalRegionBuffer( decoder
,
791 lastError
= FT_THROW( Invalid_Glyph_Format
);
792 goto exit
; /* subroutine lookup or stream error */
797 /* cf2_cmdCALLSUBR */
798 FT_TRACE4(( " (idx %d, entering level %d)\n",
799 subrNum
+ decoder
->locals_bias
,
800 charstringIndex
+ 1 ));
802 if ( cf2_initLocalRegionBuffer( decoder
,
806 lastError
= FT_THROW( Invalid_Glyph_Format
);
807 goto exit
; /* subroutine lookup or stream error */
811 charstringIndex
+= 1; /* entry is valid now */
813 continue; /* do not clear the stack */
816 FT_TRACE4(( " return (leaving level %d)\n", charstringIndex
));
818 if ( charstringIndex
< 1 )
820 /* Note: cannot return from top charstring */
821 lastError
= FT_THROW( Invalid_Glyph_Format
);
822 goto exit
; /* underflow of stack */
825 /* restore position in previous charstring */
826 charstring
= (CF2_Buffer
)
827 cf2_arrstack_getPointer(
829 (CF2_UInt
)--charstringIndex
);
830 continue; /* do not clear the stack */
834 FT_Byte op2
= (FT_Byte
)cf2_buf_readByte( charstring
);
839 case cf2_escDOTSECTION
:
840 /* something about `flip type of locking' -- ignore it */
841 FT_TRACE4(( " dotsection\n" ));
851 FT_TRACE4(( " and\n" ));
853 arg2
= cf2_stack_popFixed( opStack
);
854 arg1
= cf2_stack_popFixed( opStack
);
856 cf2_stack_pushInt( opStack
, arg1
&& arg2
);
858 continue; /* do not clear the stack */
866 FT_TRACE4(( " or\n" ));
868 arg2
= cf2_stack_popFixed( opStack
);
869 arg1
= cf2_stack_popFixed( opStack
);
871 cf2_stack_pushInt( opStack
, arg1
|| arg2
);
873 continue; /* do not clear the stack */
880 FT_TRACE4(( " not\n" ));
882 arg
= cf2_stack_popFixed( opStack
);
884 cf2_stack_pushInt( opStack
, !arg
);
886 continue; /* do not clear the stack */
893 FT_TRACE4(( " abs\n" ));
895 arg
= cf2_stack_popFixed( opStack
);
897 cf2_stack_pushFixed( opStack
, FT_ABS( arg
) );
899 continue; /* do not clear the stack */
903 CF2_F16Dot16 summand1
;
904 CF2_F16Dot16 summand2
;
907 FT_TRACE4(( " add\n" ));
909 summand2
= cf2_stack_popFixed( opStack
);
910 summand1
= cf2_stack_popFixed( opStack
);
912 cf2_stack_pushFixed( opStack
, summand1
+ summand2
);
914 continue; /* do not clear the stack */
918 CF2_F16Dot16 minuend
;
919 CF2_F16Dot16 subtrahend
;
922 FT_TRACE4(( " sub\n" ));
924 subtrahend
= cf2_stack_popFixed( opStack
);
925 minuend
= cf2_stack_popFixed( opStack
);
927 cf2_stack_pushFixed( opStack
, minuend
- subtrahend
);
929 continue; /* do not clear the stack */
933 CF2_F16Dot16 dividend
;
934 CF2_F16Dot16 divisor
;
937 FT_TRACE4(( " div\n" ));
939 divisor
= cf2_stack_popFixed( opStack
);
940 dividend
= cf2_stack_popFixed( opStack
);
942 cf2_stack_pushFixed( opStack
, FT_DivFix( dividend
, divisor
) );
944 continue; /* do not clear the stack */
951 FT_TRACE4(( " neg\n" ));
953 arg
= cf2_stack_popFixed( opStack
);
955 cf2_stack_pushFixed( opStack
, -arg
);
957 continue; /* do not clear the stack */
965 FT_TRACE4(( " eq\n" ));
967 arg2
= cf2_stack_popFixed( opStack
);
968 arg1
= cf2_stack_popFixed( opStack
);
970 cf2_stack_pushInt( opStack
, arg1
== arg2
);
972 continue; /* do not clear the stack */
975 FT_TRACE4(( " drop\n" ));
977 (void)cf2_stack_popFixed( opStack
);
978 continue; /* do not clear the stack */
986 FT_TRACE4(( " put\n" ));
988 idx
= cf2_stack_popInt( opStack
);
989 val
= cf2_stack_popFixed( opStack
);
991 if ( idx
>= 0 && idx
< CF2_STORAGE_SIZE
)
994 continue; /* do not clear the stack */
1001 FT_TRACE4(( " get\n" ));
1003 idx
= cf2_stack_popInt( opStack
);
1005 if ( idx
>= 0 && idx
< CF2_STORAGE_SIZE
)
1006 cf2_stack_pushFixed( opStack
, storage
[idx
] );
1008 continue; /* do not clear the stack */
1018 FT_TRACE4(( " ifelse\n" ));
1020 cond2
= cf2_stack_popFixed( opStack
);
1021 cond1
= cf2_stack_popFixed( opStack
);
1022 arg2
= cf2_stack_popFixed( opStack
);
1023 arg1
= cf2_stack_popFixed( opStack
);
1025 cf2_stack_pushFixed( opStack
, cond1
<= cond2
? arg1
: arg2
);
1027 continue; /* do not clear the stack */
1029 case cf2_escRANDOM
: /* in spec */
1030 FT_TRACE4(( " random\n" ));
1037 CF2_F16Dot16 factor1
;
1038 CF2_F16Dot16 factor2
;
1041 FT_TRACE4(( " mul\n" ));
1043 factor2
= cf2_stack_popFixed( opStack
);
1044 factor1
= cf2_stack_popFixed( opStack
);
1046 cf2_stack_pushFixed( opStack
, FT_MulFix( factor1
, factor2
) );
1048 continue; /* do not clear the stack */
1055 FT_TRACE4(( " sqrt\n" ));
1057 arg
= cf2_stack_popFixed( opStack
);
1060 FT_Fixed root
= arg
;
1064 /* Babylonian method */
1067 new_root
= ( root
+ FT_DivFix( arg
, root
) + 1 ) >> 1;
1068 if ( new_root
== root
)
1077 cf2_stack_pushFixed( opStack
, arg
);
1079 continue; /* do not clear the stack */
1086 FT_TRACE4(( " dup\n" ));
1088 arg
= cf2_stack_popFixed( opStack
);
1090 cf2_stack_pushFixed( opStack
, arg
);
1091 cf2_stack_pushFixed( opStack
, arg
);
1093 continue; /* do not clear the stack */
1101 FT_TRACE4(( " exch\n" ));
1103 arg2
= cf2_stack_popFixed( opStack
);
1104 arg1
= cf2_stack_popFixed( opStack
);
1106 cf2_stack_pushFixed( opStack
, arg2
);
1107 cf2_stack_pushFixed( opStack
, arg1
);
1109 continue; /* do not clear the stack */
1117 FT_TRACE4(( " index\n" ));
1119 idx
= cf2_stack_popInt( opStack
);
1120 size
= cf2_stack_count( opStack
);
1124 /* for `cf2_stack_getReal', index 0 is bottom of stack */
1130 else if ( (CF2_UInt
)idx
>= size
)
1133 gr_idx
= size
- 1 - (CF2_UInt
)idx
;
1135 cf2_stack_pushFixed( opStack
,
1136 cf2_stack_getReal( opStack
, gr_idx
) );
1139 continue; /* do not clear the stack */
1147 FT_TRACE4(( " roll\n" ));
1149 idx
= cf2_stack_popInt( opStack
);
1150 count
= cf2_stack_popInt( opStack
);
1152 cf2_stack_roll( opStack
, count
, idx
);
1154 continue; /* do not clear the stack */
1158 static const FT_Bool readFromStack
[12] =
1160 TRUE
/* dx1 */, FALSE
/* dy1 */,
1161 TRUE
/* dx2 */, TRUE
/* dy2 */,
1162 TRUE
/* dx3 */, FALSE
/* dy3 */,
1163 TRUE
/* dx4 */, FALSE
/* dy4 */,
1164 TRUE
/* dx5 */, FALSE
/* dy5 */,
1165 TRUE
/* dx6 */, FALSE
/* dy6 */
1169 FT_TRACE4(( " hflex\n" ));
1171 cf2_doFlex( opStack
,
1176 FALSE
/* doConditionalLastRead */ );
1182 static const FT_Bool readFromStack
[12] =
1184 TRUE
/* dx1 */, TRUE
/* dy1 */,
1185 TRUE
/* dx2 */, TRUE
/* dy2 */,
1186 TRUE
/* dx3 */, TRUE
/* dy3 */,
1187 TRUE
/* dx4 */, TRUE
/* dy4 */,
1188 TRUE
/* dx5 */, TRUE
/* dy5 */,
1189 TRUE
/* dx6 */, TRUE
/* dy6 */
1193 FT_TRACE4(( " flex\n" ));
1195 cf2_doFlex( opStack
,
1200 FALSE
/* doConditionalLastRead */ );
1202 break; /* TODO: why is this not a continue? */
1206 static const FT_Bool readFromStack
[12] =
1208 TRUE
/* dx1 */, TRUE
/* dy1 */,
1209 TRUE
/* dx2 */, TRUE
/* dy2 */,
1210 TRUE
/* dx3 */, FALSE
/* dy3 */,
1211 TRUE
/* dx4 */, FALSE
/* dy4 */,
1212 TRUE
/* dx5 */, TRUE
/* dy5 */,
1213 TRUE
/* dx6 */, FALSE
/* dy6 */
1217 FT_TRACE4(( " hflex1\n" ));
1219 cf2_doFlex( opStack
,
1224 FALSE
/* doConditionalLastRead */ );
1230 static const FT_Bool readFromStack
[12] =
1232 TRUE
/* dx1 */, TRUE
/* dy1 */,
1233 TRUE
/* dx2 */, TRUE
/* dy2 */,
1234 TRUE
/* dx3 */, TRUE
/* dy3 */,
1235 TRUE
/* dx4 */, TRUE
/* dy4 */,
1236 TRUE
/* dx5 */, TRUE
/* dy5 */,
1237 FALSE
/* dx6 */, FALSE
/* dy6 */
1241 FT_TRACE4(( " flex1\n" ));
1243 cf2_doFlex( opStack
,
1248 TRUE
/* doConditionalLastRead */ );
1252 case cf2_escRESERVED_1
:
1253 case cf2_escRESERVED_2
:
1254 case cf2_escRESERVED_6
:
1255 case cf2_escRESERVED_7
:
1256 case cf2_escRESERVED_8
:
1257 case cf2_escRESERVED_13
:
1258 case cf2_escRESERVED_16
:
1259 case cf2_escRESERVED_17
:
1260 case cf2_escRESERVED_19
:
1261 case cf2_escRESERVED_25
:
1262 case cf2_escRESERVED_31
:
1263 case cf2_escRESERVED_32
:
1264 case cf2_escRESERVED_33
:
1266 FT_TRACE4(( " unknown op (12, %d)\n", op2
));
1268 }; /* end of switch statement checking `op2' */
1270 } /* case cf2_cmdESC */
1273 case cf2_cmdENDCHAR
:
1274 FT_TRACE4(( " endchar\n" ));
1276 if ( cf2_stack_count( opStack
) == 1 ||
1277 cf2_stack_count( opStack
) == 5 )
1280 *width
= cf2_stack_getReal( opStack
, 0 ) + nominalWidthX
;
1283 /* width is defined or default after this */
1286 if ( font
->decoder
->width_only
)
1289 /* close path if still open */
1290 cf2_glyphpath_closeOpenPath( &glyphPath
);
1292 if ( cf2_stack_count( opStack
) > 1 )
1294 /* must be either 4 or 5 -- */
1295 /* this is a (deprecated) implied `seac' operator */
1299 CF2_BufferRec component
;
1300 CF2_Fixed dummyWidth
; /* ignore component width */
1306 lastError
= FT_THROW( Invalid_Glyph_Format
);
1307 goto exit
; /* nested seac */
1310 achar
= cf2_stack_popInt( opStack
);
1311 bchar
= cf2_stack_popInt( opStack
);
1313 curY
= cf2_stack_popFixed( opStack
);
1314 curX
= cf2_stack_popFixed( opStack
);
1316 error2
= cf2_getSeacComponent( decoder
, achar
, &component
);
1319 lastError
= error2
; /* pass FreeType error through */
1322 cf2_interpT2CharString( font
,
1330 cf2_freeSeacComponent( decoder
, &component
);
1332 error2
= cf2_getSeacComponent( decoder
, bchar
, &component
);
1335 lastError
= error2
; /* pass FreeType error through */
1338 cf2_interpT2CharString( font
,
1346 cf2_freeSeacComponent( decoder
, &component
);
1350 case cf2_cmdCNTRMASK
:
1351 case cf2_cmdHINTMASK
:
1352 /* the final \n in the tracing message gets added in */
1353 /* `cf2_hintmask_read' (which also traces the mask bytes) */
1354 FT_TRACE4(( op1
== cf2_cmdCNTRMASK
? " cntrmask" : " hintmask" ));
1356 /* never add hints after the mask is computed */
1357 if ( cf2_stack_count( opStack
) > 1 &&
1358 cf2_hintmask_isValid( &hintMask
) )
1360 FT_TRACE4(( "cf2_interpT2CharString: invalid hint mask\n" ));
1364 /* if there are arguments on the stack, there this is an */
1365 /* implied cf2_cmdVSTEMHM */
1373 if ( font
->decoder
->width_only
)
1376 if ( op1
== cf2_cmdHINTMASK
)
1378 /* consume the hint mask bytes which follow the operator */
1379 cf2_hintmask_read( &hintMask
,
1381 cf2_arrstack_size( &hStemHintArray
) +
1382 cf2_arrstack_size( &vStemHintArray
) );
1387 * Consume the counter mask bytes which follow the operator:
1388 * Build a temporary hint map, just to place and lock those
1389 * stems participating in the counter mask. These are most
1390 * likely the dominant hstems, and are grouped together in a
1391 * few counter groups, not necessarily in correspondence
1392 * with the hint groups. This reduces the chances of
1393 * conflicts between hstems that are initially placed in
1394 * separate hint groups and then brought together. The
1395 * positions are copied back to `hStemHintArray', so we can
1396 * discard `counterMask' and `counterHintMap'.
1400 CF2_HintMapRec
*counterHintMap
= malloc(sizeof(CF2_HintMapRec
));
1401 /* Ugly but it allows us to reduce the diff */
1402 #define counterHintMap (*counterHintMap)
1404 CF2_HintMapRec counterHintMap
;
1406 CF2_HintMaskRec counterMask
;
1409 cf2_hintmap_init( &counterHintMap
,
1411 &glyphPath
.initialHintMap
,
1412 &glyphPath
.hintMoves
,
1414 cf2_hintmask_init( &counterMask
, error
);
1416 cf2_hintmask_read( &counterMask
,
1418 cf2_arrstack_size( &hStemHintArray
) +
1419 cf2_arrstack_size( &vStemHintArray
) );
1420 cf2_hintmap_build( &counterHintMap
,
1427 free(&counterHintMap
);
1432 case cf2_cmdRMOVETO
:
1433 FT_TRACE4(( " rmoveto\n" ));
1435 if ( cf2_stack_count( opStack
) > 2 && !haveWidth
)
1436 *width
= cf2_stack_getReal( opStack
, 0 ) + nominalWidthX
;
1438 /* width is defined or default after this */
1441 if ( font
->decoder
->width_only
)
1444 curY
+= cf2_stack_popFixed( opStack
);
1445 curX
+= cf2_stack_popFixed( opStack
);
1447 cf2_glyphpath_moveTo( &glyphPath
, curX
, curY
);
1451 case cf2_cmdHMOVETO
:
1452 FT_TRACE4(( " hmoveto\n" ));
1454 if ( cf2_stack_count( opStack
) > 1 && !haveWidth
)
1455 *width
= cf2_stack_getReal( opStack
, 0 ) + nominalWidthX
;
1457 /* width is defined or default after this */
1460 if ( font
->decoder
->width_only
)
1463 curX
+= cf2_stack_popFixed( opStack
);
1465 cf2_glyphpath_moveTo( &glyphPath
, curX
, curY
);
1469 case cf2_cmdRLINECURVE
:
1471 CF2_UInt count
= cf2_stack_count( opStack
);
1475 FT_TRACE4(( " rlinecurve\n" ));
1477 while ( index
+ 6 < count
)
1479 curX
+= cf2_stack_getReal( opStack
, index
+ 0 );
1480 curY
+= cf2_stack_getReal( opStack
, index
+ 1 );
1482 cf2_glyphpath_lineTo( &glyphPath
, curX
, curY
);
1486 while ( index
< count
)
1488 CF2_Fixed x1
= cf2_stack_getReal( opStack
, index
+ 0 ) + curX
;
1489 CF2_Fixed y1
= cf2_stack_getReal( opStack
, index
+ 1 ) + curY
;
1490 CF2_Fixed x2
= cf2_stack_getReal( opStack
, index
+ 2 ) + x1
;
1491 CF2_Fixed y2
= cf2_stack_getReal( opStack
, index
+ 3 ) + y1
;
1492 CF2_Fixed x3
= cf2_stack_getReal( opStack
, index
+ 4 ) + x2
;
1493 CF2_Fixed y3
= cf2_stack_getReal( opStack
, index
+ 5 ) + y2
;
1496 cf2_glyphpath_curveTo( &glyphPath
, x1
, y1
, x2
, y2
, x3
, y3
);
1503 cf2_stack_clear( opStack
);
1505 continue; /* no need to clear stack again */
1507 case cf2_cmdVVCURVETO
:
1509 CF2_UInt count
, count1
= cf2_stack_count( opStack
);
1513 /* if `cf2_stack_count' isn't of the form 4n or 4n+1, */
1514 /* we enforce it by clearing the second bit */
1515 /* (and sorting the stack indexing to suit) */
1516 count
= count1
& ~2U;
1517 index
+= count1
- count
;
1519 FT_TRACE4(( " vvcurveto\n" ));
1521 while ( index
< count
)
1523 CF2_Fixed x1
, y1
, x2
, y2
, x3
, y3
;
1526 if ( ( count
- index
) & 1 )
1528 x1
= cf2_stack_getReal( opStack
, index
) + curX
;
1535 y1
= cf2_stack_getReal( opStack
, index
+ 0 ) + curY
;
1536 x2
= cf2_stack_getReal( opStack
, index
+ 1 ) + x1
;
1537 y2
= cf2_stack_getReal( opStack
, index
+ 2 ) + y1
;
1539 y3
= cf2_stack_getReal( opStack
, index
+ 3 ) + y2
;
1541 cf2_glyphpath_curveTo( &glyphPath
, x1
, y1
, x2
, y2
, x3
, y3
);
1548 cf2_stack_clear( opStack
);
1550 continue; /* no need to clear stack again */
1552 case cf2_cmdHHCURVETO
:
1554 CF2_UInt count
, count1
= cf2_stack_count( opStack
);
1558 /* if `cf2_stack_count' isn't of the form 4n or 4n+1, */
1559 /* we enforce it by clearing the second bit */
1560 /* (and sorting the stack indexing to suit) */
1561 count
= count1
& ~2U;
1562 index
+= count1
- count
;
1564 FT_TRACE4(( " hhcurveto\n" ));
1566 while ( index
< count
)
1568 CF2_Fixed x1
, y1
, x2
, y2
, x3
, y3
;
1571 if ( ( count
- index
) & 1 )
1573 y1
= cf2_stack_getReal( opStack
, index
) + curY
;
1580 x1
= cf2_stack_getReal( opStack
, index
+ 0 ) + curX
;
1581 x2
= cf2_stack_getReal( opStack
, index
+ 1 ) + x1
;
1582 y2
= cf2_stack_getReal( opStack
, index
+ 2 ) + y1
;
1583 x3
= cf2_stack_getReal( opStack
, index
+ 3 ) + x2
;
1586 cf2_glyphpath_curveTo( &glyphPath
, x1
, y1
, x2
, y2
, x3
, y3
);
1593 cf2_stack_clear( opStack
);
1595 continue; /* no need to clear stack again */
1597 case cf2_cmdVHCURVETO
:
1598 case cf2_cmdHVCURVETO
:
1600 CF2_UInt count
, count1
= cf2_stack_count( opStack
);
1603 FT_Bool alternate
= op1
== cf2_cmdHVCURVETO
;
1606 /* if `cf2_stack_count' isn't of the form 8n, 8n+1, */
1607 /* 8n+4, or 8n+5, we enforce it by clearing the */
1609 /* (and sorting the stack indexing to suit) */
1610 count
= count1
& ~2U;
1611 index
+= count1
- count
;
1613 FT_TRACE4(( alternate
? " hvcurveto\n" : " vhcurveto\n" ));
1615 while ( index
< count
)
1617 CF2_Fixed x1
, x2
, x3
, y1
, y2
, y3
;
1622 x1
= cf2_stack_getReal( opStack
, index
+ 0 ) + curX
;
1624 x2
= cf2_stack_getReal( opStack
, index
+ 1 ) + x1
;
1625 y2
= cf2_stack_getReal( opStack
, index
+ 2 ) + y1
;
1626 y3
= cf2_stack_getReal( opStack
, index
+ 3 ) + y2
;
1628 if ( count
- index
== 5 )
1630 x3
= cf2_stack_getReal( opStack
, index
+ 4 ) + x2
;
1642 y1
= cf2_stack_getReal( opStack
, index
+ 0 ) + curY
;
1643 x2
= cf2_stack_getReal( opStack
, index
+ 1 ) + x1
;
1644 y2
= cf2_stack_getReal( opStack
, index
+ 2 ) + y1
;
1645 x3
= cf2_stack_getReal( opStack
, index
+ 3 ) + x2
;
1647 if ( count
- index
== 5 )
1649 y3
= cf2_stack_getReal( opStack
, index
+ 4 ) + y2
;
1659 cf2_glyphpath_curveTo( &glyphPath
, x1
, y1
, x2
, y2
, x3
, y3
);
1666 cf2_stack_clear( opStack
);
1668 continue; /* no need to clear stack again */
1670 case cf2_cmdEXTENDEDNMBR
:
1674 CF2_Int byte1
= cf2_buf_readByte( charstring
);
1675 CF2_Int byte2
= cf2_buf_readByte( charstring
);
1678 v
= (FT_Short
)( ( byte1
<< 8 ) |
1681 FT_TRACE4(( " %d", v
));
1683 cf2_stack_pushInt( opStack
, v
);
1690 if ( /* op1 >= 32 && */ op1
<= 246 )
1697 FT_TRACE4(( " %d", v
));
1700 cf2_stack_pushInt( opStack
, v
);
1703 else if ( /* op1 >= 247 && */ op1
<= 250 )
1711 v
+= cf2_buf_readByte( charstring
);
1714 FT_TRACE4(( " %d", v
));
1717 cf2_stack_pushInt( opStack
, v
);
1720 else if ( /* op1 >= 251 && */ op1
<= 254 )
1728 v
+= cf2_buf_readByte( charstring
);
1731 FT_TRACE4(( " %d", v
));
1734 cf2_stack_pushInt( opStack
, v
);
1737 else /* op1 == 255 */
1741 FT_UInt32 byte1
= (FT_UInt32
)cf2_buf_readByte( charstring
);
1742 FT_UInt32 byte2
= (FT_UInt32
)cf2_buf_readByte( charstring
);
1743 FT_UInt32 byte3
= (FT_UInt32
)cf2_buf_readByte( charstring
);
1744 FT_UInt32 byte4
= (FT_UInt32
)cf2_buf_readByte( charstring
);
1747 v
= (CF2_Fixed
)( ( byte1
<< 24 ) |
1752 FT_TRACE4(( " %.2f", v
/ 65536.0 ));
1754 cf2_stack_pushFixed( opStack
, v
);
1757 continue; /* don't clear stack */
1759 } /* end of switch statement checking `op1' */
1761 cf2_stack_clear( opStack
);
1763 } /* end of main interpreter loop */
1765 /* we get here if the charstring ends without cf2_cmdENDCHAR */
1766 FT_TRACE4(( "cf2_interpT2CharString:"
1767 " charstring ends without ENDCHAR\n" ));
1770 /* check whether last error seen is also the first one */
1771 cf2_setError( error
, lastError
);
1773 /* free resources from objects we've used */
1774 cf2_glyphpath_finalize( &glyphPath
);
1775 cf2_arrstack_finalize( &vStemHintArray
);
1776 cf2_arrstack_finalize( &hStemHintArray
);
1777 cf2_arrstack_finalize( &subrStack
);
1778 cf2_stack_free( opStack
);
1780 FT_TRACE4(( "\n" ));
1784 #undef counterHintMap