From b70acb123f185296b71d043b46e2b28f4bef495b Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sun, 8 Nov 2026 14:06:34 -0600 Subject: [PATCH] [aat] Outline state-machine action handlers (#6598) Keep per-transition bookkeeping in small always-inlined wrappers and move the larger action bodies into never-inlined perform_action helpers for all six morx/kerx state-machine contexts. Preserve mark-update order and early exits when an action fails. Adding a glyph-set update to ligature deletion enlarged the inlined driver substantially. Outlining actions avoids duplicating those paths inside the driver while keeping ordinary transitions cheap. Use the standard inline attributes directly, including size-optimized builds. Clang 21 Linux/x86-64 measurements improve the sampled Helvetica Neue, Menlo, Lucida Grande, and Devanagari Sangam cases by about 1-3% at -O3 and 3-8% at -Oz. The action-heavy Geeza word-list case is a small tradeoff at -O2. The full default Meson suite passes: 298 tests passed and one skipped. Separate -Oz shaping tests and GCC reduced-feature syntax checks pass. Assisted-by: OpenAI Codex --- src/hb-aat-layout-common.hh | 11 - src/hb-aat-layout-kerx-table.hh | 391 ++++++++++++++++---------------- src/hb-aat-layout-morx-table.hh | 309 +++++++++++++------------ 3 files changed, 370 insertions(+), 341 deletions(-) diff --git a/src/hb-aat-layout-common.hh b/src/hb-aat-layout-common.hh index 42a5a76fe..85f1f8293 100644 --- a/src/hb-aat-layout-common.hh +++ b/src/hb-aat-layout-common.hh @@ -49,17 +49,6 @@ struct ankr; using hb_aat_class_cache_t = hb_ot_layout_mapping_cache_t; -/* The state-machine subtables' transition functions are called once per - * transition from the drive loop, but are big enough that the compiler - * declines to inline them, leaving a call in the hottest loop of AAT - * shaping. Forcing them inline is worth 3-10% instructions on - * morx/kerx-heavy fonts, for about 16KB of code. */ -#ifndef HB_OPTIMIZE_SIZE -#define HB_AAT_TRANSITION_INLINE HB_ALWAYS_INLINE -#else -#define HB_AAT_TRANSITION_INLINE -#endif - struct hb_aat_scratch_t { hb_aat_scratch_t () = default; diff --git a/src/hb-aat-layout-kerx-table.hh b/src/hb-aat-layout-kerx-table.hh index 7b22098f2..218771632 100644 --- a/src/hb-aat-layout-kerx-table.hh +++ b/src/hb-aat-layout-kerx-table.hh @@ -259,7 +259,7 @@ struct KerxSubTableFormat1 stack_pos (0), crossStream (table->header.coverage & table->header.CrossStream) {} - HB_AAT_TRANSITION_INLINE + HB_ALWAYS_INLINE void transition (hb_buffer_t *buffer, StateTableDriver *driver, const Entry &entry) @@ -282,124 +282,129 @@ struct KerxSubTableFormat1 } if (Format1EntryT::performAction (entry) && depth) + perform_action (buffer, entry); + } + + HB_NEVER_INLINE + void perform_action (hb_buffer_t *buffer, + const Entry &entry) + { + unsigned int tuple_count = hb_max (1u, table->header.tuple_count ()); + + unsigned int kern_idx = Format1EntryT::kernActionIndex (entry); + kern_idx = Types::offsetToIndex (kern_idx, &table->machine, kernAction.arrayZ); + const FWORD *actions = &kernAction[kern_idx]; + if (!Types::extended && !c->sanitizer.check_array (actions, depth, tuple_count)) { - unsigned int tuple_count = hb_max (1u, table->header.tuple_count ()); + depth = 0; + return; + } + hb_barrier (); - unsigned int kern_idx = Format1EntryT::kernActionIndex (entry); - kern_idx = Types::offsetToIndex (kern_idx, &table->machine, kernAction.arrayZ); - const FWORD *actions = &kernAction[kern_idx]; - if (!Types::extended && !c->sanitizer.check_array (actions, depth, tuple_count)) + hb_mask_t kern_mask = c->plan->kern_mask; + + /* Contrary to the kerx spec's destructive pops, CoreText retains an + * eight-slot circular history and walks it anew for each action list. + * See test/shape/data/in-house/tests/aat-kerx.tests for native cases. */ + unsigned int cursor = stack_pos; + bool last = false; + while (!last && depth) + { + if (Types::extended) { - depth = 0; - return; + if (unlikely (buffer->max_ops-- <= 0)) + { + buffer->successful = false; + return; + } + if (!actions->sanitize (&c->sanitizer)) + { + depth = 0; + return; + } + hb_barrier (); + /* kerx ends a list with a single 0xFFFF, without popping a glyph. */ + if (*actions == -1) + break; + if (tuple_count > 1 && !c->sanitizer.check_array (actions, 1, tuple_count)) + { + depth = 0; + return; + } + hb_barrier (); } - hb_barrier (); - hb_mask_t kern_mask = c->plan->kern_mask; - - /* Contrary to the kerx spec's destructive pops, CoreText retains an - * eight-slot circular history and walks it anew for each action list. - * See test/shape/data/in-house/tests/aat-kerx.tests for native cases. */ - unsigned int cursor = stack_pos; - bool last = false; - while (!last && depth) + unsigned int idx; + if (Types::extended) { - if (Types::extended) - { - if (unlikely (buffer->max_ops-- <= 0)) - { - buffer->successful = false; - return; - } - if (!actions->sanitize (&c->sanitizer)) - { - depth = 0; - return; - } - hb_barrier (); - /* kerx ends a list with a single 0xFFFF, without popping a glyph. */ - if (*actions == -1) - break; - if (tuple_count > 1 && !c->sanitizer.check_array (actions, 1, tuple_count)) - { - depth = 0; - return; - } - hb_barrier (); - } + cursor = (cursor + ARRAY_LENGTH (stack) - 1) % ARRAY_LENGTH (stack); + idx = stack[cursor]; + } + else + idx = stack[--depth]; + int v = *actions; + actions += tuple_count; + if (idx >= buffer->len) continue; + if (Types::extended) + buffer->unsafe_to_break (idx, buffer->idx + 1); - unsigned int idx; - if (Types::extended) - { - cursor = (cursor + ARRAY_LENGTH (stack) - 1) % ARRAY_LENGTH (stack); - idx = stack[cursor]; - } - else - idx = stack[--depth]; - int v = *actions; - actions += tuple_count; - if (idx >= buffer->len) continue; - if (Types::extended) - buffer->unsafe_to_break (idx, buffer->idx + 1); + /* kern uses the low bit of the final value as the list terminator. */ + if (!Types::extended) + { + last = v & 1; + v &= ~1; + } - /* kern uses the low bit of the final value as the list terminator. */ - if (!Types::extended) - { - last = v & 1; - v &= ~1; - } + hb_glyph_position_t &o = buffer->pos[idx]; - hb_glyph_position_t &o = buffer->pos[idx]; - - if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction)) + if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction)) + { + if (crossStream) { - if (crossStream) + /* The following flag is undocumented in the spec, but described + * in the 'kern' table example. */ + if (v == -0x8000) { - /* The following flag is undocumented in the spec, but described - * in the 'kern' table example. */ - if (v == -0x8000) - { - o.attach_type() = OT::Layout::GPOS_impl::ATTACH_TYPE_NONE; - o.attach_chain() = 0; - o.y_offset = 0; - } - else if (o.attach_type()) - { - o.y_offset = hb_saturate_add (o.y_offset, c->font->em_scale_y (v)); - buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT; - } + o.attach_type() = OT::Layout::GPOS_impl::ATTACH_TYPE_NONE; + o.attach_chain() = 0; + o.y_offset = 0; } - else if (buffer->info[idx].mask & kern_mask) + else if (o.attach_type()) { - auto scaled = c->font->em_scale_x (v); - o.x_advance = hb_saturate_add (o.x_advance, scaled); - o.x_offset = hb_saturate_add (o.x_offset, scaled); + o.y_offset = hb_saturate_add (o.y_offset, c->font->em_scale_y (v)); + buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT; } } - else + else if (buffer->info[idx].mask & kern_mask) { - if (crossStream) + auto scaled = c->font->em_scale_x (v); + o.x_advance = hb_saturate_add (o.x_advance, scaled); + o.x_offset = hb_saturate_add (o.x_offset, scaled); + } + } + else + { + if (crossStream) + { + /* CoreText doesn't do crossStream kerning in vertical. We do. */ + if (v == -0x8000) { - /* CoreText doesn't do crossStream kerning in vertical. We do. */ - if (v == -0x8000) - { - o.attach_type() = OT::Layout::GPOS_impl::ATTACH_TYPE_NONE; - o.attach_chain() = 0; - o.x_offset = 0; - } - else if (o.attach_type()) - { - o.x_offset = hb_saturate_add (o.x_offset, c->font->em_scale_x (v)); - buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT; - } + o.attach_type() = OT::Layout::GPOS_impl::ATTACH_TYPE_NONE; + o.attach_chain() = 0; + o.x_offset = 0; } - else if (buffer->info[idx].mask & kern_mask) + else if (o.attach_type()) { - auto scaled = c->font->em_scale_y (v); - o.y_advance = hb_saturate_add (o.y_advance, scaled); - o.y_offset = hb_saturate_add (o.y_offset, scaled); + o.x_offset = hb_saturate_add (o.x_offset, c->font->em_scale_x (v)); + buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT; } } + else if (buffer->info[idx].mask & kern_mask) + { + auto scaled = c->font->em_scale_y (v); + o.y_advance = hb_saturate_add (o.y_advance, scaled); + o.y_offset = hb_saturate_add (o.y_offset, scaled); + } } } } @@ -595,103 +600,13 @@ struct KerxSubTableFormat4 mark_set (false), mark (0) {} - HB_AAT_TRANSITION_INLINE + HB_ALWAYS_INLINE void transition (hb_buffer_t *buffer, StateTableDriver *driver, const Entry &entry) { if (mark_set && entry.data.ankrActionIndex != 0xFFFF && buffer->idx < buffer->len) - { - hb_glyph_position_t &o = buffer->cur_pos(); - switch (action_type) - { - case 0: /* Control Point Actions.*/ - { - /* Indexed into glyph outline. */ - /* Each action (record in ankrData) contains two 16-bit fields, so we must - double the ankrActionIndex to get the correct offset here. */ - const HBUINT16 *data = &ankrData[entry.data.ankrActionIndex * 2]; - if (!c->sanitizer.check_array (data, 2)) return; - hb_barrier (); - /* Font functions may load the whole glyph outline to find a contour - * point, and a non-advancing transition can repeat that at will. - * Charge the two lookups to the buffer's work budget. */ - if (unlikely ((buffer->max_ops -= 2 * HB_BUDGET_256) <= 0)) return; - unsigned int markControlPoint = *data++; - unsigned int currControlPoint = *data++; - hb_position_t markX = 0; - hb_position_t markY = 0; - hb_position_t currX = 0; - hb_position_t currY = 0; - if (!c->font->get_glyph_contour_point_for_origin (c->buffer->info[mark].codepoint, - markControlPoint, - HB_DIRECTION_LTR /*XXX*/, - &markX, &markY) || - !c->font->get_glyph_contour_point_for_origin (c->buffer->cur ().codepoint, - currControlPoint, - HB_DIRECTION_LTR /*XXX*/, - &currX, &currY)) - return; - - o.x_offset = hb_saturate_sub (markX, currX); - o.y_offset = hb_saturate_sub (markY, currY); - } - break; - - case 1: /* Anchor Point Actions. */ - { - /* Indexed into 'ankr' table. */ - /* Each action (record in ankrData) contains two 16-bit fields, so we must - double the ankrActionIndex to get the correct offset here. */ - const HBUINT16 *data = &ankrData[entry.data.ankrActionIndex * 2]; - if (!c->sanitizer.check_array (data, 2)) return; - hb_barrier (); - unsigned int markAnchorPoint = *data++; - unsigned int currAnchorPoint = *data++; - const Anchor &markAnchor = c->ankr_table->get_anchor (c->buffer->info[mark].codepoint, - markAnchorPoint, - c->sanitizer.get_num_glyphs ()); - const Anchor &currAnchor = c->ankr_table->get_anchor (c->buffer->cur ().codepoint, - currAnchorPoint, - c->sanitizer.get_num_glyphs ()); - - o.x_offset = hb_saturate_sub (c->font->em_scale_x (markAnchor.xCoordinate), - c->font->em_scale_x (currAnchor.xCoordinate)); - o.y_offset = hb_saturate_sub (c->font->em_scale_y (markAnchor.yCoordinate), - c->font->em_scale_y (currAnchor.yCoordinate)); - } - break; - - case 2: /* Control Point Coordinate Actions. */ - { - /* Each action contains four 16-bit fields, so we multiply the ankrActionIndex - by 4 to get the correct offset for the given action. */ - const FWORD *data = (const FWORD *) &ankrData[entry.data.ankrActionIndex * 4]; - if (!c->sanitizer.check_array (data, 4)) return; - hb_barrier (); - int markX = *data++; - int markY = *data++; - int currX = *data++; - int currY = *data++; - - o.x_offset = hb_saturate_sub (c->font->em_scale_x (markX), c->font->em_scale_x (currX)); - o.y_offset = hb_saturate_sub (c->font->em_scale_y (markY), c->font->em_scale_y (currY)); - } - break; - } - hb_position_t base_offset = OT::Layout::GPOS_impl::resolve_cross_offset (buffer->pos, buffer->len, mark, - buffer->props.direction, - c->buffer_is_reversed); - if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction)) - o.y_offset = hb_saturate_add (o.y_offset, base_offset); - else - o.x_offset = hb_saturate_add (o.x_offset, base_offset); - o.attach_type() = OT::Layout::GPOS_impl::ATTACH_TYPE_MARK; - o.attach_chain() = (int) mark - (int) buffer->idx; - if (c->buffer_is_reversed) - o.attach_chain() = -o.attach_chain(); - buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT; - } + if (unlikely (!perform_action (buffer, entry))) return; if (entry.flags & Mark) { @@ -700,6 +615,102 @@ struct KerxSubTableFormat4 } } + HB_NEVER_INLINE + HB_NODISCARD bool perform_action (hb_buffer_t *buffer, + const Entry &entry) + { + hb_glyph_position_t &o = buffer->cur_pos(); + switch (action_type) + { + case 0: /* Control Point Actions.*/ + { + /* Indexed into glyph outline. */ + /* Each action (record in ankrData) contains two 16-bit fields, so we must + double the ankrActionIndex to get the correct offset here. */ + const HBUINT16 *data = &ankrData[entry.data.ankrActionIndex * 2]; + if (!c->sanitizer.check_array (data, 2)) return false; + hb_barrier (); + /* Font functions may load the whole glyph outline to find a contour + * point, and a non-advancing transition can repeat that at will. + * Charge the two lookups to the buffer's work budget. */ + if (unlikely ((buffer->max_ops -= 2 * HB_BUDGET_256) <= 0)) return false; + unsigned int markControlPoint = *data++; + unsigned int currControlPoint = *data++; + hb_position_t markX = 0; + hb_position_t markY = 0; + hb_position_t currX = 0; + hb_position_t currY = 0; + if (!c->font->get_glyph_contour_point_for_origin (c->buffer->info[mark].codepoint, + markControlPoint, + HB_DIRECTION_LTR /*XXX*/, + &markX, &markY) || + !c->font->get_glyph_contour_point_for_origin (c->buffer->cur ().codepoint, + currControlPoint, + HB_DIRECTION_LTR /*XXX*/, + &currX, &currY)) + return false; + + o.x_offset = hb_saturate_sub (markX, currX); + o.y_offset = hb_saturate_sub (markY, currY); + } + break; + + case 1: /* Anchor Point Actions. */ + { + /* Indexed into 'ankr' table. */ + /* Each action (record in ankrData) contains two 16-bit fields, so we must + double the ankrActionIndex to get the correct offset here. */ + const HBUINT16 *data = &ankrData[entry.data.ankrActionIndex * 2]; + if (!c->sanitizer.check_array (data, 2)) return false; + hb_barrier (); + unsigned int markAnchorPoint = *data++; + unsigned int currAnchorPoint = *data++; + const Anchor &markAnchor = c->ankr_table->get_anchor (c->buffer->info[mark].codepoint, + markAnchorPoint, + c->sanitizer.get_num_glyphs ()); + const Anchor &currAnchor = c->ankr_table->get_anchor (c->buffer->cur ().codepoint, + currAnchorPoint, + c->sanitizer.get_num_glyphs ()); + + o.x_offset = hb_saturate_sub (c->font->em_scale_x (markAnchor.xCoordinate), + c->font->em_scale_x (currAnchor.xCoordinate)); + o.y_offset = hb_saturate_sub (c->font->em_scale_y (markAnchor.yCoordinate), + c->font->em_scale_y (currAnchor.yCoordinate)); + } + break; + + case 2: /* Control Point Coordinate Actions. */ + { + /* Each action contains four 16-bit fields, so we multiply the ankrActionIndex + by 4 to get the correct offset for the given action. */ + const FWORD *data = (const FWORD *) &ankrData[entry.data.ankrActionIndex * 4]; + if (!c->sanitizer.check_array (data, 4)) return false; + hb_barrier (); + int markX = *data++; + int markY = *data++; + int currX = *data++; + int currY = *data++; + + o.x_offset = hb_saturate_sub (c->font->em_scale_x (markX), c->font->em_scale_x (currX)); + o.y_offset = hb_saturate_sub (c->font->em_scale_y (markY), c->font->em_scale_y (currY)); + } + break; + } + hb_position_t base_offset = OT::Layout::GPOS_impl::resolve_cross_offset (buffer->pos, buffer->len, mark, + buffer->props.direction, + c->buffer_is_reversed); + if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction)) + o.y_offset = hb_saturate_add (o.y_offset, base_offset); + else + o.x_offset = hb_saturate_add (o.x_offset, base_offset); + o.attach_type() = OT::Layout::GPOS_impl::ATTACH_TYPE_MARK; + o.attach_chain() = (int) mark - (int) buffer->idx; + if (c->buffer_is_reversed) + o.attach_chain() = -o.attach_chain(); + buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT; + return true; + } + public: hb_aat_apply_context_t *c; const KerxSubTableFormat4 *table; diff --git a/src/hb-aat-layout-morx-table.hh b/src/hb-aat-layout-morx-table.hh index fdd2776a8..aef11ddff 100644 --- a/src/hb-aat-layout-morx-table.hh +++ b/src/hb-aat-layout-morx-table.hh @@ -84,7 +84,7 @@ struct RearrangementSubtable table (table_), start (0), end (0) {} - HB_AAT_TRANSITION_INLINE + HB_ALWAYS_INLINE void transition (hb_buffer_t *buffer, StateTableDriver *driver, const Entry &entry) @@ -98,65 +98,69 @@ struct RearrangementSubtable end = hb_min (buffer->idx + 1, buffer->len); if ((flags & Verb) && start < end) + perform_action (buffer, flags); + } + + HB_NEVER_INLINE + void perform_action (hb_buffer_t *buffer, unsigned int flags) + { + /* The following map has two nibbles, for start-side + * and end-side. Values of 0,1,2 mean move that many + * to the other side. Value of 3 means move 2 and + * flip them. */ + const unsigned char map[16] = { - /* The following map has two nibbles, for start-side - * and end-side. Values of 0,1,2 mean move that many - * to the other side. Value of 3 means move 2 and - * flip them. */ - const unsigned char map[16] = + 0x00, /* 0 no change */ + 0x10, /* 1 Ax => xA */ + 0x01, /* 2 xD => Dx */ + 0x11, /* 3 AxD => DxA */ + 0x20, /* 4 ABx => xAB */ + 0x30, /* 5 ABx => xBA */ + 0x02, /* 6 xCD => CDx */ + 0x03, /* 7 xCD => DCx */ + 0x12, /* 8 AxCD => CDxA */ + 0x13, /* 9 AxCD => DCxA */ + 0x21, /* 10 ABxD => DxAB */ + 0x31, /* 11 ABxD => DxBA */ + 0x22, /* 12 ABxCD => CDxAB */ + 0x32, /* 13 ABxCD => CDxBA */ + 0x23, /* 14 ABxCD => DCxAB */ + 0x33, /* 15 ABxCD => DCxBA */ + }; + + unsigned int m = map[flags & Verb]; + unsigned int l = hb_min (2u, m >> 4); + unsigned int r = hb_min (2u, m & 0x0F); + bool reverse_l = 3 == (m >> 4); + bool reverse_r = 3 == (m & 0x0F); + + if (end - start >= l + r && end-start <= HB_MAX_CONTEXT_LENGTH) + { + buffer->merge_clusters (start, hb_min (buffer->idx + 1, buffer->len)); + buffer->merge_clusters (start, end); + + hb_glyph_info_t *info = buffer->info; + hb_glyph_info_t buf[4]; + + hb_memcpy (buf, info + start, l * sizeof (buf[0])); + hb_memcpy (buf + 2, info + end - r, r * sizeof (buf[0])); + + if (l != r) + memmove (info + start + r, info + start + l, (end - start - l - r) * sizeof (buf[0])); + + hb_memcpy (info + start, buf + 2, r * sizeof (buf[0])); + hb_memcpy (info + end - l, buf, l * sizeof (buf[0])); + if (reverse_l) { - 0x00, /* 0 no change */ - 0x10, /* 1 Ax => xA */ - 0x01, /* 2 xD => Dx */ - 0x11, /* 3 AxD => DxA */ - 0x20, /* 4 ABx => xAB */ - 0x30, /* 5 ABx => xBA */ - 0x02, /* 6 xCD => CDx */ - 0x03, /* 7 xCD => DCx */ - 0x12, /* 8 AxCD => CDxA */ - 0x13, /* 9 AxCD => DCxA */ - 0x21, /* 10 ABxD => DxAB */ - 0x31, /* 11 ABxD => DxBA */ - 0x22, /* 12 ABxCD => CDxAB */ - 0x32, /* 13 ABxCD => CDxBA */ - 0x23, /* 14 ABxCD => DCxAB */ - 0x33, /* 15 ABxCD => DCxBA */ - }; - - unsigned int m = map[flags & Verb]; - unsigned int l = hb_min (2u, m >> 4); - unsigned int r = hb_min (2u, m & 0x0F); - bool reverse_l = 3 == (m >> 4); - bool reverse_r = 3 == (m & 0x0F); - - if (end - start >= l + r && end-start <= HB_MAX_CONTEXT_LENGTH) + buf[0] = info[end - 1]; + info[end - 1] = info[end - 2]; + info[end - 2] = buf[0]; + } + if (reverse_r) { - buffer->merge_clusters (start, hb_min (buffer->idx + 1, buffer->len)); - buffer->merge_clusters (start, end); - - hb_glyph_info_t *info = buffer->info; - hb_glyph_info_t buf[4]; - - hb_memcpy (buf, info + start, l * sizeof (buf[0])); - hb_memcpy (buf + 2, info + end - r, r * sizeof (buf[0])); - - if (l != r) - memmove (info + start + r, info + start + l, (end - start - l - r) * sizeof (buf[0])); - - hb_memcpy (info + start, buf + 2, r * sizeof (buf[0])); - hb_memcpy (info + end - l, buf, l * sizeof (buf[0])); - if (reverse_l) - { - buf[0] = info[end - 1]; - info[end - 1] = info[end - 2]; - info[end - 2] = buf[0]; - } - if (reverse_r) - { - buf[0] = info[start]; - info[start] = info[start + 1]; - info[start + 1] = buf[0]; - } + buf[0] = info[start]; + info[start] = info[start + 1]; + info[start + 1] = buf[0]; } } } @@ -244,7 +248,7 @@ struct ContextualSubtable mark (0), subs (table+table->substitutionTables) {} - HB_AAT_TRANSITION_INLINE + HB_ALWAYS_INLINE void transition (hb_buffer_t *buffer, StateTableDriver *driver, const Entry &entry) @@ -254,6 +258,21 @@ struct ContextualSubtable if (buffer->idx == buffer->len && !mark_set) return; + if (table->is_actionable (entry)) + perform_action (buffer, driver, entry); + + if (entry.flags & SetMark) + { + mark_set = true; + mark = buffer->idx; + } + } + + HB_NEVER_INLINE + void perform_action (hb_buffer_t *buffer, + StateTableDriver *driver, + const Entry &entry) + { const HBGlyphID16 *replacement; replacement = nullptr; @@ -313,12 +332,6 @@ struct ContextualSubtable c->replace_glyph_inplace (idx, *replacement); ret = true; } - - if (entry.flags & SetMark) - { - mark_set = true; - mark = buffer->idx; - } } public: @@ -492,7 +505,7 @@ struct LigatureSubtable ligature (table+table->ligature), match_length (0) {} - HB_AAT_TRANSITION_INLINE + HB_ALWAYS_INLINE void transition (hb_buffer_t *buffer, StateTableDriver *driver, const Entry &entry) @@ -509,84 +522,89 @@ struct LigatureSubtable } if (LigatureEntryT::performAction (entry)) + perform_action (buffer, entry); + } + + HB_NEVER_INLINE + void perform_action (hb_buffer_t *buffer, + const Entry &entry) + { + DEBUG_MSG (APPLY, nullptr, "Perform action with %u", match_length); + unsigned int end = buffer->out_len; + + if (unlikely (!match_length)) + return; + + if (buffer->idx >= buffer->len) + return; /* TODO Work on previous instead? */ + + unsigned int cursor = match_length; + + unsigned int action_idx = LigatureEntryT::ligActionIndex (entry); + action_idx = Types::offsetToIndex (action_idx, table, ligAction.arrayZ); + const HBUINT32 *actionData = &ligAction[action_idx]; + + unsigned int ligature_idx = 0; + unsigned int action; + do { - DEBUG_MSG (APPLY, nullptr, "Perform action with %u", match_length); - unsigned int end = buffer->out_len; - - if (unlikely (!match_length)) - return; - - if (buffer->idx >= buffer->len) - return; /* TODO Work on previous instead? */ - - unsigned int cursor = match_length; - - unsigned int action_idx = LigatureEntryT::ligActionIndex (entry); - action_idx = Types::offsetToIndex (action_idx, table, ligAction.arrayZ); - const HBUINT32 *actionData = &ligAction[action_idx]; - - unsigned int ligature_idx = 0; - unsigned int action; - do + if (unlikely (!cursor)) { - if (unlikely (!cursor)) - { - /* Stack underflow. Clear the stack. */ - DEBUG_MSG (APPLY, nullptr, "Stack underflow"); - match_length = 0; - break; - } - - DEBUG_MSG (APPLY, nullptr, "Moving to stack position %u", cursor - 1); - if (unlikely (!buffer->move_to (match_positions[--cursor % ARRAY_LENGTH (match_positions)]))) return; - - if (unlikely (!actionData->sanitize (&c->sanitizer))) break; - hb_barrier (); - action = *actionData; - - uint32_t uoffset = action & LigActionOffset; - if (uoffset & 0x20000000) - uoffset |= 0xC0000000; /* Sign-extend. */ - int32_t offset = (int32_t) uoffset; - unsigned int component_idx = buffer->cur().codepoint + offset; - component_idx = Types::wordOffsetToIndex (component_idx, table, component.arrayZ); - const HBUINT16 &componentData = component[component_idx]; - if (unlikely (!componentData.sanitize (&c->sanitizer))) break; - hb_barrier (); - ligature_idx += componentData; - - DEBUG_MSG (APPLY, nullptr, "Action store %d last %d", - bool (action & LigActionStore), - bool (action & LigActionLast)); - if (action & (LigActionStore | LigActionLast)) - { - ligature_idx = Types::offsetToIndex (ligature_idx, table, ligature.arrayZ); - const HBGlyphID16 &ligatureData = ligature[ligature_idx]; - if (unlikely (!ligatureData.sanitize (&c->sanitizer))) break; - hb_barrier (); - hb_codepoint_t lig = ligatureData; - - DEBUG_MSG (APPLY, nullptr, "Produced ligature %u", lig); - if (unlikely (!c->replace_glyph (lig))) return; - - unsigned int lig_end = match_positions[(match_length - 1u) % ARRAY_LENGTH (match_positions)] + 1u; - /* Now go and delete all subsequent components. */ - while (match_length - 1u > cursor) - { - DEBUG_MSG (APPLY, nullptr, "Skipping ligature component"); - if (unlikely (!buffer->move_to (match_positions[--match_length % ARRAY_LENGTH (match_positions)]))) return; - if (!c->delete_glyph ()) return; - } - - if (unlikely (!buffer->move_to (lig_end))) return; - buffer->merge_out_clusters (match_positions[cursor % ARRAY_LENGTH (match_positions)], buffer->out_len); - } - - actionData++; + /* Stack underflow. Clear the stack. */ + DEBUG_MSG (APPLY, nullptr, "Stack underflow"); + match_length = 0; + break; } - while (!(action & LigActionLast)); - if (unlikely (!buffer->move_to (end))) return; + + DEBUG_MSG (APPLY, nullptr, "Moving to stack position %u", cursor - 1); + if (unlikely (!buffer->move_to (match_positions[--cursor % ARRAY_LENGTH (match_positions)]))) return; + + if (unlikely (!actionData->sanitize (&c->sanitizer))) break; + hb_barrier (); + action = *actionData; + + uint32_t uoffset = action & LigActionOffset; + if (uoffset & 0x20000000) + uoffset |= 0xC0000000; /* Sign-extend. */ + int32_t offset = (int32_t) uoffset; + unsigned int component_idx = buffer->cur().codepoint + offset; + component_idx = Types::wordOffsetToIndex (component_idx, table, component.arrayZ); + const HBUINT16 &componentData = component[component_idx]; + if (unlikely (!componentData.sanitize (&c->sanitizer))) break; + hb_barrier (); + ligature_idx += componentData; + + DEBUG_MSG (APPLY, nullptr, "Action store %d last %d", + bool (action & LigActionStore), + bool (action & LigActionLast)); + if (action & (LigActionStore | LigActionLast)) + { + ligature_idx = Types::offsetToIndex (ligature_idx, table, ligature.arrayZ); + const HBGlyphID16 &ligatureData = ligature[ligature_idx]; + if (unlikely (!ligatureData.sanitize (&c->sanitizer))) break; + hb_barrier (); + hb_codepoint_t lig = ligatureData; + + DEBUG_MSG (APPLY, nullptr, "Produced ligature %u", lig); + if (unlikely (!c->replace_glyph (lig))) return; + + unsigned int lig_end = match_positions[(match_length - 1u) % ARRAY_LENGTH (match_positions)] + 1u; + /* Now go and delete all subsequent components. */ + while (match_length - 1u > cursor) + { + DEBUG_MSG (APPLY, nullptr, "Skipping ligature component"); + if (unlikely (!buffer->move_to (match_positions[--match_length % ARRAY_LENGTH (match_positions)]))) return; + if (!c->delete_glyph ()) return; + } + + if (unlikely (!buffer->move_to (lig_end))) return; + buffer->merge_out_clusters (match_positions[cursor % ARRAY_LENGTH (match_positions)], buffer->out_len); + } + + actionData++; } + while (!(action & LigActionLast)); + if (unlikely (!buffer->move_to (end))) return; } public: @@ -790,10 +808,21 @@ struct InsertionSubtable mark (0), insertionAction (table+table->insertionAction) {} - HB_AAT_TRANSITION_INLINE + HB_ALWAYS_INLINE void transition (hb_buffer_t *buffer, StateTableDriver *driver, const Entry &entry) + { + if (entry.data.markedInsertIndex != 0xFFFF || + entry.data.currentInsertIndex != 0xFFFF) + perform_action (buffer, entry); + else if (entry.flags & SetMark) + mark = buffer->out_len; + } + + HB_NEVER_INLINE + void perform_action (hb_buffer_t *buffer, + const Entry &entry) { unsigned int flags = entry.flags;