1 | #ifndef OT_GLYF_GLYF_HH |
2 | #define OT_GLYF_GLYF_HH |
3 | |
4 | |
5 | #include "../../hb-open-type.hh" |
6 | #include "../../hb-ot-head-table.hh" |
7 | #include "../../hb-ot-hmtx-table.hh" |
8 | #include "../../hb-ot-var-gvar-table.hh" |
9 | #include "../../hb-draw.hh" |
10 | #include "../../hb-paint.hh" |
11 | |
12 | #include "glyf-helpers.hh" |
13 | #include "Glyph.hh" |
14 | #include "SubsetGlyph.hh" |
15 | #include "loca.hh" |
16 | #include "path-builder.hh" |
17 | |
18 | |
19 | namespace OT { |
20 | |
21 | |
22 | /* |
23 | * glyf -- TrueType Glyph Data |
24 | * https://docs.microsoft.com/en-us/typography/opentype/spec/glyf |
25 | */ |
26 | #define HB_OT_TAG_glyf HB_TAG('g','l','y','f') |
27 | |
28 | struct glyf |
29 | { |
30 | friend struct glyf_accelerator_t; |
31 | |
32 | static constexpr hb_tag_t tableTag = HB_OT_TAG_glyf; |
33 | |
34 | static bool has_valid_glyf_format(const hb_face_t* face) |
35 | { |
36 | const OT::head &head = *face->table.head; |
37 | return head.indexToLocFormat <= 1 && head.glyphDataFormat <= 1; |
38 | } |
39 | |
40 | bool sanitize (hb_sanitize_context_t *c HB_UNUSED) const |
41 | { |
42 | TRACE_SANITIZE (this); |
43 | /* Runtime checks as eager sanitizing each glyph is costy */ |
44 | return_trace (true); |
45 | } |
46 | |
47 | /* requires source of SubsetGlyph complains the identifier isn't declared */ |
48 | template <typename Iterator> |
49 | bool serialize (hb_serialize_context_t *c, |
50 | Iterator it, |
51 | bool use_short_loca, |
52 | const hb_subset_plan_t *plan) |
53 | { |
54 | TRACE_SERIALIZE (this); |
55 | |
56 | unsigned init_len = c->length (); |
57 | for (auto &_ : it) |
58 | if (unlikely (!_.serialize (c, use_short_loca, plan))) |
59 | return false; |
60 | |
61 | /* As a special case when all glyph in the font are empty, add a zero byte |
62 | * to the table, so that OTS doesn’t reject it, and to make the table work |
63 | * on Windows as well. |
64 | * See https://github.com/khaledhosny/ots/issues/52 */ |
65 | if (init_len == c->length ()) |
66 | { |
67 | HBUINT8 empty_byte; |
68 | empty_byte = 0; |
69 | c->copy (empty_byte); |
70 | } |
71 | return_trace (true); |
72 | } |
73 | |
74 | /* Byte region(s) per glyph to output |
75 | unpadded, hints removed if so requested |
76 | If we fail to process a glyph we produce an empty (0-length) glyph */ |
77 | bool subset (hb_subset_context_t *c) const |
78 | { |
79 | TRACE_SUBSET (this); |
80 | |
81 | if (!has_valid_glyf_format (c->plan->source)) { |
82 | // glyf format is unknown don't attempt to subset it. |
83 | DEBUG_MSG (SUBSET, nullptr, |
84 | "unkown glyf format, dropping from subset." ); |
85 | return_trace (false); |
86 | } |
87 | |
88 | hb_font_t *font = nullptr; |
89 | if (c->plan->normalized_coords) |
90 | { |
91 | font = _create_font_for_instancing (c->plan); |
92 | if (unlikely (!font)) |
93 | return_trace (false); |
94 | } |
95 | |
96 | hb_vector_t<unsigned> padded_offsets; |
97 | if (unlikely (!padded_offsets.alloc (c->plan->new_to_old_gid_list.length, true))) |
98 | return_trace (false); |
99 | |
100 | hb_vector_t<glyf_impl::SubsetGlyph> glyphs; |
101 | if (!_populate_subset_glyphs (c->plan, font, glyphs)) |
102 | { |
103 | hb_font_destroy (font); |
104 | return_trace (false); |
105 | } |
106 | |
107 | if (font) |
108 | hb_font_destroy (font); |
109 | |
110 | unsigned max_offset = 0; |
111 | for (auto &g : glyphs) |
112 | { |
113 | unsigned size = g.padded_size (); |
114 | padded_offsets.push (size); |
115 | max_offset += size; |
116 | } |
117 | |
118 | bool use_short_loca = false; |
119 | if (likely (!c->plan->force_long_loca)) |
120 | use_short_loca = max_offset < 0x1FFFF; |
121 | |
122 | if (!use_short_loca) |
123 | { |
124 | padded_offsets.resize (0); |
125 | for (auto &g : glyphs) |
126 | padded_offsets.push (g.length ()); |
127 | } |
128 | |
129 | auto *glyf_prime = c->serializer->start_embed <glyf> (); |
130 | bool result = glyf_prime->serialize (c->serializer, hb_iter (glyphs), use_short_loca, c->plan); |
131 | if (c->plan->normalized_coords && !c->plan->pinned_at_default) |
132 | _free_compiled_subset_glyphs (glyphs); |
133 | |
134 | if (unlikely (!c->serializer->check_success (glyf_impl::_add_loca_and_head (c, |
135 | padded_offsets.iter (), |
136 | use_short_loca)))) |
137 | return_trace (false); |
138 | |
139 | return result; |
140 | } |
141 | |
142 | bool |
143 | _populate_subset_glyphs (const hb_subset_plan_t *plan, |
144 | hb_font_t *font, |
145 | hb_vector_t<glyf_impl::SubsetGlyph>& glyphs /* OUT */) const; |
146 | |
147 | hb_font_t * |
148 | _create_font_for_instancing (const hb_subset_plan_t *plan) const; |
149 | |
150 | void _free_compiled_subset_glyphs (hb_vector_t<glyf_impl::SubsetGlyph> &glyphs) const |
151 | { |
152 | for (auto &g : glyphs) |
153 | g.free_compiled_bytes (); |
154 | } |
155 | |
156 | protected: |
157 | UnsizedArrayOf<HBUINT8> |
158 | dataZ; /* Glyphs data. */ |
159 | public: |
160 | DEFINE_SIZE_MIN (0); /* In reality, this is UNBOUNDED() type; but since we always |
161 | * check the size externally, allow Null() object of it by |
162 | * defining it _MIN instead. */ |
163 | }; |
164 | |
165 | struct glyf_accelerator_t |
166 | { |
167 | glyf_accelerator_t (hb_face_t *face) |
168 | { |
169 | short_offset = false; |
170 | num_glyphs = 0; |
171 | loca_table = nullptr; |
172 | glyf_table = nullptr; |
173 | #ifndef HB_NO_VAR |
174 | gvar = nullptr; |
175 | #endif |
176 | hmtx = nullptr; |
177 | #ifndef HB_NO_VERTICAL |
178 | vmtx = nullptr; |
179 | #endif |
180 | const OT::head &head = *face->table.head; |
181 | if (!glyf::has_valid_glyf_format (face)) |
182 | /* Unknown format. Leave num_glyphs=0, that takes care of disabling us. */ |
183 | return; |
184 | short_offset = 0 == head.indexToLocFormat; |
185 | |
186 | loca_table = face->table.loca.get_blob (); // Needs no destruct! |
187 | glyf_table = hb_sanitize_context_t ().reference_table<glyf> (face); |
188 | #ifndef HB_NO_VAR |
189 | gvar = face->table.gvar; |
190 | #endif |
191 | hmtx = face->table.hmtx; |
192 | #ifndef HB_NO_VERTICAL |
193 | vmtx = face->table.vmtx; |
194 | #endif |
195 | |
196 | num_glyphs = hb_max (1u, loca_table.get_length () / (short_offset ? 2 : 4)) - 1; |
197 | num_glyphs = hb_min (num_glyphs, face->get_num_glyphs ()); |
198 | } |
199 | ~glyf_accelerator_t () |
200 | { |
201 | glyf_table.destroy (); |
202 | } |
203 | |
204 | bool has_data () const { return num_glyphs; } |
205 | |
206 | protected: |
207 | template<typename T> |
208 | bool get_points (hb_font_t *font, hb_codepoint_t gid, T consumer) const |
209 | { |
210 | if (gid >= num_glyphs) return false; |
211 | |
212 | /* Making this allocfree is not that easy |
213 | https://github.com/harfbuzz/harfbuzz/issues/2095 |
214 | mostly because of gvar handling in VF fonts, |
215 | perhaps a separate path for non-VF fonts can be considered */ |
216 | contour_point_vector_t all_points; |
217 | |
218 | bool phantom_only = !consumer.is_consuming_contour_points (); |
219 | if (unlikely (!glyph_for_gid (gid).get_points (font, *this, all_points, nullptr, nullptr, nullptr, true, true, phantom_only))) |
220 | return false; |
221 | |
222 | unsigned count = all_points.length; |
223 | assert (count >= glyf_impl::PHANTOM_COUNT); |
224 | count -= glyf_impl::PHANTOM_COUNT; |
225 | |
226 | if (consumer.is_consuming_contour_points ()) |
227 | { |
228 | for (auto &point : all_points.as_array ().sub_array (0, count)) |
229 | consumer.consume_point (point); |
230 | consumer.points_end (); |
231 | } |
232 | |
233 | /* Where to write phantoms, nullptr if not requested */ |
234 | contour_point_t *phantoms = consumer.get_phantoms_sink (); |
235 | if (phantoms) |
236 | for (unsigned i = 0; i < glyf_impl::PHANTOM_COUNT; ++i) |
237 | phantoms[i] = all_points.arrayZ[count + i]; |
238 | |
239 | return true; |
240 | } |
241 | |
242 | public: |
243 | |
244 | #ifndef HB_NO_VAR |
245 | struct points_aggregator_t |
246 | { |
247 | hb_font_t *font; |
248 | hb_glyph_extents_t *extents; |
249 | contour_point_t *phantoms; |
250 | bool scaled; |
251 | |
252 | struct contour_bounds_t |
253 | { |
254 | contour_bounds_t () { min_x = min_y = FLT_MAX; max_x = max_y = -FLT_MAX; } |
255 | |
256 | void add (const contour_point_t &p) |
257 | { |
258 | min_x = hb_min (min_x, p.x); |
259 | min_y = hb_min (min_y, p.y); |
260 | max_x = hb_max (max_x, p.x); |
261 | max_y = hb_max (max_y, p.y); |
262 | } |
263 | |
264 | bool empty () const { return (min_x >= max_x) || (min_y >= max_y); } |
265 | |
266 | void get_extents (hb_font_t *font, hb_glyph_extents_t *extents, bool scaled) |
267 | { |
268 | if (unlikely (empty ())) |
269 | { |
270 | extents->width = 0; |
271 | extents->x_bearing = 0; |
272 | extents->height = 0; |
273 | extents->y_bearing = 0; |
274 | return; |
275 | } |
276 | { |
277 | extents->x_bearing = roundf (min_x); |
278 | extents->width = roundf (max_x - extents->x_bearing); |
279 | extents->y_bearing = roundf (max_y); |
280 | extents->height = roundf (min_y - extents->y_bearing); |
281 | |
282 | if (scaled) |
283 | font->scale_glyph_extents (extents); |
284 | } |
285 | } |
286 | |
287 | protected: |
288 | float min_x, min_y, max_x, max_y; |
289 | } bounds; |
290 | |
291 | points_aggregator_t (hb_font_t *font_, hb_glyph_extents_t *extents_, contour_point_t *phantoms_, bool scaled_) |
292 | { |
293 | font = font_; |
294 | extents = extents_; |
295 | phantoms = phantoms_; |
296 | scaled = scaled_; |
297 | if (extents) bounds = contour_bounds_t (); |
298 | } |
299 | |
300 | HB_ALWAYS_INLINE |
301 | void consume_point (const contour_point_t &point) { bounds.add (point); } |
302 | void points_end () { bounds.get_extents (font, extents, scaled); } |
303 | |
304 | bool is_consuming_contour_points () { return extents; } |
305 | contour_point_t *get_phantoms_sink () { return phantoms; } |
306 | }; |
307 | |
308 | unsigned |
309 | get_advance_with_var_unscaled (hb_font_t *font, hb_codepoint_t gid, bool is_vertical) const |
310 | { |
311 | if (unlikely (gid >= num_glyphs)) return 0; |
312 | |
313 | bool success = false; |
314 | |
315 | contour_point_t phantoms[glyf_impl::PHANTOM_COUNT]; |
316 | if (font->num_coords) |
317 | success = get_points (font, gid, points_aggregator_t (font, nullptr, phantoms, false)); |
318 | |
319 | if (unlikely (!success)) |
320 | return |
321 | #ifndef HB_NO_VERTICAL |
322 | is_vertical ? vmtx->get_advance_without_var_unscaled (gid) : |
323 | #endif |
324 | hmtx->get_advance_without_var_unscaled (gid); |
325 | |
326 | float result = is_vertical |
327 | ? phantoms[glyf_impl::PHANTOM_TOP].y - phantoms[glyf_impl::PHANTOM_BOTTOM].y |
328 | : phantoms[glyf_impl::PHANTOM_RIGHT].x - phantoms[glyf_impl::PHANTOM_LEFT].x; |
329 | return hb_clamp (roundf (result), 0.f, (float) UINT_MAX / 2); |
330 | } |
331 | |
332 | bool get_leading_bearing_with_var_unscaled (hb_font_t *font, hb_codepoint_t gid, bool is_vertical, int *lsb) const |
333 | { |
334 | if (unlikely (gid >= num_glyphs)) return false; |
335 | |
336 | hb_glyph_extents_t extents; |
337 | |
338 | contour_point_t phantoms[glyf_impl::PHANTOM_COUNT]; |
339 | if (unlikely (!get_points (font, gid, points_aggregator_t (font, &extents, phantoms, false)))) |
340 | return false; |
341 | |
342 | *lsb = is_vertical |
343 | ? roundf (phantoms[glyf_impl::PHANTOM_TOP].y) - extents.y_bearing |
344 | : roundf (phantoms[glyf_impl::PHANTOM_LEFT].x); |
345 | return true; |
346 | } |
347 | #endif |
348 | |
349 | bool get_leading_bearing_without_var_unscaled (hb_codepoint_t gid, bool is_vertical, int *lsb) const |
350 | { |
351 | if (unlikely (gid >= num_glyphs)) return false; |
352 | if (is_vertical) return false; // TODO Humm, what to do here? |
353 | |
354 | *lsb = glyph_for_gid (gid).get_header ()->xMin; |
355 | return true; |
356 | } |
357 | |
358 | public: |
359 | bool get_extents (hb_font_t *font, hb_codepoint_t gid, hb_glyph_extents_t *extents) const |
360 | { |
361 | if (unlikely (gid >= num_glyphs)) return false; |
362 | |
363 | #ifndef HB_NO_VAR |
364 | if (font->num_coords) |
365 | return get_points (font, gid, points_aggregator_t (font, extents, nullptr, true)); |
366 | #endif |
367 | return glyph_for_gid (gid).get_extents_without_var_scaled (font, *this, extents); |
368 | } |
369 | |
370 | bool paint_glyph (hb_font_t *font, hb_codepoint_t gid, hb_paint_funcs_t *funcs, void *data, hb_color_t foreground) const |
371 | { |
372 | funcs->push_clip_glyph (data, gid, font); |
373 | funcs->color (data, true, foreground); |
374 | funcs->pop_clip (data); |
375 | |
376 | return true; |
377 | } |
378 | |
379 | const glyf_impl::Glyph |
380 | glyph_for_gid (hb_codepoint_t gid, bool needs_padding_removal = false) const |
381 | { |
382 | if (unlikely (gid >= num_glyphs)) return glyf_impl::Glyph (); |
383 | |
384 | unsigned int start_offset, end_offset; |
385 | |
386 | if (short_offset) |
387 | { |
388 | const HBUINT16 *offsets = (const HBUINT16 *) loca_table->dataZ.arrayZ; |
389 | start_offset = 2 * offsets[gid]; |
390 | end_offset = 2 * offsets[gid + 1]; |
391 | } |
392 | else |
393 | { |
394 | const HBUINT32 *offsets = (const HBUINT32 *) loca_table->dataZ.arrayZ; |
395 | start_offset = offsets[gid]; |
396 | end_offset = offsets[gid + 1]; |
397 | } |
398 | |
399 | if (unlikely (start_offset > end_offset || end_offset > glyf_table.get_length ())) |
400 | return glyf_impl::Glyph (); |
401 | |
402 | glyf_impl::Glyph glyph (hb_bytes_t ((const char *) this->glyf_table + start_offset, |
403 | end_offset - start_offset), gid); |
404 | return needs_padding_removal ? glyf_impl::Glyph (glyph.trim_padding (), gid) : glyph; |
405 | } |
406 | |
407 | bool |
408 | get_path (hb_font_t *font, hb_codepoint_t gid, hb_draw_session_t &draw_session) const |
409 | { return get_points (font, gid, glyf_impl::path_builder_t (font, draw_session)); } |
410 | |
411 | #ifndef HB_NO_VAR |
412 | const gvar_accelerator_t *gvar; |
413 | #endif |
414 | const hmtx_accelerator_t *hmtx; |
415 | #ifndef HB_NO_VERTICAL |
416 | const vmtx_accelerator_t *vmtx; |
417 | #endif |
418 | |
419 | private: |
420 | bool short_offset; |
421 | unsigned int num_glyphs; |
422 | hb_blob_ptr_t<loca> loca_table; |
423 | hb_blob_ptr_t<glyf> glyf_table; |
424 | }; |
425 | |
426 | |
427 | inline bool |
428 | glyf::_populate_subset_glyphs (const hb_subset_plan_t *plan, |
429 | hb_font_t *font, |
430 | hb_vector_t<glyf_impl::SubsetGlyph>& glyphs /* OUT */) const |
431 | { |
432 | OT::glyf_accelerator_t glyf (plan->source); |
433 | if (!glyphs.alloc (plan->new_to_old_gid_list.length, true)) return false; |
434 | |
435 | for (const auto &pair : plan->new_to_old_gid_list) |
436 | { |
437 | hb_codepoint_t new_gid = pair.first; |
438 | hb_codepoint_t old_gid = pair.second; |
439 | glyf_impl::SubsetGlyph *p = glyphs.push (); |
440 | glyf_impl::SubsetGlyph& subset_glyph = *p; |
441 | subset_glyph.old_gid = old_gid; |
442 | |
443 | if (unlikely (old_gid == 0 && new_gid == 0 && |
444 | !(plan->flags & HB_SUBSET_FLAGS_NOTDEF_OUTLINE)) && |
445 | !plan->normalized_coords) |
446 | subset_glyph.source_glyph = glyf_impl::Glyph (); |
447 | else |
448 | { |
449 | /* If plan has an accelerator, the preprocessing step already trimmed glyphs. |
450 | * Don't trim them again! */ |
451 | subset_glyph.source_glyph = glyf.glyph_for_gid (subset_glyph.old_gid, !plan->accelerator); |
452 | } |
453 | |
454 | if (plan->flags & HB_SUBSET_FLAGS_NO_HINTING) |
455 | subset_glyph.drop_hints_bytes (); |
456 | else |
457 | subset_glyph.dest_start = subset_glyph.source_glyph.get_bytes (); |
458 | |
459 | if (font) |
460 | { |
461 | if (unlikely (!subset_glyph.compile_bytes_with_deltas (plan, font, glyf))) |
462 | { |
463 | // when pinned at default, only bounds are updated, thus no need to free |
464 | if (!plan->pinned_at_default) |
465 | _free_compiled_subset_glyphs (glyphs); |
466 | return false; |
467 | } |
468 | } |
469 | } |
470 | return true; |
471 | } |
472 | |
473 | inline hb_font_t * |
474 | glyf::_create_font_for_instancing (const hb_subset_plan_t *plan) const |
475 | { |
476 | hb_font_t *font = hb_font_create (plan->source); |
477 | if (unlikely (font == hb_font_get_empty ())) return nullptr; |
478 | |
479 | hb_vector_t<hb_variation_t> vars; |
480 | if (unlikely (!vars.alloc (plan->user_axes_location.get_population (), true))) |
481 | { |
482 | hb_font_destroy (font); |
483 | return nullptr; |
484 | } |
485 | |
486 | for (auto _ : plan->user_axes_location) |
487 | { |
488 | hb_variation_t var; |
489 | var.tag = _.first; |
490 | var.value = _.second.middle; |
491 | vars.push (var); |
492 | } |
493 | |
494 | #ifndef HB_NO_VAR |
495 | hb_font_set_variations (font, vars.arrayZ, plan->user_axes_location.get_population ()); |
496 | #endif |
497 | return font; |
498 | } |
499 | |
500 | |
501 | } /* namespace OT */ |
502 | |
503 | |
504 | #endif /* OT_GLYF_GLYF_HH */ |
505 | |