1 #ifndef OT_GLYF_GLYF_HH
2 #define OT_GLYF_GLYF_HH
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"
12 #include "glyf-helpers.hh"
14 #include "SubsetGlyph.hh"
16 #include "path-builder.hh"
23 * glyf -- TrueType Glyph Data
24 * https://docs.microsoft.com/en-us/typography/opentype/spec/glyf
26 #define HB_OT_TAG_glyf HB_TAG('g','l','y','f')
30 friend struct glyf_accelerator_t;
32 static constexpr hb_tag_t tableTag = HB_OT_TAG_glyf;
34 static bool has_valid_glyf_format(const hb_face_t* face)
36 const OT::head &head = *face->table.head;
37 return head.indexToLocFormat <= 1 && head.glyphDataFormat <= 1;
40 bool sanitize (hb_sanitize_context_t *c HB_UNUSED) const
42 TRACE_SANITIZE (this);
43 /* Runtime checks as eager sanitizing each glyph is costy */
47 /* requires source of SubsetGlyph complains the identifier isn't declared */
48 template <typename Iterator>
49 bool serialize (hb_serialize_context_t *c,
52 const hb_subset_plan_t *plan)
54 TRACE_SERIALIZE (this);
56 unsigned init_len = c->length ();
58 if (unlikely (!_.serialize (c, use_short_loca, plan)))
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
64 * See https://github.com/khaledhosny/ots/issues/52 */
65 if (init_len == c->length ())
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
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.");
88 hb_font_t *font = nullptr;
89 if (c->plan->normalized_coords)
91 font = _create_font_for_instancing (c->plan);
96 hb_vector_t<unsigned> padded_offsets;
97 if (unlikely (!padded_offsets.alloc (c->plan->new_to_old_gid_list.length, true)))
100 hb_vector_t<glyf_impl::SubsetGlyph> glyphs;
101 if (!_populate_subset_glyphs (c->plan, font, glyphs))
103 hb_font_destroy (font);
104 return_trace (false);
108 hb_font_destroy (font);
110 unsigned max_offset = 0;
111 for (auto &g : glyphs)
113 unsigned size = g.padded_size ();
114 padded_offsets.push (size);
118 bool use_short_loca = false;
119 if (likely (!c->plan->force_long_loca))
120 use_short_loca = max_offset < 0x1FFFF;
124 padded_offsets.resize (0);
125 for (auto &g : glyphs)
126 padded_offsets.push (g.length ());
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);
134 if (unlikely (!c->serializer->check_success (glyf_impl::_add_loca_and_head (c,
135 padded_offsets.iter (),
137 return_trace (false);
143 _populate_subset_glyphs (const hb_subset_plan_t *plan,
145 hb_vector_t<glyf_impl::SubsetGlyph>& glyphs /* OUT */) const;
148 _create_font_for_instancing (const hb_subset_plan_t *plan) const;
150 void _free_compiled_subset_glyphs (hb_vector_t<glyf_impl::SubsetGlyph> &glyphs) const
152 for (auto &g : glyphs)
153 g.free_compiled_bytes ();
157 UnsizedArrayOf<HBUINT8>
158 dataZ; /* Glyphs data. */
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. */
165 struct glyf_accelerator_t
167 glyf_accelerator_t (hb_face_t *face)
169 short_offset = false;
171 loca_table = nullptr;
172 glyf_table = nullptr;
177 #ifndef HB_NO_VERTICAL
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. */
184 short_offset = 0 == head.indexToLocFormat;
186 loca_table = face->table.loca.get_blob (); // Needs no destruct!
187 glyf_table = hb_sanitize_context_t ().reference_table<glyf> (face);
189 gvar = face->table.gvar;
191 hmtx = face->table.hmtx;
192 #ifndef HB_NO_VERTICAL
193 vmtx = face->table.vmtx;
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 ());
199 ~glyf_accelerator_t ()
201 glyf_table.destroy ();
204 bool has_data () const { return num_glyphs; }
208 bool get_points (hb_font_t *font, hb_codepoint_t gid, T consumer) const
210 if (gid >= num_glyphs) return false;
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;
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)))
222 unsigned count = all_points.length;
223 assert (count >= glyf_impl::PHANTOM_COUNT);
224 count -= glyf_impl::PHANTOM_COUNT;
226 if (consumer.is_consuming_contour_points ())
228 for (auto &point : all_points.as_array ().sub_array (0, count))
229 consumer.consume_point (point);
230 consumer.points_end ();
233 /* Where to write phantoms, nullptr if not requested */
234 contour_point_t *phantoms = consumer.get_phantoms_sink ();
236 for (unsigned i = 0; i < glyf_impl::PHANTOM_COUNT; ++i)
237 phantoms[i] = all_points.arrayZ[count + i];
245 struct points_aggregator_t
248 hb_glyph_extents_t *extents;
249 contour_point_t *phantoms;
252 struct contour_bounds_t
254 contour_bounds_t () { min_x = min_y = FLT_MAX; max_x = max_y = -FLT_MAX; }
256 void add (const contour_point_t &p)
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);
264 bool empty () const { return (min_x >= max_x) || (min_y >= max_y); }
266 void get_extents (hb_font_t *font, hb_glyph_extents_t *extents, bool scaled)
268 if (unlikely (empty ()))
271 extents->x_bearing = 0;
273 extents->y_bearing = 0;
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);
283 font->scale_glyph_extents (extents);
288 float min_x, min_y, max_x, max_y;
291 points_aggregator_t (hb_font_t *font_, hb_glyph_extents_t *extents_, contour_point_t *phantoms_, bool scaled_)
295 phantoms = phantoms_;
297 if (extents) bounds = contour_bounds_t ();
301 void consume_point (const contour_point_t &point) { bounds.add (point); }
302 void points_end () { bounds.get_extents (font, extents, scaled); }
304 bool is_consuming_contour_points () { return extents; }
305 contour_point_t *get_phantoms_sink () { return phantoms; }
309 get_advance_with_var_unscaled (hb_font_t *font, hb_codepoint_t gid, bool is_vertical) const
311 if (unlikely (gid >= num_glyphs)) return 0;
313 bool success = false;
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));
319 if (unlikely (!success))
321 #ifndef HB_NO_VERTICAL
322 is_vertical ? vmtx->get_advance_without_var_unscaled (gid) :
324 hmtx->get_advance_without_var_unscaled (gid);
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);
332 bool get_leading_bearing_with_var_unscaled (hb_font_t *font, hb_codepoint_t gid, bool is_vertical, int *lsb) const
334 if (unlikely (gid >= num_glyphs)) return false;
336 hb_glyph_extents_t extents;
338 contour_point_t phantoms[glyf_impl::PHANTOM_COUNT];
339 if (unlikely (!get_points (font, gid, points_aggregator_t (font, &extents, phantoms, false))))
343 ? roundf (phantoms[glyf_impl::PHANTOM_TOP].y) - extents.y_bearing
344 : roundf (phantoms[glyf_impl::PHANTOM_LEFT].x);
349 bool get_leading_bearing_without_var_unscaled (hb_codepoint_t gid, bool is_vertical, int *lsb) const
351 if (unlikely (gid >= num_glyphs)) return false;
352 if (is_vertical) return false; // TODO Humm, what to do here?
354 *lsb = glyph_for_gid (gid).get_header ()->xMin;
359 bool get_extents (hb_font_t *font, hb_codepoint_t gid, hb_glyph_extents_t *extents) const
361 if (unlikely (gid >= num_glyphs)) return false;
364 if (font->num_coords)
365 return get_points (font, gid, points_aggregator_t (font, extents, nullptr, true));
367 return glyph_for_gid (gid).get_extents_without_var_scaled (font, *this, extents);
370 bool paint_glyph (hb_font_t *font, hb_codepoint_t gid, hb_paint_funcs_t *funcs, void *data, hb_color_t foreground) const
372 funcs->push_clip_glyph (data, gid, font);
373 funcs->color (data, true, foreground);
374 funcs->pop_clip (data);
379 const glyf_impl::Glyph
380 glyph_for_gid (hb_codepoint_t gid, bool needs_padding_removal = false) const
382 if (unlikely (gid >= num_glyphs)) return glyf_impl::Glyph ();
384 unsigned int start_offset, end_offset;
388 const HBUINT16 *offsets = (const HBUINT16 *) loca_table->dataZ.arrayZ;
389 start_offset = 2 * offsets[gid];
390 end_offset = 2 * offsets[gid + 1];
394 const HBUINT32 *offsets = (const HBUINT32 *) loca_table->dataZ.arrayZ;
395 start_offset = offsets[gid];
396 end_offset = offsets[gid + 1];
399 if (unlikely (start_offset > end_offset || end_offset > glyf_table.get_length ()))
400 return glyf_impl::Glyph ();
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;
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)); }
412 const gvar_accelerator_t *gvar;
414 const hmtx_accelerator_t *hmtx;
415 #ifndef HB_NO_VERTICAL
416 const vmtx_accelerator_t *vmtx;
421 unsigned int num_glyphs;
422 hb_blob_ptr_t<loca> loca_table;
423 hb_blob_ptr_t<glyf> glyf_table;
428 glyf::_populate_subset_glyphs (const hb_subset_plan_t *plan,
430 hb_vector_t<glyf_impl::SubsetGlyph>& glyphs /* OUT */) const
432 OT::glyf_accelerator_t glyf (plan->source);
433 if (!glyphs.alloc (plan->new_to_old_gid_list.length, true)) return false;
435 for (const auto &pair : plan->new_to_old_gid_list)
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;
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 ();
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);
454 if (plan->flags & HB_SUBSET_FLAGS_NO_HINTING)
455 subset_glyph.drop_hints_bytes ();
457 subset_glyph.dest_start = subset_glyph.source_glyph.get_bytes ();
461 if (unlikely (!subset_glyph.compile_bytes_with_deltas (plan, font, glyf)))
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);
474 glyf::_create_font_for_instancing (const hb_subset_plan_t *plan) const
476 hb_font_t *font = hb_font_create (plan->source);
477 if (unlikely (font == hb_font_get_empty ())) return nullptr;
479 hb_vector_t<hb_variation_t> vars;
480 if (unlikely (!vars.alloc (plan->user_axes_location.get_population (), true)))
482 hb_font_destroy (font);
486 for (auto _ : plan->user_axes_location)
490 var.value = _.second.middle;
495 hb_font_set_variations (font, vars.arrayZ, plan->user_axes_location.get_population ());
504 #endif /* OT_GLYF_GLYF_HH */