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_AAT_LAYOUT_MORX_TABLE_HH
28 #define HB_AAT_LAYOUT_MORX_TABLE_HH
30 #include "hb-open-type.hh"
31 #include "hb-aat-layout-common.hh"
32 #include "hb-ot-layout-common.hh"
33 #include "hb-aat-map.hh"
36 * morx -- Extended Glyph Metamorphosis
37 * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6morx.html
38 * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6mort.html
40 #define HB_AAT_TAG_morx HB_TAG('m','o','r','x')
41 #define HB_AAT_TAG_mort HB_TAG('m','o','r','t')
48 template <typename Types>
49 struct RearrangementSubtable
51 typedef typename Types::HBUINT HBUINT;
53 typedef void EntryData;
55 struct driver_context_t
57 static constexpr bool in_place = true;
60 MarkFirst = 0x8000, /* If set, make the current glyph the first
61 * glyph to be rearranged. */
62 DontAdvance = 0x4000, /* If set, don't advance to the next glyph
63 * before going to the new state. This means
64 * that the glyph index doesn't change, even
65 * if the glyph at that index has changed. */
66 MarkLast = 0x2000, /* If set, make the current glyph the last
67 * glyph to be rearranged. */
68 Reserved = 0x1FF0, /* These bits are reserved and should be set to 0. */
69 Verb = 0x000F, /* The type of rearrangement specified. */
72 driver_context_t (const RearrangementSubtable *table HB_UNUSED) :
76 bool is_actionable (StateTableDriver<Types, EntryData> *driver HB_UNUSED,
77 const Entry<EntryData> &entry)
79 return (entry.flags & Verb) && start < end;
81 void transition (StateTableDriver<Types, EntryData> *driver,
82 const Entry<EntryData> &entry)
84 hb_buffer_t *buffer = driver->buffer;
85 unsigned int flags = entry.flags;
87 if (flags & MarkFirst)
91 end = hb_min (buffer->idx + 1, buffer->len);
93 if ((flags & Verb) && start < end)
95 /* The following map has two nibbles, for start-side
96 * and end-side. Values of 0,1,2 mean move that many
97 * to the other side. Value of 3 means move 2 and
99 const unsigned char map[16] =
101 0x00, /* 0 no change */
102 0x10, /* 1 Ax => xA */
103 0x01, /* 2 xD => Dx */
104 0x11, /* 3 AxD => DxA */
105 0x20, /* 4 ABx => xAB */
106 0x30, /* 5 ABx => xBA */
107 0x02, /* 6 xCD => CDx */
108 0x03, /* 7 xCD => DCx */
109 0x12, /* 8 AxCD => CDxA */
110 0x13, /* 9 AxCD => DCxA */
111 0x21, /* 10 ABxD => DxAB */
112 0x31, /* 11 ABxD => DxBA */
113 0x22, /* 12 ABxCD => CDxAB */
114 0x32, /* 13 ABxCD => CDxBA */
115 0x23, /* 14 ABxCD => DCxAB */
116 0x33, /* 15 ABxCD => DCxBA */
119 unsigned int m = map[flags & Verb];
120 unsigned int l = hb_min (2u, m >> 4);
121 unsigned int r = hb_min (2u, m & 0x0F);
122 bool reverse_l = 3 == (m >> 4);
123 bool reverse_r = 3 == (m & 0x0F);
125 if (end - start >= l + r)
127 buffer->merge_clusters (start, hb_min (buffer->idx + 1, buffer->len));
128 buffer->merge_clusters (start, end);
130 hb_glyph_info_t *info = buffer->info;
131 hb_glyph_info_t buf[4];
133 memcpy (buf, info + start, l * sizeof (buf[0]));
134 memcpy (buf + 2, info + end - r, r * sizeof (buf[0]));
137 memmove (info + start + r, info + start + l, (end - start - l - r) * sizeof (buf[0]));
139 memcpy (info + start, buf + 2, r * sizeof (buf[0]));
140 memcpy (info + end - l, buf, l * sizeof (buf[0]));
143 buf[0] = info[end - 1];
144 info[end - 1] = info[end - 2];
145 info[end - 2] = buf[0];
149 buf[0] = info[start];
150 info[start] = info[start + 1];
151 info[start + 1] = buf[0];
164 bool apply (hb_aat_apply_context_t *c) const
168 driver_context_t dc (this);
170 StateTableDriver<Types, EntryData> driver (machine, c->buffer, c->face);
173 return_trace (dc.ret);
176 bool sanitize (hb_sanitize_context_t *c) const
178 TRACE_SANITIZE (this);
179 return_trace (machine.sanitize (c));
183 StateTable<Types, EntryData> machine;
185 DEFINE_SIZE_STATIC (16);
188 template <typename Types>
189 struct ContextualSubtable
191 typedef typename Types::HBUINT HBUINT;
195 HBUINT16 markIndex; /* Index of the substitution table for the
196 * marked glyph (use 0xFFFF for none). */
197 HBUINT16 currentIndex; /* Index of the substitution table for the
198 * current glyph (use 0xFFFF for none). */
200 DEFINE_SIZE_STATIC (4);
203 struct driver_context_t
205 static constexpr bool in_place = true;
208 SetMark = 0x8000, /* If set, make the current glyph the marked glyph. */
209 DontAdvance = 0x4000, /* If set, don't advance to the next glyph before
210 * going to the new state. */
211 Reserved = 0x3FFF, /* These bits are reserved and should be set to 0. */
214 driver_context_t (const ContextualSubtable *table_,
215 hb_aat_apply_context_t *c_) :
221 subs (table+table->substitutionTables) {}
223 bool is_actionable (StateTableDriver<Types, EntryData> *driver,
224 const Entry<EntryData> &entry)
226 hb_buffer_t *buffer = driver->buffer;
228 if (buffer->idx == buffer->len && !mark_set)
231 return entry.data.markIndex != 0xFFFF || entry.data.currentIndex != 0xFFFF;
233 void transition (StateTableDriver<Types, EntryData> *driver,
234 const Entry<EntryData> &entry)
236 hb_buffer_t *buffer = driver->buffer;
238 /* Looks like CoreText applies neither mark nor current substitution for
239 * end-of-text if mark was not explicitly set. */
240 if (buffer->idx == buffer->len && !mark_set)
243 const HBGlyphID *replacement;
245 replacement = nullptr;
248 if (entry.data.markIndex != 0xFFFF)
250 const Lookup<HBGlyphID> &lookup = subs[entry.data.markIndex];
251 replacement = lookup.get_value (buffer->info[mark].codepoint, driver->num_glyphs);
256 unsigned int offset = entry.data.markIndex + buffer->info[mark].codepoint;
257 const UnsizedArrayOf<HBGlyphID> &subs_old = (const UnsizedArrayOf<HBGlyphID> &) subs;
258 replacement = &subs_old[Types::wordOffsetToIndex (offset, table, subs_old.arrayZ)];
259 if (!replacement->sanitize (&c->sanitizer) || !*replacement)
260 replacement = nullptr;
264 buffer->unsafe_to_break (mark, hb_min (buffer->idx + 1, buffer->len));
265 buffer->info[mark].codepoint = *replacement;
269 replacement = nullptr;
270 unsigned int idx = hb_min (buffer->idx, buffer->len - 1);
273 if (entry.data.currentIndex != 0xFFFF)
275 const Lookup<HBGlyphID> &lookup = subs[entry.data.currentIndex];
276 replacement = lookup.get_value (buffer->info[idx].codepoint, driver->num_glyphs);
281 unsigned int offset = entry.data.currentIndex + buffer->info[idx].codepoint;
282 const UnsizedArrayOf<HBGlyphID> &subs_old = (const UnsizedArrayOf<HBGlyphID> &) subs;
283 replacement = &subs_old[Types::wordOffsetToIndex (offset, table, subs_old.arrayZ)];
284 if (!replacement->sanitize (&c->sanitizer) || !*replacement)
285 replacement = nullptr;
289 buffer->info[idx].codepoint = *replacement;
293 if (entry.flags & SetMark)
303 hb_aat_apply_context_t *c;
306 const ContextualSubtable *table;
307 const UnsizedOffsetListOf<Lookup<HBGlyphID>, HBUINT, false> &subs;
310 bool apply (hb_aat_apply_context_t *c) const
314 driver_context_t dc (this, c);
316 StateTableDriver<Types, EntryData> driver (machine, c->buffer, c->face);
319 return_trace (dc.ret);
322 bool sanitize (hb_sanitize_context_t *c) const
324 TRACE_SANITIZE (this);
326 unsigned int num_entries = 0;
327 if (unlikely (!machine.sanitize (c, &num_entries))) return_trace (false);
329 if (!Types::extended)
330 return_trace (substitutionTables.sanitize (c, this, 0));
332 unsigned int num_lookups = 0;
334 const Entry<EntryData> *entries = machine.get_entries ();
335 for (unsigned int i = 0; i < num_entries; i++)
337 const EntryData &data = entries[i].data;
339 if (data.markIndex != 0xFFFF)
340 num_lookups = hb_max (num_lookups, 1 + data.markIndex);
341 if (data.currentIndex != 0xFFFF)
342 num_lookups = hb_max (num_lookups, 1 + data.currentIndex);
345 return_trace (substitutionTables.sanitize (c, this, num_lookups));
349 StateTable<Types, EntryData>
351 NNOffsetTo<UnsizedOffsetListOf<Lookup<HBGlyphID>, HBUINT, false>, HBUINT>
354 DEFINE_SIZE_STATIC (20);
358 template <bool extended>
359 struct LigatureEntry;
362 struct LigatureEntry<true>
366 SetComponent = 0x8000, /* Push this glyph onto the component stack for
367 * eventual processing. */
368 DontAdvance = 0x4000, /* Leave the glyph pointer at this glyph for the
370 PerformAction = 0x2000, /* Use the ligActionIndex to process a ligature
372 Reserved = 0x1FFF, /* These bits are reserved and should be set to 0. */
377 HBUINT16 ligActionIndex; /* Index to the first ligActionTable entry
378 * for processing this group, if indicated
381 DEFINE_SIZE_STATIC (2);
384 static bool performAction (const Entry<EntryData> &entry)
385 { return entry.flags & PerformAction; }
387 static unsigned int ligActionIndex (const Entry<EntryData> &entry)
388 { return entry.data.ligActionIndex; }
391 struct LigatureEntry<false>
395 SetComponent = 0x8000, /* Push this glyph onto the component stack for
396 * eventual processing. */
397 DontAdvance = 0x4000, /* Leave the glyph pointer at this glyph for the
399 Offset = 0x3FFF, /* Byte offset from beginning of subtable to the
400 * ligature action list. This value must be a
404 typedef void EntryData;
406 static bool performAction (const Entry<EntryData> &entry)
407 { return entry.flags & Offset; }
409 static unsigned int ligActionIndex (const Entry<EntryData> &entry)
410 { return entry.flags & Offset; }
414 template <typename Types>
415 struct LigatureSubtable
417 typedef typename Types::HBUINT HBUINT;
419 typedef LigatureEntry<Types::extended> LigatureEntryT;
420 typedef typename LigatureEntryT::EntryData EntryData;
422 struct driver_context_t
424 static constexpr bool in_place = false;
427 DontAdvance = LigatureEntryT::DontAdvance,
431 LigActionLast = 0x80000000, /* This is the last action in the list. This also
432 * implies storage. */
433 LigActionStore = 0x40000000, /* Store the ligature at the current cumulated index
434 * in the ligature table in place of the marked
435 * (i.e. currently-popped) glyph. */
436 LigActionOffset = 0x3FFFFFFF, /* A 30-bit value which is sign-extended to 32-bits
437 * and added to the glyph ID, resulting in an index
438 * into the component table. */
441 driver_context_t (const LigatureSubtable *table_,
442 hb_aat_apply_context_t *c_) :
446 ligAction (table+table->ligAction),
447 component (table+table->component),
448 ligature (table+table->ligature),
451 bool is_actionable (StateTableDriver<Types, EntryData> *driver HB_UNUSED,
452 const Entry<EntryData> &entry)
454 return LigatureEntryT::performAction (entry);
456 void transition (StateTableDriver<Types, EntryData> *driver,
457 const Entry<EntryData> &entry)
459 hb_buffer_t *buffer = driver->buffer;
461 DEBUG_MSG (APPLY, nullptr, "Ligature transition at %u", buffer->idx);
462 if (entry.flags & LigatureEntryT::SetComponent)
464 /* Never mark same index twice, in case DontAdvance was used... */
465 if (match_length && match_positions[(match_length - 1u) % ARRAY_LENGTH (match_positions)] == buffer->out_len)
468 match_positions[match_length++ % ARRAY_LENGTH (match_positions)] = buffer->out_len;
469 DEBUG_MSG (APPLY, nullptr, "Set component at %u", buffer->out_len);
472 if (LigatureEntryT::performAction (entry))
474 DEBUG_MSG (APPLY, nullptr, "Perform action with %u", match_length);
475 unsigned int end = buffer->out_len;
477 if (unlikely (!match_length))
480 if (buffer->idx >= buffer->len)
481 return; /* TODO Work on previous instead? */
483 unsigned int cursor = match_length;
485 unsigned int action_idx = LigatureEntryT::ligActionIndex (entry);
486 action_idx = Types::offsetToIndex (action_idx, table, ligAction.arrayZ);
487 const HBUINT32 *actionData = &ligAction[action_idx];
489 unsigned int ligature_idx = 0;
493 if (unlikely (!cursor))
495 /* Stack underflow. Clear the stack. */
496 DEBUG_MSG (APPLY, nullptr, "Stack underflow");
501 DEBUG_MSG (APPLY, nullptr, "Moving to stack position %u", cursor - 1);
502 buffer->move_to (match_positions[--cursor % ARRAY_LENGTH (match_positions)]);
504 if (unlikely (!actionData->sanitize (&c->sanitizer))) break;
505 action = *actionData;
507 uint32_t uoffset = action & LigActionOffset;
508 if (uoffset & 0x20000000)
509 uoffset |= 0xC0000000; /* Sign-extend. */
510 int32_t offset = (int32_t) uoffset;
511 unsigned int component_idx = buffer->cur().codepoint + offset;
512 component_idx = Types::wordOffsetToIndex (component_idx, table, component.arrayZ);
513 const HBUINT16 &componentData = component[component_idx];
514 if (unlikely (!componentData.sanitize (&c->sanitizer))) break;
515 ligature_idx += componentData;
517 DEBUG_MSG (APPLY, nullptr, "Action store %u last %u",
518 bool (action & LigActionStore),
519 bool (action & LigActionLast));
520 if (action & (LigActionStore | LigActionLast))
522 ligature_idx = Types::offsetToIndex (ligature_idx, table, ligature.arrayZ);
523 const HBGlyphID &ligatureData = ligature[ligature_idx];
524 if (unlikely (!ligatureData.sanitize (&c->sanitizer))) break;
525 hb_codepoint_t lig = ligatureData;
527 DEBUG_MSG (APPLY, nullptr, "Produced ligature %u", lig);
528 buffer->replace_glyph (lig);
530 unsigned int lig_end = match_positions[(match_length - 1u) % ARRAY_LENGTH (match_positions)] + 1u;
531 /* Now go and delete all subsequent components. */
532 while (match_length - 1u > cursor)
534 DEBUG_MSG (APPLY, nullptr, "Skipping ligature component");
535 buffer->move_to (match_positions[--match_length % ARRAY_LENGTH (match_positions)]);
536 buffer->replace_glyph (DELETED_GLYPH);
539 buffer->move_to (lig_end);
540 buffer->merge_out_clusters (match_positions[cursor % ARRAY_LENGTH (match_positions)], buffer->out_len);
545 while (!(action & LigActionLast));
546 buffer->move_to (end);
553 hb_aat_apply_context_t *c;
554 const LigatureSubtable *table;
555 const UnsizedArrayOf<HBUINT32> &ligAction;
556 const UnsizedArrayOf<HBUINT16> &component;
557 const UnsizedArrayOf<HBGlyphID> &ligature;
558 unsigned int match_length;
559 unsigned int match_positions[HB_MAX_CONTEXT_LENGTH];
562 bool apply (hb_aat_apply_context_t *c) const
566 driver_context_t dc (this, c);
568 StateTableDriver<Types, EntryData> driver (machine, c->buffer, c->face);
571 return_trace (dc.ret);
574 bool sanitize (hb_sanitize_context_t *c) const
576 TRACE_SANITIZE (this);
577 /* The rest of array sanitizations are done at run-time. */
578 return_trace (c->check_struct (this) && machine.sanitize (c) &&
579 ligAction && component && ligature);
583 StateTable<Types, EntryData>
585 NNOffsetTo<UnsizedArrayOf<HBUINT32>, HBUINT>
586 ligAction; /* Offset to the ligature action table. */
587 NNOffsetTo<UnsizedArrayOf<HBUINT16>, HBUINT>
588 component; /* Offset to the component table. */
589 NNOffsetTo<UnsizedArrayOf<HBGlyphID>, HBUINT>
590 ligature; /* Offset to the actual ligature lists. */
592 DEFINE_SIZE_STATIC (28);
595 template <typename Types>
596 struct NoncontextualSubtable
598 bool apply (hb_aat_apply_context_t *c) const
603 unsigned int num_glyphs = c->face->get_num_glyphs ();
605 hb_glyph_info_t *info = c->buffer->info;
606 unsigned int count = c->buffer->len;
607 for (unsigned int i = 0; i < count; i++)
609 const HBGlyphID *replacement = substitute.get_value (info[i].codepoint, num_glyphs);
612 info[i].codepoint = *replacement;
620 bool sanitize (hb_sanitize_context_t *c) const
622 TRACE_SANITIZE (this);
623 return_trace (substitute.sanitize (c));
627 Lookup<HBGlyphID> substitute;
632 template <typename Types>
633 struct InsertionSubtable
635 typedef typename Types::HBUINT HBUINT;
639 HBUINT16 currentInsertIndex; /* Zero-based index into the insertion glyph table.
640 * The number of glyphs to be inserted is contained
641 * in the currentInsertCount field in the flags.
642 * A value of 0xFFFF indicates no insertion is to
644 HBUINT16 markedInsertIndex; /* Zero-based index into the insertion glyph table.
645 * The number of glyphs to be inserted is contained
646 * in the markedInsertCount field in the flags.
647 * A value of 0xFFFF indicates no insertion is to
650 DEFINE_SIZE_STATIC (4);
653 struct driver_context_t
655 static constexpr bool in_place = false;
658 SetMark = 0x8000, /* If set, mark the current glyph. */
659 DontAdvance = 0x4000, /* If set, don't advance to the next glyph before
660 * going to the new state. This does not mean
661 * that the glyph pointed to is the same one as
662 * before. If you've made insertions immediately
663 * downstream of the current glyph, the next glyph
664 * processed would in fact be the first one
666 CurrentIsKashidaLike= 0x2000, /* If set, and the currentInsertList is nonzero,
667 * then the specified glyph list will be inserted
668 * as a kashida-like insertion, either before or
669 * after the current glyph (depending on the state
670 * of the currentInsertBefore flag). If clear, and
671 * the currentInsertList is nonzero, then the
672 * specified glyph list will be inserted as a
673 * split-vowel-like insertion, either before or
674 * after the current glyph (depending on the state
675 * of the currentInsertBefore flag). */
676 MarkedIsKashidaLike= 0x1000, /* If set, and the markedInsertList is nonzero,
677 * then the specified glyph list will be inserted
678 * as a kashida-like insertion, either before or
679 * after the marked glyph (depending on the state
680 * of the markedInsertBefore flag). If clear, and
681 * the markedInsertList is nonzero, then the
682 * specified glyph list will be inserted as a
683 * split-vowel-like insertion, either before or
684 * after the marked glyph (depending on the state
685 * of the markedInsertBefore flag). */
686 CurrentInsertBefore= 0x0800, /* If set, specifies that insertions are to be made
687 * to the left of the current glyph. If clear,
688 * they're made to the right of the current glyph. */
689 MarkedInsertBefore= 0x0400, /* If set, specifies that insertions are to be
690 * made to the left of the marked glyph. If clear,
691 * they're made to the right of the marked glyph. */
692 CurrentInsertCount= 0x3E0, /* This 5-bit field is treated as a count of the
693 * number of glyphs to insert at the current
694 * position. Since zero means no insertions, the
695 * largest number of insertions at any given
696 * current location is 31 glyphs. */
697 MarkedInsertCount= 0x001F, /* This 5-bit field is treated as a count of the
698 * number of glyphs to insert at the marked
699 * position. Since zero means no insertions, the
700 * largest number of insertions at any given
701 * marked location is 31 glyphs. */
704 driver_context_t (const InsertionSubtable *table,
705 hb_aat_apply_context_t *c_) :
709 insertionAction (table+table->insertionAction) {}
711 bool is_actionable (StateTableDriver<Types, EntryData> *driver HB_UNUSED,
712 const Entry<EntryData> &entry)
714 return (entry.flags & (CurrentInsertCount | MarkedInsertCount)) &&
715 (entry.data.currentInsertIndex != 0xFFFF ||entry.data.markedInsertIndex != 0xFFFF);
717 void transition (StateTableDriver<Types, EntryData> *driver,
718 const Entry<EntryData> &entry)
720 hb_buffer_t *buffer = driver->buffer;
721 unsigned int flags = entry.flags;
723 unsigned mark_loc = buffer->out_len;
725 if (entry.data.markedInsertIndex != 0xFFFF)
727 unsigned int count = (flags & MarkedInsertCount);
728 unsigned int start = entry.data.markedInsertIndex;
729 const HBGlyphID *glyphs = &insertionAction[start];
730 if (unlikely (!c->sanitizer.check_array (glyphs, count))) count = 0;
732 bool before = flags & MarkedInsertBefore;
734 unsigned int end = buffer->out_len;
735 buffer->move_to (mark);
737 if (buffer->idx < buffer->len && !before)
738 buffer->copy_glyph ();
739 /* TODO We ignore KashidaLike setting. */
740 for (unsigned int i = 0; i < count; i++)
741 buffer->output_glyph (glyphs[i]);
742 if (buffer->idx < buffer->len && !before)
743 buffer->skip_glyph ();
745 buffer->move_to (end + count);
747 buffer->unsafe_to_break_from_outbuffer (mark, hb_min (buffer->idx + 1, buffer->len));
753 if (entry.data.currentInsertIndex != 0xFFFF)
755 unsigned int count = (flags & CurrentInsertCount) >> 5;
756 unsigned int start = entry.data.currentInsertIndex;
757 const HBGlyphID *glyphs = &insertionAction[start];
758 if (unlikely (!c->sanitizer.check_array (glyphs, count))) count = 0;
760 bool before = flags & CurrentInsertBefore;
762 unsigned int end = buffer->out_len;
764 if (buffer->idx < buffer->len && !before)
765 buffer->copy_glyph ();
766 /* TODO We ignore KashidaLike setting. */
767 for (unsigned int i = 0; i < count; i++)
768 buffer->output_glyph (glyphs[i]);
769 if (buffer->idx < buffer->len && !before)
770 buffer->skip_glyph ();
772 /* Humm. Not sure where to move to. There's this wording under
775 * "If set, don't update the glyph index before going to the new state.
776 * This does not mean that the glyph pointed to is the same one as
777 * before. If you've made insertions immediately downstream of the
778 * current glyph, the next glyph processed would in fact be the first
781 * This suggests that if DontAdvance is NOT set, we should move to
782 * end+count. If it *was*, then move to end, such that newly inserted
783 * glyphs are now visible.
785 * https://github.com/harfbuzz/harfbuzz/issues/1224#issuecomment-427691417
787 buffer->move_to ((flags & DontAdvance) ? end : end + count);
794 hb_aat_apply_context_t *c;
796 const UnsizedArrayOf<HBGlyphID> &insertionAction;
799 bool apply (hb_aat_apply_context_t *c) const
803 driver_context_t dc (this, c);
805 StateTableDriver<Types, EntryData> driver (machine, c->buffer, c->face);
808 return_trace (dc.ret);
811 bool sanitize (hb_sanitize_context_t *c) const
813 TRACE_SANITIZE (this);
814 /* The rest of array sanitizations are done at run-time. */
815 return_trace (c->check_struct (this) && machine.sanitize (c) &&
820 StateTable<Types, EntryData>
822 NNOffsetTo<UnsizedArrayOf<HBGlyphID>, HBUINT>
823 insertionAction; /* Byte offset from stateHeader to the start of
824 * the insertion glyph table. */
826 DEFINE_SIZE_STATIC (20);
832 bool sanitize (hb_sanitize_context_t *c) const
834 TRACE_SANITIZE (this);
835 return_trace (c->check_struct (this));
839 HBUINT16 featureType; /* The type of feature. */
840 HBUINT16 featureSetting; /* The feature's setting (aka selector). */
841 HBUINT32 enableFlags; /* Flags for the settings that this feature
842 * and setting enables. */
843 HBUINT32 disableFlags; /* Complement of flags for the settings that this
844 * feature and setting disable. */
847 DEFINE_SIZE_STATIC (12);
850 template <typename Types>
853 typedef typename Types::HBUINT HBUINT;
855 template <typename T>
858 unsigned int get_size () const { return length; }
859 unsigned int get_type () const { return coverage & 0xFF; }
860 unsigned int get_coverage () const { return coverage >> (sizeof (HBUINT) * 8 - 8); }
864 Vertical = 0x80, /* If set, this subtable will only be applied
865 * to vertical text. If clear, this subtable
866 * will only be applied to horizontal text. */
867 Backwards = 0x40, /* If set, this subtable will process glyphs
868 * in descending order. If clear, it will
869 * process the glyphs in ascending order. */
870 AllDirections = 0x20, /* If set, this subtable will be applied to
871 * both horizontal and vertical text (i.e.
872 * the state of bit 0x80000000 is ignored). */
873 Logical = 0x10, /* If set, this subtable will process glyphs
874 * in logical order (or reverse logical order,
875 * depending on the value of bit 0x80000000). */
886 template <typename context_t, typename ...Ts>
887 typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const
889 unsigned int subtable_type = get_type ();
890 TRACE_DISPATCH (this, subtable_type);
891 switch (subtable_type) {
892 case Rearrangement: return_trace (c->dispatch (u.rearrangement, hb_forward<Ts> (ds)...));
893 case Contextual: return_trace (c->dispatch (u.contextual, hb_forward<Ts> (ds)...));
894 case Ligature: return_trace (c->dispatch (u.ligature, hb_forward<Ts> (ds)...));
895 case Noncontextual: return_trace (c->dispatch (u.noncontextual, hb_forward<Ts> (ds)...));
896 case Insertion: return_trace (c->dispatch (u.insertion, hb_forward<Ts> (ds)...));
897 default: return_trace (c->default_return_value ());
901 bool apply (hb_aat_apply_context_t *c) const
904 hb_sanitize_with_object_t with (&c->sanitizer, this);
905 return_trace (dispatch (c));
908 bool sanitize (hb_sanitize_context_t *c) const
910 TRACE_SANITIZE (this);
911 if (!length.sanitize (c) ||
912 length <= min_size ||
913 !c->check_range (this, length))
914 return_trace (false);
916 hb_sanitize_with_object_t with (c, this);
917 return_trace (dispatch (c));
921 HBUINT length; /* Total subtable length, including this header. */
922 HBUINT coverage; /* Coverage flags and subtable type. */
923 HBUINT32 subFeatureFlags;/* The 32-bit mask identifying which subtable this is. */
925 RearrangementSubtable<Types> rearrangement;
926 ContextualSubtable<Types> contextual;
927 LigatureSubtable<Types> ligature;
928 NoncontextualSubtable<Types> noncontextual;
929 InsertionSubtable<Types> insertion;
932 DEFINE_SIZE_MIN (2 * sizeof (HBUINT) + 4);
935 template <typename Types>
938 typedef typename Types::HBUINT HBUINT;
940 hb_mask_t compile_flags (const hb_aat_map_builder_t *map) const
942 hb_mask_t flags = defaultFlags;
944 unsigned int count = featureCount;
945 for (unsigned i = 0; i < count; i++)
947 const Feature &feature = featureZ[i];
948 hb_aat_layout_feature_type_t type = (hb_aat_layout_feature_type_t) (unsigned int) feature.featureType;
949 hb_aat_layout_feature_selector_t setting = (hb_aat_layout_feature_selector_t) (unsigned int) feature.featureSetting;
951 // Check whether this type/setting pair was requested in the map, and if so, apply its flags.
952 // (The search here only looks at the type and setting fields of feature_info_t.)
953 hb_aat_map_builder_t::feature_info_t info = { type, setting, false, 0 };
954 if (map->features.bsearch (info))
956 flags &= feature.disableFlags;
957 flags |= feature.enableFlags;
959 else if (type == HB_AAT_LAYOUT_FEATURE_TYPE_LETTER_CASE && setting == HB_AAT_LAYOUT_FEATURE_SELECTOR_SMALL_CAPS)
961 /* Deprecated. https://github.com/harfbuzz/harfbuzz/issues/1342 */
962 type = HB_AAT_LAYOUT_FEATURE_TYPE_LOWER_CASE;
963 setting = HB_AAT_LAYOUT_FEATURE_SELECTOR_LOWER_CASE_SMALL_CAPS;
971 void apply (hb_aat_apply_context_t *c,
972 hb_mask_t flags) const
974 const ChainSubtable<Types> *subtable = &StructAfter<ChainSubtable<Types>> (featureZ.as_array (featureCount));
975 unsigned int count = subtableCount;
976 for (unsigned int i = 0; i < count; i++)
980 if (!(subtable->subFeatureFlags & flags))
983 if (!(subtable->get_coverage() & ChainSubtable<Types>::AllDirections) &&
984 HB_DIRECTION_IS_VERTICAL (c->buffer->props.direction) !=
985 bool (subtable->get_coverage() & ChainSubtable<Types>::Vertical))
988 /* Buffer contents is always in logical direction. Determine if
989 * we need to reverse before applying this subtable. We reverse
990 * back after if we did reverse indeed.
994 * Bits 28 and 30 of the coverage field control the order in which
995 * glyphs are processed when the subtable is run by the layout engine.
996 * Bit 28 is used to indicate if the glyph processing direction is
997 * the same as logical order or layout order. Bit 30 is used to
998 * indicate whether glyphs are processed forwards or backwards within
1001 Bit 30 Bit 28 Interpretation for Horizontal Text
1002 0 0 The subtable is processed in layout order
1003 (the same order as the glyphs, which is
1004 always left-to-right).
1005 1 0 The subtable is processed in reverse layout order
1006 (the order opposite that of the glyphs, which is
1007 always right-to-left).
1008 0 1 The subtable is processed in logical order
1009 (the same order as the characters, which may be
1010 left-to-right or right-to-left).
1011 1 1 The subtable is processed in reverse logical order
1012 (the order opposite that of the characters, which
1013 may be right-to-left or left-to-right).
1015 reverse = subtable->get_coverage () & ChainSubtable<Types>::Logical ?
1016 bool (subtable->get_coverage () & ChainSubtable<Types>::Backwards) :
1017 bool (subtable->get_coverage () & ChainSubtable<Types>::Backwards) !=
1018 HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction);
1020 if (!c->buffer->message (c->font, "start chain subtable %d", c->lookup_index))
1024 c->buffer->reverse ();
1026 subtable->apply (c);
1029 c->buffer->reverse ();
1031 (void) c->buffer->message (c->font, "end chain subtable %d", c->lookup_index);
1033 if (unlikely (!c->buffer->successful)) return;
1036 subtable = &StructAfter<ChainSubtable<Types>> (*subtable);
1037 c->set_lookup_index (c->lookup_index + 1);
1041 unsigned int get_size () const { return length; }
1043 bool sanitize (hb_sanitize_context_t *c, unsigned int version HB_UNUSED) const
1045 TRACE_SANITIZE (this);
1046 if (!length.sanitize (c) ||
1047 length < min_size ||
1048 !c->check_range (this, length))
1049 return_trace (false);
1051 if (!c->check_array (featureZ.arrayZ, featureCount))
1052 return_trace (false);
1054 const ChainSubtable<Types> *subtable = &StructAfter<ChainSubtable<Types>> (featureZ.as_array (featureCount));
1055 unsigned int count = subtableCount;
1056 for (unsigned int i = 0; i < count; i++)
1058 if (!subtable->sanitize (c))
1059 return_trace (false);
1060 subtable = &StructAfter<ChainSubtable<Types>> (*subtable);
1063 return_trace (true);
1067 HBUINT32 defaultFlags; /* The default specification for subtables. */
1068 HBUINT32 length; /* Total byte count, including this header. */
1069 HBUINT featureCount; /* Number of feature subtable entries. */
1070 HBUINT subtableCount; /* The number of subtables in the chain. */
1072 UnsizedArrayOf<Feature> featureZ; /* Features. */
1073 /*ChainSubtable firstSubtable;*//* Subtables. */
1074 /*subtableGlyphCoverageArray*/ /* Only if version >= 3. We don't use. */
1077 DEFINE_SIZE_MIN (8 + 2 * sizeof (HBUINT));
1082 * The 'mort'/'morx' Table
1085 template <typename Types, hb_tag_t TAG>
1088 static constexpr hb_tag_t tableTag = TAG;
1090 bool has_data () const { return version != 0; }
1092 void compile_flags (const hb_aat_map_builder_t *mapper,
1093 hb_aat_map_t *map) const
1095 const Chain<Types> *chain = &firstChain;
1096 unsigned int count = chainCount;
1097 for (unsigned int i = 0; i < count; i++)
1099 map->chain_flags.push (chain->compile_flags (mapper));
1100 chain = &StructAfter<Chain<Types>> (*chain);
1104 void apply (hb_aat_apply_context_t *c) const
1106 if (unlikely (!c->buffer->successful)) return;
1107 c->set_lookup_index (0);
1108 const Chain<Types> *chain = &firstChain;
1109 unsigned int count = chainCount;
1110 for (unsigned int i = 0; i < count; i++)
1112 chain->apply (c, c->plan->aat_map.chain_flags[i]);
1113 if (unlikely (!c->buffer->successful)) return;
1114 chain = &StructAfter<Chain<Types>> (*chain);
1118 bool sanitize (hb_sanitize_context_t *c) const
1120 TRACE_SANITIZE (this);
1121 if (!version.sanitize (c) || !version || !chainCount.sanitize (c))
1122 return_trace (false);
1124 const Chain<Types> *chain = &firstChain;
1125 unsigned int count = chainCount;
1126 for (unsigned int i = 0; i < count; i++)
1128 if (!chain->sanitize (c, version))
1129 return_trace (false);
1130 chain = &StructAfter<Chain<Types>> (*chain);
1133 return_trace (true);
1137 HBUINT16 version; /* Version number of the glyph metamorphosis table.
1139 HBUINT16 unused; /* Set to 0. */
1140 HBUINT32 chainCount; /* Number of metamorphosis chains contained in this
1142 Chain<Types> firstChain; /* Chains. */
1145 DEFINE_SIZE_MIN (8);
1148 struct morx : mortmorx<ExtendedTypes, HB_AAT_TAG_morx> {};
1149 struct mort : mortmorx<ObsoleteTypes, HB_AAT_TAG_mort> {};
1152 } /* namespace AAT */
1155 #endif /* HB_AAT_LAYOUT_MORX_TABLE_HH */