1 #ifndef OT_GLYF_SIMPLEGLYPH_HH
2 #define OT_GLYF_SIMPLEGLYPH_HH
5 #include "../../hb-open-type.hh"
14 enum simple_glyph_flag_t
22 FLAG_OVERLAP_SIMPLE = 0x40,
26 const GlyphHeader &header;
28 SimpleGlyph (const GlyphHeader &header_, hb_bytes_t bytes_) :
29 header (header_), bytes (bytes_) {}
31 unsigned int instruction_len_offset () const
32 { return GlyphHeader::static_size + 2 * header.numberOfContours; }
34 unsigned int length (unsigned int instruction_len) const
35 { return instruction_len_offset () + 2 + instruction_len; }
37 bool has_instructions_length () const
39 return instruction_len_offset () + 2 <= bytes.length;
42 unsigned int instructions_length () const
44 unsigned int instruction_length_offset = instruction_len_offset ();
45 if (unlikely (instruction_length_offset + 2 > bytes.length)) return 0;
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;
53 const hb_bytes_t trim_padding () const
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 ();
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);
65 glyph += 2 + num_instructions;
67 unsigned int coord_bytes = 0;
68 unsigned int coords_with_flags = 0;
69 while (glyph < glyph_end)
71 uint8_t flag = *glyph;
74 unsigned int repeat = 1;
75 if (flag & FLAG_REPEAT)
77 if (unlikely (glyph >= glyph_end)) return hb_bytes_t ();
82 unsigned int xBytes, yBytes;
84 if (flag & FLAG_X_SHORT) xBytes = 1;
85 else if ((flag & FLAG_X_SAME) == 0) xBytes = 2;
87 if (flag & FLAG_Y_SHORT) yBytes = 1;
88 else if ((flag & FLAG_Y_SAME) == 0) yBytes = 2;
90 coord_bytes += (xBytes + yBytes) * repeat;
91 coords_with_flags += repeat;
92 if (coords_with_flags >= num_coordinates) break;
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));
99 /* zero instruction length */
102 if (!has_instructions_length ()) return;
103 GlyphHeader &glyph_header = const_cast<GlyphHeader &> (header);
104 (HBUINT16 &) StructAtOffset<HBUINT16> (&glyph_header, instruction_len_offset ()) = 0;
107 void drop_hints_bytes (hb_bytes_t &dest_start, hb_bytes_t &dest_end) const
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);
115 void set_overlaps_flag ()
117 if (unlikely (!header.numberOfContours)) return;
119 unsigned flags_offset = length (instructions_length ());
120 if (unlikely (flags_offset + 1 > bytes.length)) return;
122 HBUINT8 &first_flag = (HBUINT8 &) StructAtOffset<HBUINT16> (&bytes, flags_offset);
123 first_flag = (uint8_t) first_flag | FLAG_OVERLAP_SIMPLE;
126 static bool read_flags (const HBUINT8 *&p /* IN/OUT */,
127 hb_array_t<contour_point_t> points_ /* IN/OUT */,
130 unsigned count = points_.length;
131 for (unsigned int i = 0; i < count;)
133 if (unlikely (p + 1 > end)) return false;
135 points_.arrayZ[i++].flag = flag;
136 if (flag & FLAG_REPEAT)
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;
148 static bool read_points (const HBUINT8 *&p /* IN/OUT */,
149 hb_array_t<contour_point_t> points_ /* IN/OUT */,
151 float contour_point_t::*m,
152 const simple_glyph_flag_t short_flag,
153 const simple_glyph_flag_t same_flag)
157 for (auto &point : points_)
159 unsigned flag = point.flag;
160 if (flag & short_flag)
162 if (unlikely (p + 1 > end)) return false;
163 if (flag & same_flag)
170 if (!(flag & same_flag))
172 if (unlikely (p + HBINT16::static_size > end)) return false;
173 v += *(const HBINT16 *) p;
174 p += HBINT16::static_size;
182 bool get_contour_points (contour_point_vector_t &points /* OUT */,
183 bool phantom_only = false) const
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;
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);
197 hb_memset (points_.arrayZ, 0, sizeof (contour_point_t) * num_points);
198 if (phantom_only) return true;
200 for (int i = 0; i < num_contours; i++)
201 points_[endPtsOfContours[i]].is_end_point = true;
203 /* Skip instructions */
204 const HBUINT8 *p = &StructAtOffset<HBUINT8> (&endPtsOfContours[num_contours + 1],
205 endPtsOfContours[num_contours]);
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;
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);
219 static void encode_coord (int value,
221 const simple_glyph_flag_t short_flag,
222 const simple_glyph_flag_t same_flag,
223 hb_vector_t<uint8_t> &coords /* OUT */)
229 else if (value >= -255 && value <= 255)
232 if (value > 0) flag |= same_flag;
235 coords.arrayZ[coords.length++] = (uint8_t) value;
240 coords.arrayZ[coords.length++] = val >> 8;
241 coords.arrayZ[coords.length++] = val & 0xff;
245 static void encode_flag (unsigned flag,
248 hb_vector_t<uint8_t> &flags /* OUT */)
250 if (flag == lastflag && repeat != 255)
255 /* We know there's room. */
256 flags.arrayZ[flags.length++] = flag;
260 unsigned len = flags.length;
261 flags.arrayZ[len-2] = flag | FLAG_REPEAT;
262 flags.arrayZ[len-1] = repeat;
268 flags.arrayZ[flags.length++] = flag;
272 bool compile_bytes_with_deltas (const contour_point_vector_t &all_points,
274 hb_bytes_t &dest_bytes /* OUT */)
276 if (header.numberOfContours == 0 || all_points.length <= 4)
278 dest_bytes = hb_bytes_t ();
281 unsigned num_points = all_points.length - 4;
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;
288 unsigned lastflag = 255, repeat = 0;
289 int prev_x = 0, prev_y = 0;
291 for (unsigned i = 0; i < num_points; i++)
293 unsigned flag = all_points.arrayZ[i].flag;
294 flag &= FLAG_ON_CURVE | FLAG_OVERLAP_SIMPLE | FLAG_CUBIC;
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);
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;
312 total_len += len_instrs;
314 char *p = (char *) hb_malloc (total_len);
315 if (unlikely (!p)) return false;
317 const char *src = bytes.arrayZ + GlyphHeader::static_size;
319 hb_memcpy (p, src, len_before_instrs);
321 cur += len_before_instrs;
322 src += len_before_instrs;
326 hb_memcpy (cur, src, len_instrs);
330 hb_memcpy (cur, flags.arrayZ, flags.length);
333 hb_memcpy (cur, x_coords.arrayZ, x_coords.length);
334 cur += x_coords.length;
336 hb_memcpy (cur, y_coords.arrayZ, y_coords.length);
338 dest_bytes = hb_bytes_t (p, total_len);
344 } /* namespace glyf_impl */
348 #endif /* OT_GLYF_SIMPLEGLYPH_HH */