1 /* 2 * Copyright © 2018 Adobe Inc. 3 * 4 * This is part of HarfBuzz, a text shaping library. 5 * 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. 11 * 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 16 * DAMAGE. 17 * 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. 23 * 24 * Adobe Author(s): Michiharu Ariza 25 */ 26 #ifndef HB_CFF2_INTERP_CS_HH 27 #define HB_CFF2_INTERP_CS_HH 28 29 #include "hb.hh" 30 #include "hb-cff-interp-cs-common.hh" 31 32 namespace CFF { 33 34 using namespace OT; 35 36 struct blend_arg_t : number_t 37 { set_intCFF::blend_arg_t38 void set_int (int v) { reset_blends (); number_t::set_int (v); } set_fixedCFF::blend_arg_t39 void set_fixed (int32_t v) { reset_blends (); number_t::set_fixed (v); } set_realCFF::blend_arg_t40 void set_real (double v) { reset_blends (); number_t::set_real (v); } 41 set_blendsCFF::blend_arg_t42 void set_blends (unsigned int numValues_, unsigned int valueIndex_, 43 hb_array_t<const blend_arg_t> blends_) 44 { 45 numValues = numValues_; 46 valueIndex = valueIndex_; 47 unsigned numBlends = blends_.length; 48 if (unlikely (!deltas.resize_exact (numBlends))) 49 return; 50 for (unsigned int i = 0; i < numBlends; i++) 51 deltas.arrayZ[i] = blends_.arrayZ[i]; 52 } 53 blendingCFF::blend_arg_t54 bool blending () const { return deltas.length > 0; } reset_blendsCFF::blend_arg_t55 void reset_blends () 56 { 57 numValues = valueIndex = 0; 58 deltas.shrink (0); 59 } 60 61 unsigned int numValues; 62 unsigned int valueIndex; 63 hb_vector_t<number_t> deltas; 64 }; 65 66 typedef biased_subrs_t<CFF2Subrs> cff2_biased_subrs_t; 67 68 template <typename ELEM> 69 struct cff2_cs_interp_env_t : cs_interp_env_t<ELEM, CFF2Subrs> 70 { 71 template <typename ACC> cff2_cs_interp_env_tCFF::cff2_cs_interp_env_t72 cff2_cs_interp_env_t (const hb_ubytes_t &str, ACC &acc, unsigned int fd, 73 const int *coords_=nullptr, unsigned int num_coords_=0) 74 : SUPER (str, acc.globalSubrs, acc.privateDicts[fd].localSubrs) 75 { 76 coords = coords_; 77 num_coords = num_coords_; 78 varStore = acc.varStore; 79 do_blend = num_coords && coords && varStore->size; 80 set_ivs (acc.privateDicts[fd].ivs); 81 } 82 finiCFF::cff2_cs_interp_env_t83 void fini () 84 { 85 SUPER::fini (); 86 } 87 fetch_opCFF::cff2_cs_interp_env_t88 op_code_t fetch_op () 89 { 90 if (this->str_ref.avail ()) 91 return SUPER::fetch_op (); 92 93 /* make up return or endchar op */ 94 if (this->callStack.is_empty ()) 95 return OpCode_endchar; 96 else 97 return OpCode_return; 98 } 99 eval_argCFF::cff2_cs_interp_env_t100 const ELEM& eval_arg (unsigned int i) 101 { 102 return SUPER::argStack[i]; 103 } 104 pop_argCFF::cff2_cs_interp_env_t105 const ELEM& pop_arg () 106 { 107 return SUPER::argStack.pop (); 108 } 109 process_blendCFF::cff2_cs_interp_env_t110 void process_blend () 111 { 112 if (!seen_blend) 113 { 114 region_count = varStore->varStore.get_region_index_count (get_ivs ()); 115 if (do_blend) 116 { 117 if (unlikely (!scalars.resize_exact (region_count))) 118 SUPER::set_error (); 119 else 120 varStore->varStore.get_region_scalars (get_ivs (), coords, num_coords, 121 &scalars[0], region_count); 122 } 123 seen_blend = true; 124 } 125 } 126 process_vsindexCFF::cff2_cs_interp_env_t127 void process_vsindex () 128 { 129 unsigned int index = SUPER::argStack.pop_uint (); 130 if (unlikely (seen_vsindex () || seen_blend)) 131 { 132 SUPER::set_error (); 133 } 134 else 135 { 136 set_ivs (index); 137 } 138 seen_vsindex_ = true; 139 } 140 get_region_countCFF::cff2_cs_interp_env_t141 unsigned int get_region_count () const { return region_count; } set_region_countCFF::cff2_cs_interp_env_t142 void set_region_count (unsigned int region_count_) { region_count = region_count_; } get_ivsCFF::cff2_cs_interp_env_t143 unsigned int get_ivs () const { return ivs; } set_ivsCFF::cff2_cs_interp_env_t144 void set_ivs (unsigned int ivs_) { ivs = ivs_; } seen_vsindexCFF::cff2_cs_interp_env_t145 bool seen_vsindex () const { return seen_vsindex_; } 146 blend_deltasCFF::cff2_cs_interp_env_t147 double blend_deltas (hb_array_t<const ELEM> deltas) const 148 { 149 double v = 0; 150 if (do_blend) 151 { 152 if (likely (scalars.length == deltas.length)) 153 { 154 unsigned count = scalars.length; 155 for (unsigned i = 0; i < count; i++) 156 v += (double) scalars.arrayZ[i] * deltas.arrayZ[i].to_real (); 157 } 158 } 159 return v; 160 } 161 have_coordsCFF::cff2_cs_interp_env_t162 bool have_coords () const { return num_coords; } 163 164 protected: 165 const int *coords; 166 unsigned int num_coords; 167 const CFF2ItemVariationStore *varStore; 168 unsigned int region_count; 169 unsigned int ivs; 170 hb_vector_t<float> scalars; 171 bool do_blend; 172 bool seen_vsindex_ = false; 173 bool seen_blend = false; 174 175 typedef cs_interp_env_t<ELEM, CFF2Subrs> SUPER; 176 }; 177 template <typename OPSET, typename PARAM, typename ELEM, typename PATH=path_procs_null_t<cff2_cs_interp_env_t<ELEM>, PARAM>> 178 struct cff2_cs_opset_t : cs_opset_t<ELEM, OPSET, cff2_cs_interp_env_t<ELEM>, PARAM, PATH> 179 { process_opCFF::cff2_cs_opset_t180 static void process_op (op_code_t op, cff2_cs_interp_env_t<ELEM> &env, PARAM& param) 181 { 182 switch (op) { 183 case OpCode_callsubr: 184 case OpCode_callgsubr: 185 /* a subroutine number shouldn't be a blended value */ 186 #if 0 187 if (unlikely (env.argStack.peek ().blending ())) 188 { 189 env.set_error (); 190 break; 191 } 192 #endif 193 SUPER::process_op (op, env, param); 194 break; 195 196 case OpCode_blendcs: 197 OPSET::process_blend (env, param); 198 break; 199 200 case OpCode_vsindexcs: 201 #if 0 202 if (unlikely (env.argStack.peek ().blending ())) 203 { 204 env.set_error (); 205 break; 206 } 207 #endif 208 OPSET::process_vsindex (env, param); 209 break; 210 211 default: 212 SUPER::process_op (op, env, param); 213 } 214 } 215 216 template <typename T = ELEM, 217 hb_enable_if (hb_is_same (T, blend_arg_t))> process_arg_blendCFF::cff2_cs_opset_t218 static void process_arg_blend (cff2_cs_interp_env_t<ELEM> &env, 219 ELEM &arg, 220 const hb_array_t<const ELEM> blends, 221 unsigned n, unsigned i) 222 { 223 if (env.have_coords ()) 224 arg.set_int (round (arg.to_real () + env.blend_deltas (blends))); 225 else 226 arg.set_blends (n, i, blends); 227 } 228 template <typename T = ELEM, 229 hb_enable_if (!hb_is_same (T, blend_arg_t))> process_arg_blendCFF::cff2_cs_opset_t230 static void process_arg_blend (cff2_cs_interp_env_t<ELEM> &env, 231 ELEM &arg, 232 const hb_array_t<const ELEM> blends, 233 unsigned n, unsigned i) 234 { 235 arg.set_real (arg.to_real () + env.blend_deltas (blends)); 236 } 237 process_blendCFF::cff2_cs_opset_t238 static void process_blend (cff2_cs_interp_env_t<ELEM> &env, PARAM& param) 239 { 240 unsigned int n, k; 241 242 env.process_blend (); 243 k = env.get_region_count (); 244 n = env.argStack.pop_uint (); 245 /* copy the blend values into blend array of the default values */ 246 unsigned int start = env.argStack.get_count () - ((k+1) * n); 247 /* let an obvious error case fail, but note CFF2 spec doesn't forbid n==0 */ 248 if (unlikely (start > env.argStack.get_count ())) 249 { 250 env.set_error (); 251 return; 252 } 253 for (unsigned int i = 0; i < n; i++) 254 { 255 const hb_array_t<const ELEM> blends = env.argStack.sub_array (start + n + (i * k), k); 256 process_arg_blend (env, env.argStack[start + i], blends, n, i); 257 } 258 259 /* pop off blend values leaving default values now adorned with blend values */ 260 env.argStack.pop (k * n); 261 } 262 process_vsindexCFF::cff2_cs_opset_t263 static void process_vsindex (cff2_cs_interp_env_t<ELEM> &env, PARAM& param) 264 { 265 env.process_vsindex (); 266 env.clear_args (); 267 } 268 269 private: 270 typedef cs_opset_t<ELEM, OPSET, cff2_cs_interp_env_t<ELEM>, PARAM, PATH> SUPER; 271 }; 272 273 template <typename OPSET, typename PARAM, typename ELEM> 274 using cff2_cs_interpreter_t = cs_interpreter_t<cff2_cs_interp_env_t<ELEM>, OPSET, PARAM>; 275 276 } /* namespace CFF */ 277 278 #endif /* HB_CFF2_INTERP_CS_HH */ 279