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-private.hh"
31 #include "hb-ot-hhea-table.hh"
32 #include "hb-ot-os2-table.hh"
33 #include "hb-ot-var-hvar-table.hh"
40 * hmtx -- The Horizontal Metrics Table
41 * vmtx -- The Vertical Metrics Table
44 #define HB_OT_TAG_hmtx HB_TAG('h','m','t','x')
45 #define HB_OT_TAG_vmtx HB_TAG('v','m','t','x')
50 UFWORD advance; /* Advance width/height. */
51 FWORD lsb; /* Leading (left/top) side bearing. */
53 DEFINE_SIZE_STATIC (4);
56 template <typename T, typename H>
59 inline bool sanitize (hb_sanitize_context_t *c) const
61 TRACE_SANITIZE (this);
62 /* We don't check for anything specific here. The users of the
63 * struct do all the hard work... */
68 inline bool subset_update_header (hb_subset_plan_t *plan,
69 unsigned int num_hmetrics) const
71 hb_blob_t *src_blob = OT::Sanitizer<H> ().sanitize (plan->source->reference_table (H::tableTag));
72 hb_blob_t *dest_blob = hb_blob_copy_writable_or_fail(src_blob);
73 hb_blob_destroy (src_blob);
75 if (unlikely (!dest_blob)) {
80 H *table = (H *) hb_blob_get_data (dest_blob, &length);
81 table->numberOfLongMetrics.set (num_hmetrics);
83 bool result = hb_subset_plan_add_table (plan, H::tableTag, dest_blob);
84 hb_blob_destroy (dest_blob);
89 inline bool subset (hb_subset_plan_t *plan) const
91 typename T::accelerator_t _mtx;
92 _mtx.init (plan->source);
94 /* All the trailing glyphs with the same advance can use one LongMetric
95 * and just keep LSB */
96 hb_prealloced_array_t<hb_codepoint_t> &gids = plan->gids_to_retain_sorted;
97 unsigned int num_advances = gids.len;
98 unsigned int last_advance = _mtx.get_advance (gids[num_advances - 1]);
99 while (num_advances > 1
100 && last_advance == _mtx.get_advance (gids[num_advances - 2]))
105 /* alloc the new table */
106 size_t dest_sz = num_advances * 4
107 + (gids.len - num_advances) * 2;
108 void *dest = (void *) malloc (dest_sz);
109 if (unlikely (!dest))
113 DEBUG_MSG(SUBSET, nullptr, "%c%c%c%c in src has %d advances, %d lsbs", HB_UNTAG(T::tableTag), _mtx.num_advances, _mtx.num_metrics - _mtx.num_advances);
114 DEBUG_MSG(SUBSET, nullptr, "%c%c%c%c in dest has %d advances, %d lsbs, %u bytes", HB_UNTAG(T::tableTag), num_advances, gids.len - num_advances, (unsigned int) dest_sz);
116 const char *source_table = hb_blob_get_data (_mtx.blob, nullptr);
117 // Copy everything over
118 LongMetric * old_metrics = (LongMetric *) source_table;
119 FWORD *lsbs = (FWORD *) (old_metrics + _mtx.num_advances);
120 char * dest_pos = (char *) dest;
121 for (unsigned int i = 0; i < gids.len; i++)
123 /* the last metric or the one for gids[i] */
124 LongMetric *src_metric = old_metrics + MIN ((hb_codepoint_t) _mtx.num_advances - 1, gids[i]);
125 if (gids[i] < _mtx.num_advances)
127 /* src is a LongMetric */
128 if (i < num_advances)
130 /* dest is a LongMetric, copy it */
131 *((LongMetric *) dest_pos) = *src_metric;
136 *((FWORD *) dest_pos) = src_metric->lsb;
141 FWORD src_lsb = *(lsbs + gids[i] - _mtx.num_advances);
142 if (i < num_advances)
144 /* dest needs a full LongMetric */
145 LongMetric *metric = (LongMetric *)dest_pos;
146 metric->advance = src_metric->advance;
147 metric->lsb = src_lsb;
151 /* dest just needs an lsb */
152 *((FWORD *) dest_pos) = src_lsb;
155 dest_pos += (i < num_advances ? 4 : 2);
159 // Amend header num hmetrics
160 if (unlikely (!subset_update_header (plan, num_advances)))
166 hb_blob_t *result = hb_blob_create ((const char *)dest,
168 HB_MEMORY_MODE_READONLY,
171 bool success = hb_subset_plan_add_table (plan, T::tableTag, result);
172 hb_blob_destroy (result);
178 friend struct hmtxvmtx;
180 inline void init (hb_face_t *face,
181 unsigned int default_advance_ = 0)
183 default_advance = default_advance_ ? default_advance_ : hb_face_get_upem (face);
185 bool got_font_extents = false;
188 hb_blob_t *os2_blob = Sanitizer<os2> ().sanitize (face->reference_table (T::os2Tag));
189 const os2 *os2_table = Sanitizer<os2>::lock_instance (os2_blob);
190 #define USE_TYPO_METRICS (1u<<7)
191 if (0 != (os2_table->fsSelection & USE_TYPO_METRICS))
193 ascender = os2_table->sTypoAscender;
194 descender = os2_table->sTypoDescender;
195 line_gap = os2_table->sTypoLineGap;
196 got_font_extents = (ascender | descender) != 0;
198 hb_blob_destroy (os2_blob);
201 hb_blob_t *_hea_blob = Sanitizer<H> ().sanitize (face->reference_table (H::tableTag));
202 const H *_hea_table = Sanitizer<H>::lock_instance (_hea_blob);
203 num_advances = _hea_table->numberOfLongMetrics;
204 if (!got_font_extents)
206 ascender = _hea_table->ascender;
207 descender = _hea_table->descender;
208 line_gap = _hea_table->lineGap;
209 got_font_extents = (ascender | descender) != 0;
211 hb_blob_destroy (_hea_blob);
213 has_font_extents = got_font_extents;
215 blob = Sanitizer<hmtxvmtx> ().sanitize (face->reference_table (T::tableTag));
217 /* Cap num_metrics() and num_advances() based on table length. */
218 unsigned int len = hb_blob_get_length (blob);
219 if (unlikely (num_advances * 4 > len))
220 num_advances = len / 4;
221 num_metrics = num_advances + (len - 4 * num_advances) / 2;
223 /* We MUST set num_metrics to zero if num_advances is zero.
224 * Our get_advance() depends on that. */
225 if (unlikely (!num_advances))
227 num_metrics = num_advances = 0;
228 hb_blob_destroy (blob);
229 blob = hb_blob_get_empty ();
231 table = Sanitizer<hmtxvmtx>::lock_instance (blob);
233 var_blob = Sanitizer<HVARVVAR> ().sanitize (face->reference_table (T::variationsTag));
234 var_table = Sanitizer<HVARVVAR>::lock_instance (var_blob);
237 inline void fini (void)
239 hb_blob_destroy (blob);
240 hb_blob_destroy (var_blob);
243 inline unsigned int get_advance (hb_codepoint_t glyph) const
245 if (unlikely (glyph >= num_metrics))
247 /* If num_metrics is zero, it means we don't have the metrics table
248 * for this direction: return default advance. Otherwise, it means that the
249 * glyph index is out of bound: return zero. */
253 return default_advance;
256 return table->longMetric[MIN (glyph, (uint32_t) num_advances - 1)].advance;
259 inline unsigned int get_advance (hb_codepoint_t glyph,
260 hb_font_t *font) const
262 unsigned int advance = get_advance (glyph);
263 if (likely(glyph < num_metrics))
265 advance += (font->num_coords ? var_table->get_advance_var (glyph, font->coords, font->num_coords) : 0); // TODO Optimize?!
271 bool has_font_extents;
272 unsigned short ascender;
273 unsigned short descender;
274 unsigned short line_gap;
277 unsigned int num_metrics;
278 unsigned int num_advances;
279 unsigned int default_advance;
282 const hmtxvmtx *table;
284 const HVARVVAR *var_table;
289 LongMetric longMetric[VAR]; /* Paired advance width and leading
290 * bearing values for each glyph. The
291 * value numOfHMetrics comes from
292 * the 'hhea' table. If the font is
293 * monospaced, only one entry need
294 * be in the array, but that entry is
295 * required. The last entry applies to
296 * all subsequent glyphs. */
297 /*FWORD leadingBearingX[VAR];*/ /* Here the advance is assumed
298 * to be the same as the advance
299 * for the last entry above. The
300 * number of entries in this array is
301 * derived from numGlyphs (from 'maxp'
302 * table) minus numberOfLongMetrics.
303 * This generally is used with a run
304 * of monospaced glyphs (e.g., Kanji
305 * fonts or Courier fonts). Only one
306 * run is allowed and it must be at
307 * the end. This allows a monospaced
308 * font to vary the side bearing
309 * values for each glyph. */
311 DEFINE_SIZE_ARRAY (0, longMetric);
314 struct hmtx : hmtxvmtx<hmtx, hhea> {
315 static const hb_tag_t tableTag = HB_OT_TAG_hmtx;
316 static const hb_tag_t variationsTag = HB_OT_TAG_HVAR;
317 static const hb_tag_t os2Tag = HB_OT_TAG_os2;
319 struct vmtx : hmtxvmtx<vmtx, vhea> {
320 static const hb_tag_t tableTag = HB_OT_TAG_vmtx;
321 static const hb_tag_t variationsTag = HB_OT_TAG_VVAR;
322 static const hb_tag_t os2Tag = HB_TAG_NONE;
328 #endif /* HB_OT_HMTX_TABLE_HH */