SimpleGlyph.hh (10406B)
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 &header; 27 hb_bytes_t bytes; 28 SimpleGlyph (const GlyphHeader &header_, 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 &glyph_header = 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 auto *points = points_.arrayZ; 131 unsigned count = points_.length; 132 for (unsigned int i = 0; i < count;) 133 { 134 if (unlikely (p + 1 > end)) return false; 135 uint8_t flag = *p++; 136 points[i++].flag = flag; 137 if (flag & FLAG_REPEAT) 138 { 139 if (unlikely (p + 1 > end)) return false; 140 unsigned int repeat_count = *p++; 141 unsigned stop = hb_min (i + repeat_count, count); 142 for (; i < stop; i++) 143 points[i].flag = flag; 144 } 145 } 146 return true; 147 } 148 149 static bool read_points (const HBUINT8 *&p /* IN/OUT */, 150 hb_array_t<contour_point_t> points_ /* IN/OUT */, 151 const HBUINT8 *end, 152 float contour_point_t::*m, 153 const simple_glyph_flag_t short_flag, 154 const simple_glyph_flag_t same_flag) 155 { 156 int v = 0; 157 158 for (auto &point : points_) 159 { 160 unsigned flag = point.flag; 161 if (flag & short_flag) 162 { 163 if (unlikely (p + 1 > end)) return false; 164 v += (bool(flag & same_flag) * 2 - 1) * *p++; 165 } 166 else 167 { 168 if (!(flag & same_flag)) 169 { 170 if (unlikely (p + HBINT16::static_size > end)) return false; 171 v += *(const HBINT16 *) p; 172 p += HBINT16::static_size; 173 } 174 } 175 point.*m = v; 176 } 177 return true; 178 } 179 180 bool get_contour_points (contour_point_vector_t &points /* OUT */, 181 bool phantom_only = false) const 182 { 183 const HBUINT16 *endPtsOfContours = &StructAfter<HBUINT16> (header); 184 int num_contours = header.numberOfContours; 185 assert (num_contours > 0); 186 /* One extra item at the end, for the instruction-count below. */ 187 if (unlikely (!bytes.check_range (&endPtsOfContours[num_contours]))) return false; 188 unsigned int num_points = endPtsOfContours[num_contours - 1] + 1; 189 190 unsigned old_length = points.length; 191 points.alloc (points.length + num_points + 4); // Allocate for phantom points, to avoid a possible copy 192 if (unlikely (!points.resize_dirty (points.length + num_points))) return false; 193 auto points_ = points.as_array ().sub_array (old_length); 194 if (!phantom_only) 195 hb_memset (points_.arrayZ, 0, sizeof (contour_point_t) * num_points); 196 if (phantom_only) return true; 197 198 for (int i = 0; i < num_contours; i++) 199 points_[endPtsOfContours[i]].is_end_point = true; 200 201 /* Skip instructions */ 202 const HBUINT8 *p = &StructAtOffset<HBUINT8> (&endPtsOfContours[num_contours + 1], 203 endPtsOfContours[num_contours]); 204 205 if (unlikely ((const char *) p < bytes.arrayZ)) return false; /* Unlikely overflow */ 206 const HBUINT8 *end = (const HBUINT8 *) (bytes.arrayZ + bytes.length); 207 if (unlikely (p >= end)) return false; 208 209 /* Read x & y coordinates */ 210 return read_flags (p, points_, end) 211 && read_points (p, points_, end, &contour_point_t::x, 212 FLAG_X_SHORT, FLAG_X_SAME) 213 && read_points (p, points_, end, &contour_point_t::y, 214 FLAG_Y_SHORT, FLAG_Y_SAME); 215 } 216 217 static void encode_coord (int value, 218 unsigned &flag, 219 const simple_glyph_flag_t short_flag, 220 const simple_glyph_flag_t same_flag, 221 hb_vector_t<uint8_t> &coords /* OUT */) 222 { 223 if (value == 0) 224 { 225 flag |= same_flag; 226 } 227 else if (value >= -255 && value <= 255) 228 { 229 flag |= short_flag; 230 if (value > 0) flag |= same_flag; 231 else value = -value; 232 233 coords.arrayZ[coords.length++] = (uint8_t) value; 234 } 235 else 236 { 237 int16_t val = value; 238 coords.arrayZ[coords.length++] = val >> 8; 239 coords.arrayZ[coords.length++] = val & 0xff; 240 } 241 } 242 243 static void encode_flag (unsigned flag, 244 unsigned &repeat, 245 unsigned lastflag, 246 hb_vector_t<uint8_t> &flags /* OUT */) 247 { 248 if (flag == lastflag && repeat != 255) 249 { 250 repeat++; 251 if (repeat == 1) 252 { 253 /* We know there's room. */ 254 flags.arrayZ[flags.length++] = flag; 255 } 256 else 257 { 258 unsigned len = flags.length; 259 flags.arrayZ[len-2] = flag | FLAG_REPEAT; 260 flags.arrayZ[len-1] = repeat; 261 } 262 } 263 else 264 { 265 repeat = 0; 266 flags.arrayZ[flags.length++] = flag; 267 } 268 } 269 270 bool compile_bytes_with_deltas (const contour_point_vector_t &all_points, 271 bool no_hinting, 272 hb_bytes_t &dest_bytes /* OUT */) 273 { 274 if (header.numberOfContours == 0 || all_points.length <= 4) 275 { 276 dest_bytes = hb_bytes_t (); 277 return true; 278 } 279 unsigned num_points = all_points.length - 4; 280 281 hb_vector_t<uint8_t> flags, x_coords, y_coords; 282 if (unlikely (!flags.alloc_exact (num_points))) return false; 283 if (unlikely (!x_coords.alloc_exact (2*num_points))) return false; 284 if (unlikely (!y_coords.alloc_exact (2*num_points))) return false; 285 286 unsigned lastflag = 255, repeat = 0; 287 int prev_x = 0, prev_y = 0; 288 289 for (unsigned i = 0; i < num_points; i++) 290 { 291 unsigned flag = all_points.arrayZ[i].flag; 292 flag &= FLAG_ON_CURVE | FLAG_OVERLAP_SIMPLE | FLAG_CUBIC; 293 294 int cur_x = roundf (all_points.arrayZ[i].x); 295 int cur_y = roundf (all_points.arrayZ[i].y); 296 encode_coord (cur_x - prev_x, flag, FLAG_X_SHORT, FLAG_X_SAME, x_coords); 297 encode_coord (cur_y - prev_y, flag, FLAG_Y_SHORT, FLAG_Y_SAME, y_coords); 298 encode_flag (flag, repeat, lastflag, flags); 299 300 prev_x = cur_x; 301 prev_y = cur_y; 302 lastflag = flag; 303 } 304 305 unsigned len_before_instrs = 2 * header.numberOfContours + 2; 306 unsigned len_instrs = instructions_length (); 307 unsigned total_len = len_before_instrs + flags.length + x_coords.length + y_coords.length; 308 309 if (!no_hinting) 310 total_len += len_instrs; 311 312 char *p = (char *) hb_malloc (total_len); 313 if (unlikely (!p)) return false; 314 315 const char *src = bytes.arrayZ + GlyphHeader::static_size; 316 char *cur = p; 317 hb_memcpy (p, src, len_before_instrs); 318 319 cur += len_before_instrs; 320 src += len_before_instrs; 321 322 if (!no_hinting) 323 { 324 hb_memcpy (cur, src, len_instrs); 325 cur += len_instrs; 326 } 327 328 hb_memcpy (cur, flags.arrayZ, flags.length); 329 cur += flags.length; 330 331 hb_memcpy (cur, x_coords.arrayZ, x_coords.length); 332 cur += x_coords.length; 333 334 hb_memcpy (cur, y_coords.arrayZ, y_coords.length); 335 336 dest_bytes = hb_bytes_t (p, total_len); 337 return true; 338 } 339 }; 340 341 342 } /* namespace glyf_impl */ 343 } /* namespace OT */ 344 345 346 #endif /* OT_GLYF_SIMPLEGLYPH_HH */