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"
34 * https://docs.microsoft.com/en-us/typography/opentype/spec/kern
35 * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6kern.html
37 #define HB_OT_TAG_kern HB_TAG('k','e','r','n')
43 struct hb_glyph_pair_t
51 inline int get_kerning (void) const
54 inline int cmp (const hb_glyph_pair_t &o) const
56 int ret = left.cmp (o.left);
58 return right.cmp (o.right);
61 inline bool sanitize (hb_sanitize_context_t *c) const
63 TRACE_SANITIZE (this);
64 return_trace (c->check_struct (this));
72 DEFINE_SIZE_STATIC (6);
75 struct KernSubTableFormat0
77 inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const
79 hb_glyph_pair_t pair = {left, right};
80 int i = pairs.bsearch (pair);
83 return pairs[i].get_kerning ();
86 inline bool sanitize (hb_sanitize_context_t *c) const
88 TRACE_SANITIZE (this);
89 return_trace (pairs.sanitize (c));
93 BinSearchArrayOf<KernPair> pairs; /* Array of kerning pairs. */
95 DEFINE_SIZE_ARRAY (8, pairs);
100 inline unsigned int get_class (hb_codepoint_t g) const { return classes[g - firstGlyph]; }
102 inline bool sanitize (hb_sanitize_context_t *c) const
104 TRACE_SANITIZE (this);
105 return_trace (firstGlyph.sanitize (c) && classes.sanitize (c));
109 HBUINT16 firstGlyph; /* First glyph in class range. */
110 ArrayOf<HBUINT16> classes; /* Glyph classes. */
112 DEFINE_SIZE_ARRAY (4, classes);
115 struct KernSubTableFormat2
117 inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right, const char *end) const
119 unsigned int l = (this+leftClassTable).get_class (left);
120 unsigned int r = (this+rightClassTable).get_class (right);
121 unsigned int offset = l * rowWidth + r * sizeof (FWORD);
122 const FWORD *arr = &(this+array);
123 if (unlikely ((const void *) arr < (const void *) this || (const void *) arr >= (const void *) end))
125 const FWORD *v = &StructAtOffset<FWORD> (arr, offset);
126 if (unlikely ((const void *) v < (const void *) arr || (const void *) (v + 1) > (const void *) end))
131 inline bool sanitize (hb_sanitize_context_t *c) const
133 TRACE_SANITIZE (this);
134 return_trace (rowWidth.sanitize (c) &&
135 leftClassTable.sanitize (c, this) &&
136 rightClassTable.sanitize (c, this) &&
137 array.sanitize (c, this));
141 HBUINT16 rowWidth; /* The width, in bytes, of a row in the table. */
142 OffsetTo<KernClassTable>
143 leftClassTable; /* Offset from beginning of this subtable to
144 * left-hand class table. */
145 OffsetTo<KernClassTable>
146 rightClassTable;/* Offset from beginning of this subtable to
147 * right-hand class table. */
149 array; /* Offset from beginning of this subtable to
150 * the start of the kerning array. */
157 inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right, const char *end, unsigned int format) const
160 case 0: return u.format0.get_kerning (left, right);
161 case 2: return u.format2.get_kerning (left, right, end);
166 inline bool sanitize (hb_sanitize_context_t *c, unsigned int format) const
168 TRACE_SANITIZE (this);
170 case 0: return_trace (u.format0.sanitize (c));
171 case 2: return_trace (u.format2.sanitize (c));
172 default:return_trace (true);
178 KernSubTableFormat0 format0;
179 KernSubTableFormat2 format2;
186 template <typename T>
187 struct KernSubTableWrapper
189 /* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */
190 inline const T* thiz (void) const { return static_cast<const T *> (this); }
192 inline bool is_horizontal (void) const
193 { return (thiz()->coverage & T::COVERAGE_CHECK_FLAGS) == T::COVERAGE_CHECK_HORIZONTAL; }
195 inline bool is_override (void) const
196 { return bool (thiz()->coverage & T::COVERAGE_OVERRIDE_FLAG); }
198 inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right, const char *end) const
199 { return thiz()->subtable.get_kerning (left, right, end, thiz()->format); }
201 inline int get_h_kerning (hb_codepoint_t left, hb_codepoint_t right, const char *end) const
202 { return is_horizontal () ? get_kerning (left, right, end) : 0; }
204 inline unsigned int get_size (void) const { return thiz()->length; }
206 inline bool sanitize (hb_sanitize_context_t *c) const
208 TRACE_SANITIZE (this);
209 return_trace (c->check_struct (thiz()) &&
210 thiz()->length >= T::min_size &&
211 c->check_array (thiz(), 1, thiz()->length) &&
212 thiz()->subtable.sanitize (c, thiz()->format));
216 template <typename T>
219 /* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */
220 inline const T* thiz (void) const { return static_cast<const T *> (this); }
222 inline int get_h_kerning (hb_codepoint_t left, hb_codepoint_t right, unsigned int table_length) const
225 const typename T::SubTableWrapper *st = CastP<typename T::SubTableWrapper> (thiz()->data);
226 unsigned int count = thiz()->nTables;
227 for (unsigned int i = 0; i < count; i++)
229 if (st->is_override ())
231 v += st->get_h_kerning (left, right, table_length + (const char *) this);
232 st = &StructAfter<typename T::SubTableWrapper> (*st);
237 inline bool sanitize (hb_sanitize_context_t *c) const
239 TRACE_SANITIZE (this);
240 if (unlikely (!c->check_struct (thiz()) ||
241 thiz()->version != T::VERSION))
242 return_trace (false);
244 const typename T::SubTableWrapper *st = CastP<typename T::SubTableWrapper> (thiz()->data);
245 unsigned int count = thiz()->nTables;
246 for (unsigned int i = 0; i < count; i++)
248 if (unlikely (!st->sanitize (c)))
249 return_trace (false);
250 st = &StructAfter<typename T::SubTableWrapper> (*st);
257 struct KernOT : KernTable<KernOT>
259 friend struct KernTable<KernOT>;
261 static const uint16_t VERSION = 0x0000u;
263 struct SubTableWrapper : KernSubTableWrapper<SubTableWrapper>
265 friend struct KernSubTableWrapper<SubTableWrapper>;
267 enum coverage_flags_t {
268 COVERAGE_DIRECTION_FLAG = 0x01u,
269 COVERAGE_MINIMUM_FLAG = 0x02u,
270 COVERAGE_CROSSSTREAM_FLAG = 0x04u,
271 COVERAGE_OVERRIDE_FLAG = 0x08u,
273 COVERAGE_VARIATION_FLAG = 0x00u, /* Not supported. */
275 COVERAGE_CHECK_FLAGS = 0x07u,
276 COVERAGE_CHECK_HORIZONTAL = 0x01u
280 HBUINT16 versionZ; /* Unused. */
281 HBUINT16 length; /* Length of the subtable (including this header). */
282 HBUINT8 format; /* Subtable format. */
283 HBUINT8 coverage; /* Coverage bits. */
284 KernSubTable subtable; /* Subtable data. */
290 HBUINT16 version; /* Version--0x0000u */
291 HBUINT16 nTables; /* Number of subtables in the kerning table. */
294 DEFINE_SIZE_ARRAY (4, data);
297 struct KernAAT : KernTable<KernAAT>
299 friend struct KernTable<KernAAT>;
301 static const uint32_t VERSION = 0x00010000u;
303 struct SubTableWrapper : KernSubTableWrapper<SubTableWrapper>
305 friend struct KernSubTableWrapper<SubTableWrapper>;
307 enum coverage_flags_t {
308 COVERAGE_DIRECTION_FLAG = 0x80u,
309 COVERAGE_CROSSSTREAM_FLAG = 0x40u,
310 COVERAGE_VARIATION_FLAG = 0x20u,
312 COVERAGE_OVERRIDE_FLAG = 0x00u, /* Not supported. */
314 COVERAGE_CHECK_FLAGS = 0xE0u,
315 COVERAGE_CHECK_HORIZONTAL = 0x00u
319 HBUINT32 length; /* Length of the subtable (including this header). */
320 HBUINT8 coverage; /* Coverage bits. */
321 HBUINT8 format; /* Subtable format. */
322 HBUINT16 tupleIndex; /* The tuple index (used for variations fonts).
323 * This value specifies which tuple this subtable covers. */
324 KernSubTable subtable; /* Subtable data. */
330 HBUINT32 version; /* Version--0x00010000u */
331 HBUINT32 nTables; /* Number of subtables in the kerning table. */
334 DEFINE_SIZE_ARRAY (8, data);
339 static const hb_tag_t tableTag = HB_OT_TAG_kern;
341 inline int get_h_kerning (hb_codepoint_t left, hb_codepoint_t right, unsigned int table_length) const
344 case 0: return u.ot.get_h_kerning (left, right, table_length);
345 case 1: return u.aat.get_h_kerning (left, right, table_length);
350 inline bool sanitize (hb_sanitize_context_t *c) const
352 TRACE_SANITIZE (this);
353 if (!u.major.sanitize (c)) return_trace (false);
355 case 0: return_trace (u.ot.sanitize (c));
356 case 1: return_trace (u.aat.sanitize (c));
357 default:return_trace (true);
363 inline void init (hb_face_t *face)
365 blob = Sanitizer<kern>().sanitize (face->reference_table (HB_OT_TAG_kern));
366 table = blob->as<kern> ();
367 table_length = blob->length;
369 inline void fini (void)
371 hb_blob_destroy (blob);
374 inline int get_h_kerning (hb_codepoint_t left, hb_codepoint_t right) const
375 { return table->get_h_kerning (left, right, table_length); }
380 unsigned int table_length;
390 DEFINE_SIZE_UNION (2, major);
396 #endif /* HB_OT_KERN_TABLE_HH */