2 * Copyright © 2018 Adobe Inc.
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 * Adobe Author(s): Michiharu Ariza
26 #ifndef HB_CFF_INTERP_DICT_COMMON_HH
27 #define HB_CFF_INTERP_DICT_COMMON_HH
29 #include "hb-cff-interp-common.hh"
37 /* an opstr and the parsed out dict value(s) */
38 struct dict_val_t : op_str_t
40 void init () { single_val.set_int (0); }
46 typedef dict_val_t num_dict_val_t;
48 template <typename VAL> struct dict_values_t : parsed_values_t<VAL> {};
50 template <typename OPSTR=op_str_t>
51 struct top_dict_values_t : dict_values_t<OPSTR>
55 dict_values_t<OPSTR>::init ();
56 charStringsOffset = 0;
59 void fini () { dict_values_t<OPSTR>::fini (); }
61 unsigned int calculate_serialized_op_size (const OPSTR& opstr) const
65 case OpCode_CharStrings:
67 return OpCode_Size (OpCode_longintdict) + 4 + OpCode_Size (opstr.op);
70 return opstr.str.length;
74 unsigned int charStringsOffset;
75 unsigned int FDArrayOffset;
78 struct dict_opset_t : opset_t<number_t>
80 static void process_op (op_code_t op, interp_env_t<number_t>& env)
83 case OpCode_longintdict: /* 5-byte integer */
84 env.argStack.push_longint_from_substr (env.str_ref);
87 case OpCode_BCD: /* real number */
88 env.argStack.push_real (parse_bcd (env.str_ref));
92 opset_t<number_t>::process_op (op, env);
97 static double parse_bcd (byte_str_ref_t& str_ref)
101 uint64_t frac_part = 0;
102 uint32_t frac_count = 0;
103 bool exp_neg = false;
104 uint32_t exp_part = 0;
105 bool exp_overflow = false;
106 enum Part { INT_PART=0, FRAC_PART, EXP_PART } part = INT_PART;
107 enum Nibble { DECIMAL=10, EXP_POS, EXP_NEG, RESERVED, NEG, END };
108 const uint64_t MAX_FRACT = 0xFFFFFFFFFFFFFull; /* 1^52-1 */
109 const uint32_t MAX_EXP = 0x7FFu; /* 1^11-1 */
112 unsigned char byte = 0;
113 for (uint32_t i = 0;; i++)
118 if (!str_ref.avail ())
120 str_ref.set_error ();
133 str_ref.set_error ();
137 value = (double)(neg? -int_part: int_part);
140 double frac = (frac_part / pow (10.0, (double)frac_count));
141 if (neg) frac = -frac;
144 if (unlikely (exp_overflow))
149 return neg? -DBL_MIN: DBL_MIN;
151 return neg? -DBL_MAX: DBL_MAX;
156 value /= pow (10.0, (double)exp_part);
158 value *= pow (10.0, (double)exp_part);
165 str_ref.set_error ();
172 if (part != INT_PART)
174 str_ref.set_error ();
185 if (part == EXP_PART)
187 str_ref.set_error ();
197 int_part = (int_part * 10) + d;
201 if (likely (frac_part <= MAX_FRACT / 10))
203 frac_part = (frac_part * 10) + (unsigned)d;
209 if (likely (exp_part * 10 + d <= MAX_EXP))
211 exp_part = (exp_part * 10) + d;
223 static bool is_hint_op (op_code_t op)
227 case OpCode_BlueValues:
228 case OpCode_OtherBlues:
229 case OpCode_FamilyBlues:
230 case OpCode_FamilyOtherBlues:
231 case OpCode_StemSnapH:
232 case OpCode_StemSnapV:
235 case OpCode_BlueScale:
236 case OpCode_BlueShift:
237 case OpCode_BlueFuzz:
238 case OpCode_ForceBold:
239 case OpCode_LanguageGroup:
240 case OpCode_ExpansionFactor:
248 template <typename VAL=op_str_t>
249 struct top_dict_opset_t : dict_opset_t
251 static void process_op (op_code_t op, interp_env_t<number_t>& env, top_dict_values_t<VAL> & dictval)
254 case OpCode_CharStrings:
255 dictval.charStringsOffset = env.argStack.pop_uint ();
259 dictval.FDArrayOffset = env.argStack.pop_uint ();
262 case OpCode_FontMatrix:
266 dict_opset_t::process_op (op, env);
272 template <typename OPSET, typename PARAM, typename ENV=num_interp_env_t>
273 struct dict_interpreter_t : interpreter_t<ENV>
275 bool interpret (PARAM& param)
278 while (SUPER::env.str_ref.avail ())
280 OPSET::process_op (SUPER::env.fetch_op (), SUPER::env, param);
281 if (unlikely (SUPER::env.in_error ()))
289 typedef interpreter_t<ENV> SUPER;
292 } /* namespace CFF */
294 #endif /* HB_CFF_INTERP_DICT_COMMON_HH */