| 1 | #ifndef OT_GLYF_SIMPLEGLYPH_HH |
| 2 | #define OT_GLYF_SIMPLEGLYPH_HH |
| 3 | |
| 4 | |
| 5 | #include "../../hb-open-type.hh" |
| 6 | |
| 7 | |
| 8 | namespace OT { |
| 9 | namespace glyf_impl { |
| 10 | |
| 11 | |
| 12 | struct SimpleGlyph |
| 13 | { |
| 14 | enum simple_glyph_flag_t |
| 15 | { |
| 16 | FLAG_ON_CURVE = 0x01, |
| 17 | FLAG_X_SHORT = 0x02, |
| 18 | FLAG_Y_SHORT = 0x04, |
| 19 | FLAG_REPEAT = 0x08, |
| 20 | FLAG_X_SAME = 0x10, |
| 21 | FLAG_Y_SAME = 0x20, |
| 22 | FLAG_OVERLAP_SIMPLE = 0x40, |
| 23 | FLAG_CUBIC = 0x80 |
| 24 | }; |
| 25 | |
| 26 | const GlyphHeader &; |
| 27 | hb_bytes_t bytes; |
| 28 | (const GlyphHeader &, hb_bytes_t bytes_) : |
| 29 | header (header_), bytes (bytes_) {} |
| 30 | |
| 31 | unsigned int instruction_len_offset () const |
| 32 | { return GlyphHeader::static_size + 2 * header.numberOfContours; } |
| 33 | |
| 34 | unsigned int length (unsigned int instruction_len) const |
| 35 | { return instruction_len_offset () + 2 + instruction_len; } |
| 36 | |
| 37 | bool has_instructions_length () const |
| 38 | { |
| 39 | return instruction_len_offset () + 2 <= bytes.length; |
| 40 | } |
| 41 | |
| 42 | unsigned int instructions_length () const |
| 43 | { |
| 44 | unsigned int instruction_length_offset = instruction_len_offset (); |
| 45 | if (unlikely (instruction_length_offset + 2 > bytes.length)) return 0; |
| 46 | |
| 47 | const HBUINT16 &instructionLength = StructAtOffset<HBUINT16> (&bytes, instruction_length_offset); |
| 48 | /* Out of bounds of the current glyph */ |
| 49 | if (unlikely (length (instructionLength) > bytes.length)) return 0; |
| 50 | return instructionLength; |
| 51 | } |
| 52 | |
| 53 | const hb_bytes_t trim_padding () const |
| 54 | { |
| 55 | /* based on FontTools _g_l_y_f.py::trim */ |
| 56 | const uint8_t *glyph = (uint8_t*) bytes.arrayZ; |
| 57 | const uint8_t *glyph_end = glyph + bytes.length; |
| 58 | /* simple glyph w/contours, possibly trimmable */ |
| 59 | glyph += instruction_len_offset (); |
| 60 | |
| 61 | if (unlikely (glyph + 2 >= glyph_end)) return hb_bytes_t (); |
| 62 | unsigned int num_coordinates = StructAtOffset<HBUINT16> (glyph - 2, 0) + 1; |
| 63 | unsigned int num_instructions = StructAtOffset<HBUINT16> (glyph, 0); |
| 64 | |
| 65 | glyph += 2 + num_instructions; |
| 66 | |
| 67 | unsigned int coord_bytes = 0; |
| 68 | unsigned int coords_with_flags = 0; |
| 69 | while (glyph < glyph_end) |
| 70 | { |
| 71 | uint8_t flag = *glyph; |
| 72 | glyph++; |
| 73 | |
| 74 | unsigned int repeat = 1; |
| 75 | if (flag & FLAG_REPEAT) |
| 76 | { |
| 77 | if (unlikely (glyph >= glyph_end)) return hb_bytes_t (); |
| 78 | repeat = *glyph + 1; |
| 79 | glyph++; |
| 80 | } |
| 81 | |
| 82 | unsigned int xBytes, yBytes; |
| 83 | xBytes = yBytes = 0; |
| 84 | if (flag & FLAG_X_SHORT) xBytes = 1; |
| 85 | else if ((flag & FLAG_X_SAME) == 0) xBytes = 2; |
| 86 | |
| 87 | if (flag & FLAG_Y_SHORT) yBytes = 1; |
| 88 | else if ((flag & FLAG_Y_SAME) == 0) yBytes = 2; |
| 89 | |
| 90 | coord_bytes += (xBytes + yBytes) * repeat; |
| 91 | coords_with_flags += repeat; |
| 92 | if (coords_with_flags >= num_coordinates) break; |
| 93 | } |
| 94 | |
| 95 | if (unlikely (coords_with_flags != num_coordinates)) return hb_bytes_t (); |
| 96 | return bytes.sub_array (0, bytes.length + coord_bytes - (glyph_end - glyph)); |
| 97 | } |
| 98 | |
| 99 | /* zero instruction length */ |
| 100 | void drop_hints () |
| 101 | { |
| 102 | if (!has_instructions_length ()) return; |
| 103 | GlyphHeader & = const_cast<GlyphHeader &> (header); |
| 104 | (HBUINT16 &) StructAtOffset<HBUINT16> (&glyph_header, instruction_len_offset ()) = 0; |
| 105 | } |
| 106 | |
| 107 | void drop_hints_bytes (hb_bytes_t &dest_start, hb_bytes_t &dest_end) const |
| 108 | { |
| 109 | unsigned int instructions_len = instructions_length (); |
| 110 | unsigned int glyph_length = length (instructions_len); |
| 111 | dest_start = bytes.sub_array (0, glyph_length - instructions_len); |
| 112 | dest_end = bytes.sub_array (glyph_length, bytes.length - glyph_length); |
| 113 | } |
| 114 | |
| 115 | void set_overlaps_flag () |
| 116 | { |
| 117 | if (unlikely (!header.numberOfContours)) return; |
| 118 | |
| 119 | unsigned flags_offset = length (instructions_length ()); |
| 120 | if (unlikely (flags_offset + 1 > bytes.length)) return; |
| 121 | |
| 122 | HBUINT8 &first_flag = (HBUINT8 &) StructAtOffset<HBUINT16> (&bytes, flags_offset); |
| 123 | first_flag = (uint8_t) first_flag | FLAG_OVERLAP_SIMPLE; |
| 124 | } |
| 125 | |
| 126 | static bool read_flags (const HBUINT8 *&p /* IN/OUT */, |
| 127 | hb_array_t<contour_point_t> points_ /* IN/OUT */, |
| 128 | const HBUINT8 *end) |
| 129 | { |
| 130 | unsigned count = points_.length; |
| 131 | for (unsigned int i = 0; i < count;) |
| 132 | { |
| 133 | if (unlikely (p + 1 > end)) return false; |
| 134 | uint8_t flag = *p++; |
| 135 | points_.arrayZ[i++].flag = flag; |
| 136 | if (flag & FLAG_REPEAT) |
| 137 | { |
| 138 | if (unlikely (p + 1 > end)) return false; |
| 139 | unsigned int repeat_count = *p++; |
| 140 | unsigned stop = hb_min (i + repeat_count, count); |
| 141 | for (; i < stop; i++) |
| 142 | points_.arrayZ[i].flag = flag; |
| 143 | } |
| 144 | } |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | static bool read_points (const HBUINT8 *&p /* IN/OUT */, |
| 149 | hb_array_t<contour_point_t> points_ /* IN/OUT */, |
| 150 | const HBUINT8 *end, |
| 151 | float contour_point_t::*m, |
| 152 | const simple_glyph_flag_t short_flag, |
| 153 | const simple_glyph_flag_t same_flag) |
| 154 | { |
| 155 | int v = 0; |
| 156 | |
| 157 | for (auto &point : points_) |
| 158 | { |
| 159 | unsigned flag = point.flag; |
| 160 | if (flag & short_flag) |
| 161 | { |
| 162 | if (unlikely (p + 1 > end)) return false; |
| 163 | if (flag & same_flag) |
| 164 | v += *p++; |
| 165 | else |
| 166 | v -= *p++; |
| 167 | } |
| 168 | else |
| 169 | { |
| 170 | if (!(flag & same_flag)) |
| 171 | { |
| 172 | if (unlikely (p + HBINT16::static_size > end)) return false; |
| 173 | v += *(const HBINT16 *) p; |
| 174 | p += HBINT16::static_size; |
| 175 | } |
| 176 | } |
| 177 | point.*m = v; |
| 178 | } |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | bool get_contour_points (contour_point_vector_t &points /* OUT */, |
| 183 | bool phantom_only = false) const |
| 184 | { |
| 185 | const HBUINT16 *endPtsOfContours = &StructAfter<HBUINT16> (header); |
| 186 | int num_contours = header.numberOfContours; |
| 187 | assert (num_contours > 0); |
| 188 | /* One extra item at the end, for the instruction-count below. */ |
| 189 | if (unlikely (!bytes.check_range (&endPtsOfContours[num_contours]))) return false; |
| 190 | unsigned int num_points = endPtsOfContours[num_contours - 1] + 1; |
| 191 | |
| 192 | unsigned old_length = points.length; |
| 193 | points.alloc (points.length + num_points + 4, true); // Allocate for phantom points, to avoid a possible copy |
| 194 | if (unlikely (!points.resize (points.length + num_points, false))) return false; |
| 195 | auto points_ = points.as_array ().sub_array (old_length); |
| 196 | if (!phantom_only) |
| 197 | hb_memset (points_.arrayZ, 0, sizeof (contour_point_t) * num_points); |
| 198 | if (phantom_only) return true; |
| 199 | |
| 200 | for (int i = 0; i < num_contours; i++) |
| 201 | points_[endPtsOfContours[i]].is_end_point = true; |
| 202 | |
| 203 | /* Skip instructions */ |
| 204 | const HBUINT8 *p = &StructAtOffset<HBUINT8> (&endPtsOfContours[num_contours + 1], |
| 205 | endPtsOfContours[num_contours]); |
| 206 | |
| 207 | if (unlikely ((const char *) p < bytes.arrayZ)) return false; /* Unlikely overflow */ |
| 208 | const HBUINT8 *end = (const HBUINT8 *) (bytes.arrayZ + bytes.length); |
| 209 | if (unlikely (p >= end)) return false; |
| 210 | |
| 211 | /* Read x & y coordinates */ |
| 212 | return read_flags (p, points_, end) |
| 213 | && read_points (p, points_, end, &contour_point_t::x, |
| 214 | FLAG_X_SHORT, FLAG_X_SAME) |
| 215 | && read_points (p, points_, end, &contour_point_t::y, |
| 216 | FLAG_Y_SHORT, FLAG_Y_SAME); |
| 217 | } |
| 218 | |
| 219 | static void encode_coord (int value, |
| 220 | unsigned &flag, |
| 221 | const simple_glyph_flag_t short_flag, |
| 222 | const simple_glyph_flag_t same_flag, |
| 223 | hb_vector_t<uint8_t> &coords /* OUT */) |
| 224 | { |
| 225 | if (value == 0) |
| 226 | { |
| 227 | flag |= same_flag; |
| 228 | } |
| 229 | else if (value >= -255 && value <= 255) |
| 230 | { |
| 231 | flag |= short_flag; |
| 232 | if (value > 0) flag |= same_flag; |
| 233 | else value = -value; |
| 234 | |
| 235 | coords.arrayZ[coords.length++] = (uint8_t) value; |
| 236 | } |
| 237 | else |
| 238 | { |
| 239 | int16_t val = value; |
| 240 | coords.arrayZ[coords.length++] = val >> 8; |
| 241 | coords.arrayZ[coords.length++] = val & 0xff; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | static void encode_flag (unsigned flag, |
| 246 | unsigned &repeat, |
| 247 | unsigned lastflag, |
| 248 | hb_vector_t<uint8_t> &flags /* OUT */) |
| 249 | { |
| 250 | if (flag == lastflag && repeat != 255) |
| 251 | { |
| 252 | repeat++; |
| 253 | if (repeat == 1) |
| 254 | { |
| 255 | /* We know there's room. */ |
| 256 | flags.arrayZ[flags.length++] = flag; |
| 257 | } |
| 258 | else |
| 259 | { |
| 260 | unsigned len = flags.length; |
| 261 | flags.arrayZ[len-2] = flag | FLAG_REPEAT; |
| 262 | flags.arrayZ[len-1] = repeat; |
| 263 | } |
| 264 | } |
| 265 | else |
| 266 | { |
| 267 | repeat = 0; |
| 268 | flags.arrayZ[flags.length++] = flag; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | bool compile_bytes_with_deltas (const contour_point_vector_t &all_points, |
| 273 | bool no_hinting, |
| 274 | hb_bytes_t &dest_bytes /* OUT */) |
| 275 | { |
| 276 | if (header.numberOfContours == 0 || all_points.length <= 4) |
| 277 | { |
| 278 | dest_bytes = hb_bytes_t (); |
| 279 | return true; |
| 280 | } |
| 281 | unsigned num_points = all_points.length - 4; |
| 282 | |
| 283 | hb_vector_t<uint8_t> flags, x_coords, y_coords; |
| 284 | if (unlikely (!flags.alloc (num_points, true))) return false; |
| 285 | if (unlikely (!x_coords.alloc (2*num_points, true))) return false; |
| 286 | if (unlikely (!y_coords.alloc (2*num_points, true))) return false; |
| 287 | |
| 288 | unsigned lastflag = 255, repeat = 0; |
| 289 | int prev_x = 0, prev_y = 0; |
| 290 | |
| 291 | for (unsigned i = 0; i < num_points; i++) |
| 292 | { |
| 293 | unsigned flag = all_points.arrayZ[i].flag; |
| 294 | flag &= FLAG_ON_CURVE | FLAG_OVERLAP_SIMPLE | FLAG_CUBIC; |
| 295 | |
| 296 | int cur_x = roundf (all_points.arrayZ[i].x); |
| 297 | int cur_y = roundf (all_points.arrayZ[i].y); |
| 298 | encode_coord (cur_x - prev_x, flag, FLAG_X_SHORT, FLAG_X_SAME, x_coords); |
| 299 | encode_coord (cur_y - prev_y, flag, FLAG_Y_SHORT, FLAG_Y_SAME, y_coords); |
| 300 | encode_flag (flag, repeat, lastflag, flags); |
| 301 | |
| 302 | prev_x = cur_x; |
| 303 | prev_y = cur_y; |
| 304 | lastflag = flag; |
| 305 | } |
| 306 | |
| 307 | unsigned len_before_instrs = 2 * header.numberOfContours + 2; |
| 308 | unsigned len_instrs = instructions_length (); |
| 309 | unsigned total_len = len_before_instrs + flags.length + x_coords.length + y_coords.length; |
| 310 | |
| 311 | if (!no_hinting) |
| 312 | total_len += len_instrs; |
| 313 | |
| 314 | char *p = (char *) hb_malloc (total_len); |
| 315 | if (unlikely (!p)) return false; |
| 316 | |
| 317 | const char *src = bytes.arrayZ + GlyphHeader::static_size; |
| 318 | char *cur = p; |
| 319 | hb_memcpy (p, src, len_before_instrs); |
| 320 | |
| 321 | cur += len_before_instrs; |
| 322 | src += len_before_instrs; |
| 323 | |
| 324 | if (!no_hinting) |
| 325 | { |
| 326 | hb_memcpy (cur, src, len_instrs); |
| 327 | cur += len_instrs; |
| 328 | } |
| 329 | |
| 330 | hb_memcpy (cur, flags.arrayZ, flags.length); |
| 331 | cur += flags.length; |
| 332 | |
| 333 | hb_memcpy (cur, x_coords.arrayZ, x_coords.length); |
| 334 | cur += x_coords.length; |
| 335 | |
| 336 | hb_memcpy (cur, y_coords.arrayZ, y_coords.length); |
| 337 | |
| 338 | dest_bytes = hb_bytes_t (p, total_len); |
| 339 | return true; |
| 340 | } |
| 341 | }; |
| 342 | |
| 343 | |
| 344 | } /* namespace glyf_impl */ |
| 345 | } /* namespace OT */ |
| 346 | |
| 347 | |
| 348 | #endif /* OT_GLYF_SIMPLEGLYPH_HH */ |
| 349 | |