blob: d0624ecc41531f256a015d34a32a322660399ed5 [file] [log] [blame]
Ebrahim Byagowi79756c92018-02-19 03:17:44 +03301/*
Ebrahim Byagowi79756c92018-02-19 03:17:44 +03302 * Copyright © 2018 Ebrahim Byagowi
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +03303 * Copyright © 2018 Google, Inc.
Ebrahim Byagowi79756c92018-02-19 03:17:44 +03304 *
5 * This is part of HarfBuzz, a text shaping library.
6 *
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.
12 *
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
17 * DAMAGE.
18 *
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.
24 *
25 * Google Author(s): Behdad Esfahbod
26 */
27
28#ifndef HB_AAT_LAYOUT_KERX_TABLE_HH
29#define HB_AAT_LAYOUT_KERX_TABLE_HH
30
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +033031#include "hb-open-type-private.hh"
Ebrahim Byagowi79756c92018-02-19 03:17:44 +033032#include "hb-aat-layout-common-private.hh"
33
Ebrahim Byagowi158f2812018-03-26 12:04:30 +043034#define HB_AAT_TAG_kerx HB_TAG('k','e','r','x')
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +033035
36
Ebrahim Byagowi79756c92018-02-19 03:17:44 +033037namespace AAT {
38
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +033039using namespace OT;
Ebrahim Byagowi79756c92018-02-19 03:17:44 +033040
Ebrahim Byagowi79756c92018-02-19 03:17:44 +033041
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +033042struct KerxFormat0Records
Ebrahim Byagowi79756c92018-02-19 03:17:44 +033043{
Ebrahim Byagowi79756c92018-02-19 03:17:44 +033044 inline bool sanitize (hb_sanitize_context_t *c) const
45 {
46 TRACE_SANITIZE (this);
47 return_trace (c->check_struct (this));
48 }
49
50 protected:
51 GlyphID left;
52 GlyphID right;
53 FWORD value;
Ebrahim Byagowi79756c92018-02-19 03:17:44 +033054 public:
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +033055 DEFINE_SIZE_STATIC (6);
Ebrahim Byagowi79756c92018-02-19 03:17:44 +033056};
57
58struct KerxSubTableFormat0
59{
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +033060 // TODO(ebraminio) Enable when we got suitable BinSearchArrayOf
61 // inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const
62 // {
63 // hb_glyph_pair_t pair = {left, right};
64 // int i = pairs.bsearch (pair);
65 // if (i == -1)
66 // return 0;
67 // return pairs[i].get_kerning ();
68 // }
Ebrahim Byagowi79756c92018-02-19 03:17:44 +033069
70 inline bool sanitize (hb_sanitize_context_t *c) const
71 {
72 TRACE_SANITIZE (this);
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +033073 return_trace (c->check_struct (this) &&
74 c->check_array (records, records[0].static_size, nPairs));
Ebrahim Byagowi79756c92018-02-19 03:17:44 +033075 }
76
77 protected:
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +033078 // TODO(ebraminio): A custom version of "BinSearchArrayOf<KerxPair> pairs;" is
79 // needed here to use HBUINT32 instead
80 HBUINT32 nPairs; /* The number of kerning pairs in this subtable */
81 HBUINT32 searchRange; /* The largest power of two less than or equal to the value of nPairs,
82 * multiplied by the size in bytes of an entry in the subtable. */
83 HBUINT32 entrySelector; /* This is calculated as log2 of the largest power of two less
84 * than or equal to the value of nPairs. */
85 HBUINT32 rangeShift; /* The value of nPairs minus the largest power of two less than or equal to nPairs. */
86 KerxFormat0Records records[VAR]; /* VAR=nPairs */
Ebrahim Byagowi79756c92018-02-19 03:17:44 +033087 public:
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +033088 DEFINE_SIZE_ARRAY (16, records);
Ebrahim Byagowi79756c92018-02-19 03:17:44 +033089};
90
91struct KerxSubTableFormat1
92{
93 inline bool sanitize (hb_sanitize_context_t *c) const
94 {
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +033095 TRACE_SANITIZE (this);
96 return_trace (c->check_struct (this) &&
97 stateHeader.sanitize (c));
Ebrahim Byagowi79756c92018-02-19 03:17:44 +033098 }
99
100 protected:
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +0330101 StateTable<HBUINT16> stateHeader;
102 LOffsetTo<ArrayOf<HBUINT16> > valueTable;
Ebrahim Byagowi79756c92018-02-19 03:17:44 +0330103 public:
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +0330104 DEFINE_SIZE_STATIC (20);
Ebrahim Byagowi79756c92018-02-19 03:17:44 +0330105};
106
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +0330107// TODO(ebraminio): Maybe this can be replaced with Lookup<HBUINT16>?
Ebrahim Byagowi79756c92018-02-19 03:17:44 +0330108struct KerxClassTable
109{
110 inline unsigned int get_class (hb_codepoint_t g) const { return classes[g - firstGlyph]; }
111
112 inline bool sanitize (hb_sanitize_context_t *c) const
113 {
114 TRACE_SANITIZE (this);
115 return_trace (firstGlyph.sanitize (c) && classes.sanitize (c));
116 }
117
118 protected:
119 HBUINT16 firstGlyph; /* First glyph in class range. */
120 ArrayOf<HBUINT16> classes; /* Glyph classes. */
121 public:
122 DEFINE_SIZE_ARRAY (4, classes);
123};
124
125struct KerxSubTableFormat2
126{
127 inline int get_kerning (hb_codepoint_t left, hb_codepoint_t right, const char *end) const
128 {
129 unsigned int l = (this+leftClassTable).get_class (left);
130 unsigned int r = (this+leftClassTable).get_class (left);
131 unsigned int offset = l * rowWidth + r * sizeof (FWORD);
132 const FWORD *arr = &(this+array);
133 if (unlikely ((const void *) arr < (const void *) this || (const void *) arr >= (const void *) end))
134 return 0;
135 const FWORD *v = &StructAtOffset<FWORD> (arr, offset);
136 if (unlikely ((const void *) v < (const void *) arr || (const void *) (v + 1) > (const void *) end))
137 return 0;
138 return *v;
139 }
140
141 inline bool sanitize (hb_sanitize_context_t *c) const
142 {
143 TRACE_SANITIZE (this);
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +0330144 return_trace (c->check_struct (this) &&
145 rowWidth.sanitize (c) &&
Ebrahim Byagowi79756c92018-02-19 03:17:44 +0330146 leftClassTable.sanitize (c, this) &&
147 rightClassTable.sanitize (c, this) &&
148 array.sanitize (c, this));
149 }
150
151 protected:
152 HBUINT32 rowWidth; /* The width, in bytes, of a row in the table. */
153 LOffsetTo<KerxClassTable>
154 leftClassTable; /* Offset from beginning of this subtable to
155 * left-hand class table. */
156 LOffsetTo<KerxClassTable>
157 rightClassTable;/* Offset from beginning of this subtable to
158 * right-hand class table. */
159 LOffsetTo<FWORD>
160 array; /* Offset from beginning of this subtable to
161 * the start of the kerning array. */
162 public:
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +0330163 DEFINE_SIZE_STATIC (16);
Ebrahim Byagowi79756c92018-02-19 03:17:44 +0330164};
165
166struct KerxSubTableFormat4
167{
168 inline bool sanitize (hb_sanitize_context_t *c) const
169 {
170 TRACE_SANITIZE (this);
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +0330171 return_trace (c->check_struct (this) &&
172 rowWidth.sanitize (c) &&
Ebrahim Byagowi79756c92018-02-19 03:17:44 +0330173 leftClassTable.sanitize (c, this) &&
174 rightClassTable.sanitize (c, this) &&
175 array.sanitize (c, this));
176 }
177
178 protected:
179 HBUINT32 rowWidth; /* The width, in bytes, of a row in the table. */
180 LOffsetTo<KerxClassTable>
181 leftClassTable; /* Offset from beginning of this subtable to
182 * left-hand class table. */
183 LOffsetTo<KerxClassTable>
184 rightClassTable;/* Offset from beginning of this subtable to
185 * right-hand class table. */
186 LOffsetTo<FWORD>
187 array; /* Offset from beginning of this subtable to
188 * the start of the kerning array. */
189 public:
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +0330190 DEFINE_SIZE_STATIC (16);
Ebrahim Byagowi79756c92018-02-19 03:17:44 +0330191};
192
193struct KerxSubTableFormat6
194{
195 inline bool sanitize (hb_sanitize_context_t *c) const
196 {
Ebrahim Byagowi79756c92018-02-19 03:17:44 +0330197 TRACE_SANITIZE (this);
Ebrahim Byagowi71b45982018-03-02 11:04:09 +0330198 return_trace (c->check_struct (this) &&
199 rowIndexTable.sanitize (c, this) &&
200 columnIndexTable.sanitize (c, this) &&
201 kerningArray.sanitize (c, this) &&
202 kerningVector.sanitize (c, this));
Ebrahim Byagowi79756c92018-02-19 03:17:44 +0330203 }
204
205 protected:
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +0330206 HBUINT32 flags;
207 HBUINT16 rowCount;
208 HBUINT16 columnCount;
Ebrahim Byagowi71b45982018-03-02 11:04:09 +0330209 LOffsetTo<Lookup<HBUINT16> > rowIndexTable;
210 LOffsetTo<Lookup<HBUINT16> > columnIndexTable;
211 LOffsetTo<Lookup<HBUINT16> > kerningArray;
212 LOffsetTo<Lookup<HBUINT16> > kerningVector;
Ebrahim Byagowi79756c92018-02-19 03:17:44 +0330213 public:
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +0330214 DEFINE_SIZE_STATIC (24);
Ebrahim Byagowi79756c92018-02-19 03:17:44 +0330215};
216
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +0330217enum coverage_flags_t
Ebrahim Byagowi79756c92018-02-19 03:17:44 +0330218{
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +0330219 COVERAGE_VERTICAL_FLAG = 0x80u,
220 COVERAGE_CROSSSTREAM_FLAG = 0x40u,
221 COVERAGE_VARIATION_FLAG = 0x20u,
222 COVERAGE_PROCESS_DIRECTION = 0x10u,
223};
Ebrahim Byagowi79756c92018-02-19 03:17:44 +0330224
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +0330225struct KerxTable
226{
Ebrahim Byagowiae14dd02018-02-26 00:31:09 +0330227 inline bool apply (hb_aat_apply_context_t *c, const AAT::ankr *ankr) const
Ebrahim Byagowi1ab16f42018-02-24 12:49:42 +0330228 {
229 TRACE_APPLY (this);
230 /* TODO */
231 return_trace (false);
232 }
233
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +0330234 inline unsigned int get_size (void) const { return length; }
235
236 inline bool sanitize (hb_sanitize_context_t *c) const
Ebrahim Byagowi79756c92018-02-19 03:17:44 +0330237 {
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +0330238 TRACE_SANITIZE (this);
239 if (!c->check_struct (this))
240 return_trace (false);
Ebrahim Byagowi79756c92018-02-19 03:17:44 +0330241
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +0330242 switch (format) {
243 case 0: return u.format0.sanitize (c);
244 case 1: return u.format1.sanitize (c);
245 case 2: return u.format2.sanitize (c);
246 case 4: return u.format4.sanitize (c);
247 case 6: return u.format6.sanitize (c);
248 default:return_trace (false);
Ebrahim Byagowi79756c92018-02-19 03:17:44 +0330249 }
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +0330250 }
Ebrahim Byagowi79756c92018-02-19 03:17:44 +0330251
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +0330252protected:
253 HBUINT32 length;
254 HBUINT8 coverage;
255 HBUINT16 unused;
256 HBUINT8 format;
257 HBUINT32 tupleIndex;
258 union {
259 KerxSubTableFormat0 format0;
260 KerxSubTableFormat1 format1;
261 KerxSubTableFormat2 format2;
262 KerxSubTableFormat4 format4;
263 KerxSubTableFormat6 format6;
264 } u;
265public:
266 DEFINE_SIZE_MIN (12);
267};
268
269struct SubtableGlyphCoverageArray
270{
271 inline bool sanitize (hb_sanitize_context_t *c) const
Ebrahim Byagowi79756c92018-02-19 03:17:44 +0330272 {
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +0330273 TRACE_SANITIZE (this);
274 return_trace (c->check_struct (this));
275 }
276
277 protected:
278 HBUINT32 length;
279 HBUINT32 coverage;
280 HBUINT32 tupleCount;
281 public:
282 DEFINE_SIZE_STATIC (12);
283};
284
285struct kerx
286{
Ebrahim Byagowi158f2812018-03-26 12:04:30 +0430287 static const hb_tag_t tableTag = HB_AAT_TAG_kerx;
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +0330288
289 inline bool apply (hb_aat_apply_context_t *c, const AAT::ankr *ankr) const
290 {
291 TRACE_APPLY (this);
292 const KerxTable &table = StructAfter<KerxTable> (*this);
293 return_trace (table.apply (c, ankr));
Ebrahim Byagowi79756c92018-02-19 03:17:44 +0330294 }
295
296 inline bool sanitize (hb_sanitize_context_t *c) const
297 {
298 TRACE_SANITIZE (this);
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +0330299 if (!(c->check_struct (this)))
300 return_trace (false);
301
Ebrahim Byagowia3e29fd2018-03-04 02:56:27 +0330302 /* TODO: Something like `morx`s ChainSubtable should be done here instead */
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +0330303 const KerxTable *table = &StructAfter<KerxTable> (*this);
304 if (!(table->sanitize (c)))
305 return_trace (false);
306
307 for (unsigned int i = 0; i < nTables - 1; ++i)
Ebrahim Byagowi79756c92018-02-19 03:17:44 +0330308 {
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +0330309 table = &StructAfter<KerxTable> (*table);
310 if (!(table->sanitize (c)))
311 return_trace (false);
Ebrahim Byagowi79756c92018-02-19 03:17:44 +0330312 }
313
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +0330314 // If version is less than 3, we are done here; otherwise better to check footer also
315 if (version < 3)
316 return_trace (true);
317
318 // TODO: Investigate why this just work on some fonts no matter of version
319 // const SubtableGlyphCoverageArray &footer =
320 // StructAfter<SubtableGlyphCoverageArray> (*table);
321 // return_trace (footer.sanitize (c));
322
Ebrahim Byagowi79756c92018-02-19 03:17:44 +0330323 return_trace (true);
324 }
325
Ebrahim Byagowi79756c92018-02-19 03:17:44 +0330326 protected:
327 HBUINT16 version;
328 HBUINT16 padding;
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +0330329 HBUINT32 nTables;
330/*KerxTable tables[VAR];*/
331/*SubtableGlyphCoverageArray coverage_array;*/
Ebrahim Byagowi79756c92018-02-19 03:17:44 +0330332 public:
Ebrahim Byagowib73a5a12018-03-02 00:07:26 +0330333 DEFINE_SIZE_STATIC (8);
Ebrahim Byagowi79756c92018-02-19 03:17:44 +0330334};
335
336} /* namespace AAT */
337
338
339#endif /* HB_AAT_LAYOUT_KERX_TABLE_HH */