• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CFF_INTERP_COMMON_HH
27 #define HB_CFF_INTERP_COMMON_HH
28 
29 namespace CFF {
30 
31 using namespace OT;
32 
33 typedef unsigned int OpCode;
34 
35 
36 /* === Dict operators === */
37 
38 /* One byte operators (0-31) */
39 #define OpCode_version		  0 /* CFF Top */
40 #define OpCode_Notice		  1 /* CFF Top */
41 #define OpCode_FullName		  2 /* CFF Top */
42 #define OpCode_FamilyName	  3 /* CFF Top */
43 #define OpCode_Weight		  4 /* CFF Top */
44 #define OpCode_FontBBox		  5 /* CFF Top */
45 #define OpCode_BlueValues	  6 /* CFF Private, CFF2 Private */
46 #define OpCode_OtherBlues	  7 /* CFF Private, CFF2 Private */
47 #define OpCode_FamilyBlues	  8 /* CFF Private, CFF2 Private */
48 #define OpCode_FamilyOtherBlues	  9 /* CFF Private, CFF2 Private */
49 #define OpCode_StdHW		 10 /* CFF Private, CFF2 Private */
50 #define OpCode_StdVW		 11 /* CFF Private, CFF2 Private */
51 #define OpCode_escape		 12 /* All. Shared with CS */
52 #define OpCode_UniqueID		 13 /* CFF Top */
53 #define OpCode_XUID		 14 /* CFF Top */
54 #define OpCode_charset		 15 /* CFF Top (0) */
55 #define OpCode_Encoding		 16 /* CFF Top (0) */
56 #define OpCode_CharStrings	 17 /* CFF Top, CFF2 Top */
57 #define OpCode_Private		 18 /* CFF Top, CFF2 FD */
58 #define OpCode_Subrs		 19 /* CFF Private, CFF2 Private */
59 #define OpCode_defaultWidthX	 20 /* CFF Private (0) */
60 #define OpCode_nominalWidthX	 21 /* CFF Private (0) */
61 #define OpCode_vsindexdict	 22 /* CFF2 Private/CS */
62 #define OpCode_blenddict	 23 /* CFF2 Private/CS */
63 #define OpCode_vstore		 24 /* CFF2 Top */
64 #define OpCode_reserved25	 25
65 #define OpCode_reserved26	 26
66 #define OpCode_reserved27	 27
67 
68 /* Numbers */
69 #define OpCode_shortint		 28 /* 16-bit integer, All */
70 #define OpCode_longintdict	 29 /* 32-bit integer, All */
71 #define OpCode_BCD		 30 /* Real number, CFF2 Top/FD */
72 #define OpCode_reserved31	 31
73 
74 /* 1-byte integers */
75 #define OpCode_OneByteIntFirst	 32 /* All. beginning of the range of first byte ints */
76 #define OpCode_OneByteIntLast	246 /* All. ending of the range of first byte int */
77 
78 /* 2-byte integers */
79 #define OpCode_TwoBytePosInt0	247 /* All. first byte of two byte positive int (+108 to +1131) */
80 #define OpCode_TwoBytePosInt1	248
81 #define OpCode_TwoBytePosInt2	249
82 #define OpCode_TwoBytePosInt3	250
83 
84 #define OpCode_TwoByteNegInt0	251 /* All. first byte of two byte negative int (-1131 to -108) */
85 #define OpCode_TwoByteNegInt1	252
86 #define OpCode_TwoByteNegInt2	253
87 #define OpCode_TwoByteNegInt3	254
88 
89 /* Two byte escape operators 12, (0-41) */
90 #define OpCode_ESC_Base		256
91 #define Make_OpCode_ESC(byte2)	((OpCode)(OpCode_ESC_Base + (byte2)))
92 
Unmake_OpCode_ESC(OpCode op)93 inline OpCode Unmake_OpCode_ESC (OpCode op)  { return (OpCode)(op - OpCode_ESC_Base); }
Is_OpCode_ESC(OpCode op)94 inline bool Is_OpCode_ESC (OpCode op) { return op >= OpCode_ESC_Base; }
OpCode_Size(OpCode op)95 inline unsigned int OpCode_Size (OpCode op) { return Is_OpCode_ESC (op) ? 2: 1; }
96 
97 #define OpCode_Copyright	Make_OpCode_ESC(0) /* CFF Top */
98 #define OpCode_isFixedPitch	Make_OpCode_ESC(1) /* CFF Top (false) */
99 #define OpCode_ItalicAngle	Make_OpCode_ESC(2) /* CFF Top (0) */
100 #define OpCode_UnderlinePosition Make_OpCode_ESC(3) /* CFF Top (-100) */
101 #define OpCode_UnderlineThickness Make_OpCode_ESC(4) /* CFF Top (50) */
102 #define OpCode_PaintType	Make_OpCode_ESC(5) /* CFF Top (0) */
103 #define OpCode_CharstringType	Make_OpCode_ESC(6) /* CFF Top (2) */
104 #define OpCode_FontMatrix	Make_OpCode_ESC(7) /* CFF Top, CFF2 Top (.001 0 0 .001 0 0)*/
105 #define OpCode_StrokeWidth	Make_OpCode_ESC(8) /* CFF Top (0) */
106 #define OpCode_BlueScale	Make_OpCode_ESC(9) /* CFF Private, CFF2 Private (0.039625) */
107 #define OpCode_BlueShift	Make_OpCode_ESC(10) /* CFF Private, CFF2 Private (7) */
108 #define OpCode_BlueFuzz		Make_OpCode_ESC(11) /* CFF Private, CFF2 Private (1) */
109 #define OpCode_StemSnapH	Make_OpCode_ESC(12) /* CFF Private, CFF2 Private */
110 #define OpCode_StemSnapV	Make_OpCode_ESC(13) /* CFF Private, CFF2 Private */
111 #define OpCode_ForceBold	Make_OpCode_ESC(14) /* CFF Private (false) */
112 #define OpCode_reservedESC15	Make_OpCode_ESC(15)
113 #define OpCode_reservedESC16	Make_OpCode_ESC(16)
114 #define OpCode_LanguageGroup	Make_OpCode_ESC(17) /* CFF Private, CFF2 Private (0) */
115 #define OpCode_ExpansionFactor	Make_OpCode_ESC(18) /* CFF Private, CFF2 Private (0.06) */
116 #define OpCode_initialRandomSeed Make_OpCode_ESC(19) /* CFF Private (0) */
117 #define OpCode_SyntheticBase	Make_OpCode_ESC(20) /* CFF Top */
118 #define OpCode_PostScript	Make_OpCode_ESC(21) /* CFF Top */
119 #define OpCode_BaseFontName	Make_OpCode_ESC(22) /* CFF Top */
120 #define OpCode_BaseFontBlend	Make_OpCode_ESC(23) /* CFF Top */
121 #define OpCode_reservedESC24	Make_OpCode_ESC(24)
122 #define OpCode_reservedESC25	Make_OpCode_ESC(25)
123 #define OpCode_reservedESC26	Make_OpCode_ESC(26)
124 #define OpCode_reservedESC27	Make_OpCode_ESC(27)
125 #define OpCode_reservedESC28	Make_OpCode_ESC(28)
126 #define OpCode_reservedESC29	Make_OpCode_ESC(29)
127 #define OpCode_ROS		Make_OpCode_ESC(30) /* CFF Top_CID */
128 #define OpCode_CIDFontVersion	Make_OpCode_ESC(31) /* CFF Top_CID (0) */
129 #define OpCode_CIDFontRevision	Make_OpCode_ESC(32) /* CFF Top_CID (0) */
130 #define OpCode_CIDFontType	Make_OpCode_ESC(33) /* CFF Top_CID (0) */
131 #define OpCode_CIDCount		Make_OpCode_ESC(34) /* CFF Top_CID (8720) */
132 #define OpCode_UIDBase		Make_OpCode_ESC(35) /* CFF Top_CID */
133 #define OpCode_FDArray		Make_OpCode_ESC(36) /* CFF Top_CID, CFF2 Top */
134 #define OpCode_FDSelect		Make_OpCode_ESC(37) /* CFF Top_CID, CFF2 Top */
135 #define OpCode_FontName		Make_OpCode_ESC(38) /* CFF Top_CID */
136 
137 
138 /* === CharString operators === */
139 
140 #define OpCode_hstem		  1 /* CFF, CFF2 */
141 #define OpCode_Reserved2	  2
142 #define OpCode_vstem		  3 /* CFF, CFF2 */
143 #define OpCode_vmoveto		  4 /* CFF, CFF2 */
144 #define OpCode_rlineto		  5 /* CFF, CFF2 */
145 #define OpCode_hlineto		  6 /* CFF, CFF2 */
146 #define OpCode_vlineto		  7 /* CFF, CFF2 */
147 #define OpCode_rrcurveto	  8 /* CFF, CFF2 */
148 #define OpCode_Reserved9	  9
149 #define OpCode_callsubr		 10 /* CFF, CFF2 */
150 #define OpCode_return		 11 /* CFF */
151 //#define OpCode_escape		 12 /* CFF, CFF2 */
152 #define OpCode_Reserved13	 13
153 #define OpCode_endchar		 14 /* CFF */
154 #define OpCode_vsindexcs	 15 /* CFF2 */
155 #define OpCode_blendcs		 16 /* CFF2 */
156 #define OpCode_Reserved17	 17
157 #define OpCode_hstemhm		 18 /* CFF, CFF2 */
158 #define OpCode_hintmask		 19 /* CFF, CFF2 */
159 #define OpCode_cntrmask		 20 /* CFF, CFF2 */
160 #define OpCode_rmoveto		 21 /* CFF, CFF2 */
161 #define OpCode_hmoveto		 22 /* CFF, CFF2 */
162 #define OpCode_vstemhm		 23 /* CFF, CFF2 */
163 #define OpCode_rcurveline	 24 /* CFF, CFF2 */
164 #define OpCode_rlinecurve	 25 /* CFF, CFF2 */
165 #define OpCode_vvcurveto	 26 /* CFF, CFF2 */
166 #define OpCode_hhcurveto	 27 /* CFF, CFF2 */
167 //#define OpCode_shortint	 28 /* CFF, CFF2 */
168 #define OpCode_callgsubr	 29 /* CFF, CFF2 */
169 #define OpCode_vhcurveto	 30 /* CFF, CFF2 */
170 #define OpCode_hvcurveto	 31 /* CFF, CFF2 */
171 
172 #define OpCode_fixedcs		255 /* 32-bit fixed */
173 
174 /* Two byte escape operators 12, (0-41) */
175 #define OpCode_dotsection	Make_OpCode_ESC(0) /* CFF (obsoleted) */
176 #define OpCode_ReservedESC1	Make_OpCode_ESC(1)
177 #define OpCode_ReservedESC2	Make_OpCode_ESC(2)
178 #define OpCode_and		Make_OpCode_ESC(3) /* CFF */
179 #define OpCode_or		Make_OpCode_ESC(4) /* CFF */
180 #define OpCode_not		Make_OpCode_ESC(5) /* CFF */
181 #define OpCode_ReservedESC6	Make_OpCode_ESC(6)
182 #define OpCode_ReservedESC7	Make_OpCode_ESC(7)
183 #define OpCode_ReservedESC8	Make_OpCode_ESC(8)
184 #define OpCode_abs		Make_OpCode_ESC(9) /* CFF */
185 #define OpCode_add		Make_OpCode_ESC(10) /* CFF */
186 #define OpCode_sub		Make_OpCode_ESC(11) /* CFF */
187 #define OpCode_div		Make_OpCode_ESC(12) /* CFF */
188 #define OpCode_ReservedESC13	Make_OpCode_ESC(13)
189 #define OpCode_neg		Make_OpCode_ESC(14) /* CFF */
190 #define OpCode_eq		Make_OpCode_ESC(15) /* CFF */
191 #define OpCode_ReservedESC16	Make_OpCode_ESC(16)
192 #define OpCode_ReservedESC17	Make_OpCode_ESC(17)
193 #define OpCode_drop		Make_OpCode_ESC(18) /* CFF */
194 #define OpCode_ReservedESC19	Make_OpCode_ESC(19)
195 #define OpCode_put		Make_OpCode_ESC(20) /* CFF */
196 #define OpCode_get		Make_OpCode_ESC(21) /* CFF */
197 #define OpCode_ifelse		Make_OpCode_ESC(22) /* CFF */
198 #define OpCode_random		Make_OpCode_ESC(23) /* CFF */
199 #define OpCode_mul		Make_OpCode_ESC(24) /* CFF */
200 //#define OpCode_reservedESC25	Make_OpCode_ESC(25)
201 #define OpCode_sqrt		Make_OpCode_ESC(26) /* CFF */
202 #define OpCode_dup		Make_OpCode_ESC(27) /* CFF */
203 #define OpCode_exch		Make_OpCode_ESC(28) /* CFF */
204 #define OpCode_index		Make_OpCode_ESC(29) /* CFF */
205 #define OpCode_roll		Make_OpCode_ESC(30) /* CFF */
206 #define OpCode_reservedESC31	Make_OpCode_ESC(31)
207 #define OpCode_reservedESC32	Make_OpCode_ESC(32)
208 #define OpCode_reservedESC33	Make_OpCode_ESC(33)
209 #define OpCode_hflex		Make_OpCode_ESC(34) /* CFF, CFF2 */
210 #define OpCode_flex		Make_OpCode_ESC(35) /* CFF, CFF2 */
211 #define OpCode_hflex1		Make_OpCode_ESC(36) /* CFF, CFF2 */
212 #define OpCode_flex1		Make_OpCode_ESC(37) /* CFF, CFF2 */
213 
214 
215 #define OpCode_Invalid		0xFFFFu
216 
217 
218 struct Number
219 {
initCFF::Number220   void init () { set_real (0.0); }
finiCFF::Number221   void fini () {}
222 
set_intCFF::Number223   void set_int (int v)       { value = (double) v; }
to_intCFF::Number224   int to_int () const        { return (int) value; }
225 
set_fixedCFF::Number226   void set_fixed (int32_t v) { value = v / 65536.0; }
to_fixedCFF::Number227   int32_t to_fixed () const  { return (int32_t) (value * 65536.0); }
228 
set_realCFF::Number229   void set_real (double v)	 { value = v; }
to_realCFF::Number230   double to_real () const    { return value; }
231 
ceilCFF::Number232   int ceil () const          { return (int) ::ceil (value); }
floorCFF::Number233   int floor () const         { return (int) ::floor (value); }
234 
in_int_rangeCFF::Number235   bool in_int_range () const
236   { return ((double) (int16_t) to_int () == value); }
237 
operator >CFF::Number238   bool operator > (const Number &n) const
239   { return value > n.to_real (); }
240 
operator <CFF::Number241   bool operator < (const Number &n) const
242   { return n > *this; }
243 
operator >=CFF::Number244   bool operator >= (const Number &n) const
245   { return !(*this < n); }
246 
operator <=CFF::Number247   bool operator <= (const Number &n) const
248   { return !(*this > n); }
249 
operator +=CFF::Number250   const Number &operator += (const Number &n)
251   {
252     set_real (to_real () + n.to_real ());
253 
254     return *this;
255   }
256 
257   protected:
258   double  value;
259 };
260 
261 /* byte string */
262 struct UnsizedByteStr : UnsizedArrayOf <HBUINT8>
263 {
264   // encode 2-byte int (Dict/CharString) or 4-byte int (Dict)
265   template <typename INTTYPE, int minVal, int maxVal>
serialize_intCFF::UnsizedByteStr266   static bool serialize_int (hb_serialize_context_t *c, OpCode intOp, int value)
267   {
268     TRACE_SERIALIZE (this);
269 
270     if (unlikely ((value < minVal || value > maxVal)))
271       return_trace (false);
272 
273     HBUINT8 *p = c->allocate_size<HBUINT8> (1);
274     if (unlikely (p == nullptr)) return_trace (false);
275     p->set (intOp);
276 
277     INTTYPE *ip = c->allocate_size<INTTYPE> (INTTYPE::static_size);
278     if (unlikely (ip == nullptr)) return_trace (false);
279     ip->set ((unsigned int)value);
280 
281     return_trace (true);
282   }
283 
serialize_int4CFF::UnsizedByteStr284   static bool serialize_int4 (hb_serialize_context_t *c, int value)
285   { return serialize_int<HBUINT32, 0, 0x7FFFFFFF> (c, OpCode_longintdict, value); }
286 
serialize_int2CFF::UnsizedByteStr287   static bool serialize_int2 (hb_serialize_context_t *c, int value)
288   { return serialize_int<HBUINT16, 0, 0x7FFF> (c, OpCode_shortint, value); }
289 
290   /* Defining null_size allows a Null object may be created. Should be safe because:
291    * A descendent struct Dict uses a Null pointer to indicate a missing table,
292    * checked before access.
293    * ByteStr, a wrapper struct pairing a byte pointer along with its length, always
294    * checks the length before access. A Null pointer is used as the initial pointer
295    * along with zero length by the default ctor.
296    */
297   DEFINE_SIZE_MIN(0);
298 };
299 
300 struct ByteStr
301 {
ByteStrCFF::ByteStr302   ByteStr ()
303     : str (&Null(UnsizedByteStr)), len (0) {}
ByteStrCFF::ByteStr304   ByteStr (const UnsizedByteStr& s, unsigned int l)
305     : str (&s), len (l) {}
ByteStrCFF::ByteStr306   ByteStr (const char *s, unsigned int l=0)
307     : str ((const UnsizedByteStr *)s), len (l) {}
308   /* sub-string */
ByteStrCFF::ByteStr309   ByteStr (const ByteStr &bs, unsigned int offset, unsigned int len_)
310   {
311     str = (const UnsizedByteStr *)&bs.str[offset];
312     len = len_;
313   }
314 
sanitizeCFF::ByteStr315   bool sanitize (hb_sanitize_context_t *c) const { return str->sanitize (c, len); }
316 
operator []CFF::ByteStr317   const HBUINT8& operator [] (unsigned int i) const
318   {
319     if (likely (str && (i < len)))
320       return (*str)[i];
321     else
322       return Null(HBUINT8);
323   }
324 
serializeCFF::ByteStr325   bool serialize (hb_serialize_context_t *c, const ByteStr &src)
326   {
327     TRACE_SERIALIZE (this);
328     HBUINT8 *dest = c->allocate_size<HBUINT8> (src.len);
329     if (unlikely (dest == nullptr))
330       return_trace (false);
331     memcpy (dest, src.str, src.len);
332     return_trace (true);
333   }
334 
get_sizeCFF::ByteStr335   unsigned int get_size () const { return len; }
336 
check_limitCFF::ByteStr337   bool check_limit (unsigned int offset, unsigned int count) const
338   { return (offset + count <= len); }
339 
340   const UnsizedByteStr *str;
341   unsigned int len;
342 };
343 
344 struct SubByteStr
345 {
SubByteStrCFF::SubByteStr346   SubByteStr ()
347   { init (); }
348 
initCFF::SubByteStr349   void init ()
350   {
351     str = ByteStr (0);
352     offset = 0;
353     error = false;
354   }
355 
finiCFF::SubByteStr356   void fini () {}
357 
SubByteStrCFF::SubByteStr358   SubByteStr (const ByteStr &str_, unsigned int offset_ = 0)
359     : str (str_), offset (offset_), error (false) {}
360 
resetCFF::SubByteStr361   void reset (const ByteStr &str_, unsigned int offset_ = 0)
362   {
363     str = str_;
364     offset = offset_;
365     error = false;
366   }
367 
operator []CFF::SubByteStr368   const HBUINT8& operator [] (int i) {
369     if (unlikely ((unsigned int)(offset + i) >= str.len))
370     {
371       set_error ();
372       return Null(HBUINT8);
373     }
374     else
375       return str[offset + i];
376   }
377 
operator ByteStrCFF::SubByteStr378   operator ByteStr () const { return ByteStr (str, offset, str.len - offset); }
379 
availCFF::SubByteStr380   bool avail (unsigned int count=1) const
381   {
382     return (!in_error () && str.check_limit (offset, count));
383   }
incCFF::SubByteStr384   void inc (unsigned int count=1)
385   {
386     if (likely (!in_error () && (offset <= str.len) && (offset + count <= str.len)))
387     {
388       offset += count;
389     }
390     else
391     {
392       offset = str.len;
393       set_error ();
394     }
395   }
396 
set_errorCFF::SubByteStr397   void set_error ()      { error = true; }
in_errorCFF::SubByteStr398   bool in_error () const { return error; }
399 
400   ByteStr       str;
401   unsigned int  offset; /* beginning of the sub-string within str */
402 
403   protected:
404   bool	  error;
405 };
406 
407 typedef hb_vector_t<ByteStr> ByteStrArray;
408 
409 /* stack */
410 template <typename ELEM, int LIMIT>
411 struct Stack
412 {
initCFF::Stack413   void init ()
414   {
415     error = false;
416     count = 0;
417     elements.init ();
418     elements.resize (kSizeLimit);
419     for (unsigned int i = 0; i < elements.len; i++)
420       elements[i].init ();
421   }
422 
finiCFF::Stack423   void fini ()
424   {
425     elements.fini_deep ();
426   }
427 
operator []CFF::Stack428   ELEM& operator [] (unsigned int i)
429   {
430     if (unlikely (i >= count)) set_error ();
431     return elements[i];
432   }
433 
pushCFF::Stack434   void push (const ELEM &v)
435   {
436     if (likely (count < elements.len))
437       elements[count++] = v;
438     else
439       set_error ();
440   }
441 
pushCFF::Stack442   ELEM &push ()
443   {
444     if (likely (count < elements.len))
445       return elements[count++];
446     else
447     {
448       set_error ();
449       return Crap(ELEM);
450     }
451   }
452 
popCFF::Stack453   ELEM& pop ()
454   {
455     if (likely (count > 0))
456       return elements[--count];
457     else
458     {
459       set_error ();
460       return Crap(ELEM);
461     }
462   }
463 
popCFF::Stack464   void pop (unsigned int n)
465   {
466     if (likely (count >= n))
467       count -= n;
468     else
469       set_error ();
470   }
471 
peekCFF::Stack472   const ELEM& peek ()
473   {
474     if (likely (count > 0))
475       return elements[count-1];
476     else
477     {
478       set_error ();
479       return Null(ELEM);
480     }
481   }
482 
unpopCFF::Stack483   void unpop ()
484   {
485     if (likely (count < elements.len))
486       count++;
487     else
488       set_error ();
489   }
490 
clearCFF::Stack491   void clear () { count = 0; }
492 
in_errorCFF::Stack493   bool in_error () const { return (error || elements.in_error ()); }
set_errorCFF::Stack494   void set_error ()      { error = true; }
495 
get_countCFF::Stack496   unsigned int get_count () const { return count; }
is_emptyCFF::Stack497   bool is_empty () const { return count == 0; }
498 
499   static const unsigned int kSizeLimit = LIMIT;
500 
501   protected:
502   bool error;
503   unsigned int count;
504   hb_vector_t<ELEM, kSizeLimit> elements;
505 };
506 
507 /* argument stack */
508 template <typename ARG=Number>
509 struct ArgStack : Stack<ARG, 513>
510 {
push_intCFF::ArgStack511   void push_int (int v)
512   {
513     ARG &n = S::push ();
514     n.set_int (v);
515   }
516 
push_fixedCFF::ArgStack517   void push_fixed (int32_t v)
518   {
519     ARG &n = S::push ();
520     n.set_fixed (v);
521   }
522 
push_realCFF::ArgStack523   void push_real (double v)
524   {
525     ARG &n = S::push ();
526     n.set_real (v);
527   }
528 
pop_numCFF::ArgStack529   ARG& pop_num () { return this->pop (); }
530 
pop_intCFF::ArgStack531   int pop_int ()  { return this->pop ().to_int (); }
532 
pop_uintCFF::ArgStack533   unsigned int pop_uint ()
534   {
535     int i = pop_int ();
536     if (unlikely (i < 0))
537     {
538       i = 0;
539       S::set_error ();
540     }
541     return (unsigned)i;
542   }
543 
push_longint_from_substrCFF::ArgStack544   void push_longint_from_substr (SubByteStr& substr)
545   {
546     push_int ((substr[0] << 24) | (substr[1] << 16) | (substr[2] << 8) | (substr[3]));
547     substr.inc (4);
548   }
549 
push_fixed_from_substrCFF::ArgStack550   bool push_fixed_from_substr (SubByteStr& substr)
551   {
552     if (unlikely (!substr.avail (4)))
553       return false;
554     push_fixed ((int32_t)*(const HBUINT32*)&substr[0]);
555     substr.inc (4);
556     return true;
557   }
558 
get_subarrayCFF::ArgStack559   hb_array_t<const ARG> get_subarray (unsigned int start) const
560   {
561     return S::elements.sub_array (start);
562   }
563 
564   private:
565   typedef Stack<ARG, 513> S;
566 };
567 
568 /* an operator prefixed by its operands in a byte string */
569 struct OpStr
570 {
initCFF::OpStr571   void init () {}
finiCFF::OpStr572   void fini () {}
573 
574   OpCode  op;
575   ByteStr str;
576 };
577 
578 /* base of OP_SERIALIZER */
579 struct OpSerializer
580 {
581   protected:
copy_opstrCFF::OpSerializer582   bool copy_opstr (hb_serialize_context_t *c, const OpStr& opstr) const
583   {
584     TRACE_SERIALIZE (this);
585 
586     HBUINT8 *d = c->allocate_size<HBUINT8> (opstr.str.len);
587     if (unlikely (d == nullptr)) return_trace (false);
588     memcpy (d, &opstr.str.str[0], opstr.str.len);
589     return_trace (true);
590   }
591 };
592 
593 template <typename VAL>
594 struct ParsedValues
595 {
initCFF::ParsedValues596   void init ()
597   {
598     opStart = 0;
599     values.init ();
600   }
finiCFF::ParsedValues601   void fini () { values.fini_deep (); }
602 
add_opCFF::ParsedValues603   void add_op (OpCode op, const SubByteStr& substr = SubByteStr ())
604   {
605     VAL *val = values.push ();
606     val->op = op;
607     val->str = ByteStr (substr.str, opStart, substr.offset - opStart);
608     opStart = substr.offset;
609   }
610 
add_opCFF::ParsedValues611   void add_op (OpCode op, const SubByteStr& substr, const VAL &v)
612   {
613     VAL *val = values.push (v);
614     val->op = op;
615     val->str = ByteStr (substr.str, opStart, substr.offset - opStart);
616     opStart = substr.offset;
617   }
618 
has_opCFF::ParsedValues619   bool has_op (OpCode op) const
620   {
621     for (unsigned int i = 0; i < get_count (); i++)
622       if (get_value (i).op == op) return true;
623     return false;
624   }
625 
get_countCFF::ParsedValues626   unsigned get_count () const { return values.len; }
get_valueCFF::ParsedValues627   const VAL &get_value (unsigned int i) const { return values[i]; }
operator []CFF::ParsedValues628   const VAL &operator [] (unsigned int i) const { return get_value (i); }
629 
630   unsigned int       opStart;
631   hb_vector_t<VAL>   values;
632 };
633 
634 template <typename ARG=Number>
635 struct InterpEnv
636 {
initCFF::InterpEnv637   void init (const ByteStr &str_)
638   {
639     substr.reset (str_);
640     argStack.init ();
641     error = false;
642   }
finiCFF::InterpEnv643   void fini () { argStack.fini (); }
644 
in_errorCFF::InterpEnv645   bool in_error () const
646   { return error || substr.in_error () || argStack.in_error (); }
647 
set_errorCFF::InterpEnv648   void set_error () { error = true; }
649 
fetch_opCFF::InterpEnv650   OpCode fetch_op ()
651   {
652     OpCode  op = OpCode_Invalid;
653     if (unlikely (!substr.avail ()))
654       return OpCode_Invalid;
655     op = (OpCode)(unsigned char)substr[0];
656     if (op == OpCode_escape) {
657       if (unlikely (!substr.avail ()))
658 	return OpCode_Invalid;
659       op = Make_OpCode_ESC(substr[1]);
660       substr.inc ();
661     }
662     substr.inc ();
663     return op;
664   }
665 
eval_argCFF::InterpEnv666   const ARG& eval_arg (unsigned int i)
667   {
668     return argStack[i];
669   }
670 
pop_argCFF::InterpEnv671   ARG& pop_arg ()
672   {
673     return argStack.pop ();
674   }
675 
pop_n_argsCFF::InterpEnv676   void pop_n_args (unsigned int n)
677   {
678     argStack.pop (n);
679   }
680 
clear_argsCFF::InterpEnv681   void clear_args ()
682   {
683     pop_n_args (argStack.get_count ());
684   }
685 
686   SubByteStr    substr;
687   ArgStack<ARG> argStack;
688   protected:
689   bool	  error;
690 };
691 
692 typedef InterpEnv<> NumInterpEnv;
693 
694 template <typename ARG=Number>
695 struct OpSet
696 {
process_opCFF::OpSet697   static void process_op (OpCode op, InterpEnv<ARG>& env)
698   {
699     switch (op) {
700       case OpCode_shortint:
701 	env.argStack.push_int ((int16_t)((env.substr[0] << 8) | env.substr[1]));
702 	env.substr.inc (2);
703 	break;
704 
705       case OpCode_TwoBytePosInt0: case OpCode_TwoBytePosInt1:
706       case OpCode_TwoBytePosInt2: case OpCode_TwoBytePosInt3:
707 	env.argStack.push_int ((int16_t)((op - OpCode_TwoBytePosInt0) * 256 + env.substr[0] + 108));
708 	env.substr.inc ();
709 	break;
710 
711       case OpCode_TwoByteNegInt0: case OpCode_TwoByteNegInt1:
712       case OpCode_TwoByteNegInt2: case OpCode_TwoByteNegInt3:
713 	env.argStack.push_int ((int16_t)(-(op - OpCode_TwoByteNegInt0) * 256 - env.substr[0] - 108));
714 	env.substr.inc ();
715 	break;
716 
717       default:
718 	/* 1-byte integer */
719 	if (likely ((OpCode_OneByteIntFirst <= op) && (op <= OpCode_OneByteIntLast)))
720 	{
721 	  env.argStack.push_int ((int)op - 139);
722 	} else {
723 	  /* invalid unknown operator */
724 	  env.clear_args ();
725 	  env.set_error ();
726 	}
727 	break;
728     }
729   }
730 };
731 
732 template <typename ENV>
733 struct Interpreter {
734 
~InterpreterCFF::Interpreter735   ~Interpreter() { fini (); }
736 
finiCFF::Interpreter737   void fini () { env.fini (); }
738 
739   ENV env;
740 };
741 
742 } /* namespace CFF */
743 
744 #endif /* HB_CFF_INTERP_COMMON_HH */
745