From 63fb2eec90d7fc86ff262b6c191072aaafbde128 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 20 Feb 2026 11:25:24 -0700 Subject: [PATCH] Monotone characters fixups (#5752) * [buffer] Fix a typo and doc string * [buffer] Add merge_grapheme_clusters / merge_out_grapheme_clusters Cluster merges serve two distinct purposes that require different level gates: - Grapheme grouping: combining marks merged into their base cluster. Correct gate: IS_GRAPHEMES (levels 0 and 3). New functions: merge_grapheme_clusters / merge_out_grapheme_clusters. - Monotonicity maintenance: clusters merged to prevent non-monotone output after ligature formation, reordering, etc. Correct gate: IS_MONOTONE (levels 0 and 1). Existing functions: merge_clusters / merge_out_clusters. Previously both paths shared merge_clusters_impl's single IS_MONOTONE gate, so GRAPHEMES (level 3) was incorrectly blocked from merging grapheme clusters. This also caused a regression in the Thai and Hangul shapers for MONOTONE_CHARACTERS (level 1): when their explicit == MONOTONE_GRAPHEMES call-site guards were removed in #5069, the impls' IS_MONOTONE gate no longer protected them. Gates are now in the inline wrappers in hb-buffer.hh; the _impl functions are gate-free. hb_form_clusters, CoreText grapheme detection, and the Thai/Hangul shapers are updated to call the grapheme-semantic variants. Fixes #5750. Fixes https://github.com/harfbuzz/harfbuzz/issues/5751 Fixes https://github.com/harfbuzz/harfbuzz/issues/5750 Co-Authored-By: Claude Sonnet 4.6 * [thai] Adjust grapheme merging logic * [hangul] Tweak grapheme merging --------- Co-authored-by: Claude Sonnet 4.6 --- src/hb-buffer.cc | 16 ++------- src/hb-buffer.h | 4 +-- src/hb-buffer.hh | 34 +++++++++++++++++++- src/hb-coretext-shape.cc | 2 +- src/hb-ot-shape.cc | 8 ++--- src/hb-ot-shaper-hangul.cc | 4 +-- src/hb-ot-shaper-thai.cc | 12 +++---- test/shape/data/in-house/tests/cluster.tests | 12 ++++++- 8 files changed, 58 insertions(+), 34 deletions(-) diff --git a/src/hb-buffer.cc b/src/hb-buffer.cc index 00c004e8b..8511f3613 100644 --- a/src/hb-buffer.cc +++ b/src/hb-buffer.cc @@ -547,12 +547,6 @@ void hb_buffer_t::merge_clusters_impl (unsigned int start, unsigned int end) { - if (!HB_BUFFER_CLUSTER_LEVEL_IS_MONOTONE (cluster_level)) - { - unsafe_to_break (start, end); - return; - } - max_ops -= end - start; if (unlikely (max_ops < 0)) successful = false; @@ -581,15 +575,9 @@ hb_buffer_t::merge_clusters_impl (unsigned int start, set_cluster (info[i], cluster); } void -hb_buffer_t::merge_out_clusters (unsigned int start, - unsigned int end) +hb_buffer_t::merge_out_clusters_impl (unsigned int start, + unsigned int end) { - if (!HB_BUFFER_CLUSTER_LEVEL_IS_MONOTONE (cluster_level)) - return; - - if (unlikely (end - start < 2)) - return; - max_ops -= end - start; if (unlikely (max_ops < 0)) successful = false; diff --git a/src/hb-buffer.h b/src/hb-buffer.h index 449ce7e20..89e1213f7 100644 --- a/src/hb-buffer.h +++ b/src/hb-buffer.h @@ -495,7 +495,7 @@ typedef enum { (1u << HB_BUFFER_CLUSTER_LEVEL_GRAPHEMES)))) /** - * HB_BUFFER_CLUSTER_LEVEL_IS_CHARACTERS + * HB_BUFFER_CLUSTER_LEVEL_IS_CHARACTERS: * @level: #hb_buffer_cluster_level_t to test * * Tests whether a cluster level does not group cluster values by graphemes. @@ -505,7 +505,7 @@ typedef enum { */ #define HB_BUFFER_CLUSTER_LEVEL_IS_CHARACTERS(level) \ ((bool) ((1u << (unsigned) (level)) & \ - ((1u << HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARCATERS) | \ + ((1u << HB_BUFFER_CLUSTER_LEVEL_MONOTONE_CHARACTERS) | \ (1u << HB_BUFFER_CLUSTER_LEVEL_CHARACTERS)))) HB_EXTERN void diff --git a/src/hb-buffer.hh b/src/hb-buffer.hh index 52fac5246..6b0500993 100644 --- a/src/hb-buffer.hh +++ b/src/hb-buffer.hh @@ -398,10 +398,42 @@ struct hb_buffer_t { if (end - start < 2) return; + if (!HB_BUFFER_CLUSTER_LEVEL_IS_MONOTONE (cluster_level)) + { + unsafe_to_break (start, end); + return; + } + merge_clusters_impl (start, end); + } + void merge_grapheme_clusters (unsigned int start, unsigned int end) + { + if (end - start < 2) + return; + if (!HB_BUFFER_CLUSTER_LEVEL_IS_GRAPHEMES (cluster_level)) + { + unsafe_to_break (start, end); + return; + } merge_clusters_impl (start, end); } HB_INTERNAL void merge_clusters_impl (unsigned int start, unsigned int end); - HB_INTERNAL void merge_out_clusters (unsigned int start, unsigned int end); + void merge_out_clusters (unsigned int start, unsigned int end) + { + if (end - start < 2) + return; + if (!HB_BUFFER_CLUSTER_LEVEL_IS_MONOTONE (cluster_level)) + return; + merge_out_clusters_impl (start, end); + } + void merge_out_grapheme_clusters (unsigned int start, unsigned int end) + { + if (end - start < 2) + return; + if (!HB_BUFFER_CLUSTER_LEVEL_IS_GRAPHEMES (cluster_level)) + return; + merge_out_clusters_impl (start, end); + } + HB_INTERNAL void merge_out_clusters_impl (unsigned int start, unsigned int end); /* Merge clusters for deleting current glyph, and skip it. */ HB_INTERNAL void delete_glyph (); HB_INTERNAL void delete_glyphs_inplace (bool (*filter) (const hb_glyph_info_t *info)); diff --git a/src/hb-coretext-shape.cc b/src/hb-coretext-shape.cc index ebd0bc132..89bf090bb 100644 --- a/src/hb-coretext-shape.cc +++ b/src/hb-coretext-shape.cc @@ -199,7 +199,7 @@ _hb_coretext_shape (hb_shape_plan_t *shape_plan, hb_glyph_info_t *info = buffer->info; for (unsigned int i = 1; i < count; i++) if (HB_UNICODE_GENERAL_CATEGORY_IS_MARK (unicode->general_category (info[i].codepoint))) - buffer->merge_clusters (i - 1, i + 1); + buffer->merge_grapheme_clusters (i - 1, i + 1); } hb_vector_t range_records; diff --git a/src/hb-ot-shape.cc b/src/hb-ot-shape.cc index e8d43180e..7b3b854d5 100644 --- a/src/hb-ot-shape.cc +++ b/src/hb-ot-shape.cc @@ -580,12 +580,8 @@ hb_form_clusters (hb_buffer_t *buffer) if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_CONTINUATIONS)) return; - if (HB_BUFFER_CLUSTER_LEVEL_IS_GRAPHEMES (buffer->cluster_level)) - foreach_grapheme (buffer, start, end) - buffer->merge_clusters (start, end); - else - foreach_grapheme (buffer, start, end) - buffer->unsafe_to_break (start, end); + foreach_grapheme (buffer, start, end) + buffer->merge_grapheme_clusters (start, end); } static void diff --git a/src/hb-ot-shaper-hangul.cc b/src/hb-ot-shaper-hangul.cc index 792b658bf..ce87a9156 100644 --- a/src/hb-ot-shaper-hangul.cc +++ b/src/hb-ot-shaper-hangul.cc @@ -298,7 +298,7 @@ preprocess_text_hangul (const hb_ot_shape_plan_t *plan HB_UNUSED, end = start + 2; if (unlikely (!buffer->successful)) break; - buffer->merge_out_clusters (start, end); + buffer->merge_out_grapheme_clusters (start, end); continue; } } @@ -371,7 +371,7 @@ preprocess_text_hangul (const hb_ot_shape_plan_t *plan HB_UNUSED, if (i < end) info[i++].hangul_shaping_feature() = TJMO; - buffer->merge_out_clusters (start, end); + buffer->merge_out_grapheme_clusters (start, end); continue; } else if ((!tindex && buffer->idx + 1 < count && isT (buffer->cur(+1).codepoint))) diff --git a/src/hb-ot-shaper-thai.cc b/src/hb-ot-shaper-thai.cc index 9487e1c5b..52566fe29 100644 --- a/src/hb-ot-shaper-thai.cc +++ b/src/hb-ot-shaper-thai.cc @@ -356,13 +356,11 @@ preprocess_text_thai (const hb_ot_shape_plan_t *plan, sizeof (buffer->out_info[0]) * (end - start - 2)); buffer->out_info[start] = t; } - else - { - /* Since we decomposed, and NIKHAHIT is combining, merge clusters with the - * previous cluster. */ - if (start) - buffer->merge_out_clusters (start - 1, end); - } + + /* Since we decomposed, and NIKHAHIT is combining, merge clusters with the + * previous cluster. */ + if (start) + buffer->merge_out_grapheme_clusters (start - 1, end); } buffer->sync (); diff --git a/test/shape/data/in-house/tests/cluster.tests b/test/shape/data/in-house/tests/cluster.tests index 2c8669bee..a3151edb3 100644 --- a/test/shape/data/in-house/tests/cluster.tests +++ b/test/shape/data/in-house/tests/cluster.tests @@ -1,7 +1,17 @@ # Our cluster-level=3 doesn't really test anything here. -../fonts/4fac3929fc3332834e93673780ec0fe94342d193.ttf;--cluster-level=3;U+0078,U+030A,U+0058,U+030A;[gid2=0+1083|gid3=1@-1132,-8+0|gid1=2+1200|gid3=3@-1190,349+0] +../fonts/4fac3929fc3332834e93673780ec0fe94342d193.ttf;--cluster-level=3;U+0078,U+030A,U+0058,U+030A;[gid2=0+1083|gid3=0@-1132,-8+0|gid1=2+1200|gid3=2@-1190,349+0] ../fonts/4fac3929fc3332834e93673780ec0fe94342d193.ttf;--cluster-level=2;U+0078,U+030A,U+0058,U+030A;[gid2=0+1083|gid3=1@-1132,-8+0|gid1=2+1200|gid3=3@-1190,349+0] ../fonts/43ef465752be9af900745f72fe29cb853a1401a5.ttf;--cluster-level=1;U+05D4,U+05B7,U+05E9,U+05BC,U+05C1,U+05B8,U+05DE,U+05B4,U+05DD;[uni05DD=8+1359|uni05B4=7@111,0+0|uni05DE=6+1391|uni05B8=5+0|uni05BC=3+0|uni05C1=3+0|uni05E9=2+1451|uni05B7=1@28,0+0|uni05D4=0+1338] ../fonts/6f36d056bad6d478fc0bf7397bd52dc3bd197d5f.ttf;--cluster-level=1;U+099B,U+09CB,U+09C8,U+09C2,U+09CB,U+098C;[evowelsigninibeng=0+346|aivowelsignbeng=0+346|evowelsignbeng=0+346|chabeng=0+687|uuvowelsignlongbeng=0@-96,0+0|aavowelsignbeng=0+266|aavowelsignbeng=4+266|lvocalicbeng=5+639] ../fonts/fd07ea46e4d8368ada1776208c07fd596f727852.ttf;--cluster-level=1;U+0D4E,U+0D4D,U+200D;[uni0D4E=0+0|uni25CC=0+418|uni0D4D=0+0|space=0+0] ../fonts/c2d320136762887c43d245ecd2ffc2c0d57cfcb3.ttf;;U+0DAF,U+0DCF,U+0DAF;[dasinh=0+547|aavowelsignsinh=0+379|dasinh=2+532] + +# https://github.com/harfbuzz/harfbuzz/issues/5750 +../fonts/4fac3929fc3332834e93673780ec0fe94342d193.ttf;--cluster-level=0 --no-positions --no-glyph-names;U+0020,U+0E33;[0=0|0=0|0=0] +../fonts/4fac3929fc3332834e93673780ec0fe94342d193.ttf;--cluster-level=1 --no-positions --no-glyph-names;U+0020,U+0E33;[0=0|0=1|0=1] +../fonts/4fac3929fc3332834e93673780ec0fe94342d193.ttf;--cluster-level=2 --no-positions --no-glyph-names;U+0020,U+0E33;[0=0|0=1|0=1] +../fonts/4fac3929fc3332834e93673780ec0fe94342d193.ttf;--cluster-level=3 --no-positions --no-glyph-names;U+0020,U+0E33;[0=0|0=0|0=0] +../fonts/4fac3929fc3332834e93673780ec0fe94342d193.ttf;--cluster-level=0 --no-positions --no-glyph-names;U+0020,U+0E34,U+0E33;[0=0|0=0|0=0|0=0] +../fonts/4fac3929fc3332834e93673780ec0fe94342d193.ttf;--cluster-level=1 --no-positions --no-glyph-names;U+0020,U+0E34,U+0E33;[0=0|0=1|0=1|0=1] +../fonts/4fac3929fc3332834e93673780ec0fe94342d193.ttf;--cluster-level=2 --no-positions --no-glyph-names;U+0020,U+0E34,U+0E33;[0=0|0=2|0=1|0=2] +../fonts/4fac3929fc3332834e93673780ec0fe94342d193.ttf;--cluster-level=3 --no-positions --no-glyph-names;U+0020,U+0E34,U+0E33;[0=0|0=0|0=0|0=0]