2018-08-15 21:00:19 +02:00
|
|
|
/*
|
2018-11-12 17:47:07 +01:00
|
|
|
* Copyright © 2018 Adobe Inc.
|
2018-08-15 21:00:19 +02:00
|
|
|
*
|
|
|
|
* 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-12-05 07:30:33 +01:00
|
|
|
#include <float.h>
|
2018-08-15 21:00:19 +02:00
|
|
|
|
|
|
|
namespace CFF {
|
|
|
|
|
|
|
|
using namespace OT;
|
|
|
|
|
|
|
|
/* an opstr and the parsed out dict value(s) */
|
2018-12-22 16:47:04 +01:00
|
|
|
struct dict_val_t : op_str_t
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
2018-12-17 19:01:01 +01:00
|
|
|
void init () { single_val.set_int (0); }
|
|
|
|
void fini () {}
|
2018-08-15 21:00:19 +02:00
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
number_t single_val;
|
2018-08-15 21:00:19 +02:00
|
|
|
};
|
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
typedef dict_val_t num_dict_val_t;
|
2018-09-01 01:28:47 +02:00
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
template <typename VAL> struct dict_values_t : parsed_values_t<VAL> {};
|
2018-08-15 21:00:19 +02:00
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
template <typename OPSTR=op_str_t>
|
|
|
|
struct top_dict_values_t : dict_values_t<OPSTR>
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
2018-12-17 19:01:01 +01:00
|
|
|
void init ()
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
2018-12-22 16:47:04 +01:00
|
|
|
dict_values_t<OPSTR>::init ();
|
2018-08-15 21:00:19 +02:00
|
|
|
charStringsOffset = 0;
|
|
|
|
FDArrayOffset = 0;
|
|
|
|
}
|
2018-12-22 16:47:04 +01:00
|
|
|
void fini () { dict_values_t<OPSTR>::fini (); }
|
2018-08-15 21:00:19 +02:00
|
|
|
|
2018-12-16 20:08:10 +01:00
|
|
|
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:
|
2018-12-01 05:04:59 +01:00
|
|
|
return OpCode_Size (OpCode_longintdict) + 4 + OpCode_Size (opstr.op);
|
2018-08-15 21:00:19 +02:00
|
|
|
|
|
|
|
default:
|
2018-12-22 16:47:04 +01:00
|
|
|
return opstr.str.length;
|
2018-08-15 21:00:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int charStringsOffset;
|
|
|
|
unsigned int FDArrayOffset;
|
|
|
|
};
|
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
struct dict_opset_t : opset_t<number_t>
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
2018-12-22 16:47:04 +01:00
|
|
|
static void process_op (op_code_t op, interp_env_t<number_t>& env)
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
|
|
|
switch (op) {
|
|
|
|
case OpCode_longintdict: /* 5-byte integer */
|
2018-12-22 16:47:04 +01:00
|
|
|
env.argStack.push_longint_from_substr (env.str_ref);
|
2018-12-01 05:04:59 +01:00
|
|
|
break;
|
2018-10-12 11:16:07 +02:00
|
|
|
|
2018-08-15 21:00:19 +02:00
|
|
|
case OpCode_BCD: /* real number */
|
2018-12-22 16:47:04 +01:00
|
|
|
env.argStack.push_real (parse_bcd (env.str_ref));
|
2018-12-01 05:04:59 +01:00
|
|
|
break;
|
2018-08-15 21:00:19 +02:00
|
|
|
|
|
|
|
default:
|
2018-12-22 16:47:04 +01:00
|
|
|
opset_t<number_t>::process_op (op, env);
|
2018-12-01 05:04:59 +01:00
|
|
|
break;
|
2018-08-15 21:00:19 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-18 01:50:13 +02:00
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
static double parse_bcd (byte_str_ref_t& str_ref)
|
2018-09-11 19:43:15 +02:00
|
|
|
{
|
2018-09-11 22:24:27 +02:00
|
|
|
bool neg = false;
|
|
|
|
double int_part = 0;
|
2018-12-05 09:26:03 +01:00
|
|
|
uint64_t frac_part = 0;
|
2018-12-05 08:58:26 +01:00
|
|
|
uint32_t frac_count = 0;
|
2018-09-11 22:24:27 +02:00
|
|
|
bool exp_neg = false;
|
2018-12-05 08:58:26 +01:00
|
|
|
uint32_t exp_part = 0;
|
2018-12-05 07:24:38 +01:00
|
|
|
bool exp_overflow = false;
|
2018-09-11 22:24:27 +02:00
|
|
|
enum Part { INT_PART=0, FRAC_PART, EXP_PART } part = INT_PART;
|
|
|
|
enum Nibble { DECIMAL=10, EXP_POS, EXP_NEG, RESERVED, NEG, END };
|
2018-12-20 04:26:54 +01:00
|
|
|
const uint64_t MAX_FRACT = 0xFFFFFFFFFFFFFull; /* 1^52-1 */
|
2018-12-05 08:58:26 +01:00
|
|
|
const uint32_t MAX_EXP = 0x7FFu; /* 1^11-1 */
|
2018-09-11 22:24:27 +02:00
|
|
|
|
|
|
|
double value = 0.0;
|
2018-09-11 19:43:15 +02:00
|
|
|
unsigned char byte = 0;
|
2018-12-05 08:58:26 +01:00
|
|
|
for (uint32_t 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-12-22 16:47:04 +01:00
|
|
|
if (!str_ref.avail ())
|
2018-12-01 05:04:59 +01:00
|
|
|
{
|
2018-12-22 16:47:04 +01:00
|
|
|
str_ref.set_error ();
|
2018-12-04 00:32:42 +01:00
|
|
|
return 0.0;
|
2018-12-01 05:04:59 +01:00
|
|
|
}
|
2018-12-22 16:47:04 +01:00
|
|
|
byte = str_ref[0];
|
|
|
|
str_ref.inc ();
|
2018-12-01 05:04:59 +01:00
|
|
|
d = byte >> 4;
|
2018-09-11 19:43:15 +02:00
|
|
|
}
|
|
|
|
else
|
2018-12-01 05:04:59 +01: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-12-01 05:04:59 +01:00
|
|
|
case RESERVED:
|
2018-12-22 16:47:04 +01:00
|
|
|
str_ref.set_error ();
|
2018-12-05 07:24:38 +01:00
|
|
|
return value;
|
2018-12-01 05:04:59 +01:00
|
|
|
|
|
|
|
case END:
|
|
|
|
value = (double)(neg? -int_part: int_part);
|
|
|
|
if (frac_count > 0)
|
2018-12-05 20:18:16 +01:00
|
|
|
{
|
|
|
|
double frac = (frac_part / pow (10.0, (double)frac_count));
|
|
|
|
if (neg) frac = -frac;
|
|
|
|
value += frac;
|
|
|
|
}
|
2018-12-05 07:24:38 +01:00
|
|
|
if (unlikely (exp_overflow))
|
|
|
|
{
|
|
|
|
if (value == 0.0)
|
|
|
|
return value;
|
|
|
|
if (exp_neg)
|
|
|
|
return neg? -DBL_MIN: DBL_MIN;
|
|
|
|
else
|
|
|
|
return neg? -DBL_MAX: DBL_MAX;
|
|
|
|
}
|
2018-12-01 05:04:59 +01:00
|
|
|
if (exp_part != 0)
|
|
|
|
{
|
|
|
|
if (exp_neg)
|
|
|
|
value /= pow (10.0, (double)exp_part);
|
|
|
|
else
|
|
|
|
value *= pow (10.0, (double)exp_part);
|
|
|
|
}
|
2018-12-04 00:32:42 +01:00
|
|
|
return value;
|
2018-12-01 05:04:59 +01:00
|
|
|
|
|
|
|
case NEG:
|
|
|
|
if (i != 0)
|
|
|
|
{
|
2018-12-22 16:47:04 +01:00
|
|
|
str_ref.set_error ();
|
2018-12-04 00:32:42 +01:00
|
|
|
return 0.0;
|
2018-12-01 05:04:59 +01:00
|
|
|
}
|
|
|
|
neg = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DECIMAL:
|
|
|
|
if (part != INT_PART)
|
|
|
|
{
|
2018-12-22 16:47:04 +01:00
|
|
|
str_ref.set_error ();
|
2018-12-05 07:24:38 +01:00
|
|
|
return value;
|
2018-12-01 05:04:59 +01:00
|
|
|
}
|
|
|
|
part = FRAC_PART;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EXP_NEG:
|
|
|
|
exp_neg = true;
|
|
|
|
HB_FALLTHROUGH;
|
|
|
|
|
|
|
|
case EXP_POS:
|
|
|
|
if (part == EXP_PART)
|
|
|
|
{
|
2018-12-22 16:47:04 +01:00
|
|
|
str_ref.set_error ();
|
2018-12-05 07:24:38 +01:00
|
|
|
return value;
|
2018-12-01 05:04:59 +01:00
|
|
|
}
|
|
|
|
part = EXP_PART;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
switch (part) {
|
|
|
|
default:
|
|
|
|
case INT_PART:
|
|
|
|
int_part = (int_part * 10) + d;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FRAC_PART:
|
2018-12-06 19:27:37 +01:00
|
|
|
if (likely (frac_part <= MAX_FRACT / 10))
|
2018-12-05 07:24:38 +01:00
|
|
|
{
|
2018-12-05 09:26:03 +01:00
|
|
|
frac_part = (frac_part * 10) + (unsigned)d;
|
2018-12-05 07:24:38 +01:00
|
|
|
frac_count++;
|
|
|
|
}
|
2018-12-01 05:04:59 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EXP_PART:
|
2018-12-06 19:27:37 +01:00
|
|
|
if (likely (exp_part * 10 + d <= MAX_EXP))
|
2018-12-05 07:24:38 +01:00
|
|
|
{
|
|
|
|
exp_part = (exp_part * 10) + d;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
exp_overflow = true;
|
2018-12-01 05:04:59 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-09-11 19:43:15 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-11 22:24:27 +02:00
|
|
|
|
2018-12-05 07:24:38 +01:00
|
|
|
return value;
|
2018-09-11 19:43:15 +02:00
|
|
|
}
|
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
static bool is_hint_op (op_code_t op)
|
2018-08-18 01:50:13 +02:00
|
|
|
{
|
|
|
|
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:
|
2018-12-01 05:04:59 +01:00
|
|
|
return true;
|
2018-08-18 01:50:13 +02:00
|
|
|
default:
|
2018-12-01 05:04:59 +01:00
|
|
|
return false;
|
2018-08-18 01:50:13 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-15 21:00:19 +02:00
|
|
|
};
|
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
template <typename VAL=op_str_t>
|
|
|
|
struct top_dict_opset_t : dict_opset_t
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
2018-12-22 16:47:04 +01:00
|
|
|
static void process_op (op_code_t op, interp_env_t<number_t>& env, top_dict_values_t<VAL> & dictval)
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
|
|
|
switch (op) {
|
|
|
|
case OpCode_CharStrings:
|
2018-12-01 05:04:59 +01:00
|
|
|
dictval.charStringsOffset = env.argStack.pop_uint ();
|
|
|
|
env.clear_args ();
|
|
|
|
break;
|
2018-08-15 21:00:19 +02:00
|
|
|
case OpCode_FDArray:
|
2018-12-01 05:04:59 +01:00
|
|
|
dictval.FDArrayOffset = env.argStack.pop_uint ();
|
|
|
|
env.clear_args ();
|
|
|
|
break;
|
2018-09-12 01:47:55 +02:00
|
|
|
case OpCode_FontMatrix:
|
2018-12-01 05:04:59 +01:00
|
|
|
env.clear_args ();
|
|
|
|
break;
|
2018-08-15 21:00:19 +02:00
|
|
|
default:
|
2018-12-22 16:47:04 +01:00
|
|
|
dict_opset_t::process_op (op, env);
|
2018-12-01 05:04:59 +01:00
|
|
|
break;
|
2018-08-15 21:00:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-22 16:47:04 +01:00
|
|
|
template <typename OPSET, typename PARAM, typename ENV=num_interp_env_t>
|
|
|
|
struct dict_interpreter_t : interpreter_t<ENV>
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
2018-12-16 20:08:10 +01:00
|
|
|
bool interpret (PARAM& param)
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
|
|
|
param.init ();
|
2018-12-22 16:47:04 +01:00
|
|
|
while (SUPER::env.str_ref.avail ())
|
2018-08-15 21:00:19 +02:00
|
|
|
{
|
2018-10-12 11:16:07 +02:00
|
|
|
OPSET::process_op (SUPER::env.fetch_op (), SUPER::env, param);
|
|
|
|
if (unlikely (SUPER::env.in_error ()))
|
2018-12-01 05:04:59 +01:00
|
|
|
return false;
|
2018-11-07 23:48:37 +01:00
|
|
|
}
|
2018-12-01 04:54:57 +01:00
|
|
|
|
2018-08-15 21:00:19 +02:00
|
|
|
return true;
|
|
|
|
}
|
2018-09-01 01:28:47 +02:00
|
|
|
|
|
|
|
private:
|
2018-12-22 16:47:04 +01:00
|
|
|
typedef interpreter_t<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 */
|