Debian Codegraph @

src/glyph.cc

raw
1 /* Copyright 2013 Google Inc. All Rights Reserved.
2
3 Distributed under MIT license.
4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5 */
6
7 /* Glyph manipulation */
8
9 #include "./glyph.h"
10
11 #include <stdlib.h>
12 #include <limits>
13 #include "./buffer.h"
14 #include "./store_bytes.h"
15
16 namespace woff2 {
17
18 static const int32_t kFLAG_ONCURVE = 1;
19 static const int32_t kFLAG_XSHORT = 1 << 1;
20 static const int32_t kFLAG_YSHORT = 1 << 2;
21 static const int32_t kFLAG_REPEAT = 1 << 3;
22 static const int32_t kFLAG_XREPEATSIGN = 1 << 4;
23 static const int32_t kFLAG_YREPEATSIGN = 1 << 5;
24 static const int32_t kFLAG_ARG_1_AND_2_ARE_WORDS = 1 << 0;
25 static const int32_t kFLAG_WE_HAVE_A_SCALE = 1 << 3;
26 static const int32_t kFLAG_MORE_COMPONENTS = 1 << 5;
27 static const int32_t kFLAG_WE_HAVE_AN_X_AND_Y_SCALE = 1 << 6;
28 static const int32_t kFLAG_WE_HAVE_A_TWO_BY_TWO = 1 << 7;
29 static const int32_t kFLAG_WE_HAVE_INSTRUCTIONS = 1 << 8;
30
31 bool ReadCompositeGlyphData(Buffer* buffer, Glyph* glyph) {
32 glyph->have_instructions = false;
33 glyph->composite_data = buffer->buffer() + buffer->offset();
34 size_t start_offset = buffer->offset();
35 uint16_t flags = kFLAG_MORE_COMPONENTS;
36 while (flags & kFLAG_MORE_COMPONENTS) {
37 if (!buffer->ReadU16(&flags)) {
38 return FONT_COMPRESSION_FAILURE();
39 }
40 glyph->have_instructions |= (flags & kFLAG_WE_HAVE_INSTRUCTIONS) != 0;
41 size_t arg_size = 2; // glyph index
42 if (flags & kFLAG_ARG_1_AND_2_ARE_WORDS) {
43 arg_size += 4;
44 } else {
45 arg_size += 2;
46 }
47 if (flags & kFLAG_WE_HAVE_A_SCALE) {
48 arg_size += 2;
49 } else if (flags & kFLAG_WE_HAVE_AN_X_AND_Y_SCALE) {
50 arg_size += 4;
51 } else if (flags & kFLAG_WE_HAVE_A_TWO_BY_TWO) {
52 arg_size += 8;
53 }
54 if (!buffer->Skip(arg_size)) {
55 return FONT_COMPRESSION_FAILURE();
56 }
57 }
58 if (buffer->offset() - start_offset > std::numeric_limits<uint32_t>::max()) {
59 return FONT_COMPRESSION_FAILURE();
60 }
61 glyph->composite_data_size = buffer->offset() - start_offset;
62 return true;
63 }
64
65 bool ReadGlyph(const uint8_t* data, size_t len, Glyph* glyph) {
66 Buffer buffer(data, len);
67
68 int16_t num_contours;
69 if (!buffer.ReadS16(&num_contours)) {
70 return FONT_COMPRESSION_FAILURE();
71 }
72
73 // Read the bounding box.
74 if (!buffer.ReadS16(&glyph->x_min) ||
75 !buffer.ReadS16(&glyph->y_min) ||
76 !buffer.ReadS16(&glyph->x_max) ||
77 !buffer.ReadS16(&glyph->y_max)) {
78 return FONT_COMPRESSION_FAILURE();
79 }
80
81 if (num_contours == 0) {
82 // Empty glyph.
83 return true;
84 }
85
86 if (num_contours > 0) {
87 // Simple glyph.
88 glyph->contours.resize(num_contours);
89
90 // Read the number of points per contour.
91 uint16_t last_point_index = 0;
92 for (int i = 0; i < num_contours; ++i) {
93 uint16_t point_index;
94 if (!buffer.ReadU16(&point_index)) {
95 return FONT_COMPRESSION_FAILURE();
96 }
97 uint16_t num_points = point_index - last_point_index + (i == 0 ? 1 : 0);
98 glyph->contours[i].resize(num_points);
99 last_point_index = point_index;
100 }
101
102 // Read the instructions.
103 if (!buffer.ReadU16(&glyph->instructions_size)) {
104 return FONT_COMPRESSION_FAILURE();
105 }
106 glyph->instructions_data = data + buffer.offset();
107 if (!buffer.Skip(glyph->instructions_size)) {
108 return FONT_COMPRESSION_FAILURE();
109 }
110
111 // Read the run-length coded flags.
112 std::vector<std::vector<uint8_t> > flags(num_contours);
113 {
114 uint8_t flag = 0;
115 uint8_t flag_repeat = 0;
116 for (int i = 0; i < num_contours; ++i) {
117 flags[i].resize(glyph->contours[i].size());
118 for (size_t j = 0; j < glyph->contours[i].size(); ++j) {
119 if (flag_repeat == 0) {
120 if (!buffer.ReadU8(&flag)) {
121 return FONT_COMPRESSION_FAILURE();
122 }
123 if (flag & kFLAG_REPEAT) {
124 if (!buffer.ReadU8(&flag_repeat)) {
125 return FONT_COMPRESSION_FAILURE();
126 }
127 }
128 } else {
129 flag_repeat--;
130 }
131 flags[i][j] = flag;
132 glyph->contours[i][j].on_curve = flag & kFLAG_ONCURVE;
133 }
134 }
135 }
136
137 // Read the x coordinates.
138 int prev_x = 0;
139 for (int i = 0; i < num_contours; ++i) {
140 for (size_t j = 0; j < glyph->contours[i].size(); ++j) {
141 uint8_t flag = flags[i][j];
142 if (flag & kFLAG_XSHORT) {
143 // single byte x-delta coord value
144 uint8_t x_delta;
145 if (!buffer.ReadU8(&x_delta)) {
146 return FONT_COMPRESSION_FAILURE();
147 }
148 int sign = (flag & kFLAG_XREPEATSIGN) ? 1 : -1;
149 glyph->contours[i][j].x = prev_x + sign * x_delta;
150 } else {
151 // double byte x-delta coord value
152 int16_t x_delta = 0;
153 if (!(flag & kFLAG_XREPEATSIGN)) {
154 if (!buffer.ReadS16(&x_delta)) {
155 return FONT_COMPRESSION_FAILURE();
156 }
157 }
158 glyph->contours[i][j].x = prev_x + x_delta;
159 }
160 prev_x = glyph->contours[i][j].x;
161 }
162 }
163
164 // Read the y coordinates.
165 int prev_y = 0;
166 for (int i = 0; i < num_contours; ++i) {
167 for (size_t j = 0; j < glyph->contours[i].size(); ++j) {
168 uint8_t flag = flags[i][j];
169 if (flag & kFLAG_YSHORT) {
170 // single byte y-delta coord value
171 uint8_t y_delta;
172 if (!buffer.ReadU8(&y_delta)) {
173 return FONT_COMPRESSION_FAILURE();
174 }
175 int sign = (flag & kFLAG_YREPEATSIGN) ? 1 : -1;
176 glyph->contours[i][j].y = prev_y + sign * y_delta;
177 } else {
178 // double byte y-delta coord value
179 int16_t y_delta = 0;
180 if (!(flag & kFLAG_YREPEATSIGN)) {
181 if (!buffer.ReadS16(&y_delta)) {
182 return FONT_COMPRESSION_FAILURE();
183 }
184 }
185 glyph->contours[i][j].y = prev_y + y_delta;
186 }
187 prev_y = glyph->contours[i][j].y;
188 }
189 }
190 } else if (num_contours == -1) {
191 // Composite glyph.
192 if (!ReadCompositeGlyphData(&buffer, glyph)) {
193 return FONT_COMPRESSION_FAILURE();
194 }
195 // Read the instructions.
196 if (glyph->have_instructions) {
197 if (!buffer.ReadU16(&glyph->instructions_size)) {
198 return FONT_COMPRESSION_FAILURE();
199 }
200 glyph->instructions_data = data + buffer.offset();
201 if (!buffer.Skip(glyph->instructions_size)) {
202 return FONT_COMPRESSION_FAILURE();
203 }
204 } else {
205 glyph->instructions_size = 0;
206 }
207 } else {
208 return FONT_COMPRESSION_FAILURE();
209 }
210 return true;
211 }
212
213 namespace {
214
215 void StoreBbox(const Glyph& glyph, size_t* offset, uint8_t* dst) {
216 Store16(glyph.x_min, offset, dst);
217 Store16(glyph.y_min, offset, dst);
218 Store16(glyph.x_max, offset, dst);
219 Store16(glyph.y_max, offset, dst);
220 }
221
222 void StoreInstructions(const Glyph& glyph, size_t* offset, uint8_t* dst) {
223 Store16(glyph.instructions_size, offset, dst);
224 StoreBytes(glyph.instructions_data, glyph.instructions_size, offset, dst);
225 }
226
227 bool StoreEndPtsOfContours(const Glyph& glyph, size_t* offset, uint8_t* dst) {
228 int end_point = -1;
229 for (const auto& contour : glyph.contours) {
230 end_point += contour.size();
231 if (contour.size() > std::numeric_limits<uint16_t>::max() ||
232 end_point > std::numeric_limits<uint16_t>::max()) {
233 return FONT_COMPRESSION_FAILURE();
234 }
235 Store16(end_point, offset, dst);
236 }
237 return true;
238 }
239
240 bool StorePoints(const Glyph& glyph, size_t* offset,
241 uint8_t* dst, size_t dst_size) {
242 int last_flag = -1;
243 int repeat_count = 0;
244 int last_x = 0;
245 int last_y = 0;
246 size_t x_bytes = 0;
247 size_t y_bytes = 0;
248
249 // Store the flags and calculate the total size of the x and y coordinates.
250 for (const auto& contour : glyph.contours) {
251 for (const auto& point : contour) {
252 int flag = point.on_curve ? kFLAG_ONCURVE : 0;
253 int dx = point.x - last_x;
254 int dy = point.y - last_y;
255 if (dx == 0) {
256 flag |= kFLAG_XREPEATSIGN;
257 } else if (dx > -256 && dx < 256) {
258 flag |= kFLAG_XSHORT | (dx > 0 ? kFLAG_XREPEATSIGN : 0);
259 x_bytes += 1;
260 } else {
261 x_bytes += 2;
262 }
263 if (dy == 0) {
264 flag |= kFLAG_YREPEATSIGN;
265 } else if (dy > -256 && dy < 256) {
266 flag |= kFLAG_YSHORT | (dy > 0 ? kFLAG_YREPEATSIGN : 0);
267 y_bytes += 1;
268 } else {
269 y_bytes += 2;
270 }
271 if (flag == last_flag && repeat_count != 255) {
272 dst[*offset - 1] |= kFLAG_REPEAT;
273 repeat_count++;
274 } else {
275 if (repeat_count != 0) {
276 if (*offset >= dst_size) {
277 return FONT_COMPRESSION_FAILURE();
278 }
279 dst[(*offset)++] = repeat_count;
280 }
281 if (*offset >= dst_size) {
282 return FONT_COMPRESSION_FAILURE();
283 }
284 dst[(*offset)++] = flag;
285 repeat_count = 0;
286 }
287 last_x = point.x;
288 last_y = point.y;
289 last_flag = flag;
290 }
291 }
292 if (repeat_count != 0) {
293 if (*offset >= dst_size) {
294 return FONT_COMPRESSION_FAILURE();
295 }
296 dst[(*offset)++] = repeat_count;
297 }
298
299 if (*offset + x_bytes + y_bytes > dst_size) {
300 return FONT_COMPRESSION_FAILURE();
301 }
302
303 // Store the x and y coordinates.
304 size_t x_offset = *offset;
305 size_t y_offset = *offset + x_bytes;
306 last_x = 0;
307 last_y = 0;
308 for (const auto& contour : glyph.contours) {
309 for (const auto& point : contour) {
310 int dx = point.x - last_x;
311 int dy = point.y - last_y;
312 if (dx == 0) {
313 // pass
314 } else if (dx > -256 && dx < 256) {
315 dst[x_offset++] = std::abs(dx);
316 } else {
317 Store16(dx, &x_offset, dst);
318 }
319 if (dy == 0) {
320 // pass
321 } else if (dy > -256 && dy < 256) {
322 dst[y_offset++] = std::abs(dy);
323 } else {
324 Store16(dy, &y_offset, dst);
325 }
326 last_x += dx;
327 last_y += dy;
328 }
329 }
330 *offset = y_offset;
331 return true;
332 }
333
334 } // namespace
335
336 bool StoreGlyph(const Glyph& glyph, uint8_t* dst, size_t* dst_size) {
337 size_t offset = 0;
338 if (glyph.composite_data_size > 0) {
339 // Composite glyph.
340 if (*dst_size < ((10ULL + glyph.composite_data_size) +
341 ((glyph.have_instructions ? 2ULL : 0) +
342 glyph.instructions_size))) {
343 return FONT_COMPRESSION_FAILURE();
344 }
345 Store16(-1, &offset, dst);
346 StoreBbox(glyph, &offset, dst);
347 StoreBytes(glyph.composite_data, glyph.composite_data_size, &offset, dst);
348 if (glyph.have_instructions) {
349 StoreInstructions(glyph, &offset, dst);
350 }
351 } else if (glyph.contours.size() > 0) {
352 // Simple glyph.
353 if (glyph.contours.size() > std::numeric_limits<int16_t>::max()) {
354 return FONT_COMPRESSION_FAILURE();
355 }
356 if (*dst_size < ((12ULL + 2 * glyph.contours.size()) +
357 glyph.instructions_size)) {
358 return FONT_COMPRESSION_FAILURE();
359 }
360 Store16(glyph.contours.size(), &offset, dst);
361 StoreBbox(glyph, &offset, dst);
362 if (!StoreEndPtsOfContours(glyph, &offset, dst)) {
363 return FONT_COMPRESSION_FAILURE();
364 }
365 StoreInstructions(glyph, &offset, dst);
366 if (!StorePoints(glyph, &offset, dst, *dst_size)) {
367 return FONT_COMPRESSION_FAILURE();
368 }
369 }
370 *dst_size = offset;
371 return true;
372 }
373
374 } // namespace woff2