2 * Copyright © 2011,2012 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 * Google Author(s): Behdad Esfahbod, Roderick Sheeter
27 #ifndef HB_OT_HMTX_TABLE_HH
28 #define HB_OT_HMTX_TABLE_HH
30 #include "hb-open-type.hh"
31 #include "hb-ot-maxp-table.hh"
32 #include "hb-ot-hhea-table.hh"
33 #include "hb-ot-var-hvar-table.hh"
34 #include "hb-ot-var-mvar-table.hh"
35 #include "hb-ot-metrics.hh"
38 * hmtx -- Horizontal Metrics
39 * https://docs.microsoft.com/en-us/typography/opentype/spec/hmtx
40 * vmtx -- Vertical Metrics
41 * https://docs.microsoft.com/en-us/typography/opentype/spec/vmtx
43 #define HB_OT_TAG_hmtx HB_TAG('h','m','t','x')
44 #define HB_OT_TAG_vmtx HB_TAG('v','m','t','x')
48 _glyf_get_leading_bearing_with_var_unscaled (hb_font_t *font, hb_codepoint_t glyph, bool is_vertical, int *lsb);
51 _glyf_get_advance_with_var_unscaled (hb_font_t *font, hb_codepoint_t glyph, bool is_vertical);
54 _glyf_get_leading_bearing_without_var_unscaled (hb_face_t *face, hb_codepoint_t gid, bool is_vertical, int *lsb);
62 UFWORD advance; /* Advance width/height. */
63 FWORD sb; /* Leading (left/top) side bearing. */
65 DEFINE_SIZE_STATIC (4);
69 template <typename T/*Data table type*/, typename H/*Header table type*/, typename V/*Var table type*/>
72 bool sanitize (hb_sanitize_context_t *c HB_UNUSED) const
74 TRACE_SANITIZE (this);
75 /* We don't check for anything specific here. The users of the
76 * struct do all the hard work... */
80 const hb_hashmap_t<hb_codepoint_t, hb_pair_t<unsigned, int>>* get_mtx_map (const hb_subset_plan_t *plan) const
81 { return T::is_horizontal ? &plan->hmtx_map : &plan->vmtx_map; }
83 bool subset_update_header (hb_subset_context_t *c,
84 unsigned int num_hmetrics,
85 const hb_hashmap_t<hb_codepoint_t, hb_pair_t<unsigned, int>> *mtx_map,
86 const hb_vector_t<unsigned> &bounds_vec) const
88 hb_blob_t *src_blob = hb_sanitize_context_t ().reference_table<H> (c->plan->source, H::tableTag);
89 hb_blob_t *dest_blob = hb_blob_copy_writable_or_fail (src_blob);
90 hb_blob_destroy (src_blob);
92 if (unlikely (!dest_blob)) {
97 H *table = (H *) hb_blob_get_data (dest_blob, &length);
98 c->serializer->check_assign (table->numberOfLongMetrics, num_hmetrics, HB_SERIALIZE_ERROR_INT_OVERFLOW);
101 if (c->plan->normalized_coords)
103 auto &MVAR = *c->plan->source->table.MVAR;
104 if (T::is_horizontal)
106 HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_HORIZONTAL_CARET_RISE, caretSlopeRise);
107 HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_HORIZONTAL_CARET_RUN, caretSlopeRun);
108 HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_HORIZONTAL_CARET_OFFSET, caretOffset);
112 HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_VERTICAL_CARET_RISE, caretSlopeRise);
113 HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_VERTICAL_CARET_RUN, caretSlopeRun);
114 HB_ADD_MVAR_VAR (HB_OT_METRICS_TAG_VERTICAL_CARET_OFFSET, caretOffset);
118 int min_lsb = 0x7FFF;
119 int min_rsb = 0x7FFF;
120 int max_extent = -0x7FFF;
121 unsigned max_adv = 0;
122 for (const auto _ : *mtx_map)
124 hb_codepoint_t gid = _.first;
125 unsigned adv = _.second.first;
126 int lsb = _.second.second;
127 max_adv = hb_max (max_adv, adv);
129 if (bounds_vec[gid] != 0xFFFFFFFF)
132 unsigned bound_width = bounds_vec[gid];
133 int rsb = adv - lsb - bound_width;
134 int extent = lsb + bound_width;
135 min_lsb = hb_min (min_lsb, lsb);
136 min_rsb = hb_min (min_rsb, rsb);
137 max_extent = hb_max (max_extent, extent);
141 table->advanceMax = max_adv;
144 table->minLeadingBearing = min_lsb;
145 table->minTrailingBearing = min_rsb;
146 table->maxExtent = max_extent;
151 bool result = c->plan->add_table (H::tableTag, dest_blob);
152 hb_blob_destroy (dest_blob);
157 template<typename Iterator,
158 hb_requires (hb_is_iterator (Iterator))>
159 void serialize (hb_serialize_context_t *c,
161 const hb_vector_t<hb_codepoint_pair_t> new_to_old_gid_list,
162 unsigned num_long_metrics,
163 unsigned total_num_metrics)
165 LongMetric* long_metrics = c->allocate_size<LongMetric> (num_long_metrics * LongMetric::static_size);
166 FWORD* short_metrics = c->allocate_size<FWORD> ((total_num_metrics - num_long_metrics) * FWORD::static_size);
167 if (!long_metrics || !short_metrics) return;
169 short_metrics -= num_long_metrics;
171 for (auto _ : new_to_old_gid_list)
173 hb_codepoint_t gid = _.first;
176 if (gid < num_long_metrics)
178 LongMetric& lm = long_metrics[gid];
179 lm.advance = mtx.first;
182 // TODO(beyond-64k): This assumes that maxp.numGlyphs is 0xFFFF.
183 else if (gid < 0x10000u)
184 short_metrics[gid] = mtx.second;
186 ((UFWORD*) short_metrics)[gid] = mtx.first;
190 bool subset (hb_subset_context_t *c) const
194 auto *table_prime = c->serializer->start_embed <T> ();
196 accelerator_t _mtx (c->plan->source);
197 unsigned num_long_metrics;
198 const hb_hashmap_t<hb_codepoint_t, hb_pair_t<unsigned, int>> *mtx_map = get_mtx_map (c->plan);
200 /* Determine num_long_metrics to encode. */
201 auto& plan = c->plan;
203 // TODO Don't consider retaingid holes here.
205 num_long_metrics = hb_min (plan->num_output_glyphs (), 0xFFFFu);
206 unsigned int last_advance = get_new_gid_advance_unscaled (plan, mtx_map, num_long_metrics - 1, _mtx);
207 while (num_long_metrics > 1 &&
208 last_advance == get_new_gid_advance_unscaled (plan, mtx_map, num_long_metrics - 2, _mtx))
215 + hb_iter (c->plan->new_to_old_gid_list)
216 | hb_map ([c, &_mtx, mtx_map] (hb_codepoint_pair_t _)
218 hb_codepoint_t new_gid = _.first;
219 hb_codepoint_t old_gid = _.second;
221 hb_pair_t<unsigned, int> *v = nullptr;
222 if (!mtx_map->has (new_gid, &v))
225 if (!_mtx.get_leading_bearing_without_var_unscaled (old_gid, &lsb))
226 (void) _glyf_get_leading_bearing_without_var_unscaled (c->plan->source, old_gid, !T::is_horizontal, &lsb);
227 return hb_pair (_mtx.get_advance_without_var_unscaled (old_gid), +lsb);
233 table_prime->serialize (c->serializer,
235 c->plan->new_to_old_gid_list,
237 c->plan->num_output_glyphs ());
239 if (unlikely (c->serializer->in_error ()))
240 return_trace (false);
242 // Amend header num hmetrics
243 if (unlikely (!subset_update_header (c, num_long_metrics, mtx_map,
244 T::is_horizontal ? c->plan->bounds_width_vec : c->plan->bounds_height_vec)))
245 return_trace (false);
252 friend struct hmtxvmtx;
254 accelerator_t (hb_face_t *face)
256 table = hb_sanitize_context_t ().reference_table<hmtxvmtx> (face, T::tableTag);
257 var_table = hb_sanitize_context_t ().reference_table<V> (face, T::variationsTag);
259 default_advance = T::is_horizontal ? hb_face_get_upem (face) / 2 : hb_face_get_upem (face);
261 /* Populate count variables and sort them out as we go */
263 unsigned int len = table.get_length ();
267 num_long_metrics = T::is_horizontal ?
268 face->table.hhea->numberOfLongMetrics :
269 #ifndef HB_NO_VERTICAL
270 face->table.vhea->numberOfLongMetrics
275 if (unlikely (num_long_metrics * 4 > len))
276 num_long_metrics = len / 4;
277 len -= num_long_metrics * 4;
279 num_bearings = face->table.maxp->get_num_glyphs ();
281 if (unlikely (num_bearings < num_long_metrics))
282 num_bearings = num_long_metrics;
283 if (unlikely ((num_bearings - num_long_metrics) * 2 > len))
284 num_bearings = num_long_metrics + len / 2;
285 len -= (num_bearings - num_long_metrics) * 2;
287 /* We MUST set num_bearings to zero if num_long_metrics is zero.
288 * Our get_advance() depends on that. */
289 if (unlikely (!num_long_metrics))
290 num_bearings = num_long_metrics = 0;
292 num_advances = num_bearings + len / 2;
293 num_glyphs = face->get_num_glyphs ();
294 if (num_glyphs < num_advances)
295 num_glyphs = num_advances;
300 var_table.destroy ();
303 bool has_data () const { return (bool) num_bearings; }
305 bool get_leading_bearing_without_var_unscaled (hb_codepoint_t glyph,
308 if (glyph < num_long_metrics)
310 *lsb = table->longMetricZ[glyph].sb;
314 if (unlikely (glyph >= num_bearings))
317 const FWORD *bearings = (const FWORD *) &table->longMetricZ[num_long_metrics];
318 *lsb = bearings[glyph - num_long_metrics];
322 bool get_leading_bearing_with_var_unscaled (hb_font_t *font,
323 hb_codepoint_t glyph,
326 if (!font->num_coords)
327 return get_leading_bearing_without_var_unscaled (glyph, lsb);
331 if (var_table->get_lsb_delta_unscaled (glyph, font->coords, font->num_coords, &delta) &&
332 get_leading_bearing_without_var_unscaled (glyph, lsb))
334 *lsb += roundf (delta);
338 return _glyf_get_leading_bearing_with_var_unscaled (font, glyph, T::tableTag == HB_OT_TAG_vmtx, lsb);
344 unsigned int get_advance_without_var_unscaled (hb_codepoint_t glyph) const
347 if (glyph < num_bearings)
348 return table->longMetricZ[hb_min (glyph, (uint32_t) num_long_metrics - 1)].advance;
350 /* If num_advances is zero, it means we don't have the metrics table
351 * for this direction: return default advance. Otherwise, there's a
352 * well-defined answer. */
353 if (unlikely (!num_advances))
354 return default_advance;
356 #ifdef HB_NO_BEYOND_64K
360 if (unlikely (glyph >= num_glyphs))
363 /* num_bearings <= glyph < num_glyphs;
364 * num_bearings <= num_advances */
366 if (num_bearings == num_advances)
367 return get_advance_without_var_unscaled (num_bearings - 1);
369 const FWORD *bearings = (const FWORD *) &table->longMetricZ[num_long_metrics];
370 const UFWORD *advances = (const UFWORD *) &bearings[num_bearings - num_long_metrics];
372 return advances[hb_min (glyph - num_bearings, num_advances - num_bearings - 1)];
375 unsigned get_advance_with_var_unscaled (hb_codepoint_t glyph,
377 VariationStore::cache_t *store_cache = nullptr) const
379 unsigned int advance = get_advance_without_var_unscaled (glyph);
382 if (unlikely (glyph >= num_bearings) || !font->num_coords)
385 if (var_table.get_length ())
386 return advance + roundf (var_table->get_advance_delta_unscaled (glyph,
387 font->coords, font->num_coords,
390 return _glyf_get_advance_with_var_unscaled (font, glyph, T::tableTag == HB_OT_TAG_vmtx);
397 // 0 <= num_long_metrics <= num_bearings <= num_advances <= num_glyphs
398 unsigned num_long_metrics;
399 unsigned num_bearings;
400 unsigned num_advances;
403 unsigned int default_advance;
406 hb_blob_ptr_t<hmtxvmtx> table;
407 hb_blob_ptr_t<V> var_table;
410 /* get advance: when no variations, call get_advance_without_var_unscaled.
411 * when there're variations, get advance value from mtx_map in subset_plan*/
412 unsigned get_new_gid_advance_unscaled (const hb_subset_plan_t *plan,
413 const hb_hashmap_t<hb_codepoint_t, hb_pair_t<unsigned, int>> *mtx_map,
415 const accelerator_t &_mtx) const
417 if (mtx_map->is_empty ())
419 hb_codepoint_t old_gid = 0;
420 return plan->old_gid_for_new_gid (new_gid, &old_gid) ?
421 _mtx.get_advance_without_var_unscaled (old_gid) : 0;
423 return mtx_map->get (new_gid).first;
427 UnsizedArrayOf<LongMetric>
428 longMetricZ; /* Paired advance width and leading
429 * bearing values for each glyph. The
430 * value numOfHMetrics comes from
431 * the 'hhea' table. If the font is
432 * monospaced, only one entry need
433 * be in the array, but that entry is
434 * required. The last entry applies to
435 * all subsequent glyphs. */
436 /*UnsizedArrayOf<FWORD> leadingBearingX;*/
437 /* Here the advance is assumed
438 * to be the same as the advance
439 * for the last entry above. The
440 * number of entries in this array is
441 * derived from numGlyphs (from 'maxp'
442 * table) minus numberOfLongMetrics.
443 * This generally is used with a run
444 * of monospaced glyphs (e.g., Kanji
445 * fonts or Courier fonts). Only one
446 * run is allowed and it must be at
447 * the end. This allows a monospaced
448 * font to vary the side bearing
449 * values for each glyph. */
450 /*UnsizedArrayOf<UFWORD>advancesX;*/
453 DEFINE_SIZE_ARRAY (0, longMetricZ);
456 struct hmtx : hmtxvmtx<hmtx, hhea, HVAR> {
457 static constexpr hb_tag_t tableTag = HB_OT_TAG_hmtx;
458 static constexpr hb_tag_t variationsTag = HB_OT_TAG_HVAR;
459 static constexpr bool is_horizontal = true;
461 struct vmtx : hmtxvmtx<vmtx, vhea, VVAR> {
462 static constexpr hb_tag_t tableTag = HB_OT_TAG_vmtx;
463 static constexpr hb_tag_t variationsTag = HB_OT_TAG_VVAR;
464 static constexpr bool is_horizontal = false;
467 struct hmtx_accelerator_t : hmtx::accelerator_t {
468 hmtx_accelerator_t (hb_face_t *face) : hmtx::accelerator_t (face) {}
470 struct vmtx_accelerator_t : vmtx::accelerator_t {
471 vmtx_accelerator_t (hb_face_t *face) : vmtx::accelerator_t (face) {}
477 #endif /* HB_OT_HMTX_TABLE_HH */