From 20bc59869e2d8f987615a1bfdb2c8ea87c722953 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 26 May 2026 18:43:06 +0200 Subject: [PATCH] [aat] Fix mort contextual substitution offsets The legacy mort contextual subtable uses signed word offsets to per-glyph substitution tables. A zero offset means that there is no table for that action. We were treating the fields like morx lookup indices with 0xFFFF as the sentinel, so zero-offset state entries tried to substitute arbitrary glyphs from the start of the state table. Keep morx using lookup-index semantics, but use signed offsets and the zero sentinel for mort. This fixes CharcoalCY shaping of text such as "Empty-handed", which previously produced out-of-font glyph IDs. Testing: - b/util/hb-shape CharcoalCY.dfont Empty-handed - b/util/hb-shape CharcoalCY.dfont --unicodes=002D,002D - meson test -Cb Fixes: https://github.com/harfbuzz/harfbuzz/issues/5887 Assisted-by: OpenAI Codex --- src/hb-aat-layout-common.hh | 22 +++++++-------- src/hb-aat-layout-morx-table.hh | 49 ++++++++++++++++++++------------- 2 files changed, 41 insertions(+), 30 deletions(-) diff --git a/src/hb-aat-layout-common.hh b/src/hb-aat-layout-common.hh index cec42c54f..e4040446e 100644 --- a/src/hb-aat-layout-common.hh +++ b/src/hb-aat-layout-common.hh @@ -1134,31 +1134,31 @@ struct ObsoleteTypes typedef ClassTable ClassTypeWide; template - static unsigned int offsetToIndex (unsigned int offset, - const void *base, - const T *array) + static int offsetToIndex (int offset, + const void *base, + const T *array) { /* https://github.com/harfbuzz/harfbuzz/issues/3483 */ /* If offset is less than base, return an offset that would * result in an address half a 32bit address-space away, * to make sure sanitize fails even on 32bit builds. */ - if (unlikely (offset < unsigned ((const char *) array - (const char *) base))) + if (unlikely (offset < int ((const char *) array - (const char *) base))) return INT_MAX / T::static_size; /* https://github.com/harfbuzz/harfbuzz/issues/2816 */ - return (offset - unsigned ((const char *) array - (const char *) base)) / T::static_size; + return (offset - int ((const char *) array - (const char *) base)) / T::static_size; } template - static unsigned int byteOffsetToIndex (unsigned int offset, - const void *base, - const T *array) + static int byteOffsetToIndex (int offset, + const void *base, + const T *array) { return offsetToIndex (offset, base, array); } template - static unsigned int wordOffsetToIndex (unsigned int offset, - const void *base, - const T *array) + static int wordOffsetToIndex (int offset, + const void *base, + const T *array) { return offsetToIndex (2 * offset, base, array); } diff --git a/src/hb-aat-layout-morx-table.hh b/src/hb-aat-layout-morx-table.hh index c56bdb7c7..fc09af240 100644 --- a/src/hb-aat-layout-morx-table.hh +++ b/src/hb-aat-layout-morx-table.hh @@ -200,10 +200,12 @@ struct ContextualSubtable struct EntryData { - HBUINT16 markIndex; /* Index of the substitution table for the - * marked glyph (use 0xFFFF for none). */ - HBUINT16 currentIndex; /* Index of the substitution table for the - * current glyph (use 0xFFFF for none). */ + typedef typename std::conditional::type OffsetType; + + OffsetType markIndex; /* Index/offset of the substitution table for the + * marked glyph. */ + OffsetType currentIndex; /* Index/offset of the substitution table for the + * current glyph. */ public: DEFINE_SIZE_STATIC (4); }; @@ -222,7 +224,10 @@ struct ContextualSubtable } bool is_actionable (const Entry &entry) const { - return entry.data.markIndex != 0xFFFF || entry.data.currentIndex != 0xFFFF; + if (Types::extended) + return entry.data.markIndex != 0xFFFF || entry.data.currentIndex != 0xFFFF; + else + return entry.data.markIndex || entry.data.currentIndex; } struct driver_context_t @@ -260,13 +265,16 @@ struct ContextualSubtable } else { - unsigned int offset = entry.data.markIndex + buffer->info[mark].codepoint; - const UnsizedArrayOf &subs_old = (const UnsizedArrayOf &) subs; - replacement = &subs_old[Types::wordOffsetToIndex (offset, table, subs_old.arrayZ)]; - if (!(replacement->sanitize (&c->sanitizer) && - hb_barrier () && - *replacement)) - replacement = nullptr; + if (entry.data.markIndex) + { + int offset = (int) entry.data.markIndex + buffer->info[mark].codepoint; + const UnsizedArrayOf &subs_old = (const UnsizedArrayOf &) subs; + replacement = &subs_old[Types::wordOffsetToIndex (offset, table, subs_old.arrayZ)]; + if (!(replacement->sanitize (&c->sanitizer) && + hb_barrier () && + *replacement)) + replacement = nullptr; + } } if (replacement) { @@ -287,13 +295,16 @@ struct ContextualSubtable } else { - unsigned int offset = entry.data.currentIndex + buffer->info[idx].codepoint; - const UnsizedArrayOf &subs_old = (const UnsizedArrayOf &) subs; - replacement = &subs_old[Types::wordOffsetToIndex (offset, table, subs_old.arrayZ)]; - if (!(replacement->sanitize (&c->sanitizer) && - hb_barrier () && - *replacement)) - replacement = nullptr; + if (entry.data.currentIndex) + { + int offset = (int) entry.data.currentIndex + buffer->info[idx].codepoint; + const UnsizedArrayOf &subs_old = (const UnsizedArrayOf &) subs; + replacement = &subs_old[Types::wordOffsetToIndex (offset, table, subs_old.arrayZ)]; + if (!(replacement->sanitize (&c->sanitizer) && + hb_barrier () && + *replacement)) + replacement = nullptr; + } } if (replacement) {