2018-08-15 21:00:19 +02:00
|
|
|
/*
|
|
|
|
* Copyright © 2018 Adobe Systems Incorporated.
|
|
|
|
*
|
|
|
|
* This is part of HarfBuzz, a text shaping library.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, without written agreement and without
|
|
|
|
* license or royalty fees, to use, copy, modify, and distribute this
|
|
|
|
* software and its documentation for any purpose, provided that the
|
|
|
|
* above copyright notice and the following two paragraphs appear in
|
|
|
|
* all copies of this software.
|
|
|
|
*
|
|
|
|
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
|
|
|
|
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
|
|
|
|
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
|
|
|
|
* DAMAGE.
|
|
|
|
*
|
|
|
|
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
|
|
|
|
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
|
|
|
|
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
|
|
|
*
|
|
|
|
* Adobe Author(s): Michiharu Ariza
|
|
|
|
*/
|
2018-08-16 09:13:09 +02:00
|
|
|
#ifndef HB_CFF1_INTERP_CS_HH
|
|
|
|
#define HB_CFF1_INTERP_CS_HH
|
2018-08-15 21:00:19 +02:00
|
|
|
|
2018-08-29 22:26:17 +02:00
|
|
|
#include "hb.hh"
|
|
|
|
#include "hb-cff-interp-cs-common.hh"
|
2018-08-15 21:00:19 +02:00
|
|
|
|
|
|
|
namespace CFF {
|
|
|
|
|
|
|
|
using namespace OT;
|
|
|
|
|
2018-09-01 01:28:47 +02:00
|
|
|
struct CFF1CSInterpEnv : CSInterpEnv<Number, CFF1Subrs>
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
2018-09-04 19:25:21 +02:00
|
|
|
template <typename ACC>
|
|
|
|
inline void init (const ByteStr &str, ACC &acc, unsigned int fd)
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
2018-09-04 19:25:21 +02:00
|
|
|
SUPER::init (str, *acc.globalSubrs, *acc.privateDicts[fd].localSubrs);
|
2018-08-29 21:14:30 +02:00
|
|
|
processed_width = false;
|
|
|
|
has_width = false;
|
2018-08-15 21:00:19 +02:00
|
|
|
for (unsigned int i = 0; i < kTransientArraySize; i++)
|
|
|
|
transient_array[i].set_int (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool check_transient_array_index (unsigned int i) const
|
|
|
|
{ return i < kTransientArraySize; }
|
|
|
|
|
2018-09-19 02:24:30 +02:00
|
|
|
inline unsigned int check_width (void)
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
2018-09-19 02:24:30 +02:00
|
|
|
unsigned int arg_start = 0;
|
2018-08-29 21:14:30 +02:00
|
|
|
if (!processed_width)
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
2018-09-17 20:14:56 +02:00
|
|
|
if ((SUPER::argStack.get_count () & 1) != 0)
|
2018-08-29 21:14:30 +02:00
|
|
|
{
|
2018-09-17 20:14:56 +02:00
|
|
|
width = SUPER::argStack[0];
|
2018-08-29 21:14:30 +02:00
|
|
|
has_width = true;
|
2018-09-19 02:24:30 +02:00
|
|
|
arg_start = 1;
|
2018-08-29 21:14:30 +02:00
|
|
|
}
|
|
|
|
processed_width = true;
|
2018-08-15 21:00:19 +02:00
|
|
|
}
|
2018-09-19 02:24:30 +02:00
|
|
|
return arg_start;
|
2018-08-15 21:00:19 +02:00
|
|
|
}
|
|
|
|
|
2018-08-29 21:14:30 +02:00
|
|
|
bool processed_width;
|
|
|
|
bool has_width;
|
2018-08-15 21:00:19 +02:00
|
|
|
Number width;
|
|
|
|
|
|
|
|
static const unsigned int kTransientArraySize = 32;
|
|
|
|
Number transient_array[kTransientArraySize];
|
2018-09-01 01:28:47 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
typedef CSInterpEnv<Number, CFF1Subrs> SUPER;
|
2018-08-15 21:00:19 +02:00
|
|
|
};
|
|
|
|
|
2018-09-17 20:14:56 +02:00
|
|
|
template <typename OPSET, typename PARAM, typename PATH=PathProcsNull<CFF1CSInterpEnv, PARAM> >
|
|
|
|
struct CFF1CSOpSet : CSOpSet<Number, OPSET, CFF1CSInterpEnv, PARAM, PATH>
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
2018-08-16 09:13:09 +02:00
|
|
|
static inline bool process_op (OpCode op, CFF1CSInterpEnv &env, PARAM& param)
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
|
|
|
Number n1, n2;
|
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
|
|
|
|
case OpCode_and:
|
|
|
|
if (unlikely (!env.argStack.check_pop_num2 (n1, n2))) return false;
|
2018-08-16 00:00:32 +02:00
|
|
|
env.argStack.push_int ((n1.to_real() != 0.0f) && (n2.to_real() != 0.0f));
|
2018-08-15 21:00:19 +02:00
|
|
|
break;
|
|
|
|
case OpCode_or:
|
|
|
|
if (unlikely (!env.argStack.check_pop_num2 (n1, n2))) return false;
|
2018-08-16 00:00:32 +02:00
|
|
|
env.argStack.push_int ((n1.to_real() != 0.0f) || (n2.to_real() != 0.0f));
|
2018-08-15 21:00:19 +02:00
|
|
|
break;
|
|
|
|
case OpCode_not:
|
|
|
|
if (unlikely (!env.argStack.check_pop_num (n1))) return false;
|
|
|
|
env.argStack.push_int (n1.to_real() == 0.0f);
|
|
|
|
break;
|
|
|
|
case OpCode_abs:
|
|
|
|
if (unlikely (!env.argStack.check_pop_num (n1))) return false;
|
|
|
|
env.argStack.push_real (fabs(n1.to_real ()));
|
|
|
|
break;
|
|
|
|
case OpCode_add:
|
|
|
|
if (unlikely (!env.argStack.check_pop_num2 (n1, n2))) return false;
|
2018-08-16 00:00:32 +02:00
|
|
|
env.argStack.push_real (n1.to_real() + n2.to_real());
|
2018-08-15 21:00:19 +02:00
|
|
|
break;
|
|
|
|
case OpCode_sub:
|
|
|
|
if (unlikely (!env.argStack.check_pop_num2 (n1, n2))) return false;
|
2018-08-16 00:00:32 +02:00
|
|
|
env.argStack.push_real (n1.to_real() - n2.to_real());
|
2018-08-15 21:00:19 +02:00
|
|
|
break;
|
|
|
|
case OpCode_div:
|
|
|
|
if (unlikely (!env.argStack.check_pop_num2 (n1, n2))) return false;
|
|
|
|
if (unlikely (n2.to_real() == 0.0f))
|
|
|
|
env.argStack.push_int (0);
|
|
|
|
else
|
|
|
|
env.argStack.push_real (n1.to_real() / n2.to_real());
|
|
|
|
break;
|
|
|
|
case OpCode_neg:
|
|
|
|
if (unlikely (!env.argStack.check_pop_num (n1))) return false;
|
|
|
|
env.argStack.push_real (-n1.to_real ());
|
|
|
|
break;
|
|
|
|
case OpCode_eq:
|
|
|
|
if (unlikely (!env.argStack.check_pop_num2 (n1, n2))) return false;
|
2018-08-16 00:00:32 +02:00
|
|
|
env.argStack.push_int (n1.to_real() == n2.to_real());
|
2018-08-15 21:00:19 +02:00
|
|
|
break;
|
|
|
|
case OpCode_drop:
|
|
|
|
if (unlikely (!env.argStack.check_pop_num (n1))) return false;
|
|
|
|
break;
|
|
|
|
case OpCode_put:
|
|
|
|
if (unlikely (!env.argStack.check_pop_num2 (n1, n2) ||
|
|
|
|
!env.check_transient_array_index (n2.to_int ()))) return false;
|
|
|
|
env.transient_array[n2.to_int ()] = n1;
|
|
|
|
break;
|
|
|
|
case OpCode_get:
|
|
|
|
if (unlikely (!env.argStack.check_pop_num (n1) ||
|
|
|
|
!env.check_transient_array_index (n1.to_int ()))) return false;
|
|
|
|
env.argStack.push (env.transient_array[n1.to_int ()]);
|
|
|
|
break;
|
|
|
|
case OpCode_ifelse:
|
|
|
|
{
|
|
|
|
if (unlikely (!env.argStack.check_pop_num2 (n1, n2))) return false;
|
|
|
|
bool test = n1.to_real () <= n2.to_real ();
|
|
|
|
if (unlikely (!env.argStack.check_pop_num2 (n1, n2))) return false;
|
|
|
|
env.argStack.push (test? n1: n2);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OpCode_random:
|
|
|
|
if (unlikely (!env.argStack.check_overflow (1))) return false;
|
2018-08-29 21:14:30 +02:00
|
|
|
env.argStack.push_int (1); /* we can't deal with random behavior; make it constant */
|
2018-08-15 21:00:19 +02:00
|
|
|
case OpCode_mul:
|
|
|
|
if (unlikely (!env.argStack.check_pop_num2 (n1, n2))) return false;
|
|
|
|
env.argStack.push_real (n1.to_real() * n2.to_real());
|
|
|
|
break;
|
|
|
|
case OpCode_sqrt:
|
|
|
|
if (unlikely (!env.argStack.check_pop_num (n1))) return false;
|
|
|
|
env.argStack.push_real ((float)sqrt (n1.to_real ()));
|
|
|
|
break;
|
|
|
|
case OpCode_dup:
|
|
|
|
if (unlikely (!env.argStack.check_pop_num (n1))) return false;
|
|
|
|
env.argStack.push (n1);
|
|
|
|
env.argStack.push (n1);
|
|
|
|
break;
|
|
|
|
case OpCode_exch:
|
|
|
|
if (unlikely (!env.argStack.check_pop_num2 (n1, n2))) return false;
|
|
|
|
env.argStack.push (n2);
|
|
|
|
env.argStack.push (n1);
|
|
|
|
break;
|
|
|
|
case OpCode_index:
|
|
|
|
{
|
|
|
|
if (unlikely (!env.argStack.check_pop_num (n1))) return false;
|
|
|
|
int i = n1.to_int ();
|
|
|
|
if (i < 0) i = 0;
|
2018-09-17 20:14:56 +02:00
|
|
|
if (unlikely (i >= env.argStack.get_count () || !env.argStack.check_overflow (1))) return false;
|
|
|
|
env.argStack.push (env.argStack[env.argStack.get_count () - i - 1]);
|
2018-08-15 21:00:19 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case OpCode_roll:
|
|
|
|
{
|
|
|
|
if (unlikely (!env.argStack.check_pop_num2 (n1, n2))) return false;
|
|
|
|
int n = n1.to_int ();
|
|
|
|
int j = n2.to_int ();
|
2018-09-17 20:14:56 +02:00
|
|
|
if (unlikely (n < 0 || n > env.argStack.get_count ())) return false;
|
2018-08-15 21:00:19 +02:00
|
|
|
if (likely (n > 0))
|
|
|
|
{
|
|
|
|
if (j < 0)
|
|
|
|
j = n - (-j % n);
|
|
|
|
j %= n;
|
2018-09-17 20:14:56 +02:00
|
|
|
unsigned int top = env.argStack.get_count () - 1;
|
2018-08-15 21:00:19 +02:00
|
|
|
unsigned int bot = top - n + 1;
|
|
|
|
env.argStack.reverse_range (top - j + 1, top);
|
|
|
|
env.argStack.reverse_range (bot, top - j);
|
|
|
|
env.argStack.reverse_range (bot, top);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (unlikely (!SUPER::process_op (op, env, param)))
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2018-08-29 21:14:30 +02:00
|
|
|
|
2018-09-19 02:24:30 +02:00
|
|
|
static inline void flush_args (CFF1CSInterpEnv &env, PARAM& param, unsigned int start_arg = 0)
|
2018-08-29 21:14:30 +02:00
|
|
|
{
|
2018-09-19 02:24:30 +02:00
|
|
|
start_arg = env.check_width ();
|
|
|
|
SUPER::flush_args (env, param, start_arg);
|
2018-08-29 21:14:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-09-17 20:14:56 +02:00
|
|
|
typedef CSOpSet<Number, OPSET, CFF1CSInterpEnv, PARAM, PATH> SUPER;
|
2018-08-15 21:00:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename OPSET, typename PARAM>
|
2018-08-16 09:13:09 +02:00
|
|
|
struct CFF1CSInterpreter : CSInterpreter<CFF1CSInterpEnv, OPSET, PARAM> {};
|
2018-08-15 21:00:19 +02:00
|
|
|
|
|
|
|
} /* namespace CFF */
|
|
|
|
|
2018-08-16 09:13:09 +02:00
|
|
|
#endif /* HB_CFF1_INTERP_CS_HH */
|