1727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/***************************************************************************/
2727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*                                                                         */
3727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  cf2stack.c                                                             */
4727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*                                                                         */
5727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*    Adobe's code for emulating a CFF stack (body).                       */
6727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*                                                                         */
7727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  Copyright 2007-2013 Adobe Systems Incorporated.                        */
8727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*                                                                         */
9727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  This software, and all works of authorship, whether in source or       */
10727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  object code form as indicated by the copyright notice(s) included      */
11727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  herein (collectively, the "Work") is made available, and may only be   */
12727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  used, modified, and distributed under the FreeType Project License,    */
13727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  LICENSE.TXT.  Additionally, subject to the terms and conditions of the */
14727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  FreeType Project License, each contributor to the Work hereby grants   */
15727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  to any individual or legal entity exercising permissions granted by    */
16727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  the FreeType Project License and this section (hereafter, "You" or     */
17727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  "Your") a perpetual, worldwide, non-exclusive, no-charge,              */
18727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  royalty-free, irrevocable (except as stated in this section) patent    */
19727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  license to make, have made, use, offer to sell, sell, import, and      */
20727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  otherwise transfer the Work, where such license applies only to those  */
21727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  patent claims licensable by such contributor that are necessarily      */
22727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  infringed by their contribution(s) alone or by combination of their    */
23727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  contribution(s) with the Work to which such contribution(s) was        */
24727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  submitted.  If You institute patent litigation against any entity      */
25727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  (including a cross-claim or counterclaim in a lawsuit) alleging that   */
26727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  the Work or a contribution incorporated within the Work constitutes    */
27727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  direct or contributory patent infringement, then any patent licenses   */
28727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  granted to You under this License for that Work shall terminate as of  */
29727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  the date such litigation is filed.                                     */
30727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*                                                                         */
31727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  By using, modifying, or distributing the Work you indicate that you    */
32727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  have read and understood the terms and conditions of the               */
33727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  FreeType Project License as well as those provided in this section,    */
34727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*  and you accept them fully.                                             */
35727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/*                                                                         */
36727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/***************************************************************************/
37727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
38727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
39727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease#include "cf2ft.h"
40727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease#include FT_INTERNAL_DEBUG_H
41727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
42727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease#include "cf2glue.h"
43727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease#include "cf2font.h"
44727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease#include "cf2stack.h"
45727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
46727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease#include "cf2error.h"
47727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
48727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
49727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  /* Allocate and initialize an instance of CF2_Stack.       */
50727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  /* Note: This function returns NULL on error (does not set */
51727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  /* `error').                                               */
52727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  FT_LOCAL_DEF( CF2_Stack )
53727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  cf2_stack_init( FT_Memory  memory,
54727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease                  FT_Error*  e )
55727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  {
56727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    FT_Error  error = FT_Err_Ok;     /* for FT_QNEW */
57727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
58727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    CF2_Stack  stack = NULL;
59727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
60727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
61727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    if ( !FT_QNEW( stack ) )
62727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    {
63727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      /* initialize the structure; FT_QNEW zeroes it */
64727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      stack->memory = memory;
65727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      stack->error  = e;
66727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      stack->top    = &stack->buffer[0]; /* empty stack */
67727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    }
68727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
69727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    return stack;
70727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  }
71727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
72727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
73727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  FT_LOCAL_DEF( void )
74727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  cf2_stack_free( CF2_Stack  stack )
75727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  {
76727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    if ( stack )
77727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    {
78727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      FT_Memory  memory = stack->memory;
79727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
80727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
81727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      /* free the main structure */
82727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      FT_FREE( stack );
83727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    }
84727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  }
85727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
86727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
87727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  FT_LOCAL_DEF( CF2_UInt )
88727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  cf2_stack_count( CF2_Stack  stack )
89727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  {
90727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    return (CF2_UInt)( stack->top - &stack->buffer[0] );
91727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  }
92727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
93727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
94727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  FT_LOCAL_DEF( void )
95727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  cf2_stack_pushInt( CF2_Stack  stack,
96727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease                     CF2_Int    val )
97727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  {
98727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    if ( stack->top == &stack->buffer[CF2_OPERAND_STACK_SIZE] )
99727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    {
100727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      CF2_SET_ERROR( stack->error, Stack_Overflow );
101727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      return;     /* stack overflow */
102727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    }
103727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
104727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    stack->top->u.i  = val;
105727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    stack->top->type = CF2_NumberInt;
106727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    ++stack->top;
107727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  }
108727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
109727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
110727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  FT_LOCAL_DEF( void )
111727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  cf2_stack_pushFixed( CF2_Stack  stack,
112727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease                       CF2_Fixed  val )
113727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  {
114727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    if ( stack->top == &stack->buffer[CF2_OPERAND_STACK_SIZE] )
115727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    {
116727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      CF2_SET_ERROR( stack->error, Stack_Overflow );
117727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      return;     /* stack overflow */
118727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    }
119727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
120727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    stack->top->u.r  = val;
121727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    stack->top->type = CF2_NumberFixed;
122727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    ++stack->top;
123727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  }
124727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
125727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
126727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  /* this function is only allowed to pop an integer type */
127727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  FT_LOCAL_DEF( CF2_Int )
128727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  cf2_stack_popInt( CF2_Stack  stack )
129727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  {
130727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    if ( stack->top == &stack->buffer[0] )
131727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    {
132727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      CF2_SET_ERROR( stack->error, Stack_Underflow );
133727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      return 0;   /* underflow */
134727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    }
135727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    if ( stack->top[-1].type != CF2_NumberInt )
136727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    {
137727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      CF2_SET_ERROR( stack->error, Syntax_Error );
138727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      return 0;   /* type mismatch */
139727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    }
140727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
141727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    --stack->top;
142727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
143727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    return stack->top->u.i;
144727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  }
145727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
146727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
147727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  /* Note: type mismatch is silently cast */
148727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  /* TODO: check this */
149727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  FT_LOCAL_DEF( CF2_Fixed )
150727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  cf2_stack_popFixed( CF2_Stack  stack )
151727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  {
152727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    if ( stack->top == &stack->buffer[0] )
153727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    {
154727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      CF2_SET_ERROR( stack->error, Stack_Underflow );
155727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      return cf2_intToFixed( 0 );    /* underflow */
156727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    }
157727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
158727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    --stack->top;
159727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
160727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    switch ( stack->top->type )
161727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    {
162727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    case CF2_NumberInt:
163727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      return cf2_intToFixed( stack->top->u.i );
164727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    case CF2_NumberFrac:
165727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      return cf2_fracToFixed( stack->top->u.f );
166727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    default:
167727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      return stack->top->u.r;
168727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    }
169727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  }
170727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
171727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
172727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  /* Note: type mismatch is silently cast */
173727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  /* TODO: check this */
174727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  FT_LOCAL_DEF( CF2_Fixed )
175727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  cf2_stack_getReal( CF2_Stack  stack,
176727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease                     CF2_UInt   idx )
177727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  {
178727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    FT_ASSERT( cf2_stack_count( stack ) <= CF2_OPERAND_STACK_SIZE );
179727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
180727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    if ( idx >= cf2_stack_count( stack ) )
181727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    {
182727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      CF2_SET_ERROR( stack->error, Stack_Overflow );
183727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      return cf2_intToFixed( 0 );    /* bounds error */
184727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    }
185727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
186727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    switch ( stack->buffer[idx].type )
187727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    {
188727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    case CF2_NumberInt:
189727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      return cf2_intToFixed( stack->buffer[idx].u.i );
190727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    case CF2_NumberFrac:
191727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      return cf2_fracToFixed( stack->buffer[idx].u.f );
192727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    default:
193727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease      return stack->buffer[idx].u.r;
194727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    }
195727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  }
196727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
197727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
198727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  FT_LOCAL_DEF( void )
199727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  cf2_stack_clear( CF2_Stack  stack )
200727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  {
201727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease    stack->top = &stack->buffer[0];
202727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease  }
203727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
204727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease
205727dee178a392d20eb050d0c446f2fcc29058fa1Victoria Lease/* END */
206