2 * Copyright © 2018 Ebrahim Byagowi
3 * Copyright © 2018 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 * Google Author(s): Behdad Esfahbod
28 #ifndef HB_AAT_LAYOUT_KERX_TABLE_HH
29 #define HB_AAT_LAYOUT_KERX_TABLE_HH
31 #include "hb-open-type-private.hh"
32 #include "hb-aat-layout-common-private.hh"
33 #include "hb-aat-layout-ankr-table.hh"
36 * kerx -- Extended Kerning
37 * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6kerx.html
39 #define HB_AAT_TAG_kerx HB_TAG('k','e','r','x')
47 struct KerxFormat0Records
49 inline bool sanitize (hb_sanitize_context_t *c) const
51 TRACE_SANITIZE (this);
52 return_trace (likely (c->check_struct (this)));
60 DEFINE_SIZE_STATIC (6);
63 struct KerxSubTableFormat0
65 // TODO(ebraminio) Enable when we got suitable BinSearchArrayOf
66 // inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const
68 // hb_glyph_pair_t pair = {left, right};
69 // int i = pairs.bsearch (pair);
72 // return pairs[i].get_kerning ();
75 inline bool sanitize (hb_sanitize_context_t *c) const
77 TRACE_SANITIZE (this);
78 return_trace (likely (c->check_struct (this) &&
79 recordsZ.sanitize (c, nPairs)));
83 // TODO(ebraminio): A custom version of "BinSearchArrayOf<KerxPair> pairs;" is
84 // needed here to use HBUINT32 instead
85 HBUINT32 nPairs; /* The number of kerning pairs in this subtable */
86 HBUINT32 searchRange; /* The largest power of two less than or equal to the value of nPairs,
87 * multiplied by the size in bytes of an entry in the subtable. */
88 HBUINT32 entrySelector; /* This is calculated as log2 of the largest power of two less
89 * than or equal to the value of nPairs. */
90 HBUINT32 rangeShift; /* The value of nPairs minus the largest power of two less than or equal to nPairs. */
91 UnsizedArrayOf<KerxFormat0Records>
92 recordsZ; /* VAR=nPairs */
94 DEFINE_SIZE_ARRAY (16, recordsZ);
97 struct KerxSubTableFormat1
99 inline bool sanitize (hb_sanitize_context_t *c) const
101 TRACE_SANITIZE (this);
102 return_trace (likely (c->check_struct (this) &&
103 stateHeader.sanitize (c)));
107 StateTable<HBUINT16> stateHeader;
108 LOffsetTo<ArrayOf<HBUINT16> > valueTable;
110 DEFINE_SIZE_STATIC (20);
113 // TODO(ebraminio): Maybe this can be replaced with Lookup<HBUINT16>?
114 struct KerxClassTable
116 inline unsigned int get_class (hb_codepoint_t g) const { return classes[g - firstGlyph]; }
118 inline bool sanitize (hb_sanitize_context_t *c) const
120 TRACE_SANITIZE (this);
121 return_trace (likely (firstGlyph.sanitize (c) &&
122 classes.sanitize (c)));
126 HBUINT16 firstGlyph; /* First glyph in class range. */
127 ArrayOf<HBUINT16> classes; /* Glyph classes. */
129 DEFINE_SIZE_ARRAY (4, classes);
132 struct KerxSubTableFormat2
134 inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right, const char *end) const
136 unsigned int l = (this+leftClassTable).get_class (left);
137 unsigned int r = (this+leftClassTable).get_class (left);
138 unsigned int offset = l * rowWidth + r * sizeof (FWORD);
139 const FWORD *arr = &(this+array);
140 if (unlikely ((const void *) arr < (const void *) this || (const void *) arr >= (const void *) end))
142 const FWORD *v = &StructAtOffset<FWORD> (arr, offset);
143 if (unlikely ((const void *) v < (const void *) arr || (const void *) (v + 1) > (const void *) end))
148 inline bool sanitize (hb_sanitize_context_t *c) const
150 TRACE_SANITIZE (this);
151 return_trace (likely (c->check_struct (this) &&
152 rowWidth.sanitize (c) &&
153 leftClassTable.sanitize (c, this) &&
154 rightClassTable.sanitize (c, this) &&
155 array.sanitize (c, this)));
159 HBUINT32 rowWidth; /* The width, in bytes, of a row in the table. */
160 LOffsetTo<KerxClassTable>
161 leftClassTable; /* Offset from beginning of this subtable to
162 * left-hand class table. */
163 LOffsetTo<KerxClassTable>
164 rightClassTable;/* Offset from beginning of this subtable to
165 * right-hand class table. */
167 array; /* Offset from beginning of this subtable to
168 * the start of the kerning array. */
170 DEFINE_SIZE_STATIC (16);
173 struct KerxSubTableFormat4
175 inline bool sanitize (hb_sanitize_context_t *c) const
177 TRACE_SANITIZE (this);
178 return_trace (likely (c->check_struct (this) &&
179 rowWidth.sanitize (c) &&
180 leftClassTable.sanitize (c, this) &&
181 rightClassTable.sanitize (c, this) &&
182 array.sanitize (c, this)));
186 HBUINT32 rowWidth; /* The width, in bytes, of a row in the table. */
187 LOffsetTo<KerxClassTable>
188 leftClassTable; /* Offset from beginning of this subtable to
189 * left-hand class table. */
190 LOffsetTo<KerxClassTable>
191 rightClassTable;/* Offset from beginning of this subtable to
192 * right-hand class table. */
194 array; /* Offset from beginning of this subtable to
195 * the start of the kerning array. */
197 DEFINE_SIZE_STATIC (16);
200 struct KerxSubTableFormat6
202 inline bool sanitize (hb_sanitize_context_t *c) const
204 TRACE_SANITIZE (this);
205 return_trace (likely (c->check_struct (this) &&
206 rowIndexTable.sanitize (c, this) &&
207 columnIndexTable.sanitize (c, this) &&
208 kerningArray.sanitize (c, this) &&
209 kerningVector.sanitize (c, this)));
215 HBUINT16 columnCount;
216 LOffsetTo<Lookup<HBUINT16> > rowIndexTable;
217 LOffsetTo<Lookup<HBUINT16> > columnIndexTable;
218 LOffsetTo<Lookup<HBUINT16> > kerningArray;
219 LOffsetTo<Lookup<HBUINT16> > kerningVector;
221 DEFINE_SIZE_STATIC (24);
224 enum coverage_flags_t
226 COVERAGE_VERTICAL_FLAG = 0x80u,
227 COVERAGE_CROSSSTREAM_FLAG = 0x40u,
228 COVERAGE_VARIATION_FLAG = 0x20u,
229 COVERAGE_PROCESS_DIRECTION = 0x10u,
234 inline bool apply (hb_aat_apply_context_t *c, const AAT::ankr *ankr) const
238 return_trace (false);
241 inline unsigned int get_size (void) const { return length; }
243 inline bool sanitize (hb_sanitize_context_t *c) const
245 TRACE_SANITIZE (this);
246 if (unlikely (!c->check_struct (this)))
247 return_trace (false);
250 case 0: return u.format0.sanitize (c);
251 case 1: return u.format1.sanitize (c);
252 case 2: return u.format2.sanitize (c);
253 case 4: return u.format4.sanitize (c);
254 case 6: return u.format6.sanitize (c);
255 default:return_trace (false);
266 KerxSubTableFormat0 format0;
267 KerxSubTableFormat1 format1;
268 KerxSubTableFormat2 format2;
269 KerxSubTableFormat4 format4;
270 KerxSubTableFormat6 format6;
273 DEFINE_SIZE_MIN (12);
276 struct SubtableGlyphCoverageArray
278 inline bool sanitize (hb_sanitize_context_t *c) const
280 TRACE_SANITIZE (this);
281 return_trace (likely (c->check_struct (this)));
289 DEFINE_SIZE_STATIC (12);
294 static const hb_tag_t tableTag = HB_AAT_TAG_kerx;
296 inline bool apply (hb_aat_apply_context_t *c, const AAT::ankr *ankr) const
299 const KerxTable &table = StructAfter<KerxTable> (*this);
300 return_trace (table.apply (c, ankr));
303 inline bool sanitize (hb_sanitize_context_t *c) const
305 TRACE_SANITIZE (this);
306 if (unlikely (!(c->check_struct (this))))
307 return_trace (false);
309 /* TODO: Something like `morx`s ChainSubtable should be done here instead */
310 const KerxTable *table = &StructAfter<KerxTable> (*this);
311 if (unlikely (!(table->sanitize (c))))
312 return_trace (false);
314 for (unsigned int i = 0; i < nTables - 1; ++i)
316 table = &StructAfter<KerxTable> (*table);
317 if (unlikely (!(table->sanitize (c))))
318 return_trace (false);
321 // If version is less than 3, we are done here; otherwise better to check footer also
325 // TODO: Investigate why this just work on some fonts no matter of version
326 // const SubtableGlyphCoverageArray &footer =
327 // StructAfter<SubtableGlyphCoverageArray> (*table);
328 // return_trace (footer.sanitize (c));
337 /*KerxTable tablesZ[VAR]; XXX ArrayOf??? */
338 /*SubtableGlyphCoverageArray coverage_array;*/
340 DEFINE_SIZE_STATIC (8);
343 } /* namespace AAT */
346 #endif /* HB_AAT_LAYOUT_KERX_TABLE_HH */