Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 1 | /* |
Michiharu Ariza | 0dfa584 | 2018-11-12 08:47:07 -0800 | [diff] [blame] | 2 | * Copyright © 2018 Adobe Inc. |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 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 | */ |
Michiharu Ariza | 1d1afdd | 2018-08-29 13:36:39 -0700 | [diff] [blame] | 26 | #ifndef HB_CFF_INTERP_COMMON_HH |
| 27 | #define HB_CFF_INTERP_COMMON_HH |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 28 | |
Behdad Esfahbod | 054f966 | 2023-06-03 22:52:16 -0600 | [diff] [blame] | 29 | extern HB_INTERNAL const unsigned char *endchar_str; |
| 30 | |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 31 | namespace CFF { |
| 32 | |
| 33 | using namespace OT; |
| 34 | |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 35 | typedef unsigned int op_code_t; |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 36 | |
| 37 | |
Michiharu Ariza | eed7e6b | 2018-10-02 13:45:14 -0700 | [diff] [blame] | 38 | /* === Dict operators === */ |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 39 | |
Michiharu Ariza | eed7e6b | 2018-10-02 13:45:14 -0700 | [diff] [blame] | 40 | /* One byte operators (0-31) */ |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 41 | #define OpCode_version 0 /* CFF Top */ |
| 42 | #define OpCode_Notice 1 /* CFF Top */ |
| 43 | #define OpCode_FullName 2 /* CFF Top */ |
| 44 | #define OpCode_FamilyName 3 /* CFF Top */ |
| 45 | #define OpCode_Weight 4 /* CFF Top */ |
| 46 | #define OpCode_FontBBox 5 /* CFF Top */ |
| 47 | #define OpCode_BlueValues 6 /* CFF Private, CFF2 Private */ |
| 48 | #define OpCode_OtherBlues 7 /* CFF Private, CFF2 Private */ |
| 49 | #define OpCode_FamilyBlues 8 /* CFF Private, CFF2 Private */ |
| 50 | #define OpCode_FamilyOtherBlues 9 /* CFF Private, CFF2 Private */ |
| 51 | #define OpCode_StdHW 10 /* CFF Private, CFF2 Private */ |
| 52 | #define OpCode_StdVW 11 /* CFF Private, CFF2 Private */ |
| 53 | #define OpCode_escape 12 /* All. Shared with CS */ |
| 54 | #define OpCode_UniqueID 13 /* CFF Top */ |
| 55 | #define OpCode_XUID 14 /* CFF Top */ |
| 56 | #define OpCode_charset 15 /* CFF Top (0) */ |
| 57 | #define OpCode_Encoding 16 /* CFF Top (0) */ |
| 58 | #define OpCode_CharStrings 17 /* CFF Top, CFF2 Top */ |
| 59 | #define OpCode_Private 18 /* CFF Top, CFF2 FD */ |
| 60 | #define OpCode_Subrs 19 /* CFF Private, CFF2 Private */ |
| 61 | #define OpCode_defaultWidthX 20 /* CFF Private (0) */ |
| 62 | #define OpCode_nominalWidthX 21 /* CFF Private (0) */ |
| 63 | #define OpCode_vsindexdict 22 /* CFF2 Private/CS */ |
| 64 | #define OpCode_blenddict 23 /* CFF2 Private/CS */ |
| 65 | #define OpCode_vstore 24 /* CFF2 Top */ |
| 66 | #define OpCode_reserved25 25 |
| 67 | #define OpCode_reserved26 26 |
| 68 | #define OpCode_reserved27 27 |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 69 | |
Michiharu Ariza | eed7e6b | 2018-10-02 13:45:14 -0700 | [diff] [blame] | 70 | /* Numbers */ |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 71 | #define OpCode_shortint 28 /* 16-bit integer, All */ |
| 72 | #define OpCode_longintdict 29 /* 32-bit integer, All */ |
| 73 | #define OpCode_BCD 30 /* Real number, CFF2 Top/FD */ |
| 74 | #define OpCode_reserved31 31 |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 75 | |
Michiharu Ariza | eed7e6b | 2018-10-02 13:45:14 -0700 | [diff] [blame] | 76 | /* 1-byte integers */ |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 77 | #define OpCode_OneByteIntFirst 32 /* All. beginning of the range of first byte ints */ |
| 78 | #define OpCode_OneByteIntLast 246 /* All. ending of the range of first byte int */ |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 79 | |
Michiharu Ariza | eed7e6b | 2018-10-02 13:45:14 -0700 | [diff] [blame] | 80 | /* 2-byte integers */ |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 81 | #define OpCode_TwoBytePosInt0 247 /* All. first byte of two byte positive int (+108 to +1131) */ |
| 82 | #define OpCode_TwoBytePosInt1 248 |
| 83 | #define OpCode_TwoBytePosInt2 249 |
| 84 | #define OpCode_TwoBytePosInt3 250 |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 85 | |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 86 | #define OpCode_TwoByteNegInt0 251 /* All. first byte of two byte negative int (-1131 to -108) */ |
| 87 | #define OpCode_TwoByteNegInt1 252 |
| 88 | #define OpCode_TwoByteNegInt2 253 |
| 89 | #define OpCode_TwoByteNegInt3 254 |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 90 | |
Michiharu Ariza | eed7e6b | 2018-10-02 13:45:14 -0700 | [diff] [blame] | 91 | /* Two byte escape operators 12, (0-41) */ |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 92 | #define OpCode_ESC_Base 256 |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 93 | #define Make_OpCode_ESC(byte2) ((op_code_t)(OpCode_ESC_Base + (byte2))) |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 94 | |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 95 | inline op_code_t Unmake_OpCode_ESC (op_code_t op) { return (op_code_t)(op - OpCode_ESC_Base); } |
| 96 | inline bool Is_OpCode_ESC (op_code_t op) { return op >= OpCode_ESC_Base; } |
| 97 | inline unsigned int OpCode_Size (op_code_t op) { return Is_OpCode_ESC (op) ? 2: 1; } |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 98 | |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 99 | #define OpCode_Copyright Make_OpCode_ESC(0) /* CFF Top */ |
| 100 | #define OpCode_isFixedPitch Make_OpCode_ESC(1) /* CFF Top (false) */ |
| 101 | #define OpCode_ItalicAngle Make_OpCode_ESC(2) /* CFF Top (0) */ |
| 102 | #define OpCode_UnderlinePosition Make_OpCode_ESC(3) /* CFF Top (-100) */ |
| 103 | #define OpCode_UnderlineThickness Make_OpCode_ESC(4) /* CFF Top (50) */ |
| 104 | #define OpCode_PaintType Make_OpCode_ESC(5) /* CFF Top (0) */ |
| 105 | #define OpCode_CharstringType Make_OpCode_ESC(6) /* CFF Top (2) */ |
| 106 | #define OpCode_FontMatrix Make_OpCode_ESC(7) /* CFF Top, CFF2 Top (.001 0 0 .001 0 0)*/ |
| 107 | #define OpCode_StrokeWidth Make_OpCode_ESC(8) /* CFF Top (0) */ |
| 108 | #define OpCode_BlueScale Make_OpCode_ESC(9) /* CFF Private, CFF2 Private (0.039625) */ |
| 109 | #define OpCode_BlueShift Make_OpCode_ESC(10) /* CFF Private, CFF2 Private (7) */ |
| 110 | #define OpCode_BlueFuzz Make_OpCode_ESC(11) /* CFF Private, CFF2 Private (1) */ |
| 111 | #define OpCode_StemSnapH Make_OpCode_ESC(12) /* CFF Private, CFF2 Private */ |
| 112 | #define OpCode_StemSnapV Make_OpCode_ESC(13) /* CFF Private, CFF2 Private */ |
| 113 | #define OpCode_ForceBold Make_OpCode_ESC(14) /* CFF Private (false) */ |
| 114 | #define OpCode_reservedESC15 Make_OpCode_ESC(15) |
| 115 | #define OpCode_reservedESC16 Make_OpCode_ESC(16) |
| 116 | #define OpCode_LanguageGroup Make_OpCode_ESC(17) /* CFF Private, CFF2 Private (0) */ |
| 117 | #define OpCode_ExpansionFactor Make_OpCode_ESC(18) /* CFF Private, CFF2 Private (0.06) */ |
| 118 | #define OpCode_initialRandomSeed Make_OpCode_ESC(19) /* CFF Private (0) */ |
| 119 | #define OpCode_SyntheticBase Make_OpCode_ESC(20) /* CFF Top */ |
| 120 | #define OpCode_PostScript Make_OpCode_ESC(21) /* CFF Top */ |
| 121 | #define OpCode_BaseFontName Make_OpCode_ESC(22) /* CFF Top */ |
| 122 | #define OpCode_BaseFontBlend Make_OpCode_ESC(23) /* CFF Top */ |
| 123 | #define OpCode_reservedESC24 Make_OpCode_ESC(24) |
| 124 | #define OpCode_reservedESC25 Make_OpCode_ESC(25) |
| 125 | #define OpCode_reservedESC26 Make_OpCode_ESC(26) |
| 126 | #define OpCode_reservedESC27 Make_OpCode_ESC(27) |
| 127 | #define OpCode_reservedESC28 Make_OpCode_ESC(28) |
| 128 | #define OpCode_reservedESC29 Make_OpCode_ESC(29) |
| 129 | #define OpCode_ROS Make_OpCode_ESC(30) /* CFF Top_CID */ |
| 130 | #define OpCode_CIDFontVersion Make_OpCode_ESC(31) /* CFF Top_CID (0) */ |
| 131 | #define OpCode_CIDFontRevision Make_OpCode_ESC(32) /* CFF Top_CID (0) */ |
| 132 | #define OpCode_CIDFontType Make_OpCode_ESC(33) /* CFF Top_CID (0) */ |
| 133 | #define OpCode_CIDCount Make_OpCode_ESC(34) /* CFF Top_CID (8720) */ |
| 134 | #define OpCode_UIDBase Make_OpCode_ESC(35) /* CFF Top_CID */ |
| 135 | #define OpCode_FDArray Make_OpCode_ESC(36) /* CFF Top_CID, CFF2 Top */ |
| 136 | #define OpCode_FDSelect Make_OpCode_ESC(37) /* CFF Top_CID, CFF2 Top */ |
| 137 | #define OpCode_FontName Make_OpCode_ESC(38) /* CFF Top_CID */ |
Michiharu Ariza | eed7e6b | 2018-10-02 13:45:14 -0700 | [diff] [blame] | 138 | |
Michiharu Ariza | eed7e6b | 2018-10-02 13:45:14 -0700 | [diff] [blame] | 139 | |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 140 | /* === CharString operators === */ |
Michiharu Ariza | eed7e6b | 2018-10-02 13:45:14 -0700 | [diff] [blame] | 141 | |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 142 | #define OpCode_hstem 1 /* CFF, CFF2 */ |
| 143 | #define OpCode_Reserved2 2 |
| 144 | #define OpCode_vstem 3 /* CFF, CFF2 */ |
| 145 | #define OpCode_vmoveto 4 /* CFF, CFF2 */ |
| 146 | #define OpCode_rlineto 5 /* CFF, CFF2 */ |
| 147 | #define OpCode_hlineto 6 /* CFF, CFF2 */ |
| 148 | #define OpCode_vlineto 7 /* CFF, CFF2 */ |
| 149 | #define OpCode_rrcurveto 8 /* CFF, CFF2 */ |
| 150 | #define OpCode_Reserved9 9 |
| 151 | #define OpCode_callsubr 10 /* CFF, CFF2 */ |
| 152 | #define OpCode_return 11 /* CFF */ |
| 153 | //#define OpCode_escape 12 /* CFF, CFF2 */ |
| 154 | #define OpCode_Reserved13 13 |
| 155 | #define OpCode_endchar 14 /* CFF */ |
| 156 | #define OpCode_vsindexcs 15 /* CFF2 */ |
| 157 | #define OpCode_blendcs 16 /* CFF2 */ |
| 158 | #define OpCode_Reserved17 17 |
| 159 | #define OpCode_hstemhm 18 /* CFF, CFF2 */ |
| 160 | #define OpCode_hintmask 19 /* CFF, CFF2 */ |
| 161 | #define OpCode_cntrmask 20 /* CFF, CFF2 */ |
| 162 | #define OpCode_rmoveto 21 /* CFF, CFF2 */ |
| 163 | #define OpCode_hmoveto 22 /* CFF, CFF2 */ |
| 164 | #define OpCode_vstemhm 23 /* CFF, CFF2 */ |
| 165 | #define OpCode_rcurveline 24 /* CFF, CFF2 */ |
| 166 | #define OpCode_rlinecurve 25 /* CFF, CFF2 */ |
| 167 | #define OpCode_vvcurveto 26 /* CFF, CFF2 */ |
| 168 | #define OpCode_hhcurveto 27 /* CFF, CFF2 */ |
| 169 | //#define OpCode_shortint 28 /* CFF, CFF2 */ |
| 170 | #define OpCode_callgsubr 29 /* CFF, CFF2 */ |
| 171 | #define OpCode_vhcurveto 30 /* CFF, CFF2 */ |
| 172 | #define OpCode_hvcurveto 31 /* CFF, CFF2 */ |
| 173 | |
| 174 | #define OpCode_fixedcs 255 /* 32-bit fixed */ |
Michiharu Ariza | eed7e6b | 2018-10-02 13:45:14 -0700 | [diff] [blame] | 175 | |
| 176 | /* Two byte escape operators 12, (0-41) */ |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 177 | #define OpCode_dotsection Make_OpCode_ESC(0) /* CFF (obsoleted) */ |
| 178 | #define OpCode_ReservedESC1 Make_OpCode_ESC(1) |
| 179 | #define OpCode_ReservedESC2 Make_OpCode_ESC(2) |
| 180 | #define OpCode_and Make_OpCode_ESC(3) /* CFF */ |
| 181 | #define OpCode_or Make_OpCode_ESC(4) /* CFF */ |
| 182 | #define OpCode_not Make_OpCode_ESC(5) /* CFF */ |
| 183 | #define OpCode_ReservedESC6 Make_OpCode_ESC(6) |
| 184 | #define OpCode_ReservedESC7 Make_OpCode_ESC(7) |
| 185 | #define OpCode_ReservedESC8 Make_OpCode_ESC(8) |
| 186 | #define OpCode_abs Make_OpCode_ESC(9) /* CFF */ |
| 187 | #define OpCode_add Make_OpCode_ESC(10) /* CFF */ |
| 188 | #define OpCode_sub Make_OpCode_ESC(11) /* CFF */ |
| 189 | #define OpCode_div Make_OpCode_ESC(12) /* CFF */ |
| 190 | #define OpCode_ReservedESC13 Make_OpCode_ESC(13) |
| 191 | #define OpCode_neg Make_OpCode_ESC(14) /* CFF */ |
| 192 | #define OpCode_eq Make_OpCode_ESC(15) /* CFF */ |
| 193 | #define OpCode_ReservedESC16 Make_OpCode_ESC(16) |
| 194 | #define OpCode_ReservedESC17 Make_OpCode_ESC(17) |
| 195 | #define OpCode_drop Make_OpCode_ESC(18) /* CFF */ |
| 196 | #define OpCode_ReservedESC19 Make_OpCode_ESC(19) |
| 197 | #define OpCode_put Make_OpCode_ESC(20) /* CFF */ |
| 198 | #define OpCode_get Make_OpCode_ESC(21) /* CFF */ |
| 199 | #define OpCode_ifelse Make_OpCode_ESC(22) /* CFF */ |
| 200 | #define OpCode_random Make_OpCode_ESC(23) /* CFF */ |
| 201 | #define OpCode_mul Make_OpCode_ESC(24) /* CFF */ |
| 202 | //#define OpCode_reservedESC25 Make_OpCode_ESC(25) |
| 203 | #define OpCode_sqrt Make_OpCode_ESC(26) /* CFF */ |
| 204 | #define OpCode_dup Make_OpCode_ESC(27) /* CFF */ |
| 205 | #define OpCode_exch Make_OpCode_ESC(28) /* CFF */ |
| 206 | #define OpCode_index Make_OpCode_ESC(29) /* CFF */ |
| 207 | #define OpCode_roll Make_OpCode_ESC(30) /* CFF */ |
| 208 | #define OpCode_reservedESC31 Make_OpCode_ESC(31) |
| 209 | #define OpCode_reservedESC32 Make_OpCode_ESC(32) |
| 210 | #define OpCode_reservedESC33 Make_OpCode_ESC(33) |
| 211 | #define OpCode_hflex Make_OpCode_ESC(34) /* CFF, CFF2 */ |
| 212 | #define OpCode_flex Make_OpCode_ESC(35) /* CFF, CFF2 */ |
| 213 | #define OpCode_hflex1 Make_OpCode_ESC(36) /* CFF, CFF2 */ |
| 214 | #define OpCode_flex1 Make_OpCode_ESC(37) /* CFF, CFF2 */ |
Michiharu Ariza | eed7e6b | 2018-10-02 13:45:14 -0700 | [diff] [blame] | 215 | |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 216 | |
| 217 | #define OpCode_Invalid 0xFFFFu |
| 218 | |
Michiharu Ariza | 7d99a6c | 2018-10-12 02:16:07 -0700 | [diff] [blame] | 219 | |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 220 | struct number_t |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 221 | { |
Ebrahim Byagowi | 5179b96 | 2019-10-08 11:09:12 +0330 | [diff] [blame] | 222 | void set_int (int v) { value = v; } |
| 223 | int to_int () const { return value; } |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 224 | |
Behdad Esfahbod | cb320d2 | 2023-07-27 10:43:07 -0600 | [diff] [blame] | 225 | void set_fixed (int32_t v) { value = v / 65536.0; } |
| 226 | int32_t to_fixed () const { return value * 65536.0; } |
Michiharu Ariza | c0c85b8 | 2018-09-17 11:14:56 -0700 | [diff] [blame] | 227 | |
Behdad Esfahbod | cb320d2 | 2023-07-27 10:43:07 -0600 | [diff] [blame] | 228 | void set_real (double v) { value = v; } |
| 229 | double to_real () const { return value; } |
Michiharu Ariza | c0c85b8 | 2018-09-17 11:14:56 -0700 | [diff] [blame] | 230 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 231 | bool in_int_range () const |
Behdad Esfahbod | cb320d2 | 2023-07-27 10:43:07 -0600 | [diff] [blame] | 232 | { return ((double) (int16_t) to_int () == value); } |
Michiharu Ariza | c0c85b8 | 2018-09-17 11:14:56 -0700 | [diff] [blame] | 233 | |
Ebrahim Byagowi | 9db7ce7 | 2019-06-28 23:11:52 +0430 | [diff] [blame] | 234 | bool operator > (const number_t &n) const { return value > n.to_real (); } |
| 235 | bool operator < (const number_t &n) const { return n > *this; } |
| 236 | bool operator >= (const number_t &n) const { return !(*this < n); } |
| 237 | bool operator <= (const number_t &n) const { return !(*this > n); } |
Michiharu Ariza | c0c85b8 | 2018-09-17 11:14:56 -0700 | [diff] [blame] | 238 | |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 239 | const number_t &operator += (const number_t &n) |
Michiharu Ariza | c0c85b8 | 2018-09-17 11:14:56 -0700 | [diff] [blame] | 240 | { |
Michiharu Ariza | 9af33d7 | 2018-12-03 14:48:46 -0800 | [diff] [blame] | 241 | set_real (to_real () + n.to_real ()); |
Michiharu Ariza | b51418f | 2018-10-08 15:05:36 -0700 | [diff] [blame] | 242 | |
Michiharu Ariza | c0c85b8 | 2018-09-17 11:14:56 -0700 | [diff] [blame] | 243 | return *this; |
| 244 | } |
| 245 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 246 | protected: |
Behdad Esfahbod | cb320d2 | 2023-07-27 10:43:07 -0600 | [diff] [blame] | 247 | double value = 0.; |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | /* byte string */ |
| 251 | struct UnsizedByteStr : UnsizedArrayOf <HBUINT8> |
| 252 | { |
Behdad Esfahbod | fe1d85a | 2022-05-09 16:04:52 -0600 | [diff] [blame] | 253 | hb_ubytes_t as_ubytes (unsigned l) const |
| 254 | { return hb_ubytes_t ((const unsigned char *) this, l); } |
| 255 | |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 256 | // encode 2-byte int (Dict/CharString) or 4-byte int (Dict) |
ariza | 002f0e2 | 2020-02-27 17:34:26 -0800 | [diff] [blame] | 257 | template <typename T, typename V> |
| 258 | static bool serialize_int (hb_serialize_context_t *c, op_code_t intOp, V value) |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 259 | { |
| 260 | TRACE_SERIALIZE (this); |
| 261 | |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 262 | HBUINT8 *p = c->allocate_size<HBUINT8> (1); |
Ebrahim Byagowi | 2be859d | 2020-04-20 23:48:23 +0430 | [diff] [blame] | 263 | if (unlikely (!p)) return_trace (false); |
Behdad Esfahbod | b986c6a | 2019-03-29 20:17:46 -0700 | [diff] [blame] | 264 | *p = intOp; |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 265 | |
ariza | 002f0e2 | 2020-02-27 17:34:26 -0800 | [diff] [blame] | 266 | T *ip = c->allocate_size<T> (T::static_size); |
Ebrahim Byagowi | 2be859d | 2020-04-20 23:48:23 +0430 | [diff] [blame] | 267 | if (unlikely (!ip)) return_trace (false); |
Garret Rieger | b14475d | 2021-03-18 10:51:26 -0700 | [diff] [blame] | 268 | return_trace (c->check_assign (*ip, value, HB_SERIALIZE_ERROR_INT_OVERFLOW)); |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 269 | } |
Behdad Esfahbod | 592f39b | 2018-11-30 22:54:57 -0500 | [diff] [blame] | 270 | |
ariza | 002f0e2 | 2020-02-27 17:34:26 -0800 | [diff] [blame] | 271 | template <typename V> |
| 272 | static bool serialize_int4 (hb_serialize_context_t *c, V value) |
| 273 | { return serialize_int<HBINT32> (c, OpCode_longintdict, value); } |
Behdad Esfahbod | 592f39b | 2018-11-30 22:54:57 -0500 | [diff] [blame] | 274 | |
ariza | 002f0e2 | 2020-02-27 17:34:26 -0800 | [diff] [blame] | 275 | template <typename V> |
| 276 | static bool serialize_int2 (hb_serialize_context_t *c, V value) |
| 277 | { return serialize_int<HBINT16> (c, OpCode_shortint, value); } |
Michiharu Ariza | 471db3a | 2018-11-28 17:06:11 -0800 | [diff] [blame] | 278 | |
| 279 | /* Defining null_size allows a Null object may be created. Should be safe because: |
| 280 | * A descendent struct Dict uses a Null pointer to indicate a missing table, |
| 281 | * checked before access. |
Michiharu Ariza | 471db3a | 2018-11-28 17:06:11 -0800 | [diff] [blame] | 282 | */ |
| 283 | DEFINE_SIZE_MIN(0); |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 284 | }; |
| 285 | |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 286 | /* A byte string associated with the current offset and an error condition */ |
| 287 | struct byte_str_ref_t |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 288 | { |
Behdad Esfahbod | f66415c | 2022-11-21 09:39:27 -0700 | [diff] [blame] | 289 | byte_str_ref_t () |
Behdad Esfahbod | b51ab1a | 2022-11-21 10:27:07 -0700 | [diff] [blame] | 290 | : str () {} |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 291 | |
Behdad Esfahbod | 8aa54aa | 2022-05-09 16:09:56 -0600 | [diff] [blame] | 292 | byte_str_ref_t (const hb_ubytes_t &str_, unsigned int offset_ = 0) |
Behdad Esfahbod | b51ab1a | 2022-11-21 10:27:07 -0700 | [diff] [blame] | 293 | : str (str_) { set_offset (offset_); } |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 294 | |
Behdad Esfahbod | 8aa54aa | 2022-05-09 16:09:56 -0600 | [diff] [blame] | 295 | void reset (const hb_ubytes_t &str_, unsigned int offset_ = 0) |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 296 | { |
| 297 | str = str_; |
Behdad Esfahbod | b51ab1a | 2022-11-21 10:27:07 -0700 | [diff] [blame] | 298 | set_offset (offset_); |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 301 | const unsigned char& operator [] (int i) { |
Behdad Esfahbod | b51ab1a | 2022-11-21 10:27:07 -0700 | [diff] [blame] | 302 | if (unlikely ((unsigned int) (get_offset () + i) >= str.length)) |
Michiharu Ariza | 7d99a6c | 2018-10-12 02:16:07 -0700 | [diff] [blame] | 303 | { |
| 304 | set_error (); |
Ebrahim Byagowi | 9db7ce7 | 2019-06-28 23:11:52 +0430 | [diff] [blame] | 305 | return Null (unsigned char); |
Michiharu Ariza | 7d99a6c | 2018-10-12 02:16:07 -0700 | [diff] [blame] | 306 | } |
Behdad Esfahbod | d9de515 | 2022-11-21 14:23:07 -0700 | [diff] [blame] | 307 | return str.arrayZ[get_offset () + i]; |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Behdad Esfahbod | dcce53d | 2022-11-23 13:19:38 -0700 | [diff] [blame] | 310 | unsigned char head_unchecked () const { return str.arrayZ[get_offset ()]; } |
| 311 | |
Behdad Esfahbod | 8aa54aa | 2022-05-09 16:09:56 -0600 | [diff] [blame] | 312 | /* Conversion to hb_ubytes_t */ |
Behdad Esfahbod | b51ab1a | 2022-11-21 10:27:07 -0700 | [diff] [blame] | 313 | operator hb_ubytes_t () const { return str.sub_array (get_offset ()); } |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 314 | |
Behdad Esfahbod | 8aa54aa | 2022-05-09 16:09:56 -0600 | [diff] [blame] | 315 | hb_ubytes_t sub_array (unsigned int offset_, unsigned int len_) const |
Behdad Esfahbod | fe1d85a | 2022-05-09 16:04:52 -0600 | [diff] [blame] | 316 | { return str.sub_array (offset_, len_); } |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 317 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 318 | bool avail (unsigned int count=1) const |
Behdad Esfahbod | b51ab1a | 2022-11-21 10:27:07 -0700 | [diff] [blame] | 319 | { return get_offset () + count <= str.length; } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 320 | void inc (unsigned int count=1) |
Michiharu Ariza | 6708c55 | 2018-12-05 12:51:18 -0800 | [diff] [blame] | 321 | { |
Behdad Esfahbod | a81ec9b | 2022-11-21 14:03:28 -0700 | [diff] [blame] | 322 | /* Automatically puts us in error if count is out-of-range. */ |
| 323 | set_offset (get_offset () + count); |
Michiharu Ariza | 6708c55 | 2018-12-05 12:51:18 -0800 | [diff] [blame] | 324 | } |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 325 | |
Behdad Esfahbod | b51ab1a | 2022-11-21 10:27:07 -0700 | [diff] [blame] | 326 | /* We (ab)use ubytes backwards_length as a cursor (called offset), |
| 327 | * as well as to store error condition. */ |
Behdad Esfahbod | 7a39464 | 2022-11-21 09:48:54 -0700 | [diff] [blame] | 328 | |
Behdad Esfahbod | b51ab1a | 2022-11-21 10:27:07 -0700 | [diff] [blame] | 329 | unsigned get_offset () const { return str.backwards_length; } |
| 330 | void set_offset (unsigned offset) { str.backwards_length = offset; } |
| 331 | |
| 332 | void set_error () { str.backwards_length = str.length + 1; } |
| 333 | bool in_error () const { return str.backwards_length > str.length; } |
Michiharu Ariza | 7d99a6c | 2018-10-12 02:16:07 -0700 | [diff] [blame] | 334 | |
Behdad Esfahbod | dc3bb5e | 2022-11-21 18:18:48 -0700 | [diff] [blame] | 335 | unsigned total_size () const { return str.length; } |
| 336 | |
Behdad Esfahbod | 7a39464 | 2022-11-21 09:48:54 -0700 | [diff] [blame] | 337 | protected: |
Behdad Esfahbod | 8aa54aa | 2022-05-09 16:09:56 -0600 | [diff] [blame] | 338 | hb_ubytes_t str; |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 339 | }; |
| 340 | |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 341 | /* stack */ |
| 342 | template <typename ELEM, int LIMIT> |
Ebrahim Byagowi | 9fea6b4 | 2019-07-05 18:46:41 +0430 | [diff] [blame] | 343 | struct cff_stack_t |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 344 | { |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 345 | ELEM& operator [] (unsigned int i) |
Michiharu Ariza | 7d99a6c | 2018-10-12 02:16:07 -0700 | [diff] [blame] | 346 | { |
Behdad Esfahbod | 6106ef8 | 2022-05-09 18:12:09 -0600 | [diff] [blame] | 347 | if (unlikely (i >= count)) |
| 348 | { |
| 349 | set_error (); |
| 350 | return Crap (ELEM); |
| 351 | } |
Michiharu Ariza | 7d99a6c | 2018-10-12 02:16:07 -0700 | [diff] [blame] | 352 | return elements[i]; |
| 353 | } |
Michiharu Ariza | c0c85b8 | 2018-09-17 11:14:56 -0700 | [diff] [blame] | 354 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 355 | void push (const ELEM &v) |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 356 | { |
Behdad Esfahbod | 8c616a6 | 2022-05-09 17:49:54 -0600 | [diff] [blame] | 357 | if (likely (count < LIMIT)) |
Michiharu Ariza | 968168b | 2018-08-31 13:28:16 -0700 | [diff] [blame] | 358 | elements[count++] = v; |
Michiharu Ariza | 7d99a6c | 2018-10-12 02:16:07 -0700 | [diff] [blame] | 359 | else |
| 360 | set_error (); |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 361 | } |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 362 | ELEM &push () |
Michiharu Ariza | f2d299b | 2018-09-04 10:25:21 -0700 | [diff] [blame] | 363 | { |
Behdad Esfahbod | 8c616a6 | 2022-05-09 17:49:54 -0600 | [diff] [blame] | 364 | if (likely (count < LIMIT)) |
Michiharu Ariza | f2d299b | 2018-09-04 10:25:21 -0700 | [diff] [blame] | 365 | return elements[count++]; |
| 366 | else |
Michiharu Ariza | 7d99a6c | 2018-10-12 02:16:07 -0700 | [diff] [blame] | 367 | { |
| 368 | set_error (); |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 369 | return Crap (ELEM); |
Michiharu Ariza | 7d99a6c | 2018-10-12 02:16:07 -0700 | [diff] [blame] | 370 | } |
Michiharu Ariza | f2d299b | 2018-09-04 10:25:21 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 373 | ELEM& pop () |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 374 | { |
Michiharu Ariza | 968168b | 2018-08-31 13:28:16 -0700 | [diff] [blame] | 375 | if (likely (count > 0)) |
| 376 | return elements[--count]; |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 377 | else |
Michiharu Ariza | 7d99a6c | 2018-10-12 02:16:07 -0700 | [diff] [blame] | 378 | { |
| 379 | set_error (); |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 380 | return Crap (ELEM); |
Michiharu Ariza | 7d99a6c | 2018-10-12 02:16:07 -0700 | [diff] [blame] | 381 | } |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 382 | } |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 383 | void pop (unsigned int n) |
Michiharu Ariza | c0c85b8 | 2018-09-17 11:14:56 -0700 | [diff] [blame] | 384 | { |
| 385 | if (likely (count >= n)) |
| 386 | count -= n; |
Michiharu Ariza | 7d99a6c | 2018-10-12 02:16:07 -0700 | [diff] [blame] | 387 | else |
| 388 | set_error (); |
Michiharu Ariza | c0c85b8 | 2018-09-17 11:14:56 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 391 | const ELEM& peek () |
Michiharu Ariza | f2d299b | 2018-09-04 10:25:21 -0700 | [diff] [blame] | 392 | { |
Behdad Esfahbod | 1b14d2f | 2022-05-09 18:15:31 -0600 | [diff] [blame] | 393 | if (unlikely (count == 0)) |
Michiharu Ariza | 7d99a6c | 2018-10-12 02:16:07 -0700 | [diff] [blame] | 394 | { |
| 395 | set_error (); |
Ebrahim Byagowi | 2dda6dd | 2020-04-20 14:12:45 +0430 | [diff] [blame] | 396 | return Null (ELEM); |
Michiharu Ariza | 7d99a6c | 2018-10-12 02:16:07 -0700 | [diff] [blame] | 397 | } |
Ebrahim Byagowi | 9db7ce7 | 2019-06-28 23:11:52 +0430 | [diff] [blame] | 398 | return elements[count - 1]; |
Michiharu Ariza | f2d299b | 2018-09-04 10:25:21 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 401 | void unpop () |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 402 | { |
Behdad Esfahbod | 8c616a6 | 2022-05-09 17:49:54 -0600 | [diff] [blame] | 403 | if (likely (count < LIMIT)) |
Michiharu Ariza | 968168b | 2018-08-31 13:28:16 -0700 | [diff] [blame] | 404 | count++; |
Michiharu Ariza | 7d99a6c | 2018-10-12 02:16:07 -0700 | [diff] [blame] | 405 | else |
| 406 | set_error (); |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 409 | void clear () { count = 0; } |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 410 | |
Behdad Esfahbod | 8c616a6 | 2022-05-09 17:49:54 -0600 | [diff] [blame] | 411 | bool in_error () const { return (error); } |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 412 | void set_error () { error = true; } |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 413 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 414 | unsigned int get_count () const { return count; } |
Ebrahim Byagowi | 9db7ce7 | 2019-06-28 23:11:52 +0430 | [diff] [blame] | 415 | bool is_empty () const { return !count; } |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 416 | |
Behdad Esfahbod | 0a42410 | 2022-05-10 12:05:19 -0600 | [diff] [blame] | 417 | hb_array_t<const ELEM> sub_array (unsigned start, unsigned length) const |
| 418 | { return hb_array_t<const ELEM> (elements).sub_array (start, length); } |
Behdad Esfahbod | 6106ef8 | 2022-05-09 18:12:09 -0600 | [diff] [blame] | 419 | |
| 420 | private: |
Behdad Esfahbod | bff78e6 | 2022-05-10 16:33:37 -0600 | [diff] [blame] | 421 | bool error = false; |
| 422 | unsigned int count = 0; |
Behdad Esfahbod | 8c616a6 | 2022-05-09 17:49:54 -0600 | [diff] [blame] | 423 | ELEM elements[LIMIT]; |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 424 | }; |
| 425 | |
| 426 | /* argument stack */ |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 427 | template <typename ARG=number_t> |
Ebrahim Byagowi | 9fea6b4 | 2019-07-05 18:46:41 +0430 | [diff] [blame] | 428 | struct arg_stack_t : cff_stack_t<ARG, 513> |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 429 | { |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 430 | void push_int (int v) |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 431 | { |
Michiharu Ariza | f2d299b | 2018-09-04 10:25:21 -0700 | [diff] [blame] | 432 | ARG &n = S::push (); |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 433 | n.set_int (v); |
Michiharu Ariza | f2d299b | 2018-09-04 10:25:21 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Ebrahim Byagowi | b2ebaa9 | 2018-12-16 22:38:10 +0330 | [diff] [blame] | 436 | void push_fixed (int32_t v) |
Michiharu Ariza | f2d299b | 2018-09-04 10:25:21 -0700 | [diff] [blame] | 437 | { |
| 438 | ARG &n = S::push (); |
| 439 | n.set_fixed (v); |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Behdad Esfahbod | cb320d2 | 2023-07-27 10:43:07 -0600 | [diff] [blame] | 442 | void push_real (double v) |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 443 | { |
Michiharu Ariza | f2d299b | 2018-09-04 10:25:21 -0700 | [diff] [blame] | 444 | ARG &n = S::push (); |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 445 | n.set_real (v); |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 448 | ARG& pop_num () { return this->pop (); } |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 449 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 450 | int pop_int () { return this->pop ().to_int (); } |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 451 | |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 452 | unsigned int pop_uint () |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 453 | { |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 454 | int i = pop_int (); |
Michiharu Ariza | 7d99a6c | 2018-10-12 02:16:07 -0700 | [diff] [blame] | 455 | if (unlikely (i < 0)) |
| 456 | { |
| 457 | i = 0; |
| 458 | S::set_error (); |
| 459 | } |
Ebrahim Byagowi | 9db7ce7 | 2019-06-28 23:11:52 +0430 | [diff] [blame] | 460 | return (unsigned) i; |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 463 | void push_longint_from_substr (byte_str_ref_t& str_ref) |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 464 | { |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 465 | push_int ((str_ref[0] << 24) | (str_ref[1] << 16) | (str_ref[2] << 8) | (str_ref[3])); |
| 466 | str_ref.inc (4); |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 469 | bool push_fixed_from_substr (byte_str_ref_t& str_ref) |
Michiharu Ariza | 27c32d8 | 2018-08-22 11:36:39 -0700 | [diff] [blame] | 470 | { |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 471 | if (unlikely (!str_ref.avail (4))) |
Michiharu Ariza | 27c32d8 | 2018-08-22 11:36:39 -0700 | [diff] [blame] | 472 | return false; |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 473 | push_fixed ((int32_t)*(const HBUINT32*)&str_ref[0]); |
| 474 | str_ref.inc (4); |
Michiharu Ariza | 27c32d8 | 2018-08-22 11:36:39 -0700 | [diff] [blame] | 475 | return true; |
| 476 | } |
| 477 | |
Michiharu Ariza | fcf1778 | 2018-08-31 16:28:47 -0700 | [diff] [blame] | 478 | private: |
Ebrahim Byagowi | 9fea6b4 | 2019-07-05 18:46:41 +0430 | [diff] [blame] | 479 | typedef cff_stack_t<ARG, 513> S; |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 480 | }; |
| 481 | |
| 482 | /* an operator prefixed by its operands in a byte string */ |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 483 | struct op_str_t |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 484 | { |
Behdad Esfahbod | 28e767d | 2022-11-21 21:59:51 -0700 | [diff] [blame] | 485 | /* This used to have a hb_ubytes_t. Using a pointer and length |
| 486 | * in a particular order, saves 8 bytes in this struct and more |
| 487 | * in our parsed_cs_op_t subclass. */ |
| 488 | |
Behdad Esfahbod | 4f056b9 | 2022-11-21 21:37:38 -0700 | [diff] [blame] | 489 | const unsigned char *ptr = nullptr; |
Behdad Esfahbod | 4f056b9 | 2022-11-21 21:37:38 -0700 | [diff] [blame] | 490 | |
Behdad Esfahbod | d3ed6ee | 2023-01-01 23:29:35 -0700 | [diff] [blame] | 491 | op_code_t op = OpCode_Invalid; |
Behdad Esfahbod | 28e767d | 2022-11-21 21:59:51 -0700 | [diff] [blame] | 492 | |
| 493 | uint8_t length = 0; |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 494 | }; |
| 495 | |
| 496 | /* base of OP_SERIALIZER */ |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 497 | struct op_serializer_t |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 498 | { |
| 499 | protected: |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 500 | bool copy_opstr (hb_serialize_context_t *c, const op_str_t& opstr) const |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 501 | { |
| 502 | TRACE_SERIALIZE (this); |
| 503 | |
Behdad Esfahbod | 13e1ca9 | 2022-11-22 12:19:28 -0700 | [diff] [blame] | 504 | unsigned char *d = c->allocate_size<unsigned char> (opstr.length); |
Ebrahim Byagowi | 2be859d | 2020-04-20 23:48:23 +0430 | [diff] [blame] | 505 | if (unlikely (!d)) return_trace (false); |
Behdad Esfahbod | 59c45f6 | 2022-11-22 12:54:50 -0700 | [diff] [blame] | 506 | /* Faster than hb_memcpy for small strings. */ |
Behdad Esfahbod | 13e1ca9 | 2022-11-22 12:19:28 -0700 | [diff] [blame] | 507 | for (unsigned i = 0; i < opstr.length; i++) |
| 508 | d[i] = opstr.ptr[i]; |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 509 | return_trace (true); |
| 510 | } |
| 511 | }; |
| 512 | |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 513 | template <typename VAL> |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 514 | struct parsed_values_t |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 515 | { |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 516 | void init () |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 517 | { |
| 518 | opStart = 0; |
| 519 | values.init (); |
| 520 | } |
Behdad Esfahbod | 6ef83c2 | 2022-01-18 13:08:34 -0700 | [diff] [blame] | 521 | void fini () { values.fini (); } |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 522 | |
Behdad Esfahbod | f085379 | 2022-05-11 12:10:03 -0600 | [diff] [blame] | 523 | void alloc (unsigned n) |
| 524 | { |
Behdad Esfahbod | 0ec0214 | 2022-12-31 13:18:32 -0700 | [diff] [blame] | 525 | values.alloc (n, true); |
Behdad Esfahbod | f085379 | 2022-05-11 12:10:03 -0600 | [diff] [blame] | 526 | } |
| 527 | |
Behdad Esfahbod | d5e1748 | 2023-01-02 12:26:43 -0700 | [diff] [blame] | 528 | void add_op (op_code_t op, const byte_str_ref_t& str_ref = byte_str_ref_t (), const VAL &v = VAL ()) |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 529 | { |
| 530 | VAL *val = values.push (v); |
| 531 | val->op = op; |
Behdad Esfahbod | 4f056b9 | 2022-11-21 21:37:38 -0700 | [diff] [blame] | 532 | auto arr = str_ref.sub_array (opStart, str_ref.get_offset () - opStart); |
| 533 | val->ptr = arr.arrayZ; |
| 534 | val->length = arr.length; |
Behdad Esfahbod | 7a39464 | 2022-11-21 09:48:54 -0700 | [diff] [blame] | 535 | opStart = str_ref.get_offset (); |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 538 | bool has_op (op_code_t op) const |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 539 | { |
Behdad Esfahbod | acdab17 | 2022-05-13 14:14:36 -0600 | [diff] [blame] | 540 | for (const auto& v : values) |
| 541 | if (v.op == op) return true; |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 542 | return false; |
| 543 | } |
| 544 | |
Behdad Esfahbod | 474a120 | 2018-12-21 18:46:51 -0500 | [diff] [blame] | 545 | unsigned get_count () const { return values.length; } |
Behdad Esfahbod | d480ae1 | 2022-11-24 13:53:43 -0700 | [diff] [blame] | 546 | const VAL &operator [] (unsigned int i) const { return values[i]; } |
Michiharu Ariza | d56e338 | 2018-10-31 22:30:34 -0700 | [diff] [blame] | 547 | |
| 548 | unsigned int opStart; |
| 549 | hb_vector_t<VAL> values; |
| 550 | }; |
| 551 | |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 552 | template <typename ARG=number_t> |
| 553 | struct interp_env_t |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 554 | { |
Behdad Esfahbod | bff78e6 | 2022-05-10 16:33:37 -0600 | [diff] [blame] | 555 | interp_env_t () {} |
| 556 | interp_env_t (const hb_ubytes_t &str_) |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 557 | { |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 558 | str_ref.reset (str_); |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 559 | } |
Ebrahim Byagowi | e412008 | 2018-12-17 21:31:01 +0330 | [diff] [blame] | 560 | bool in_error () const |
Behdad Esfahbod | 5945150 | 2022-11-21 15:23:16 -0700 | [diff] [blame] | 561 | { return str_ref.in_error () || argStack.in_error (); } |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 562 | |
Behdad Esfahbod | 5945150 | 2022-11-21 15:23:16 -0700 | [diff] [blame] | 563 | void set_error () { str_ref.set_error (); } |
Michiharu Ariza | 7d99a6c | 2018-10-12 02:16:07 -0700 | [diff] [blame] | 564 | |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 565 | op_code_t fetch_op () |
Michiharu Ariza | 7d99a6c | 2018-10-12 02:16:07 -0700 | [diff] [blame] | 566 | { |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 567 | op_code_t op = OpCode_Invalid; |
| 568 | if (unlikely (!str_ref.avail ())) |
Michiharu Ariza | 7d99a6c | 2018-10-12 02:16:07 -0700 | [diff] [blame] | 569 | return OpCode_Invalid; |
Behdad Esfahbod | dcce53d | 2022-11-23 13:19:38 -0700 | [diff] [blame] | 570 | op = (op_code_t) str_ref.head_unchecked (); |
Behdad Esfahbod | 6905d36 | 2022-11-21 10:51:33 -0700 | [diff] [blame] | 571 | str_ref.inc (); |
Michiharu Ariza | cef75ea | 2018-08-17 13:13:18 -0700 | [diff] [blame] | 572 | if (op == OpCode_escape) { |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 573 | if (unlikely (!str_ref.avail ())) |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 574 | return OpCode_Invalid; |
Behdad Esfahbod | dcce53d | 2022-11-23 13:19:38 -0700 | [diff] [blame] | 575 | op = Make_OpCode_ESC (str_ref.head_unchecked ()); |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 576 | str_ref.inc (); |
Michiharu Ariza | cef75ea | 2018-08-17 13:13:18 -0700 | [diff] [blame] | 577 | } |
Michiharu Ariza | 7d99a6c | 2018-10-12 02:16:07 -0700 | [diff] [blame] | 578 | return op; |
Michiharu Ariza | cef75ea | 2018-08-17 13:13:18 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Ebrahim Byagowi | 9db7ce7 | 2019-06-28 23:11:52 +0430 | [diff] [blame] | 581 | const ARG& eval_arg (unsigned int i) { return argStack[i]; } |
Michiharu Ariza | b51418f | 2018-10-08 15:05:36 -0700 | [diff] [blame] | 582 | |
Ebrahim Byagowi | 9db7ce7 | 2019-06-28 23:11:52 +0430 | [diff] [blame] | 583 | ARG& pop_arg () { return argStack.pop (); } |
| 584 | void pop_n_args (unsigned int n) { argStack.pop (n); } |
Michiharu Ariza | b51418f | 2018-10-08 15:05:36 -0700 | [diff] [blame] | 585 | |
Ebrahim Byagowi | 9db7ce7 | 2019-06-28 23:11:52 +0430 | [diff] [blame] | 586 | void clear_args () { pop_n_args (argStack.get_count ()); } |
Michiharu Ariza | 968168b | 2018-08-31 13:28:16 -0700 | [diff] [blame] | 587 | |
Ebrahim Byagowi | 9db7ce7 | 2019-06-28 23:11:52 +0430 | [diff] [blame] | 588 | byte_str_ref_t |
| 589 | str_ref; |
| 590 | arg_stack_t<ARG> |
| 591 | argStack; |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 592 | }; |
| 593 | |
Behdad Esfahbod | bff78e6 | 2022-05-10 16:33:37 -0600 | [diff] [blame] | 594 | using num_interp_env_t = interp_env_t<>; |
Michiharu Ariza | fcf1778 | 2018-08-31 16:28:47 -0700 | [diff] [blame] | 595 | |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 596 | template <typename ARG=number_t> |
| 597 | struct opset_t |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 598 | { |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 599 | static void process_op (op_code_t op, interp_env_t<ARG>& env) |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 600 | { |
| 601 | switch (op) { |
| 602 | case OpCode_shortint: |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 603 | env.argStack.push_int ((int16_t)((env.str_ref[0] << 8) | env.str_ref[1])); |
| 604 | env.str_ref.inc (2); |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 605 | break; |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 606 | |
| 607 | case OpCode_TwoBytePosInt0: case OpCode_TwoBytePosInt1: |
| 608 | case OpCode_TwoBytePosInt2: case OpCode_TwoBytePosInt3: |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 609 | env.argStack.push_int ((int16_t)((op - OpCode_TwoBytePosInt0) * 256 + env.str_ref[0] + 108)); |
| 610 | env.str_ref.inc (); |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 611 | break; |
Behdad Esfahbod | 592f39b | 2018-11-30 22:54:57 -0500 | [diff] [blame] | 612 | |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 613 | case OpCode_TwoByteNegInt0: case OpCode_TwoByteNegInt1: |
| 614 | case OpCode_TwoByteNegInt2: case OpCode_TwoByteNegInt3: |
Michiharu Ariza | dd4c375 | 2019-04-18 10:38:57 -0700 | [diff] [blame] | 615 | env.argStack.push_int ((-(int16_t)(op - OpCode_TwoByteNegInt0) * 256 - env.str_ref[0] - 108)); |
Michiharu Ariza | 29f0b6b | 2018-12-22 07:47:04 -0800 | [diff] [blame] | 616 | env.str_ref.inc (); |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 617 | break; |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 618 | |
| 619 | default: |
Behdad Esfahbod | 825df6d | 2018-11-30 23:04:59 -0500 | [diff] [blame] | 620 | /* 1-byte integer */ |
| 621 | if (likely ((OpCode_OneByteIntFirst <= op) && (op <= OpCode_OneByteIntLast))) |
| 622 | { |
| 623 | env.argStack.push_int ((int)op - 139); |
| 624 | } else { |
| 625 | /* invalid unknown operator */ |
| 626 | env.clear_args (); |
| 627 | env.set_error (); |
| 628 | } |
| 629 | break; |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 630 | } |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 631 | } |
| 632 | }; |
| 633 | |
| 634 | template <typename ENV> |
Ebrahim Byagowi | 9db7ce7 | 2019-06-28 23:11:52 +0430 | [diff] [blame] | 635 | struct interpreter_t |
| 636 | { |
Behdad Esfahbod | bff78e6 | 2022-05-10 16:33:37 -0600 | [diff] [blame] | 637 | interpreter_t (ENV& env_) : env (env_) {} |
| 638 | ENV& env; |
Michiharu Ariza | 633ce88 | 2018-08-15 12:00:19 -0700 | [diff] [blame] | 639 | }; |
| 640 | |
| 641 | } /* namespace CFF */ |
| 642 | |
Michiharu Ariza | 1d1afdd | 2018-08-29 13:36:39 -0700 | [diff] [blame] | 643 | #endif /* HB_CFF_INTERP_COMMON_HH */ |