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-29 22:36:39 +02:00
|
|
|
#ifndef HB_CFF_INTERP_DICT_COMMON_HH
|
|
|
|
#define HB_CFF_INTERP_DICT_COMMON_HH
|
2018-08-15 21:00:19 +02:00
|
|
|
|
2018-08-29 22:26:17 +02:00
|
|
|
#include "hb-cff-interp-common.hh"
|
2018-09-11 22:24:27 +02:00
|
|
|
#include <math.h>
|
2018-08-15 21:00:19 +02:00
|
|
|
|
|
|
|
namespace CFF {
|
|
|
|
|
|
|
|
using namespace OT;
|
|
|
|
|
|
|
|
/* an opstr and the parsed out dict value(s) */
|
|
|
|
struct DictVal : OpStr
|
|
|
|
{
|
|
|
|
inline void init (void)
|
|
|
|
{
|
|
|
|
single_val.set_int (0);
|
|
|
|
multi_val.init ();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void fini (void)
|
|
|
|
{
|
|
|
|
multi_val.fini ();
|
|
|
|
}
|
|
|
|
|
2018-09-04 20:29:15 +02:00
|
|
|
Number single_val;
|
|
|
|
hb_vector_t<Number> multi_val;
|
2018-08-15 21:00:19 +02:00
|
|
|
};
|
|
|
|
|
2018-09-04 20:29:15 +02:00
|
|
|
typedef DictVal NumDictVal;
|
2018-09-01 01:28:47 +02:00
|
|
|
|
2018-08-15 21:00:19 +02:00
|
|
|
template <typename VAL>
|
|
|
|
struct DictValues
|
|
|
|
{
|
|
|
|
inline void init (void)
|
|
|
|
{
|
|
|
|
opStart = 0;
|
|
|
|
values.init ();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void fini (void)
|
|
|
|
{
|
|
|
|
values.fini ();
|
|
|
|
}
|
|
|
|
|
2018-09-07 02:28:15 +02:00
|
|
|
inline void addOp (OpCode op, const SubByteStr& substr = SubByteStr ())
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
|
|
|
VAL *val = values.push ();
|
|
|
|
val->op = op;
|
|
|
|
val->str = ByteStr (substr.str, opStart, substr.offset - opStart);
|
|
|
|
opStart = substr.offset;
|
|
|
|
}
|
|
|
|
|
2018-09-07 02:28:15 +02:00
|
|
|
inline void addOp (OpCode op, const SubByteStr& substr, const VAL &v)
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
|
|
|
VAL *val = values.push (v);
|
|
|
|
val->op = op;
|
|
|
|
val->str = ByteStr (substr.str, opStart, substr.offset - opStart);
|
|
|
|
opStart = substr.offset;
|
|
|
|
}
|
|
|
|
|
2018-09-07 02:28:15 +02:00
|
|
|
inline bool hasOp (OpCode op) const
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < getNumValues (); i++)
|
|
|
|
if (getValue (i).op == op) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline unsigned getNumValues (void) const { return values.len; }
|
|
|
|
inline const VAL &getValue (unsigned int i) const { return values[i]; }
|
|
|
|
inline const VAL &operator [] (unsigned int i) const { return getValue (i); }
|
|
|
|
|
2018-08-15 21:00:19 +02:00
|
|
|
unsigned int opStart;
|
|
|
|
hb_vector_t<VAL> values;
|
|
|
|
};
|
|
|
|
|
2018-09-11 01:00:20 +02:00
|
|
|
template <typename OPSTR=OpStr>
|
|
|
|
struct TopDictValues : DictValues<OPSTR>
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
|
|
|
inline void init (void)
|
|
|
|
{
|
2018-09-11 01:00:20 +02:00
|
|
|
DictValues<OPSTR>::init ();
|
2018-08-15 21:00:19 +02:00
|
|
|
charStringsOffset = 0;
|
|
|
|
FDArrayOffset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void fini (void)
|
|
|
|
{
|
2018-09-11 01:00:20 +02:00
|
|
|
DictValues<OPSTR>::fini ();
|
2018-08-15 21:00:19 +02:00
|
|
|
}
|
|
|
|
|
2018-09-11 01:00:20 +02:00
|
|
|
inline unsigned int calculate_serialized_op_size (const OPSTR& opstr) const
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
|
|
|
switch (opstr.op)
|
|
|
|
{
|
|
|
|
case OpCode_CharStrings:
|
|
|
|
case OpCode_FDArray:
|
|
|
|
return OpCode_Size (OpCode_longintdict) + 4 + OpCode_Size (opstr.op);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return opstr.str.len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int charStringsOffset;
|
|
|
|
unsigned int FDArrayOffset;
|
|
|
|
};
|
|
|
|
|
2018-09-04 20:29:15 +02:00
|
|
|
struct DictOpSet : OpSet<Number>
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
2018-09-04 20:29:15 +02:00
|
|
|
static inline bool process_op (OpCode op, InterpEnv<Number>& env)
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
|
|
|
switch (op) {
|
|
|
|
case OpCode_longintdict: /* 5-byte integer */
|
|
|
|
return env.argStack.push_longint_from_substr (env.substr);
|
|
|
|
case OpCode_BCD: /* real number */
|
|
|
|
float v;
|
|
|
|
if (unlikely (!env.argStack.check_overflow (1) || !parse_bcd (env.substr, v)))
|
|
|
|
return false;
|
|
|
|
env.argStack.push_real (v);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
2018-09-04 20:29:15 +02:00
|
|
|
return OpSet<Number>::process_op (op, env);
|
2018-08-15 21:00:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-08-18 01:50:13 +02:00
|
|
|
|
2018-09-11 19:43:15 +02:00
|
|
|
static inline bool parse_bcd (SubByteStr& substr, float& v)
|
|
|
|
{
|
|
|
|
v = 0.0f;
|
|
|
|
|
2018-09-11 22:24:27 +02:00
|
|
|
bool neg = false;
|
|
|
|
double int_part = 0;
|
|
|
|
long frac_part = 0;
|
|
|
|
unsigned int frac_count = 0;
|
|
|
|
bool exp_neg = false;
|
|
|
|
unsigned int exp_part = 0;
|
|
|
|
enum Part { INT_PART=0, FRAC_PART, EXP_PART } part = INT_PART;
|
|
|
|
enum Nibble { DECIMAL=10, EXP_POS, EXP_NEG, RESERVED, NEG, END };
|
|
|
|
|
|
|
|
double value = 0.0;
|
2018-09-11 19:43:15 +02:00
|
|
|
unsigned char byte = 0;
|
2018-09-11 22:24:27 +02:00
|
|
|
for (unsigned int i = 0;; i++)
|
2018-09-11 19:43:15 +02:00
|
|
|
{
|
2018-09-11 22:24:27 +02:00
|
|
|
char d;
|
2018-09-11 19:43:15 +02:00
|
|
|
if ((i & 1) == 0)
|
|
|
|
{
|
2018-09-11 22:24:27 +02:00
|
|
|
if (!substr.avail ()) return false;
|
2018-09-11 19:43:15 +02:00
|
|
|
byte = substr[0];
|
|
|
|
substr.inc ();
|
2018-09-11 22:24:27 +02:00
|
|
|
d = byte >> 4;
|
2018-09-11 19:43:15 +02:00
|
|
|
}
|
|
|
|
else
|
2018-09-11 22:24:27 +02:00
|
|
|
d = byte & 0x0F;
|
2018-09-11 19:43:15 +02:00
|
|
|
|
2018-09-11 22:24:27 +02:00
|
|
|
switch (d)
|
2018-09-11 19:43:15 +02:00
|
|
|
{
|
2018-09-11 22:24:27 +02:00
|
|
|
case RESERVED:
|
2018-09-11 19:43:15 +02:00
|
|
|
return false;
|
2018-09-11 22:24:27 +02:00
|
|
|
|
|
|
|
case END:
|
2018-09-11 22:56:52 +02:00
|
|
|
value = (double)(neg? -int_part: int_part);
|
2018-09-11 22:24:27 +02:00
|
|
|
if (frac_count > 0)
|
|
|
|
value += (frac_part / pow (10.0, (double)frac_count));
|
|
|
|
if (exp_part != 0)
|
|
|
|
{
|
|
|
|
if (exp_neg)
|
|
|
|
value /= pow (10.0, (double)exp_part);
|
|
|
|
else
|
|
|
|
value *= pow (10.0, (double)exp_part);
|
|
|
|
}
|
|
|
|
v = (float)value;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case NEG:
|
|
|
|
if (i != 0) return false;
|
|
|
|
neg = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DECIMAL:
|
|
|
|
if (part != INT_PART) return false;
|
|
|
|
part = FRAC_PART;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EXP_NEG:
|
|
|
|
exp_neg = true;
|
2018-10-02 23:13:36 +02:00
|
|
|
HB_FALLTHROUGH;
|
|
|
|
|
2018-09-11 22:24:27 +02:00
|
|
|
case EXP_POS:
|
|
|
|
if (part == EXP_PART) return false;
|
|
|
|
part = EXP_PART;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
switch (part) {
|
|
|
|
default:
|
|
|
|
case INT_PART:
|
|
|
|
int_part = (int_part * 10) + d;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FRAC_PART:
|
|
|
|
frac_part = (frac_part * 10) + d;
|
|
|
|
frac_count++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EXP_PART:
|
|
|
|
exp_part = (exp_part * 10) + d;
|
|
|
|
break;
|
|
|
|
}
|
2018-09-11 19:43:15 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-11 22:24:27 +02:00
|
|
|
|
2018-09-11 19:43:15 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-18 01:50:13 +02:00
|
|
|
static inline bool is_hint_op (OpCode op)
|
|
|
|
{
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case OpCode_BlueValues:
|
|
|
|
case OpCode_OtherBlues:
|
|
|
|
case OpCode_FamilyBlues:
|
|
|
|
case OpCode_FamilyOtherBlues:
|
|
|
|
case OpCode_StemSnapH:
|
|
|
|
case OpCode_StemSnapV:
|
|
|
|
case OpCode_StdHW:
|
|
|
|
case OpCode_StdVW:
|
|
|
|
case OpCode_BlueScale:
|
|
|
|
case OpCode_BlueShift:
|
|
|
|
case OpCode_BlueFuzz:
|
|
|
|
case OpCode_ForceBold:
|
|
|
|
case OpCode_LanguageGroup:
|
|
|
|
case OpCode_ExpansionFactor:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-08-15 21:00:19 +02:00
|
|
|
};
|
|
|
|
|
2018-09-11 01:00:20 +02:00
|
|
|
template <typename VAL=OpStr>
|
2018-09-04 20:29:15 +02:00
|
|
|
struct TopDictOpSet : DictOpSet
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
2018-09-11 01:00:20 +02:00
|
|
|
static inline bool process_op (OpCode op, InterpEnv<Number>& env, TopDictValues<VAL> & dictval)
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
|
|
|
switch (op) {
|
|
|
|
case OpCode_CharStrings:
|
|
|
|
if (unlikely (!env.argStack.check_pop_uint (dictval.charStringsOffset)))
|
|
|
|
return false;
|
2018-08-31 22:28:16 +02:00
|
|
|
env.clear_args ();
|
2018-08-15 21:00:19 +02:00
|
|
|
break;
|
|
|
|
case OpCode_FDArray:
|
|
|
|
if (unlikely (!env.argStack.check_pop_uint (dictval.FDArrayOffset)))
|
|
|
|
return false;
|
2018-08-31 22:28:16 +02:00
|
|
|
env.clear_args ();
|
2018-08-15 21:00:19 +02:00
|
|
|
break;
|
2018-09-12 01:47:55 +02:00
|
|
|
case OpCode_FontMatrix:
|
|
|
|
env.clear_args ();
|
|
|
|
break;
|
2018-08-15 21:00:19 +02:00
|
|
|
default:
|
2018-09-04 20:29:15 +02:00
|
|
|
return DictOpSet::process_op (op, env);
|
2018-08-15 21:00:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-01 01:28:47 +02:00
|
|
|
template <typename OPSET, typename PARAM, typename ENV=NumInterpEnv>
|
|
|
|
struct DictInterpreter : Interpreter<ENV>
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
|
|
|
inline bool interpret (PARAM& param)
|
|
|
|
{
|
|
|
|
param.init ();
|
|
|
|
do
|
|
|
|
{
|
|
|
|
OpCode op;
|
2018-09-01 01:28:47 +02:00
|
|
|
if (unlikely (!SUPER::env.fetch_op (op) ||
|
|
|
|
!OPSET::process_op (op, SUPER::env, param)))
|
2018-08-15 21:00:19 +02:00
|
|
|
return false;
|
2018-09-01 01:28:47 +02:00
|
|
|
} while (SUPER::env.substr.avail ());
|
2018-08-15 21:00:19 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-01 01:28:47 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
typedef Interpreter<ENV> SUPER;
|
2018-08-15 21:00:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace CFF */
|
|
|
|
|
2018-08-29 22:36:39 +02:00
|
|
|
#endif /* HB_CFF_INTERP_DICT_COMMON_HH */
|