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-hhea-table.hh"
32 #include "hb-ot-var-hvar-table.hh"
33 #include "hb-ot-metrics.hh"
36 * hmtx -- Horizontal Metrics
37 * https://docs.microsoft.com/en-us/typography/opentype/spec/hmtx
38 * vmtx -- Vertical Metrics
39 * https://docs.microsoft.com/en-us/typography/opentype/spec/vmtx
41 #define HB_OT_TAG_hmtx HB_TAG('h','m','t','x')
42 #define HB_OT_TAG_vmtx HB_TAG('v','m','t','x')
46 _glyf_get_side_bearing_var (hb_font_t *font, hb_codepoint_t glyph, bool is_vertical);
49 _glyf_get_advance_var (hb_font_t *font, hb_codepoint_t glyph, bool is_vertical);
57 UFWORD advance; /* Advance width/height. */
58 FWORD sb; /* Leading (left/top) side bearing. */
60 DEFINE_SIZE_STATIC (4);
64 template <typename T, typename H>
67 bool sanitize (hb_sanitize_context_t *c HB_UNUSED) const
69 TRACE_SANITIZE (this);
70 /* We don't check for anything specific here. The users of the
71 * struct do all the hard work... */
76 bool subset_update_header (hb_subset_plan_t *plan,
77 unsigned int num_hmetrics) const
79 hb_blob_t *src_blob = hb_sanitize_context_t ().reference_table<H> (plan->source, H::tableTag);
80 hb_blob_t *dest_blob = hb_blob_copy_writable_or_fail (src_blob);
81 hb_blob_destroy (src_blob);
83 if (unlikely (!dest_blob)) {
88 H *table = (H *) hb_blob_get_data (dest_blob, &length);
89 table->numberOfLongMetrics = num_hmetrics;
91 bool result = plan->add_table (H::tableTag, dest_blob);
92 hb_blob_destroy (dest_blob);
97 template<typename Iterator,
98 hb_requires (hb_is_iterator (Iterator))>
99 void serialize (hb_serialize_context_t *c,
101 unsigned num_advances)
105 | hb_apply ([c, &idx, num_advances] (const hb_item_type<Iterator>& _)
107 if (idx < num_advances)
110 lm.advance = _.first;
112 if (unlikely (!c->embed<LongMetric> (&lm))) return;
116 FWORD *sb = c->allocate_size<FWORD> (FWORD::static_size);
117 if (unlikely (!sb)) return;
125 bool subset (hb_subset_context_t *c) const
129 T *table_prime = c->serializer->start_embed <T> ();
130 if (unlikely (!table_prime)) return_trace (false);
133 _mtx.init (c->plan->source);
134 unsigned num_advances = _mtx.num_advances_for_subset (c->plan);
137 + hb_range (c->plan->num_output_glyphs ())
138 | hb_map ([c, &_mtx] (unsigned _)
140 hb_codepoint_t old_gid;
141 if (!c->plan->old_gid_for_new_gid (_, &old_gid))
142 return hb_pair (0u, 0);
143 return hb_pair (_mtx.get_advance (old_gid), _mtx.get_side_bearing (old_gid));
147 table_prime->serialize (c->serializer, it, num_advances);
151 if (unlikely (c->serializer->ran_out_of_room || c->serializer->in_error ()))
152 return_trace (false);
154 // Amend header num hmetrics
155 if (unlikely (!subset_update_header (c->plan, num_advances)))
156 return_trace (false);
163 friend struct hmtxvmtx;
165 void init (hb_face_t *face,
166 unsigned int default_advance_ = 0)
168 default_advance = default_advance_ ? default_advance_ : hb_face_get_upem (face);
170 num_advances = T::is_horizontal ? face->table.hhea->numberOfLongMetrics : face->table.vhea->numberOfLongMetrics;
172 table = hb_sanitize_context_t ().reference_table<hmtxvmtx> (face, T::tableTag);
174 /* Cap num_metrics() and num_advances() based on table length. */
175 unsigned int len = table.get_length ();
176 if (unlikely (num_advances * 4 > len))
177 num_advances = len / 4;
178 num_metrics = num_advances + (len - 4 * num_advances) / 2;
180 /* We MUST set num_metrics to zero if num_advances is zero.
181 * Our get_advance() depends on that. */
182 if (unlikely (!num_advances))
184 num_metrics = num_advances = 0;
186 table = hb_blob_get_empty ();
189 var_table = hb_sanitize_context_t ().reference_table<HVARVVAR> (face, T::variationsTag);
195 var_table.destroy ();
198 int get_side_bearing (hb_codepoint_t glyph) const
200 if (glyph < num_advances)
201 return table->longMetricZ[glyph].sb;
203 if (unlikely (glyph >= num_metrics))
206 const FWORD *bearings = (const FWORD *) &table->longMetricZ[num_advances];
207 return bearings[glyph - num_advances];
210 int get_side_bearing (hb_font_t *font, hb_codepoint_t glyph) const
212 int side_bearing = get_side_bearing (glyph);
215 if (unlikely (glyph >= num_metrics) || !font->num_coords)
218 if (var_table.get_length ())
219 return side_bearing + var_table->get_side_bearing_var (glyph, font->coords, font->num_coords); // TODO Optimize?!
221 return _glyf_get_side_bearing_var (font, glyph, T::tableTag == HB_OT_TAG_vmtx);
227 unsigned int get_advance (hb_codepoint_t glyph) const
229 if (unlikely (glyph >= num_metrics))
231 /* If num_metrics is zero, it means we don't have the metrics table
232 * for this direction: return default advance. Otherwise, it means that the
233 * glyph index is out of bound: return zero. */
237 return default_advance;
240 return table->longMetricZ[hb_min (glyph, (uint32_t) num_advances - 1)].advance;
243 unsigned int get_advance (hb_codepoint_t glyph,
244 hb_font_t *font) const
246 unsigned int advance = get_advance (glyph);
249 if (unlikely (glyph >= num_metrics) || !font->num_coords)
252 if (var_table.get_length ())
253 return advance + roundf (var_table->get_advance_var (glyph, font)); // TODO Optimize?!
255 return _glyf_get_advance_var (font, glyph, T::tableTag == HB_OT_TAG_vmtx);
261 unsigned int num_advances_for_subset (const hb_subset_plan_t *plan) const
263 unsigned int num_advances = plan->num_output_glyphs ();
264 unsigned int last_advance = _advance_for_new_gid (plan,
266 while (num_advances > 1 &&
267 last_advance == _advance_for_new_gid (plan,
277 unsigned int _advance_for_new_gid (const hb_subset_plan_t *plan,
278 hb_codepoint_t new_gid) const
280 hb_codepoint_t old_gid;
281 if (!plan->old_gid_for_new_gid (new_gid, &old_gid))
284 return get_advance (old_gid);
288 unsigned int num_metrics;
289 unsigned int num_advances;
290 unsigned int default_advance;
293 hb_blob_ptr_t<hmtxvmtx> table;
294 hb_blob_ptr_t<HVARVVAR> var_table;
298 UnsizedArrayOf<LongMetric>
299 longMetricZ; /* Paired advance width and leading
300 * bearing values for each glyph. The
301 * value numOfHMetrics comes from
302 * the 'hhea' table. If the font is
303 * monospaced, only one entry need
304 * be in the array, but that entry is
305 * required. The last entry applies to
306 * all subsequent glyphs. */
307 /*UnsizedArrayOf<FWORD> leadingBearingX;*/
308 /* Here the advance is assumed
309 * to be the same as the advance
310 * for the last entry above. The
311 * number of entries in this array is
312 * derived from numGlyphs (from 'maxp'
313 * table) minus numberOfLongMetrics.
314 * This generally is used with a run
315 * of monospaced glyphs (e.g., Kanji
316 * fonts or Courier fonts). Only one
317 * run is allowed and it must be at
318 * the end. This allows a monospaced
319 * font to vary the side bearing
320 * values for each glyph. */
322 DEFINE_SIZE_ARRAY (0, longMetricZ);
325 struct hmtx : hmtxvmtx<hmtx, hhea> {
326 static constexpr hb_tag_t tableTag = HB_OT_TAG_hmtx;
327 static constexpr hb_tag_t variationsTag = HB_OT_TAG_HVAR;
328 static constexpr bool is_horizontal = true;
330 struct vmtx : hmtxvmtx<vmtx, vhea> {
331 static constexpr hb_tag_t tableTag = HB_OT_TAG_vmtx;
332 static constexpr hb_tag_t variationsTag = HB_OT_TAG_VVAR;
333 static constexpr bool is_horizontal = false;
336 struct hmtx_accelerator_t : hmtx::accelerator_t {};
337 struct vmtx_accelerator_t : vmtx::accelerator_t {};
342 #endif /* HB_OT_HMTX_TABLE_HH */