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
32 #include "hb-aat-layout-ankr-table.hh"
35 * kerx -- Extended Kerning
36 * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6kerx.html
38 #define HB_AAT_TAG_kerx HB_TAG('k','e','r','x')
47 kerxTupleKern (int value,
48 unsigned int tupleCount,
50 hb_aat_apply_context_t *c)
52 if (likely (!tupleCount || !c)) return value;
54 unsigned int offset = value;
55 const FWORD *pv = &StructAtOffset<FWORD> (base, offset);
56 if (unlikely (!c->sanitizer.check_array (pv, tupleCount))) return 0;
61 struct hb_glyph_pair_t
69 int get_kerning () const { return value; }
71 int cmp (const hb_glyph_pair_t &o) const
73 int ret = left.cmp (o.left);
75 return right.cmp (o.right);
78 bool sanitize (hb_sanitize_context_t *c) const
80 TRACE_SANITIZE (this);
81 return_trace (c->check_struct (this));
89 DEFINE_SIZE_STATIC (6);
92 template <typename KernSubTableHeader>
93 struct KerxSubTableFormat0
95 int get_kerning (hb_codepoint_t left, hb_codepoint_t right,
96 hb_aat_apply_context_t *c = nullptr) const
98 hb_glyph_pair_t pair = {left, right};
99 int v = pairs.bsearch (pair).get_kerning ();
100 return kerxTupleKern (v, header.tuple_count (), this, c);
103 bool apply (hb_aat_apply_context_t *c) const
107 if (!c->plan->requested_kerning)
110 if (header.coverage & header.Backwards)
113 accelerator_t accel (*this, c);
114 hb_kern_machine_t<accelerator_t> machine (accel, header.coverage & header.CrossStream);
115 machine.kern (c->font, c->buffer, c->plan->kern_mask);
122 const KerxSubTableFormat0 &table;
123 hb_aat_apply_context_t *c;
125 accelerator_t (const KerxSubTableFormat0 &table_,
126 hb_aat_apply_context_t *c_) :
127 table (table_), c (c_) {}
129 int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const
130 { return table.get_kerning (left, right, c); }
134 bool sanitize (hb_sanitize_context_t *c) const
136 TRACE_SANITIZE (this);
137 return_trace (likely (pairs.sanitize (c)));
141 KernSubTableHeader header;
142 BinSearchArrayOf<KernPair, typename KernSubTableHeader::Types::HBUINT>
143 pairs; /* Sorted kern records. */
145 DEFINE_SIZE_ARRAY (KernSubTableHeader::static_size + 16, pairs);
149 template <bool extended>
153 struct Format1Entry<true>
157 Push = 0x8000, /* If set, push this glyph on the kerning stack. */
158 DontAdvance = 0x4000, /* If set, don't advance to the next glyph
159 * before going to the new state. */
160 Reset = 0x2000, /* If set, reset the kerning data (clear the stack) */
161 Reserved = 0x1FFF, /* Not used; set to 0. */
166 HBUINT16 kernActionIndex;/* Index into the kerning value array. If
167 * this index is 0xFFFF, then no kerning
168 * is to be performed. */
170 DEFINE_SIZE_STATIC (2);
173 static bool performAction (const Entry<EntryData> &entry)
174 { return entry.data.kernActionIndex != 0xFFFF; }
176 static unsigned int kernActionIndex (const Entry<EntryData> &entry)
177 { return entry.data.kernActionIndex; }
180 struct Format1Entry<false>
184 Push = 0x8000, /* If set, push this glyph on the kerning stack. */
185 DontAdvance = 0x4000, /* If set, don't advance to the next glyph
186 * before going to the new state. */
187 Offset = 0x3FFF, /* Byte offset from beginning of subtable to the
188 * value table for the glyphs on the kerning stack. */
190 Reset = 0x0000, /* Not supported? */
193 typedef void EntryData;
195 static bool performAction (const Entry<EntryData> &entry)
196 { return entry.flags & Offset; }
198 static unsigned int kernActionIndex (const Entry<EntryData> &entry)
199 { return entry.flags & Offset; }
202 template <typename KernSubTableHeader>
203 struct KerxSubTableFormat1
205 typedef typename KernSubTableHeader::Types Types;
206 typedef typename Types::HBUINT HBUINT;
208 typedef Format1Entry<Types::extended> Format1EntryT;
209 typedef typename Format1EntryT::EntryData EntryData;
211 struct driver_context_t
213 static constexpr bool in_place = true;
216 DontAdvance = Format1EntryT::DontAdvance,
219 driver_context_t (const KerxSubTableFormat1 *table_,
220 hb_aat_apply_context_t *c_) :
223 /* Apparently the offset kernAction is from the beginning of the state-machine,
224 * similar to offsets in morx table, NOT from beginning of this table, like
225 * other subtables in kerx. Discovered via testing. */
226 kernAction (&table->machine + table->kernAction),
228 crossStream (table->header.coverage & table->header.CrossStream) {}
230 bool is_actionable (StateTableDriver<Types, EntryData> *driver HB_UNUSED,
231 const Entry<EntryData> &entry)
233 return Format1EntryT::performAction (entry);
235 void transition (StateTableDriver<Types, EntryData> *driver,
236 const Entry<EntryData> &entry)
238 hb_buffer_t *buffer = driver->buffer;
239 unsigned int flags = entry.flags;
241 if (flags & Format1EntryT::Reset)
244 if (flags & Format1EntryT::Push)
246 if (likely (depth < ARRAY_LENGTH (stack)))
247 stack[depth++] = buffer->idx;
249 depth = 0; /* Probably not what CoreText does, but better? */
252 if (Format1EntryT::performAction (entry) && depth)
254 unsigned int tuple_count = hb_max (1u, table->header.tuple_count ());
256 unsigned int kern_idx = Format1EntryT::kernActionIndex (entry);
257 kern_idx = Types::byteOffsetToIndex (kern_idx, &table->machine, kernAction.arrayZ);
258 const FWORD *actions = &kernAction[kern_idx];
259 if (!c->sanitizer.check_array (actions, depth, tuple_count))
265 hb_mask_t kern_mask = c->plan->kern_mask;
267 /* From Apple 'kern' spec:
268 * "Each pops one glyph from the kerning stack and applies the kerning value to it.
269 * The end of the list is marked by an odd value... */
271 while (!last && depth)
273 unsigned int idx = stack[--depth];
275 actions += tuple_count;
276 if (idx >= buffer->len) continue;
278 /* "The end of the list is marked by an odd value..." */
282 hb_glyph_position_t &o = buffer->pos[idx];
284 if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
288 /* The following flag is undocumented in the spec, but described
289 * in the 'kern' table example. */
292 o.attach_type() = ATTACH_TYPE_NONE;
293 o.attach_chain() = 0;
296 else if (o.attach_type())
298 o.y_offset += c->font->em_scale_y (v);
299 buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
302 else if (buffer->info[idx].mask & kern_mask)
304 o.x_advance += c->font->em_scale_x (v);
305 o.x_offset += c->font->em_scale_x (v);
312 /* CoreText doesn't do crossStream kerning in vertical. We do. */
315 o.attach_type() = ATTACH_TYPE_NONE;
316 o.attach_chain() = 0;
319 else if (o.attach_type())
321 o.x_offset += c->font->em_scale_x (v);
322 buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
325 else if (buffer->info[idx].mask & kern_mask)
327 o.y_advance += c->font->em_scale_y (v);
328 o.y_offset += c->font->em_scale_y (v);
336 hb_aat_apply_context_t *c;
337 const KerxSubTableFormat1 *table;
338 const UnsizedArrayOf<FWORD> &kernAction;
339 unsigned int stack[8];
344 bool apply (hb_aat_apply_context_t *c) const
348 if (!c->plan->requested_kerning &&
349 !(header.coverage & header.CrossStream))
352 driver_context_t dc (this, c);
354 StateTableDriver<Types, EntryData> driver (machine, c->buffer, c->font->face);
360 bool sanitize (hb_sanitize_context_t *c) const
362 TRACE_SANITIZE (this);
363 /* The rest of array sanitizations are done at run-time. */
364 return_trace (likely (c->check_struct (this) &&
365 machine.sanitize (c)));
369 KernSubTableHeader header;
370 StateTable<Types, EntryData> machine;
371 NNOffsetTo<UnsizedArrayOf<FWORD>, HBUINT> kernAction;
373 DEFINE_SIZE_STATIC (KernSubTableHeader::static_size + 5 * sizeof (HBUINT));
376 template <typename KernSubTableHeader>
377 struct KerxSubTableFormat2
379 typedef typename KernSubTableHeader::Types Types;
380 typedef typename Types::HBUINT HBUINT;
382 int get_kerning (hb_codepoint_t left, hb_codepoint_t right,
383 hb_aat_apply_context_t *c) const
385 unsigned int num_glyphs = c->sanitizer.get_num_glyphs ();
386 unsigned int l = (this+leftClassTable).get_class (left, num_glyphs, 0);
387 unsigned int r = (this+rightClassTable).get_class (right, num_glyphs, 0);
389 const UnsizedArrayOf<FWORD> &arrayZ = this+array;
390 unsigned int kern_idx = l + r;
391 kern_idx = Types::offsetToIndex (kern_idx, this, arrayZ.arrayZ);
392 const FWORD *v = &arrayZ[kern_idx];
393 if (unlikely (!v->sanitize (&c->sanitizer))) return 0;
395 return kerxTupleKern (*v, header.tuple_count (), this, c);
398 bool apply (hb_aat_apply_context_t *c) const
402 if (!c->plan->requested_kerning)
405 if (header.coverage & header.Backwards)
408 accelerator_t accel (*this, c);
409 hb_kern_machine_t<accelerator_t> machine (accel, header.coverage & header.CrossStream);
410 machine.kern (c->font, c->buffer, c->plan->kern_mask);
417 const KerxSubTableFormat2 &table;
418 hb_aat_apply_context_t *c;
420 accelerator_t (const KerxSubTableFormat2 &table_,
421 hb_aat_apply_context_t *c_) :
422 table (table_), c (c_) {}
424 int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const
425 { return table.get_kerning (left, right, c); }
428 bool sanitize (hb_sanitize_context_t *c) const
430 TRACE_SANITIZE (this);
431 return_trace (likely (c->check_struct (this) &&
432 leftClassTable.sanitize (c, this) &&
433 rightClassTable.sanitize (c, this) &&
434 c->check_range (this, array)));
438 KernSubTableHeader header;
439 HBUINT rowWidth; /* The width, in bytes, of a row in the table. */
440 NNOffsetTo<typename Types::ClassTypeWide, HBUINT>
441 leftClassTable; /* Offset from beginning of this subtable to
442 * left-hand class table. */
443 NNOffsetTo<typename Types::ClassTypeWide, HBUINT>
444 rightClassTable;/* Offset from beginning of this subtable to
445 * right-hand class table. */
446 NNOffsetTo<UnsizedArrayOf<FWORD>, HBUINT>
447 array; /* Offset from beginning of this subtable to
448 * the start of the kerning array. */
450 DEFINE_SIZE_STATIC (KernSubTableHeader::static_size + 4 * sizeof (HBUINT));
453 template <typename KernSubTableHeader>
454 struct KerxSubTableFormat4
456 typedef ExtendedTypes Types;
460 HBUINT16 ankrActionIndex;/* Either 0xFFFF (for no action) or the index of
461 * the action to perform. */
463 DEFINE_SIZE_STATIC (2);
466 struct driver_context_t
468 static constexpr bool in_place = true;
471 Mark = 0x8000, /* If set, remember this glyph as the marked glyph. */
472 DontAdvance = 0x4000, /* If set, don't advance to the next glyph before
473 * going to the new state. */
474 Reserved = 0x3FFF, /* Not used; set to 0. */
479 ActionType = 0xC0000000, /* A two-bit field containing the action type. */
480 Unused = 0x3F000000, /* Unused - must be zero. */
481 Offset = 0x00FFFFFF, /* Masks the offset in bytes from the beginning
482 * of the subtable to the beginning of the control
486 driver_context_t (const KerxSubTableFormat4 *table,
487 hb_aat_apply_context_t *c_) :
489 action_type ((table->flags & ActionType) >> 30),
490 ankrData ((HBUINT16 *) ((const char *) &table->machine + (table->flags & Offset))),
494 bool is_actionable (StateTableDriver<Types, EntryData> *driver HB_UNUSED,
495 const Entry<EntryData> &entry)
497 return entry.data.ankrActionIndex != 0xFFFF;
499 void transition (StateTableDriver<Types, EntryData> *driver,
500 const Entry<EntryData> &entry)
502 hb_buffer_t *buffer = driver->buffer;
504 if (mark_set && entry.data.ankrActionIndex != 0xFFFF && buffer->idx < buffer->len)
506 hb_glyph_position_t &o = buffer->cur_pos();
509 case 0: /* Control Point Actions.*/
511 /* indexed into glyph outline. */
512 const HBUINT16 *data = &ankrData[entry.data.ankrActionIndex];
513 if (!c->sanitizer.check_array (data, 2)) return;
514 HB_UNUSED unsigned int markControlPoint = *data++;
515 HB_UNUSED unsigned int currControlPoint = *data++;
516 hb_position_t markX = 0;
517 hb_position_t markY = 0;
518 hb_position_t currX = 0;
519 hb_position_t currY = 0;
520 if (!c->font->get_glyph_contour_point_for_origin (c->buffer->info[mark].codepoint,
522 HB_DIRECTION_LTR /*XXX*/,
524 !c->font->get_glyph_contour_point_for_origin (c->buffer->cur ().codepoint,
526 HB_DIRECTION_LTR /*XXX*/,
530 o.x_offset = markX - currX;
531 o.y_offset = markY - currY;
535 case 1: /* Anchor Point Actions. */
537 /* Indexed into 'ankr' table. */
538 const HBUINT16 *data = &ankrData[entry.data.ankrActionIndex];
539 if (!c->sanitizer.check_array (data, 2)) return;
540 unsigned int markAnchorPoint = *data++;
541 unsigned int currAnchorPoint = *data++;
542 const Anchor &markAnchor = c->ankr_table->get_anchor (c->buffer->info[mark].codepoint,
544 c->sanitizer.get_num_glyphs ());
545 const Anchor &currAnchor = c->ankr_table->get_anchor (c->buffer->cur ().codepoint,
547 c->sanitizer.get_num_glyphs ());
549 o.x_offset = c->font->em_scale_x (markAnchor.xCoordinate) - c->font->em_scale_x (currAnchor.xCoordinate);
550 o.y_offset = c->font->em_scale_y (markAnchor.yCoordinate) - c->font->em_scale_y (currAnchor.yCoordinate);
554 case 2: /* Control Point Coordinate Actions. */
556 const FWORD *data = (const FWORD *) &ankrData[entry.data.ankrActionIndex];
557 if (!c->sanitizer.check_array (data, 4)) return;
563 o.x_offset = c->font->em_scale_x (markX) - c->font->em_scale_x (currX);
564 o.y_offset = c->font->em_scale_y (markY) - c->font->em_scale_y (currY);
568 o.attach_type() = ATTACH_TYPE_MARK;
569 o.attach_chain() = (int) mark - (int) buffer->idx;
570 buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT;
573 if (entry.flags & Mark)
581 hb_aat_apply_context_t *c;
582 unsigned int action_type;
583 const HBUINT16 *ankrData;
588 bool apply (hb_aat_apply_context_t *c) const
592 driver_context_t dc (this, c);
594 StateTableDriver<Types, EntryData> driver (machine, c->buffer, c->font->face);
600 bool sanitize (hb_sanitize_context_t *c) const
602 TRACE_SANITIZE (this);
603 /* The rest of array sanitizations are done at run-time. */
604 return_trace (likely (c->check_struct (this) &&
605 machine.sanitize (c)));
609 KernSubTableHeader header;
610 StateTable<Types, EntryData> machine;
613 DEFINE_SIZE_STATIC (KernSubTableHeader::static_size + 20);
616 template <typename KernSubTableHeader>
617 struct KerxSubTableFormat6
621 ValuesAreLong = 0x00000001,
624 bool is_long () const { return flags & ValuesAreLong; }
626 int get_kerning (hb_codepoint_t left, hb_codepoint_t right,
627 hb_aat_apply_context_t *c) const
629 unsigned int num_glyphs = c->sanitizer.get_num_glyphs ();
632 const typename U::Long &t = u.l;
633 unsigned int l = (this+t.rowIndexTable).get_value_or_null (left, num_glyphs);
634 unsigned int r = (this+t.columnIndexTable).get_value_or_null (right, num_glyphs);
635 unsigned int offset = l + r;
636 if (unlikely (offset < l)) return 0; /* Addition overflow. */
637 if (unlikely (hb_unsigned_mul_overflows (offset, sizeof (FWORD32)))) return 0;
638 const FWORD32 *v = &StructAtOffset<FWORD32> (&(this+t.array), offset * sizeof (FWORD32));
639 if (unlikely (!v->sanitize (&c->sanitizer))) return 0;
640 return kerxTupleKern (*v, header.tuple_count (), &(this+vector), c);
644 const typename U::Short &t = u.s;
645 unsigned int l = (this+t.rowIndexTable).get_value_or_null (left, num_glyphs);
646 unsigned int r = (this+t.columnIndexTable).get_value_or_null (right, num_glyphs);
647 unsigned int offset = l + r;
648 const FWORD *v = &StructAtOffset<FWORD> (&(this+t.array), offset * sizeof (FWORD));
649 if (unlikely (!v->sanitize (&c->sanitizer))) return 0;
650 return kerxTupleKern (*v, header.tuple_count (), &(this+vector), c);
654 bool apply (hb_aat_apply_context_t *c) const
658 if (!c->plan->requested_kerning)
661 if (header.coverage & header.Backwards)
664 accelerator_t accel (*this, c);
665 hb_kern_machine_t<accelerator_t> machine (accel, header.coverage & header.CrossStream);
666 machine.kern (c->font, c->buffer, c->plan->kern_mask);
671 bool sanitize (hb_sanitize_context_t *c) const
673 TRACE_SANITIZE (this);
674 return_trace (likely (c->check_struct (this) &&
677 u.l.rowIndexTable.sanitize (c, this) &&
678 u.l.columnIndexTable.sanitize (c, this) &&
679 c->check_range (this, u.l.array)
681 u.s.rowIndexTable.sanitize (c, this) &&
682 u.s.columnIndexTable.sanitize (c, this) &&
683 c->check_range (this, u.s.array)
685 (header.tuple_count () == 0 ||
686 c->check_range (this, vector))));
691 const KerxSubTableFormat6 &table;
692 hb_aat_apply_context_t *c;
694 accelerator_t (const KerxSubTableFormat6 &table_,
695 hb_aat_apply_context_t *c_) :
696 table (table_), c (c_) {}
698 int get_kerning (hb_codepoint_t left, hb_codepoint_t right) const
699 { return table.get_kerning (left, right, c); }
703 KernSubTableHeader header;
706 HBUINT16 columnCount;
711 LNNOffsetTo<Lookup<HBUINT32>> rowIndexTable;
712 LNNOffsetTo<Lookup<HBUINT32>> columnIndexTable;
713 LNNOffsetTo<UnsizedArrayOf<FWORD32>> array;
717 LNNOffsetTo<Lookup<HBUINT16>> rowIndexTable;
718 LNNOffsetTo<Lookup<HBUINT16>> columnIndexTable;
719 LNNOffsetTo<UnsizedArrayOf<FWORD>> array;
722 LNNOffsetTo<UnsizedArrayOf<FWORD>> vector;
724 DEFINE_SIZE_STATIC (KernSubTableHeader::static_size + 24);
728 struct KerxSubTableHeader
730 typedef ExtendedTypes Types;
732 unsigned tuple_count () const { return tupleCount; }
733 bool is_horizontal () const { return !(coverage & Vertical); }
737 Vertical = 0x80000000u, /* Set if table has vertical kerning values. */
738 CrossStream = 0x40000000u, /* Set if table has cross-stream kerning values. */
739 Variation = 0x20000000u, /* Set if table has variation kerning values. */
740 Backwards = 0x10000000u, /* If clear, process the glyphs forwards, that
741 * is, from first to last in the glyph stream.
742 * If we, process them from last to first.
743 * This flag only applies to state-table based
744 * 'kerx' subtables (types 1 and 4). */
745 Reserved = 0x0FFFFF00u, /* Reserved, set to zero. */
746 SubtableType= 0x000000FFu, /* Subtable type. */
749 bool sanitize (hb_sanitize_context_t *c) const
751 TRACE_SANITIZE (this);
752 return_trace (likely (c->check_struct (this)));
760 DEFINE_SIZE_STATIC (12);
767 unsigned int get_size () const { return u.header.length; }
768 unsigned int get_type () const { return u.header.coverage & u.header.SubtableType; }
770 template <typename context_t, typename ...Ts>
771 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
773 unsigned int subtable_type = get_type ();
774 TRACE_DISPATCH (this, subtable_type);
775 switch (subtable_type) {
776 case 0: return_trace (c->dispatch (u.format0, hb_forward<Ts> (ds)...));
777 case 1: return_trace (c->dispatch (u.format1, hb_forward<Ts> (ds)...));
778 case 2: return_trace (c->dispatch (u.format2, hb_forward<Ts> (ds)...));
779 case 4: return_trace (c->dispatch (u.format4, hb_forward<Ts> (ds)...));
780 case 6: return_trace (c->dispatch (u.format6, hb_forward<Ts> (ds)...));
781 default: return_trace (c->default_return_value ());
785 bool sanitize (hb_sanitize_context_t *c) const
787 TRACE_SANITIZE (this);
788 if (!u.header.sanitize (c) ||
789 u.header.length <= u.header.static_size ||
790 !c->check_range (this, u.header.length))
791 return_trace (false);
793 return_trace (dispatch (c));
798 KerxSubTableHeader header;
799 KerxSubTableFormat0<KerxSubTableHeader> format0;
800 KerxSubTableFormat1<KerxSubTableHeader> format1;
801 KerxSubTableFormat2<KerxSubTableHeader> format2;
802 KerxSubTableFormat4<KerxSubTableHeader> format4;
803 KerxSubTableFormat6<KerxSubTableHeader> format6;
806 DEFINE_SIZE_MIN (12);
814 template <typename T>
817 /* https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern */
818 const T* thiz () const { return static_cast<const T *> (this); }
820 bool has_state_machine () const
822 typedef typename T::SubTable SubTable;
824 const SubTable *st = &thiz()->firstSubTable;
825 unsigned int count = thiz()->tableCount;
826 for (unsigned int i = 0; i < count; i++)
828 if (st->get_type () == 1)
830 st = &StructAfter<SubTable> (*st);
835 bool has_cross_stream () const
837 typedef typename T::SubTable SubTable;
839 const SubTable *st = &thiz()->firstSubTable;
840 unsigned int count = thiz()->tableCount;
841 for (unsigned int i = 0; i < count; i++)
843 if (st->u.header.coverage & st->u.header.CrossStream)
845 st = &StructAfter<SubTable> (*st);
850 int get_h_kerning (hb_codepoint_t left, hb_codepoint_t right) const
852 typedef typename T::SubTable SubTable;
855 const SubTable *st = &thiz()->firstSubTable;
856 unsigned int count = thiz()->tableCount;
857 for (unsigned int i = 0; i < count; i++)
859 if ((st->u.header.coverage & (st->u.header.Variation | st->u.header.CrossStream)) ||
860 !st->u.header.is_horizontal ())
862 v += st->get_kerning (left, right);
863 st = &StructAfter<SubTable> (*st);
868 bool apply (AAT::hb_aat_apply_context_t *c) const
870 typedef typename T::SubTable SubTable;
873 bool seenCrossStream = false;
874 c->set_lookup_index (0);
875 const SubTable *st = &thiz()->firstSubTable;
876 unsigned int count = thiz()->tableCount;
877 for (unsigned int i = 0; i < count; i++)
881 if (!T::Types::extended && (st->u.header.coverage & st->u.header.Variation))
884 if (HB_DIRECTION_IS_HORIZONTAL (c->buffer->props.direction) != st->u.header.is_horizontal ())
887 reverse = bool (st->u.header.coverage & st->u.header.Backwards) !=
888 HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction);
890 if (!c->buffer->message (c->font, "start %c%c%c%c subtable %d", HB_UNTAG (thiz()->tableTag), c->lookup_index))
893 if (!seenCrossStream &&
894 (st->u.header.coverage & st->u.header.CrossStream))
896 /* Attach all glyphs into a chain. */
897 seenCrossStream = true;
898 hb_glyph_position_t *pos = c->buffer->pos;
899 unsigned int count = c->buffer->len;
900 for (unsigned int i = 0; i < count; i++)
902 pos[i].attach_type() = ATTACH_TYPE_CURSIVE;
903 pos[i].attach_chain() = HB_DIRECTION_IS_FORWARD (c->buffer->props.direction) ? -1 : +1;
904 /* We intentionally don't set HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT,
905 * since there needs to be a non-zero attachment for post-positioning to
911 c->buffer->reverse ();
914 /* See comment in sanitize() for conditional here. */
915 hb_sanitize_with_object_t with (&c->sanitizer, i < count - 1 ? st : (const SubTable *) nullptr);
916 ret |= st->dispatch (c);
920 c->buffer->reverse ();
922 (void) c->buffer->message (c->font, "end %c%c%c%c subtable %d", HB_UNTAG (thiz()->tableTag), c->lookup_index);
925 st = &StructAfter<SubTable> (*st);
926 c->set_lookup_index (c->lookup_index + 1);
932 bool sanitize (hb_sanitize_context_t *c) const
934 TRACE_SANITIZE (this);
935 if (unlikely (!thiz()->version.sanitize (c) ||
936 (unsigned) thiz()->version < (unsigned) T::minVersion ||
937 !thiz()->tableCount.sanitize (c)))
938 return_trace (false);
940 typedef typename T::SubTable SubTable;
942 const SubTable *st = &thiz()->firstSubTable;
943 unsigned int count = thiz()->tableCount;
944 for (unsigned int i = 0; i < count; i++)
946 if (unlikely (!st->u.header.sanitize (c)))
947 return_trace (false);
948 /* OpenType kern table has 2-byte subtable lengths. That's limiting.
949 * MS implementation also only supports one subtable, of format 0,
950 * anyway. Certain versions of some fonts, like Calibry, contain
951 * kern subtable that exceeds 64kb. Looks like, the subtable length
952 * is simply ignored. Which makes sense. It's only needed if you
953 * have multiple subtables. To handle such fonts, we just ignore
954 * the length for the last subtable. */
955 hb_sanitize_with_object_t with (c, i < count - 1 ? st : (const SubTable *) nullptr);
957 if (unlikely (!st->sanitize (c)))
958 return_trace (false);
960 st = &StructAfter<SubTable> (*st);
967 struct kerx : KerxTable<kerx>
969 friend struct KerxTable<kerx>;
971 static constexpr hb_tag_t tableTag = HB_AAT_TAG_kerx;
972 static constexpr unsigned minVersion = 2u;
974 typedef KerxSubTableHeader SubTableHeader;
975 typedef SubTableHeader::Types Types;
976 typedef KerxSubTable SubTable;
978 bool has_data () const { return version; }
981 HBUINT16 version; /* The version number of the extended kerning table
982 * (currently 2, 3, or 4). */
983 HBUINT16 unused; /* Set to 0. */
984 HBUINT32 tableCount; /* The number of subtables included in the extended kerning
986 SubTable firstSubTable; /* Subtables. */
987 /*subtableGlyphCoverageArray*/ /* Only if version >= 3. We don't use. */
994 } /* namespace AAT */
997 #endif /* HB_AAT_LAYOUT_KERX_TABLE_HH */