2 * Copyright © 2017 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
27 #ifndef HB_OT_KERN_TABLE_HH
28 #define HB_OT_KERN_TABLE_HH
30 #include "hb-open-type-private.hh"
39 #define HB_OT_TAG_kern HB_TAG('k','e','r','n')
41 struct hb_glyph_pair_t
49 inline int get_kerning (void) const
52 inline int cmp (const hb_glyph_pair_t &o) const
54 int ret = left.cmp (o.left);
56 return right.cmp (o.right);
59 inline bool sanitize (hb_sanitize_context_t *c) const
61 TRACE_SANITIZE (this);
62 return_trace (c->check_struct (this));
70 DEFINE_SIZE_STATIC (6);
73 struct KernSubTableFormat0
75 inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const
77 hb_glyph_pair_t pair = {left, right};
78 int i = pairs.bsearch (pair);
81 return pairs[i].get_kerning ();
84 inline bool sanitize (hb_sanitize_context_t *c) const
86 TRACE_SANITIZE (this);
87 return_trace (pairs.sanitize (c));
91 BinSearchArrayOf<KernPair> pairs; /* Array of kerning pairs. */
93 DEFINE_SIZE_ARRAY (8, pairs);
98 inline unsigned int get_class (hb_codepoint_t g) const { return classes[g - firstGlyph]; }
100 inline bool sanitize (hb_sanitize_context_t *c) const
102 TRACE_SANITIZE (this);
103 return_trace (firstGlyph.sanitize (c) && classes.sanitize (c));
107 HBUINT16 firstGlyph; /* First glyph in class range. */
108 ArrayOf<HBUINT16> classes; /* Glyph classes. */
110 DEFINE_SIZE_ARRAY (4, classes);
113 struct KernSubTableFormat2
115 inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right, const char *end) const
117 unsigned int l = (this+leftClassTable).get_class (left);
118 unsigned int r = (this+rightClassTable).get_class (right);
119 unsigned int offset = l * rowWidth + r * sizeof (FWORD);
120 const FWORD *arr = &(this+array);
121 if (unlikely ((const void *) arr < (const void *) this || (const void *) arr >= (const void *) end))
123 const FWORD *v = &StructAtOffset<FWORD> (arr, offset);
124 if (unlikely ((const void *) v < (const void *) arr || (const void *) (v + 1) > (const void *) end))
129 inline bool sanitize (hb_sanitize_context_t *c) const
131 TRACE_SANITIZE (this);
132 return_trace (rowWidth.sanitize (c) &&
133 leftClassTable.sanitize (c, this) &&
134 rightClassTable.sanitize (c, this) &&
135 array.sanitize (c, this));
139 HBUINT16 rowWidth; /* The width, in bytes, of a row in the table. */
140 OffsetTo<KernClassTable>
141 leftClassTable; /* Offset from beginning of this subtable to
142 * left-hand class table. */
143 OffsetTo<KernClassTable>
144 rightClassTable;/* Offset from beginning of this subtable to
145 * right-hand class table. */
147 array; /* Offset from beginning of this subtable to
148 * the start of the kerning array. */
155 inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right, const char *end, unsigned int format) const
158 case 0: return u.format0.get_kerning (left, right);
159 case 2: return u.format2.get_kerning (left, right, end);
164 inline bool sanitize (hb_sanitize_context_t *c, unsigned int format) const
166 TRACE_SANITIZE (this);
168 case 0: return_trace (u.format0.sanitize (c));
169 case 2: return_trace (u.format2.sanitize (c));
170 default:return_trace (true);
176 KernSubTableFormat0 format0;
177 KernSubTableFormat2 format2;
184 template <typename T>
185 struct KernSubTableWrapper
187 /* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */
188 inline const T* thiz (void) const { return static_cast<const T *> (this); }
190 inline bool is_horizontal (void) const
191 { return (thiz()->coverage & T::COVERAGE_CHECK_FLAGS) == T::COVERAGE_CHECK_HORIZONTAL; }
193 inline bool is_override (void) const
194 { return bool (thiz()->coverage & T::COVERAGE_OVERRIDE_FLAG); }
196 inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right, const char *end) const
197 { return thiz()->subtable.get_kerning (left, right, end, thiz()->format); }
199 inline int get_h_kerning (hb_codepoint_t left, hb_codepoint_t right, const char *end) const
200 { return is_horizontal () ? get_kerning (left, right, end) : 0; }
202 inline unsigned int get_size (void) const { return thiz()->length; }
204 inline bool sanitize (hb_sanitize_context_t *c) const
206 TRACE_SANITIZE (this);
207 return_trace (c->check_struct (thiz()) &&
208 thiz()->length >= thiz()->min_size &&
209 c->check_array (thiz(), 1, thiz()->length) &&
210 thiz()->subtable.sanitize (c, thiz()->format));
214 template <typename T>
217 /* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */
218 inline const T* thiz (void) const { return static_cast<const T *> (this); }
220 inline int get_h_kerning (hb_codepoint_t left, hb_codepoint_t right, unsigned int table_length) const
223 const typename T::SubTableWrapper *st = CastP<typename T::SubTableWrapper> (thiz()->data);
224 unsigned int count = thiz()->nTables;
225 for (unsigned int i = 0; i < count; i++)
227 if (st->is_override ())
229 v += st->get_h_kerning (left, right, table_length + (const char *) this);
230 st = &StructAfter<typename T::SubTableWrapper> (*st);
235 inline bool sanitize (hb_sanitize_context_t *c) const
237 TRACE_SANITIZE (this);
238 if (unlikely (!c->check_struct (thiz()) ||
239 thiz()->version != T::VERSION))
240 return_trace (false);
242 const typename T::SubTableWrapper *st = CastP<typename T::SubTableWrapper> (thiz()->data);
243 unsigned int count = thiz()->nTables;
244 for (unsigned int i = 0; i < count; i++)
246 if (unlikely (!st->sanitize (c)))
247 return_trace (false);
248 st = &StructAfter<typename T::SubTableWrapper> (*st);
255 struct KernOT : KernTable<KernOT>
257 friend struct KernTable<KernOT>;
259 static const uint16_t VERSION = 0x0000u;
261 struct SubTableWrapper : KernSubTableWrapper<SubTableWrapper>
263 friend struct KernSubTableWrapper<SubTableWrapper>;
265 enum coverage_flags_t {
266 COVERAGE_DIRECTION_FLAG = 0x01u,
267 COVERAGE_MINIMUM_FLAG = 0x02u,
268 COVERAGE_CROSSSTREAM_FLAG = 0x04u,
269 COVERAGE_OVERRIDE_FLAG = 0x08u,
271 COVERAGE_VARIATION_FLAG = 0x00u, /* Not supported. */
273 COVERAGE_CHECK_FLAGS = 0x07u,
274 COVERAGE_CHECK_HORIZONTAL = 0x01u
278 HBUINT16 versionZ; /* Unused. */
279 HBUINT16 length; /* Length of the subtable (including this header). */
280 HBUINT8 format; /* Subtable format. */
281 HBUINT8 coverage; /* Coverage bits. */
282 KernSubTable subtable; /* Subtable data. */
288 HBUINT16 version; /* Version--0x0000u */
289 HBUINT16 nTables; /* Number of subtables in the kerning table. */
292 DEFINE_SIZE_ARRAY (4, data);
295 struct KernAAT : KernTable<KernAAT>
297 friend struct KernTable<KernAAT>;
299 static const uint32_t VERSION = 0x00010000u;
301 struct SubTableWrapper : KernSubTableWrapper<SubTableWrapper>
303 friend struct KernSubTableWrapper<SubTableWrapper>;
305 enum coverage_flags_t {
306 COVERAGE_DIRECTION_FLAG = 0x80u,
307 COVERAGE_CROSSSTREAM_FLAG = 0x40u,
308 COVERAGE_VARIATION_FLAG = 0x20u,
310 COVERAGE_OVERRIDE_FLAG = 0x00u, /* Not supported. */
312 COVERAGE_CHECK_FLAGS = 0xE0u,
313 COVERAGE_CHECK_HORIZONTAL = 0x00u
317 HBUINT32 length; /* Length of the subtable (including this header). */
318 HBUINT8 coverage; /* Coverage bits. */
319 HBUINT8 format; /* Subtable format. */
320 HBUINT16 tupleIndex; /* The tuple index (used for variations fonts).
321 * This value specifies which tuple this subtable covers. */
322 KernSubTable subtable; /* Subtable data. */
328 HBUINT32 version; /* Version--0x00010000u */
329 HBUINT32 nTables; /* Number of subtables in the kerning table. */
332 DEFINE_SIZE_ARRAY (8, data);
337 static const hb_tag_t tableTag = HB_OT_TAG_kern;
339 inline int get_h_kerning (hb_codepoint_t left, hb_codepoint_t right, unsigned int table_length) const
342 case 0: return u.ot.get_h_kerning (left, right, table_length);
343 case 1: return u.aat.get_h_kerning (left, right, table_length);
348 inline bool sanitize (hb_sanitize_context_t *c) const
350 TRACE_SANITIZE (this);
351 if (!u.major.sanitize (c)) return_trace (false);
353 case 0: return_trace (u.ot.sanitize (c));
354 case 1: return_trace (u.aat.sanitize (c));
355 default:return_trace (true);
361 inline void init (hb_face_t *face)
363 blob = Sanitizer<kern>().sanitize (face->reference_table (HB_OT_TAG_kern));
364 table = Sanitizer<kern>::lock_instance (blob);
365 table_length = hb_blob_get_length (blob);
367 inline void fini (void)
369 hb_blob_destroy (blob);
372 inline int get_h_kerning (hb_codepoint_t left, hb_codepoint_t right) const
373 { return table->get_h_kerning (left, right, table_length); }
378 unsigned int table_length;
388 DEFINE_SIZE_UNION (2, major);
394 #endif /* HB_OT_KERN_TABLE_HH */