1 /***************************************************************************/
5 /* Adobe's code for emulating a CFF stack (body). */
7 /* Copyright 2007-2013 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
49 /* Allocate and initialize an instance of CF2_Stack. */
50 /* Note: This function returns NULL on error (does not set */
52 FT_LOCAL_DEF( CF2_Stack
)
53 cf2_stack_init( FT_Memory memory
,
56 FT_Error error
= FT_Err_Ok
; /* for FT_QNEW */
58 CF2_Stack stack
= NULL
;
61 if ( !FT_QNEW( stack
) )
63 /* initialize the structure; FT_QNEW zeroes it */
64 stack
->memory
= memory
;
66 stack
->top
= &stack
->buffer
[0]; /* empty stack */
74 cf2_stack_free( CF2_Stack stack
)
78 FT_Memory memory
= stack
->memory
;
81 /* free the main structure */
87 FT_LOCAL_DEF( CF2_UInt
)
88 cf2_stack_count( CF2_Stack stack
)
90 return (CF2_UInt
)( stack
->top
- &stack
->buffer
[0] );
95 cf2_stack_pushInt( CF2_Stack stack
,
98 if ( stack
->top
== &stack
->buffer
[CF2_OPERAND_STACK_SIZE
] )
100 CF2_SET_ERROR( stack
->error
, Stack_Overflow
);
101 return; /* stack overflow */
104 stack
->top
->u
.i
= val
;
105 stack
->top
->type
= CF2_NumberInt
;
111 cf2_stack_pushFixed( CF2_Stack stack
,
114 if ( stack
->top
== &stack
->buffer
[CF2_OPERAND_STACK_SIZE
] )
116 CF2_SET_ERROR( stack
->error
, Stack_Overflow
);
117 return; /* stack overflow */
120 stack
->top
->u
.r
= val
;
121 stack
->top
->type
= CF2_NumberFixed
;
126 /* this function is only allowed to pop an integer type */
127 FT_LOCAL_DEF( CF2_Int
)
128 cf2_stack_popInt( CF2_Stack stack
)
130 if ( stack
->top
== &stack
->buffer
[0] )
132 CF2_SET_ERROR( stack
->error
, Stack_Underflow
);
133 return 0; /* underflow */
135 if ( stack
->top
[-1].type
!= CF2_NumberInt
)
137 CF2_SET_ERROR( stack
->error
, Syntax_Error
);
138 return 0; /* type mismatch */
143 return stack
->top
->u
.i
;
147 /* Note: type mismatch is silently cast */
148 /* TODO: check this */
149 FT_LOCAL_DEF( CF2_Fixed
)
150 cf2_stack_popFixed( CF2_Stack stack
)
152 if ( stack
->top
== &stack
->buffer
[0] )
154 CF2_SET_ERROR( stack
->error
, Stack_Underflow
);
155 return cf2_intToFixed( 0 ); /* underflow */
160 switch ( stack
->top
->type
)
163 return cf2_intToFixed( stack
->top
->u
.i
);
165 return cf2_fracToFixed( stack
->top
->u
.f
);
167 return stack
->top
->u
.r
;
172 /* Note: type mismatch is silently cast */
173 /* TODO: check this */
174 FT_LOCAL_DEF( CF2_Fixed
)
175 cf2_stack_getReal( CF2_Stack stack
,
178 FT_ASSERT( cf2_stack_count( stack
) <= CF2_OPERAND_STACK_SIZE
);
180 if ( idx
>= cf2_stack_count( stack
) )
182 CF2_SET_ERROR( stack
->error
, Stack_Overflow
);
183 return cf2_intToFixed( 0 ); /* bounds error */
186 switch ( stack
->buffer
[idx
].type
)
189 return cf2_intToFixed( stack
->buffer
[idx
].u
.i
);
191 return cf2_fracToFixed( stack
->buffer
[idx
].u
.f
);
193 return stack
->buffer
[idx
].u
.r
;
199 cf2_stack_clear( CF2_Stack stack
)
201 stack
->top
= &stack
->buffer
[0];