blob: 6bcb145bc72d7c402eb953dbd67afae7d3d95a3f [file] [log] [blame]
Michiharu Ariza633ce882018-08-15 12:00:19 -07001/*
Michiharu Ariza0dfa5842018-11-12 08:47:07 -08002 * Copyright © 2018 Adobe Inc.
Michiharu Ariza633ce882018-08-15 12:00:19 -07003 *
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 */
Michiharu Ariza1d1afdd2018-08-29 13:36:39 -070026#ifndef HB_CFF_INTERP_COMMON_HH
27#define HB_CFF_INTERP_COMMON_HH
Michiharu Ariza633ce882018-08-15 12:00:19 -070028
Michiharu Ariza633ce882018-08-15 12:00:19 -070029namespace CFF {
30
31using namespace OT;
32
Michiharu Arizaeed7e6b2018-10-02 13:45:14 -070033typedef unsigned int OpCode;
34/* === Dict operators === */
Michiharu Ariza633ce882018-08-15 12:00:19 -070035
Michiharu Arizaeed7e6b2018-10-02 13:45:14 -070036/* One byte operators (0-31) */
37#define OpCode_version 0 /* CFF Top */
38#define OpCode_Notice 1 /* CFF Top */
39#define OpCode_FullName 2 /* CFF Top */
40#define OpCode_FamilyName 3 /* CFF Top */
41#define OpCode_Weight 4 /* CFF Top */
42#define OpCode_FontBBox 5 /* CFF Top */
43#define OpCode_BlueValues 6 /* CFF Private, CFF2 Private */
44#define OpCode_OtherBlues 7 /* CFF Private, CFF2 Private */
45#define OpCode_FamilyBlues 8 /* CFF Private, CFF2 Private */
46#define OpCode_FamilyOtherBlues 9 /* CFF Private, CFF2 Private */
47#define OpCode_StdHW 10 /* CFF Private, CFF2 Private */
48#define OpCode_StdVW 11 /* CFF Private, CFF2 Private */
49#define OpCode_escape 12 /* All. Shared with CS */
50#define OpCode_UniqueID 13 /* CFF Top */
51#define OpCode_XUID 14 /* CFF Top */
52#define OpCode_charset 15 /* CFF Top (0) */
53#define OpCode_Encoding 16 /* CFF Top (0) */
54#define OpCode_CharStrings 17 /* CFF Top, CFF2 Top */
55#define OpCode_Private 18 /* CFF Top, CFF2 FD */
56#define OpCode_Subrs 19 /* CFF Private, CFF2 Private */
57#define OpCode_defaultWidthX 20 /* CFF Private (0) */
58#define OpCode_nominalWidthX 21 /* CFF Private (0) */
59#define OpCode_vsindexdict 22 /* CFF2 Private/CS */
60#define OpCode_blenddict 23 /* CFF2 Private/CS */
61#define OpCode_vstore 24 /* CFF2 Top */
62#define OpCode_reserved25 25
63#define OpCode_reserved26 26
64#define OpCode_reserved27 27
Michiharu Ariza633ce882018-08-15 12:00:19 -070065
Michiharu Arizaeed7e6b2018-10-02 13:45:14 -070066/* Numbers */
67#define OpCode_shortint 28 /* 16-bit integer, All */
68#define OpCode_longintdict 29 /* 32-bit integer, All */
69#define OpCode_BCD 30 /* real number, CFF2 Top/FD */
70#define OpCode_reserved31 31
Michiharu Ariza633ce882018-08-15 12:00:19 -070071
Michiharu Arizaeed7e6b2018-10-02 13:45:14 -070072/* 1-byte integers */
73#define OpCode_OneByteIntFirst 32 /* All. beginning of the range of first byte ints */
74#define OpCode_OneByteIntLast 246 /* All. ending of the range of first byte int */
Michiharu Ariza633ce882018-08-15 12:00:19 -070075
Michiharu Arizaeed7e6b2018-10-02 13:45:14 -070076/* 2-byte integers */
77#define OpCode_TwoBytePosInt0 247 /* All. first byte of two byte positive int (+108 to +1131) */
78#define OpCode_TwoBytePosInt1 248
79#define OpCode_TwoBytePosInt2 249
80#define OpCode_TwoBytePosInt3 250
Michiharu Ariza633ce882018-08-15 12:00:19 -070081
Michiharu Arizaeed7e6b2018-10-02 13:45:14 -070082#define OpCode_TwoByteNegInt0 251 /* All. first byte of two byte negative int (-1131 to -108) */
83#define OpCode_TwoByteNegInt1 252
84#define OpCode_TwoByteNegInt2 253
85#define OpCode_TwoByteNegInt3 254
Michiharu Ariza633ce882018-08-15 12:00:19 -070086
Michiharu Arizaeed7e6b2018-10-02 13:45:14 -070087/* Two byte escape operators 12, (0-41) */
88#define OpCode_ESC_Base 256
89#define Make_OpCode_ESC(byte2) ((OpCode)(OpCode_ESC_Base + (byte2)))
Michiharu Ariza633ce882018-08-15 12:00:19 -070090
Michiharu Arizaa11420b2018-08-29 12:14:30 -070091inline OpCode Unmake_OpCode_ESC (OpCode op) { return (OpCode)(op - OpCode_ESC_Base); }
92inline bool Is_OpCode_ESC (OpCode op) { return op >= OpCode_ESC_Base; }
93inline unsigned int OpCode_Size (OpCode op) { return Is_OpCode_ESC (op)? 2: 1; }
Michiharu Ariza633ce882018-08-15 12:00:19 -070094
Michiharu Arizaeed7e6b2018-10-02 13:45:14 -070095#define OpCode_Copyright Make_OpCode_ESC(0) /* CFF Top */
96#define OpCode_isFixedPitch Make_OpCode_ESC(1) /* CFF Top (false) */
97#define OpCode_ItalicAngle Make_OpCode_ESC(2) /* CFF Top (0) */
98#define OpCode_UnderlinePosition Make_OpCode_ESC(3) /* CFF Top (-100) */
99#define OpCode_UnderlineThickness Make_OpCode_ESC(4) /* CFF Top (50) */
100#define OpCode_PaintType Make_OpCode_ESC(5) /* CFF Top (0) */
101#define OpCode_CharstringType Make_OpCode_ESC(6) /* CFF Top (2) */
102#define OpCode_FontMatrix Make_OpCode_ESC(7) /* CFF Top, CFF2 Top (.001 0 0 .001 0 0)*/
103#define OpCode_StrokeWidth Make_OpCode_ESC(8) /* CFF Top (0) */
104#define OpCode_BlueScale Make_OpCode_ESC(9) /* CFF Private, CFF2 Private (0.039625) */
105#define OpCode_BlueShift Make_OpCode_ESC(10) /* CFF Private, CFF2 Private (7) */
106#define OpCode_BlueFuzz Make_OpCode_ESC(11) /* CFF Private, CFF2 Private (1) */
107#define OpCode_StemSnapH Make_OpCode_ESC(12) /* CFF Private, CFF2 Private */
108#define OpCode_StemSnapV Make_OpCode_ESC(13) /* CFF Private, CFF2 Private */
109#define OpCode_ForceBold Make_OpCode_ESC(14) /* CFF Private (false) */
110#define OpCode_reservedESC15 Make_OpCode_ESC(15)
111#define OpCode_reservedESC16 Make_OpCode_ESC(16)
112#define OpCode_LanguageGroup Make_OpCode_ESC(17) /* CFF Private, CFF2 Private (0) */
113#define OpCode_ExpansionFactor Make_OpCode_ESC(18) /* CFF Private, CFF2 Private (0.06) */
114#define OpCode_initialRandomSeed Make_OpCode_ESC(19) /* CFF Private (0) */
115#define OpCode_SyntheticBase Make_OpCode_ESC(20) /* CFF Top */
116#define OpCode_PostScript Make_OpCode_ESC(21) /* CFF Top */
117#define OpCode_BaseFontName Make_OpCode_ESC(22) /* CFF Top */
118#define OpCode_BaseFontBlend Make_OpCode_ESC(23) /* CFF Top */
119#define OpCode_reservedESC24 Make_OpCode_ESC(24)
120#define OpCode_reservedESC25 Make_OpCode_ESC(25)
121#define OpCode_reservedESC26 Make_OpCode_ESC(26)
122#define OpCode_reservedESC27 Make_OpCode_ESC(27)
123#define OpCode_reservedESC28 Make_OpCode_ESC(28)
124#define OpCode_reservedESC29 Make_OpCode_ESC(29)
125#define OpCode_ROS Make_OpCode_ESC(30) /* CFF Top_CID */
126#define OpCode_CIDFontVersion Make_OpCode_ESC(31) /* CFF Top_CID (0) */
127#define OpCode_CIDFontRevision Make_OpCode_ESC(32) /* CFF Top_CID (0) */
128#define OpCode_CIDFontType Make_OpCode_ESC(33) /* CFF Top_CID (0) */
129#define OpCode_CIDCount Make_OpCode_ESC(34) /* CFF Top_CID (8720) */
130#define OpCode_UIDBase Make_OpCode_ESC(35) /* CFF Top_CID */
131#define OpCode_FDArray Make_OpCode_ESC(36) /* CFF Top_CID, CFF2 Top */
132#define OpCode_FDSelect Make_OpCode_ESC(37) /* CFF Top_CID, CFF2 Top */
133#define OpCode_FontName Make_OpCode_ESC(38) /* CFF Top_CID */
134
135 /* === CharString operators === */
136
137#define OpCode_hstem 1 /* CFF, CFF2 */
138#define OpCode_Reserved2 2
139#define OpCode_vstem 3 /* CFF, CFF2 */
140#define OpCode_vmoveto 4 /* CFF, CFF2 */
141#define OpCode_rlineto 5 /* CFF, CFF2 */
142#define OpCode_hlineto 6 /* CFF, CFF2 */
143#define OpCode_vlineto 7 /* CFF, CFF2 */
144#define OpCode_rrcurveto 8 /* CFF, CFF2 */
145#define OpCode_Reserved9 9
146#define OpCode_callsubr 10 /* CFF, CFF2 */
147#define OpCode_return 11 /* CFF */
148// #define OpCode_escape 12 /* CFF, CFF2 */
149#define OpCode_Reserved13 13
150#define OpCode_endchar 14 /* CFF */
151#define OpCode_vsindexcs 15 /* CFF2 */
152#define OpCode_blendcs 16 /* CFF2 */
153#define OpCode_Reserved17 17
154#define OpCode_hstemhm 18 /* CFF, CFF2 */
155#define OpCode_hintmask 19 /* CFF, CFF2 */
156#define OpCode_cntrmask 20 /* CFF, CFF2 */
157#define OpCode_rmoveto 21 /* CFF, CFF2 */
158#define OpCode_hmoveto 22 /* CFF, CFF2 */
159#define OpCode_vstemhm 23 /* CFF, CFF2 */
160#define OpCode_rcurveline 24 /* CFF, CFF2 */
161#define OpCode_rlinecurve 25 /* CFF, CFF2 */
162#define OpCode_vvcurveto 26 /* CFF, CFF2 */
163#define OpCode_hhcurveto 27 /* CFF, CFF2 */
164// #define OpCode_shortint 28 /* CFF, CFF2 */
165#define OpCode_callgsubr 29 /* CFF, CFF2 */
166#define OpCode_vhcurveto 30 /* CFF, CFF2 */
167#define OpCode_hvcurveto 31 /* CFF, CFF2 */
168
169#define OpCode_fixedcs 255 /* 32-bit fixed */
170
171/* Two byte escape operators 12, (0-41) */
Michiharu Ariza3787c072018-11-14 13:38:03 -0800172#define OpCode_dotsection Make_OpCode_ESC(0) /* CFF (obsoleted) */
Michiharu Arizaeed7e6b2018-10-02 13:45:14 -0700173#define OpCode_ReservedESC1 Make_OpCode_ESC(1)
174#define OpCode_ReservedESC2 Make_OpCode_ESC(2)
175#define OpCode_and Make_OpCode_ESC(3) /* CFF */
176#define OpCode_or Make_OpCode_ESC(4) /* CFF */
177#define OpCode_not Make_OpCode_ESC(5) /* CFF */
178#define OpCode_ReservedESC6 Make_OpCode_ESC(6)
179#define OpCode_ReservedESC7 Make_OpCode_ESC(7)
180#define OpCode_ReservedESC8 Make_OpCode_ESC(8)
181#define OpCode_abs Make_OpCode_ESC(9) /* CFF */
182#define OpCode_add Make_OpCode_ESC(10) /* CFF */
183#define OpCode_sub Make_OpCode_ESC(11) /* CFF */
184#define OpCode_div Make_OpCode_ESC(12) /* CFF */
185#define OpCode_ReservedESC13 Make_OpCode_ESC(13)
186#define OpCode_neg Make_OpCode_ESC(14) /* CFF */
187#define OpCode_eq Make_OpCode_ESC(15) /* CFF */
188#define OpCode_ReservedESC16 Make_OpCode_ESC(16)
189#define OpCode_ReservedESC17 Make_OpCode_ESC(17)
190#define OpCode_drop Make_OpCode_ESC(18) /* CFF */
191#define OpCode_ReservedESC19 Make_OpCode_ESC(19)
192#define OpCode_put Make_OpCode_ESC(20) /* CFF */
193#define OpCode_get Make_OpCode_ESC(21) /* CFF */
194#define OpCode_ifelse Make_OpCode_ESC(22) /* CFF */
195#define OpCode_random Make_OpCode_ESC(23) /* CFF */
196#define OpCode_mul Make_OpCode_ESC(24) /* CFF */
197// #define OpCode_reservedESC25 Make_OpCode_ESC(25)
198#define OpCode_sqrt Make_OpCode_ESC(26) /* CFF */
199#define OpCode_dup Make_OpCode_ESC(27) /* CFF */
200#define OpCode_exch Make_OpCode_ESC(28) /* CFF */
201#define OpCode_index Make_OpCode_ESC(29) /* CFF */
202#define OpCode_roll Make_OpCode_ESC(30) /* CFF */
203#define OpCode_reservedESC31 Make_OpCode_ESC(31)
204#define OpCode_reservedESC32 Make_OpCode_ESC(32)
205#define OpCode_reservedESC33 Make_OpCode_ESC(33)
206#define OpCode_hflex Make_OpCode_ESC(34) /* CFF, CFF2 */
207#define OpCode_flex Make_OpCode_ESC(35) /* CFF, CFF2 */
208#define OpCode_hflex1 Make_OpCode_ESC(36) /* CFF, CFF2 */
209#define OpCode_flex1 Make_OpCode_ESC(37) /* CFF, CFF2 */
210
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700211#define OpCode_Invalid 65535
212
Michiharu Ariza633ce882018-08-15 12:00:19 -0700213struct Number
214{
Michiharu Arizaf2d299b2018-09-04 10:25:21 -0700215 inline void init (void)
216 { set_int (0); }
217 inline void fini (void)
218 {}
Michiharu Ariza633ce882018-08-15 12:00:19 -0700219
Michiharu Arizab1d0c582018-11-01 16:33:46 -0700220 inline void set_int (int v) { format = NumInt; u.int_val = v; }
Michiharu Arizaa11420b2018-08-29 12:14:30 -0700221 inline int to_int (void) const { return is_int ()? u.int_val: (int)to_real (); }
Michiharu Arizab1d0c582018-11-01 16:33:46 -0700222 inline void set_fixed (int32_t v) { format = NumFixed; u.fixed_val = v; }
Michiharu Arizaa11420b2018-08-29 12:14:30 -0700223 inline int32_t to_fixed (void) const
224 {
225 if (is_fixed ())
226 return u.fixed_val;
227 else if (is_real ())
Michiharu Ariza0222d572018-10-04 09:37:57 -0700228 return (int32_t)(u.real_val * 65536.0f);
Michiharu Arizaa11420b2018-08-29 12:14:30 -0700229 else
230 return (int32_t)(u.int_val << 16);
231 }
Michiharu Arizab1d0c582018-11-01 16:33:46 -0700232 inline void set_real (float v) { format = NumReal; u.real_val = v; }
Michiharu Arizaa11420b2018-08-29 12:14:30 -0700233 inline float to_real (void) const
234 {
235 if (is_real ())
236 return u.real_val;
237 if (is_fixed ())
Michiharu Ariza0222d572018-10-04 09:37:57 -0700238 return u.fixed_val / 65536.0f;
Michiharu Arizaa11420b2018-08-29 12:14:30 -0700239 else
240 return (float)u.int_val;
241 }
Michiharu Arizac0c85b82018-09-17 11:14:56 -0700242
243 inline int ceil (void) const
244 {
245 switch (format)
246 {
247 default:
248 case NumInt:
249 return u.int_val;
250 case NumFixed:
251 return (u.fixed_val + 0xFFFF) >> 16;
252 case NumReal:
253 return (int)ceilf (u.real_val);
254 }
255 }
256
257 inline int floor (void) const
258 {
259 switch (format)
260 {
261 default:
262 case NumInt:
263 return u.int_val;
264 case NumFixed:
265 return u.fixed_val >> 16;
266 case NumReal:
267 return (int)floorf (u.real_val);
268 }
269 }
270
Michiharu Arizaa11420b2018-08-29 12:14:30 -0700271 inline bool in_int_range (void) const
272 {
273 if (is_int ())
274 return true;
275 if (is_fixed () && ((u.fixed_val & 0xFFFF) == 0))
276 return true;
277 else
278 return ((float)(int16_t)to_int () == u.real_val);
279 }
Michiharu Ariza633ce882018-08-15 12:00:19 -0700280
Michiharu Arizac0c85b82018-09-17 11:14:56 -0700281 inline bool operator > (const Number &n) const
282 {
283 switch (format)
284 {
285 default:
286 case NumInt: return u.int_val > n.to_int ();
287 case NumFixed: return u.fixed_val > n.to_fixed ();
288 case NumReal: return u.real_val > n.to_real ();
289 }
290 }
291
292 inline bool operator < (const Number &n) const
293 { return n > *this; }
294
295 inline bool operator >= (const Number &n) const
296 { return ! (*this < n); }
297
298 inline bool operator <= (const Number &n) const
299 { return ! (*this > n); }
300
301 inline const Number &operator += (const Number &n)
302 {
Michiharu Arizab51418f2018-10-08 15:05:36 -0700303 if (format == NumReal || n.format == NumReal)
304 set_real (to_real () + n.to_real ());
305 else if (format == NumFixed || n.format == NumFixed)
306 set_fixed (to_fixed () + n.to_fixed ());
307 else
308 set_int (to_int () + n.to_int ());
309
Michiharu Arizac0c85b82018-09-17 11:14:56 -0700310 return *this;
311 }
312
Michiharu Ariza633ce882018-08-15 12:00:19 -0700313protected:
Michiharu Arizaa11420b2018-08-29 12:14:30 -0700314 enum NumFormat {
315 NumInt,
316 NumFixed,
317 NumReal
318 };
319 NumFormat format;
Michiharu Ariza633ce882018-08-15 12:00:19 -0700320 union {
321 int int_val;
Michiharu Arizaa11420b2018-08-29 12:14:30 -0700322 int32_t fixed_val;
Michiharu Ariza633ce882018-08-15 12:00:19 -0700323 float real_val;
324 } u;
Michiharu Arizaa11420b2018-08-29 12:14:30 -0700325
326 inline bool is_int (void) const { return format == NumInt; }
327 inline bool is_fixed (void) const { return format == NumFixed; }
328 inline bool is_real (void) const { return format == NumReal; }
Michiharu Ariza633ce882018-08-15 12:00:19 -0700329};
330
331/* byte string */
332struct UnsizedByteStr : UnsizedArrayOf <HBUINT8>
333{
334 // encode 2-byte int (Dict/CharString) or 4-byte int (Dict)
335 template <typename INTTYPE, int minVal, int maxVal>
336 inline static bool serialize_int (hb_serialize_context_t *c, OpCode intOp, int value)
337 {
338 TRACE_SERIALIZE (this);
339
340 if (unlikely ((value < minVal || value > maxVal)))
341 return_trace (false);
342
343 HBUINT8 *p = c->allocate_size<HBUINT8> (1);
344 if (unlikely (p == nullptr)) return_trace (false);
345 p->set (intOp);
346
347 INTTYPE *ip = c->allocate_size<INTTYPE> (INTTYPE::static_size);
348 if (unlikely (ip == nullptr)) return_trace (false);
349 ip->set ((unsigned int)value);
350
351 return_trace (true);
352 }
Behdad Esfahbod592f39b2018-11-30 22:54:57 -0500353
Michiharu Ariza633ce882018-08-15 12:00:19 -0700354 inline static bool serialize_int4 (hb_serialize_context_t *c, int value)
355 { return serialize_int<HBUINT32, 0, 0x7FFFFFFF> (c, OpCode_longintdict, value); }
Behdad Esfahbod592f39b2018-11-30 22:54:57 -0500356
Michiharu Ariza633ce882018-08-15 12:00:19 -0700357 inline static bool serialize_int2 (hb_serialize_context_t *c, int value)
358 { return serialize_int<HBUINT16, 0, 0x7FFF> (c, OpCode_shortint, value); }
Michiharu Ariza471db3a2018-11-28 17:06:11 -0800359
360 /* Defining null_size allows a Null object may be created. Should be safe because:
361 * A descendent struct Dict uses a Null pointer to indicate a missing table,
362 * checked before access.
363 * ByteStr, a wrapper struct pairing a byte pointer along with its length, always
364 * checks the length before access. A Null pointer is used as the initial pointer
365 * along with zero length by the default ctor.
366 */
367 DEFINE_SIZE_MIN(0);
Michiharu Ariza633ce882018-08-15 12:00:19 -0700368};
369
370struct ByteStr
371{
372 inline ByteStr (void)
373 : str (&Null(UnsizedByteStr)), len (0) {}
374 inline ByteStr (const UnsizedByteStr& s, unsigned int l)
375 : str (&s), len (l) {}
376 inline ByteStr (const char *s, unsigned int l=0)
377 : str ((const UnsizedByteStr *)s), len (l) {}
378 /* sub-string */
379 inline ByteStr (const ByteStr &bs, unsigned int offset, unsigned int len_)
380 {
381 str = (const UnsizedByteStr *)&bs.str[offset];
382 len = len_;
383 }
384
385 inline bool sanitize (hb_sanitize_context_t *c) const { return str->sanitize (c, len); }
386
387 inline const HBUINT8& operator [] (unsigned int i) const {
388 assert (str && (i < len));
389 return (*str)[i];
390 }
391
392 inline bool serialize (hb_serialize_context_t *c, const ByteStr &src)
393 {
394 TRACE_SERIALIZE (this);
395 HBUINT8 *dest = c->allocate_size<HBUINT8> (src.len);
396 if (unlikely (dest == nullptr))
397 return_trace (false);
398 memcpy (dest, src.str, src.len);
399 return_trace (true);
400 }
401
402 inline unsigned int get_size (void) const { return len; }
403
404 inline bool check_limit (unsigned int offset, unsigned int count) const
405 { return (offset + count <= len); }
406
407 const UnsizedByteStr *str;
408 unsigned int len;
409};
410
411struct SubByteStr
412{
413 inline SubByteStr (void)
Michiharu Arizaf2d299b2018-09-04 10:25:21 -0700414 { init (); }
415
416 inline void init (void)
417 {
418 str = ByteStr (0);
419 offset = 0;
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700420 error = false;
Michiharu Arizaf2d299b2018-09-04 10:25:21 -0700421 }
422
423 inline void fini (void) {}
Michiharu Ariza633ce882018-08-15 12:00:19 -0700424
425 inline SubByteStr (const ByteStr &str_, unsigned int offset_ = 0)
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700426 : str (str_), offset (offset_), error (false) {}
Michiharu Ariza633ce882018-08-15 12:00:19 -0700427
428 inline void reset (const ByteStr &str_, unsigned int offset_ = 0)
429 {
430 str = str_;
431 offset = offset_;
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700432 error = false;
Michiharu Ariza633ce882018-08-15 12:00:19 -0700433 }
434
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700435 inline const HBUINT8& operator [] (int i) {
436 if (unlikely ((unsigned int)(offset + i) >= str.len))
437 {
438 set_error ();
439 return Null(HBUINT8);
440 }
441 else
442 return str[offset + i];
Michiharu Ariza633ce882018-08-15 12:00:19 -0700443 }
444
445 inline operator ByteStr (void) const { return ByteStr (str, offset, str.len - offset); }
446
447 inline bool avail (unsigned int count=1) const { return str.check_limit (offset, count); }
448 inline void inc (unsigned int count=1) { offset += count; assert (count <= str.len); }
449
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700450 inline void set_error (void) { error = true; }
451 inline bool in_error (void) const { return error; }
452
Michiharu Ariza633ce882018-08-15 12:00:19 -0700453 ByteStr str;
454 unsigned int offset; /* beginning of the sub-string within str */
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700455
456 protected:
457 bool error;
Michiharu Ariza633ce882018-08-15 12:00:19 -0700458};
459
Michiharu Arizad56e3382018-10-31 22:30:34 -0700460typedef hb_vector_t<ByteStr> ByteStrArray;
461
Michiharu Ariza633ce882018-08-15 12:00:19 -0700462/* stack */
463template <typename ELEM, int LIMIT>
464struct Stack
465{
Michiharu Arizaf2d299b2018-09-04 10:25:21 -0700466 inline void init (void)
467 {
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700468 error = false;
Michiharu Arizaf2d299b2018-09-04 10:25:21 -0700469 count = 0;
470 elements.init ();
471 elements.resize (kSizeLimit);
472 for (unsigned int i = 0; i < elements.len; i++)
473 elements[i].init ();
474 }
475
476 inline void fini (void)
477 {
Michiharu Arizab51418f2018-10-08 15:05:36 -0700478 elements.fini_deep ();
Michiharu Arizaf2d299b2018-09-04 10:25:21 -0700479 }
Michiharu Ariza633ce882018-08-15 12:00:19 -0700480
Michiharu Arizac0c85b82018-09-17 11:14:56 -0700481 inline ELEM& operator [] (unsigned int i)
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700482 {
483 if (unlikely (i >= count)) set_error ();
484 return elements[i];
485 }
Michiharu Arizac0c85b82018-09-17 11:14:56 -0700486
Michiharu Ariza633ce882018-08-15 12:00:19 -0700487 inline void push (const ELEM &v)
488 {
Michiharu Arizaf2d299b2018-09-04 10:25:21 -0700489 if (likely (count < elements.len))
Michiharu Ariza968168b2018-08-31 13:28:16 -0700490 elements[count++] = v;
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700491 else
492 set_error ();
Michiharu Ariza633ce882018-08-15 12:00:19 -0700493 }
494
Michiharu Arizaf2d299b2018-09-04 10:25:21 -0700495 inline ELEM &push (void)
496 {
497 if (likely (count < elements.len))
498 return elements[count++];
499 else
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700500 {
501 set_error ();
Michiharu Arizaf2d299b2018-09-04 10:25:21 -0700502 return Crap(ELEM);
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700503 }
Michiharu Arizaf2d299b2018-09-04 10:25:21 -0700504 }
505
Michiharu Arizab51418f2018-10-08 15:05:36 -0700506 inline ELEM& pop (void)
Michiharu Ariza633ce882018-08-15 12:00:19 -0700507 {
Michiharu Ariza968168b2018-08-31 13:28:16 -0700508 if (likely (count > 0))
509 return elements[--count];
Michiharu Ariza633ce882018-08-15 12:00:19 -0700510 else
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700511 {
512 set_error ();
Michiharu Arizab51418f2018-10-08 15:05:36 -0700513 return Crap(ELEM);
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700514 }
Michiharu Ariza633ce882018-08-15 12:00:19 -0700515 }
516
Michiharu Arizac0c85b82018-09-17 11:14:56 -0700517 inline void pop (unsigned int n)
518 {
519 if (likely (count >= n))
520 count -= n;
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700521 else
522 set_error ();
Michiharu Arizac0c85b82018-09-17 11:14:56 -0700523 }
524
Michiharu Arizaf2d299b2018-09-04 10:25:21 -0700525 inline const ELEM& peek (void)
526 {
527 if (likely (count > 0))
Michiharu Ariza76e54d22018-09-21 12:16:36 -0700528 return elements[count-1];
Michiharu Arizaf2d299b2018-09-04 10:25:21 -0700529 else
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700530 {
531 set_error ();
Michiharu Arizaf2d299b2018-09-04 10:25:21 -0700532 return Null(ELEM);
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700533 }
Michiharu Arizaf2d299b2018-09-04 10:25:21 -0700534 }
535
Michiharu Ariza633ce882018-08-15 12:00:19 -0700536 inline void unpop (void)
537 {
Michiharu Arizaf2d299b2018-09-04 10:25:21 -0700538 if (likely (count < elements.len))
Michiharu Ariza968168b2018-08-31 13:28:16 -0700539 count++;
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700540 else
541 set_error ();
Michiharu Ariza633ce882018-08-15 12:00:19 -0700542 }
543
Michiharu Ariza968168b2018-08-31 13:28:16 -0700544 inline void clear (void) { count = 0; }
Michiharu Ariza633ce882018-08-15 12:00:19 -0700545
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700546 inline bool in_error (void) const { return (error || elements.in_error ()); }
547 inline void set_error (void) { error = true; }
Michiharu Ariza633ce882018-08-15 12:00:19 -0700548
Michiharu Ariza968168b2018-08-31 13:28:16 -0700549 inline unsigned int get_count (void) const { return count; }
550 inline bool is_empty (void) const { return count == 0; }
Michiharu Ariza633ce882018-08-15 12:00:19 -0700551
552 static const unsigned int kSizeLimit = LIMIT;
553
Michiharu Arizac0c85b82018-09-17 11:14:56 -0700554 protected:
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700555 bool error;
Michiharu Ariza968168b2018-08-31 13:28:16 -0700556 unsigned int count;
Michiharu Arizaf2d299b2018-09-04 10:25:21 -0700557 hb_vector_t<ELEM, kSizeLimit> elements;
Michiharu Ariza633ce882018-08-15 12:00:19 -0700558};
559
560/* argument stack */
Michiharu Arizafcf17782018-08-31 16:28:47 -0700561template <typename ARG=Number>
562struct ArgStack : Stack<ARG, 513>
Michiharu Ariza633ce882018-08-15 12:00:19 -0700563{
564 inline void push_int (int v)
565 {
Michiharu Arizaf2d299b2018-09-04 10:25:21 -0700566 ARG &n = S::push ();
Michiharu Ariza633ce882018-08-15 12:00:19 -0700567 n.set_int (v);
Michiharu Arizaf2d299b2018-09-04 10:25:21 -0700568 }
569
570 inline void push_fixed (int32_t v)
571 {
572 ARG &n = S::push ();
573 n.set_fixed (v);
Michiharu Ariza633ce882018-08-15 12:00:19 -0700574 }
575
576 inline void push_real (float v)
577 {
Michiharu Arizaf2d299b2018-09-04 10:25:21 -0700578 ARG &n = S::push ();
Michiharu Ariza633ce882018-08-15 12:00:19 -0700579 n.set_real (v);
Michiharu Ariza633ce882018-08-15 12:00:19 -0700580 }
581
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700582 inline ARG& pop_num (void)
Michiharu Ariza633ce882018-08-15 12:00:19 -0700583 {
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700584 return this->pop ();
Michiharu Ariza633ce882018-08-15 12:00:19 -0700585 }
586
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700587 inline int pop_int (void)
Michiharu Ariza633ce882018-08-15 12:00:19 -0700588 {
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700589 return this->pop ().to_int ();
Michiharu Ariza633ce882018-08-15 12:00:19 -0700590 }
591
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700592 inline unsigned int pop_uint (void)
Michiharu Ariza633ce882018-08-15 12:00:19 -0700593 {
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700594 int i = pop_int ();
595 if (unlikely (i < 0))
596 {
597 i = 0;
598 S::set_error ();
599 }
600 return (unsigned)i;
Michiharu Ariza633ce882018-08-15 12:00:19 -0700601 }
602
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700603 inline void push_longint_from_substr (SubByteStr& substr)
Michiharu Ariza633ce882018-08-15 12:00:19 -0700604 {
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700605 push_int ((substr[0] << 24) | (substr[1] << 16) | (substr[2] << 8) | (substr[3]));
Michiharu Ariza633ce882018-08-15 12:00:19 -0700606 substr.inc (4);
Michiharu Ariza633ce882018-08-15 12:00:19 -0700607 }
608
Michiharu Ariza27c32d82018-08-22 11:36:39 -0700609 inline bool push_fixed_from_substr (SubByteStr& substr)
610 {
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700611 if (unlikely (!substr.avail (4)))
Michiharu Ariza27c32d82018-08-22 11:36:39 -0700612 return false;
Michiharu Arizaf2d299b2018-09-04 10:25:21 -0700613 push_fixed ((int32_t)*(const HBUINT32*)&substr[0]);
Michiharu Ariza27c32d82018-08-22 11:36:39 -0700614 substr.inc (4);
615 return true;
616 }
617
Michiharu Arizafcf17782018-08-31 16:28:47 -0700618 private:
619 typedef Stack<ARG, 513> S;
Michiharu Ariza633ce882018-08-15 12:00:19 -0700620};
621
622/* an operator prefixed by its operands in a byte string */
623struct OpStr
624{
Michiharu Arizae7a045e2018-10-04 14:16:07 -0700625 inline void init (void) {}
626 inline void fini (void) {}
627
Michiharu Ariza633ce882018-08-15 12:00:19 -0700628 OpCode op;
629 ByteStr str;
630};
631
632/* base of OP_SERIALIZER */
633struct OpSerializer
634{
635 protected:
636 inline bool copy_opstr (hb_serialize_context_t *c, const OpStr& opstr) const
637 {
638 TRACE_SERIALIZE (this);
639
640 HBUINT8 *d = c->allocate_size<HBUINT8> (opstr.str.len);
641 if (unlikely (d == nullptr)) return_trace (false);
642 memcpy (d, &opstr.str.str[0], opstr.str.len);
643 return_trace (true);
644 }
645};
646
Michiharu Arizad56e3382018-10-31 22:30:34 -0700647template <typename VAL>
648struct ParsedValues
649{
650 inline void init (void)
651 {
652 opStart = 0;
653 values.init ();
654 }
655
656 inline void fini (void)
657 {
658 values.fini_deep ();
659 }
660
661 inline void add_op (OpCode op, const SubByteStr& substr = SubByteStr ())
662 {
663 VAL *val = values.push ();
664 val->op = op;
665 assert (substr.offset >= opStart);
666 val->str = ByteStr (substr.str, opStart, substr.offset - opStart);
667 opStart = substr.offset;
668 }
669
670 inline void add_op (OpCode op, const SubByteStr& substr, const VAL &v)
671 {
672 VAL *val = values.push (v);
673 val->op = op;
674 assert (substr.offset >= opStart);
675 val->str = ByteStr (substr.str, opStart, substr.offset - opStart);
676 opStart = substr.offset;
677 }
678
679 inline bool has_op (OpCode op) const
680 {
681 for (unsigned int i = 0; i < get_count (); i++)
682 if (get_value (i).op == op) return true;
683 return false;
684 }
685
686 inline unsigned get_count (void) const { return values.len; }
687 inline const VAL &get_value (unsigned int i) const { return values[i]; }
688 inline const VAL &operator [] (unsigned int i) const { return get_value (i); }
689
690 unsigned int opStart;
691 hb_vector_t<VAL> values;
692};
693
Michiharu Arizafcf17782018-08-31 16:28:47 -0700694template <typename ARG=Number>
Michiharu Ariza633ce882018-08-15 12:00:19 -0700695struct InterpEnv
696{
697 inline void init (const ByteStr &str_)
698 {
699 substr.reset (str_);
700 argStack.init ();
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700701 error = false;
Michiharu Ariza633ce882018-08-15 12:00:19 -0700702 }
703
704 inline void fini (void)
705 {
706 argStack.fini ();
707 }
708
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700709 inline bool in_error (void) const
Michiharu Arizacef75ea2018-08-17 13:13:18 -0700710 {
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700711 return error || substr.in_error () || argStack.in_error ();
712 }
713
714 inline void set_error (void) { error = true; }
715
716 inline OpCode fetch_op (void)
717 {
Michiharu Ariza6420ffe2018-10-15 14:03:18 -0700718 OpCode op = OpCode_Invalid;
Michiharu Arizacef75ea2018-08-17 13:13:18 -0700719 if (unlikely (!substr.avail ()))
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700720 return OpCode_Invalid;
Michiharu Arizacef75ea2018-08-17 13:13:18 -0700721 op = (OpCode)(unsigned char)substr[0];
722 if (op == OpCode_escape) {
723 if (unlikely (!substr.avail ()))
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700724 return OpCode_Invalid;
Michiharu Arizaeed7e6b2018-10-02 13:45:14 -0700725 op = Make_OpCode_ESC(substr[1]);
Michiharu Arizacef75ea2018-08-17 13:13:18 -0700726 substr.inc ();
727 }
728 substr.inc ();
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700729 return op;
Michiharu Arizacef75ea2018-08-17 13:13:18 -0700730 }
731
Michiharu Arizab51418f2018-10-08 15:05:36 -0700732 inline const ARG& eval_arg (unsigned int i)
733 {
734 return argStack[i];
735 }
736
737 inline ARG& pop_arg (void)
738 {
739 return argStack.pop ();
740 }
741
Michiharu Ariza968168b2018-08-31 13:28:16 -0700742 inline void pop_n_args (unsigned int n)
743 {
Michiharu Arizac0c85b82018-09-17 11:14:56 -0700744 assert (n <= argStack.get_count ());
745 argStack.pop (n);
Michiharu Ariza968168b2018-08-31 13:28:16 -0700746 }
747
748 inline void clear_args (void)
749 {
Michiharu Arizac0c85b82018-09-17 11:14:56 -0700750 pop_n_args (argStack.get_count ());
Michiharu Ariza968168b2018-08-31 13:28:16 -0700751 }
752
Michiharu Ariza633ce882018-08-15 12:00:19 -0700753 SubByteStr substr;
Michiharu Arizafcf17782018-08-31 16:28:47 -0700754 ArgStack<ARG> argStack;
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700755 protected:
756 bool error;
Michiharu Ariza633ce882018-08-15 12:00:19 -0700757};
758
Michiharu Arizafcf17782018-08-31 16:28:47 -0700759typedef InterpEnv<> NumInterpEnv;
760
761template <typename ARG=Number>
Michiharu Ariza633ce882018-08-15 12:00:19 -0700762struct OpSet
763{
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700764 static inline void process_op (OpCode op, InterpEnv<ARG>& env)
Michiharu Ariza633ce882018-08-15 12:00:19 -0700765 {
766 switch (op) {
767 case OpCode_shortint:
Michiharu Ariza6420ffe2018-10-15 14:03:18 -0700768 env.argStack.push_int ((int16_t)((env.substr[0] << 8) | env.substr[1]));
Michiharu Ariza633ce882018-08-15 12:00:19 -0700769 env.substr.inc (2);
770 break;
771
772 case OpCode_TwoBytePosInt0: case OpCode_TwoBytePosInt1:
773 case OpCode_TwoBytePosInt2: case OpCode_TwoBytePosInt3:
Michiharu Ariza633ce882018-08-15 12:00:19 -0700774 env.argStack.push_int ((int16_t)((op - OpCode_TwoBytePosInt0) * 256 + env.substr[0] + 108));
775 env.substr.inc ();
776 break;
Behdad Esfahbod592f39b2018-11-30 22:54:57 -0500777
Michiharu Ariza633ce882018-08-15 12:00:19 -0700778 case OpCode_TwoByteNegInt0: case OpCode_TwoByteNegInt1:
779 case OpCode_TwoByteNegInt2: case OpCode_TwoByteNegInt3:
Michiharu Ariza633ce882018-08-15 12:00:19 -0700780 env.argStack.push_int ((int16_t)(-(op - OpCode_TwoByteNegInt0) * 256 - env.substr[0] - 108));
781 env.substr.inc ();
782 break;
783
784 default:
785 /* 1-byte integer */
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700786 if (likely ((OpCode_OneByteIntFirst <= op) && (op <= OpCode_OneByteIntLast)))
Michiharu Ariza633ce882018-08-15 12:00:19 -0700787 {
788 env.argStack.push_int ((int)op - 139);
789 } else {
790 /* invalid unknown operator */
Michiharu Ariza968168b2018-08-31 13:28:16 -0700791 env.clear_args ();
Michiharu Ariza7d99a6c2018-10-12 02:16:07 -0700792 env.set_error ();
Michiharu Ariza633ce882018-08-15 12:00:19 -0700793 }
794 break;
795 }
Michiharu Ariza633ce882018-08-15 12:00:19 -0700796 }
797};
798
799template <typename ENV>
800struct Interpreter {
801
802 inline ~Interpreter(void) { fini (); }
803
804 inline void fini (void) { env.fini (); }
805
Michiharu Ariza633ce882018-08-15 12:00:19 -0700806 ENV env;
807};
808
809} /* namespace CFF */
810
Michiharu Ariza1d1afdd2018-08-29 13:36:39 -0700811#endif /* HB_CFF_INTERP_COMMON_HH */