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
has_valid_glyf_formatOT::glyf34 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
sanitizeOT::glyf40 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>
serializeOT::glyf49 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 */
subsetOT::glyf77 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
_free_compiled_subset_glyphsOT::glyf150 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 {
glyf_accelerator_tOT::glyf_accelerator_t167 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 }
~glyf_accelerator_tOT::glyf_accelerator_t199 ~glyf_accelerator_t ()
200 {
201 glyf_table.destroy ();
202 }
203
has_dataOT::glyf_accelerator_t204 bool has_data () const { return num_glyphs; }
205
206 protected:
207 template<typename T>
get_pointsOT::glyf_accelerator_t208 bool get_points (hb_font_t *font, hb_codepoint_t gid, T consumer,
209 hb_array_t<const int> coords = hb_array_t<const int> ()) const
210 {
211 if (!coords)
212 coords = hb_array (font->coords, font->num_coords);
213
214 if (gid >= num_glyphs) return false;
215
216 /* Making this allocfree is not that easy
217 https://github.com/harfbuzz/harfbuzz/issues/2095
218 mostly because of gvar handling in VF fonts,
219 perhaps a separate path for non-VF fonts can be considered */
220 contour_point_vector_t all_points;
221
222 bool phantom_only = !consumer.is_consuming_contour_points ();
223 if (unlikely (!glyph_for_gid (gid).get_points (font, *this, all_points, nullptr, nullptr, nullptr, true, true, phantom_only, coords)))
224 return false;
225
226 unsigned count = all_points.length;
227 assert (count >= glyf_impl::PHANTOM_COUNT);
228 count -= glyf_impl::PHANTOM_COUNT;
229
230 if (consumer.is_consuming_contour_points ())
231 {
232 for (auto &point : all_points.as_array ().sub_array (0, count))
233 consumer.consume_point (point);
234 consumer.points_end ();
235 }
236
237 /* Where to write phantoms, nullptr if not requested */
238 contour_point_t *phantoms = consumer.get_phantoms_sink ();
239 if (phantoms)
240 for (unsigned i = 0; i < glyf_impl::PHANTOM_COUNT; ++i)
241 phantoms[i] = all_points.arrayZ[count + i];
242
243 return true;
244 }
245
246 public:
247
248 #ifndef HB_NO_VAR
249 struct points_aggregator_t
250 {
251 hb_font_t *font;
252 hb_glyph_extents_t *extents;
253 contour_point_t *phantoms;
254 bool scaled;
255
256 struct contour_bounds_t
257 {
contour_bounds_tOT::glyf_accelerator_t::points_aggregator_t::contour_bounds_t258 contour_bounds_t () { min_x = min_y = FLT_MAX; max_x = max_y = -FLT_MAX; }
259
addOT::glyf_accelerator_t::points_aggregator_t::contour_bounds_t260 void add (const contour_point_t &p)
261 {
262 min_x = hb_min (min_x, p.x);
263 min_y = hb_min (min_y, p.y);
264 max_x = hb_max (max_x, p.x);
265 max_y = hb_max (max_y, p.y);
266 }
267
emptyOT::glyf_accelerator_t::points_aggregator_t::contour_bounds_t268 bool empty () const { return (min_x >= max_x) || (min_y >= max_y); }
269
get_extentsOT::glyf_accelerator_t::points_aggregator_t::contour_bounds_t270 void get_extents (hb_font_t *font, hb_glyph_extents_t *extents, bool scaled)
271 {
272 if (unlikely (empty ()))
273 {
274 extents->width = 0;
275 extents->x_bearing = 0;
276 extents->height = 0;
277 extents->y_bearing = 0;
278 return;
279 }
280 {
281 extents->x_bearing = roundf (min_x);
282 extents->width = roundf (max_x - extents->x_bearing);
283 extents->y_bearing = roundf (max_y);
284 extents->height = roundf (min_y - extents->y_bearing);
285
286 if (scaled)
287 font->scale_glyph_extents (extents);
288 }
289 }
290
291 protected:
292 float min_x, min_y, max_x, max_y;
293 } bounds;
294
points_aggregator_tOT::glyf_accelerator_t::points_aggregator_t295 points_aggregator_t (hb_font_t *font_, hb_glyph_extents_t *extents_, contour_point_t *phantoms_, bool scaled_)
296 {
297 font = font_;
298 extents = extents_;
299 phantoms = phantoms_;
300 scaled = scaled_;
301 if (extents) bounds = contour_bounds_t ();
302 }
303
304 HB_ALWAYS_INLINE
consume_pointOT::glyf_accelerator_t::points_aggregator_t305 void consume_point (const contour_point_t &point) { bounds.add (point); }
points_endOT::glyf_accelerator_t::points_aggregator_t306 void points_end () { bounds.get_extents (font, extents, scaled); }
307
is_consuming_contour_pointsOT::glyf_accelerator_t::points_aggregator_t308 bool is_consuming_contour_points () { return extents; }
get_phantoms_sinkOT::glyf_accelerator_t::points_aggregator_t309 contour_point_t *get_phantoms_sink () { return phantoms; }
310 };
311
312 unsigned
get_advance_with_var_unscaledOT::glyf_accelerator_t313 get_advance_with_var_unscaled (hb_font_t *font, hb_codepoint_t gid, bool is_vertical) const
314 {
315 if (unlikely (gid >= num_glyphs)) return 0;
316
317 bool success = false;
318
319 contour_point_t phantoms[glyf_impl::PHANTOM_COUNT];
320 if (font->num_coords)
321 success = get_points (font, gid, points_aggregator_t (font, nullptr, phantoms, false));
322
323 if (unlikely (!success))
324 return
325 #ifndef HB_NO_VERTICAL
326 is_vertical ? vmtx->get_advance_without_var_unscaled (gid) :
327 #endif
328 hmtx->get_advance_without_var_unscaled (gid);
329
330 float result = is_vertical
331 ? phantoms[glyf_impl::PHANTOM_TOP].y - phantoms[glyf_impl::PHANTOM_BOTTOM].y
332 : phantoms[glyf_impl::PHANTOM_RIGHT].x - phantoms[glyf_impl::PHANTOM_LEFT].x;
333 return hb_clamp (roundf (result), 0.f, (float) UINT_MAX / 2);
334 }
335
get_leading_bearing_with_var_unscaledOT::glyf_accelerator_t336 bool get_leading_bearing_with_var_unscaled (hb_font_t *font, hb_codepoint_t gid, bool is_vertical, int *lsb) const
337 {
338 if (unlikely (gid >= num_glyphs)) return false;
339
340 hb_glyph_extents_t extents;
341
342 contour_point_t phantoms[glyf_impl::PHANTOM_COUNT];
343 if (unlikely (!get_points (font, gid, points_aggregator_t (font, &extents, phantoms, false))))
344 return false;
345
346 *lsb = is_vertical
347 ? roundf (phantoms[glyf_impl::PHANTOM_TOP].y) - extents.y_bearing
348 : roundf (phantoms[glyf_impl::PHANTOM_LEFT].x);
349 return true;
350 }
351 #endif
352
get_leading_bearing_without_var_unscaledOT::glyf_accelerator_t353 bool get_leading_bearing_without_var_unscaled (hb_codepoint_t gid, bool is_vertical, int *lsb) const
354 {
355 if (unlikely (gid >= num_glyphs)) return false;
356 if (is_vertical) return false; // TODO Humm, what to do here?
357
358 *lsb = glyph_for_gid (gid).get_header ()->xMin;
359 return true;
360 }
361
362 public:
get_extentsOT::glyf_accelerator_t363 bool get_extents (hb_font_t *font, hb_codepoint_t gid, hb_glyph_extents_t *extents) const
364 {
365 if (unlikely (gid >= num_glyphs)) return false;
366
367 #ifndef HB_NO_VAR
368 if (font->num_coords)
369 return get_points (font, gid, points_aggregator_t (font, extents, nullptr, true));
370 #endif
371 return glyph_for_gid (gid).get_extents_without_var_scaled (font, *this, extents);
372 }
373
paint_glyphOT::glyf_accelerator_t374 bool paint_glyph (hb_font_t *font, hb_codepoint_t gid, hb_paint_funcs_t *funcs, void *data, hb_color_t foreground) const
375 {
376 funcs->push_clip_glyph (data, gid, font);
377 funcs->color (data, true, foreground);
378 funcs->pop_clip (data);
379
380 return true;
381 }
382
383 const glyf_impl::Glyph
glyph_for_gidOT::glyf_accelerator_t384 glyph_for_gid (hb_codepoint_t gid, bool needs_padding_removal = false) const
385 {
386 if (unlikely (gid >= num_glyphs)) return glyf_impl::Glyph ();
387
388 unsigned int start_offset, end_offset;
389
390 if (short_offset)
391 {
392 const HBUINT16 *offsets = (const HBUINT16 *) loca_table->dataZ.arrayZ;
393 start_offset = 2 * offsets[gid];
394 end_offset = 2 * offsets[gid + 1];
395 }
396 else
397 {
398 const HBUINT32 *offsets = (const HBUINT32 *) loca_table->dataZ.arrayZ;
399 start_offset = offsets[gid];
400 end_offset = offsets[gid + 1];
401 }
402
403 if (unlikely (start_offset > end_offset || end_offset > glyf_table.get_length ()))
404 return glyf_impl::Glyph ();
405
406 glyf_impl::Glyph glyph (hb_bytes_t ((const char *) this->glyf_table + start_offset,
407 end_offset - start_offset), gid);
408 return needs_padding_removal ? glyf_impl::Glyph (glyph.trim_padding (), gid) : glyph;
409 }
410
411 bool
get_pathOT::glyf_accelerator_t412 get_path (hb_font_t *font, hb_codepoint_t gid, hb_draw_session_t &draw_session) const
413 { return get_points (font, gid, glyf_impl::path_builder_t (font, draw_session)); }
414
415 bool
get_path_atOT::glyf_accelerator_t416 get_path_at (hb_font_t *font, hb_codepoint_t gid, hb_draw_session_t &draw_session,
417 hb_array_t<const int> coords) const
418 { return get_points (font, gid, glyf_impl::path_builder_t (font, draw_session), coords); }
419
420 #ifndef HB_NO_VAR
421 const gvar_accelerator_t *gvar;
422 #endif
423 const hmtx_accelerator_t *hmtx;
424 #ifndef HB_NO_VERTICAL
425 const vmtx_accelerator_t *vmtx;
426 #endif
427
428 private:
429 bool short_offset;
430 unsigned int num_glyphs;
431 hb_blob_ptr_t<loca> loca_table;
432 hb_blob_ptr_t<glyf> glyf_table;
433 };
434
435
436 inline bool
_populate_subset_glyphs(const hb_subset_plan_t * plan,hb_font_t * font,hb_vector_t<glyf_impl::SubsetGlyph> & glyphs) const437 glyf::_populate_subset_glyphs (const hb_subset_plan_t *plan,
438 hb_font_t *font,
439 hb_vector_t<glyf_impl::SubsetGlyph>& glyphs /* OUT */) const
440 {
441 OT::glyf_accelerator_t glyf (plan->source);
442 if (!glyphs.alloc (plan->new_to_old_gid_list.length, true)) return false;
443
444 for (const auto &pair : plan->new_to_old_gid_list)
445 {
446 hb_codepoint_t new_gid = pair.first;
447 hb_codepoint_t old_gid = pair.second;
448 glyf_impl::SubsetGlyph *p = glyphs.push ();
449 glyf_impl::SubsetGlyph& subset_glyph = *p;
450 subset_glyph.old_gid = old_gid;
451
452 if (unlikely (old_gid == 0 && new_gid == 0 &&
453 !(plan->flags & HB_SUBSET_FLAGS_NOTDEF_OUTLINE)) &&
454 !plan->normalized_coords)
455 subset_glyph.source_glyph = glyf_impl::Glyph ();
456 else
457 {
458 /* If plan has an accelerator, the preprocessing step already trimmed glyphs.
459 * Don't trim them again! */
460 subset_glyph.source_glyph = glyf.glyph_for_gid (subset_glyph.old_gid, !plan->accelerator);
461 }
462
463 if (plan->flags & HB_SUBSET_FLAGS_NO_HINTING)
464 subset_glyph.drop_hints_bytes ();
465 else
466 subset_glyph.dest_start = subset_glyph.source_glyph.get_bytes ();
467
468 if (font)
469 {
470 if (unlikely (!subset_glyph.compile_bytes_with_deltas (plan, font, glyf)))
471 {
472 // when pinned at default, only bounds are updated, thus no need to free
473 if (!plan->pinned_at_default)
474 _free_compiled_subset_glyphs (glyphs);
475 return false;
476 }
477 }
478 }
479 return true;
480 }
481
482 inline hb_font_t *
_create_font_for_instancing(const hb_subset_plan_t * plan) const483 glyf::_create_font_for_instancing (const hb_subset_plan_t *plan) const
484 {
485 hb_font_t *font = hb_font_create (plan->source);
486 if (unlikely (font == hb_font_get_empty ())) return nullptr;
487
488 hb_vector_t<hb_variation_t> vars;
489 if (unlikely (!vars.alloc (plan->user_axes_location.get_population (), true)))
490 {
491 hb_font_destroy (font);
492 return nullptr;
493 }
494
495 for (auto _ : plan->user_axes_location)
496 {
497 hb_variation_t var;
498 var.tag = _.first;
499 var.value = _.second.middle;
500 vars.push (var);
501 }
502
503 #ifndef HB_NO_VAR
504 hb_font_set_variations (font, vars.arrayZ, plan->user_axes_location.get_population ());
505 #endif
506 return font;
507 }
508
509
510 } /* namespace OT */
511
512
513 #endif /* OT_GLYF_GLYF_HH */
514