diff --git a/src/XTerm.NET.Tests/TextSizingBlockBoundaryTests.cs b/src/XTerm.NET.Tests/TextSizingBlockBoundaryTests.cs
new file mode 100644
index 0000000..16e84df
--- /dev/null
+++ b/src/XTerm.NET.Tests/TextSizingBlockBoundaryTests.cs
@@ -0,0 +1,329 @@
+using XTerm.Buffer;
+using XTerm.Options;
+using Xunit;
+
+namespace XTerm.Tests;
+
+///
+/// OSC 66 blocks that share a sizing are joined into one span on the line, but each is still its own
+/// multicell character: overwriting part of one erases that block and no other.
+///
+///
+/// tomlm/Iciclecreek.Avalonia.Terminal#211 -- OpenCode prints each CJK character as its own
+/// w=2 block, and a Backspace that overwrote the last one with two spaces blanked the line.
+///
+public class TextSizingBlockBoundaryTests
+{
+ private const string Esc = "\u001b";
+ private const string St = Esc + "\\";
+
+ private static string Sized(string metadata, string text) => $"{Esc}]66;{metadata};{text}{St}";
+
+ private static string Blocks(string metadata, string chars)
+ => string.Concat(chars.Select(c => Sized(metadata, c.ToString())));
+
+ private static Terminal Fresh(int cols = 40, int rows = 4)
+ => new(new TerminalOptions { Cols = cols, Rows = rows });
+
+ private static BufferLine Row(Terminal t, int row = 0) => t.Buffer.Lines[t.Buffer.YBase + row]!;
+
+ private static string Text(Terminal t, int row = 0) => t.GetVisibleLines()[row].TrimEnd();
+
+ private static (int Column, int End)[] Spans(Terminal t, int row = 0)
+ => Row(t, row).SizedRuns.Select(r => (r.Column, r.EndColumn)).ToArray();
+
+ [Fact]
+ public void Overwriting_the_last_of_three_wide_blocks_keeps_the_other_two()
+ {
+ var t = Fresh();
+ t.Write(Blocks("w=2", "甲乙丙"));
+ Assert.Equal("甲乙丙", Text(t));
+
+ t.Write($"{Esc}[1;5H ");
+
+ Assert.Equal("甲乙", Text(t));
+ Assert.Equal(new[] { (0, 4) }, Spans(t));
+ }
+
+ [Fact]
+ public void Overwriting_the_last_of_three_narrow_blocks_keeps_the_other_two()
+ {
+ var t = Fresh();
+ t.Write(Blocks("w=1", "ABC"));
+
+ t.Write($"{Esc}[1;3H ");
+
+ Assert.Equal("AB", Text(t));
+ }
+
+ [Fact]
+ public void Overwriting_the_first_block_keeps_the_rest()
+ {
+ var t = Fresh();
+ t.Write(Blocks("w=2", "甲乙丙"));
+
+ t.Write($"{Esc}[1;1H ");
+
+ Assert.Equal(" 乙丙", Text(t));
+ Assert.Equal(new[] { (2, 6) }, Spans(t));
+ }
+
+ [Fact]
+ public void Overwriting_the_middle_block_splits_the_span_around_it()
+ {
+ var t = Fresh();
+ t.Write(Blocks("w=2", "甲乙丙"));
+
+ t.Write($"{Esc}[1;3H ");
+
+ Assert.Equal("甲 丙", Text(t));
+
+ // Left to right, as SizedRuns is documented, and each still answers for its own columns.
+ Assert.Equal(new[] { (0, 2), (4, 6) }, Spans(t));
+ Assert.False(Row(t).TryGetSizedRunAt(2, out _));
+ Assert.True(Row(t).TryGetSizedRunAt(5, out var run));
+ Assert.Equal(4, run.Column);
+ }
+
+ [Fact]
+ public void A_replacement_character_over_half_a_block_erases_only_that_block()
+ {
+ var t = Fresh();
+ t.Write(Blocks("w=2", "甲乙丙"));
+
+ // Column 4 is the first half of the third block.
+ t.Write($"{Esc}[1;5HX");
+
+ Assert.Equal("甲乙X", Text(t));
+ Assert.Equal(" ", Row(t)[5].Content);
+ Assert.Equal(new[] { (0, 4) }, Spans(t));
+ }
+
+ [Fact]
+ public void Writing_over_the_second_half_of_a_block_erases_its_first_half_too()
+ {
+ var t = Fresh();
+ t.Write(Blocks("w=2", "甲乙丙"));
+
+ // Column 3 is the continuation of the middle block: the block it belongs to starts left of
+ // the write, and that whole block goes -- but only that one.
+ t.Write($"{Esc}[1;4HZ");
+
+ Assert.Equal("甲 Z丙", Text(t));
+ Assert.Equal(" ", Row(t)[2].Content);
+ Assert.Equal(1, Row(t)[2].Width);
+ Assert.Equal(new[] { (0, 2), (4, 6) }, Spans(t));
+ }
+
+ [Fact]
+ public void A_wide_character_straddling_two_blocks_erases_both_and_no_more()
+ {
+ var t = Fresh();
+ t.Write(Blocks("w=2", "甲乙丙丁"));
+
+ // Columns 3..4: the second half of 乙 and the first half of 丙.
+ t.Write($"{Esc}[1;4H中");
+
+ Assert.Equal("甲 中 丁", Text(t));
+ Assert.Equal(new[] { (0, 2), (6, 8) }, Spans(t));
+ }
+
+ [Fact]
+ public void Erase_in_line_keeps_the_blocks_before_the_cursor()
+ {
+ var t = Fresh();
+ t.Write(Blocks("w=2", "甲乙丙"));
+
+ t.Write($"{Esc}[1;3H{Esc}[K");
+
+ Assert.Equal("甲", Text(t));
+ Assert.Equal(new[] { (0, 2) }, Spans(t));
+ }
+
+ [Fact]
+ public void Erase_characters_keeps_the_blocks_either_side()
+ {
+ var t = Fresh();
+ t.Write(Blocks("w=2", "甲乙丙"));
+
+ t.Write($"{Esc}[1;3H{Esc}[2X");
+
+ Assert.Equal("甲 丙", Text(t));
+ Assert.Equal(new[] { (0, 2), (4, 6) }, Spans(t));
+ }
+
+ [Fact]
+ public void Delete_characters_keeps_the_blocks_before_the_cursor()
+ {
+ var t = Fresh();
+ t.Write(Blocks("w=2", "甲乙丙"));
+
+ // A shift cannot carry a block along, so everything from the cursor on is blanked -- but
+ // the block before the cursor was never in the way.
+ t.Write($"{Esc}[1;3H{Esc}[2P");
+
+ Assert.Equal("甲", Text(t));
+ Assert.Equal(new[] { (0, 2) }, Spans(t));
+ }
+
+ [Fact]
+ public void A_single_block_with_a_multi_character_payload_is_still_erased_whole()
+ {
+ var t = Fresh();
+ t.Write(Sized("w=4", "甲乙"));
+
+ t.Write($"{Esc}[1;4Hx");
+
+ Assert.False(Row(t).HasSizedRuns);
+ Assert.Equal(" x", Text(t));
+ }
+
+ [Fact]
+ public void A_single_wide_block_is_erased_whole_by_writing_its_second_half()
+ {
+ var t = Fresh();
+ t.Write(Sized("w=2", "甲"));
+
+ t.Write($"{Esc}[1;2Hx");
+
+ Assert.False(Row(t).HasSizedRuns);
+ Assert.Equal(" x", Text(t));
+ Assert.Equal(1, Row(t)[0].Width);
+ }
+
+ [Fact]
+ public void A_multi_character_block_beside_others_is_erased_whole_and_alone()
+ {
+ var t = Fresh();
+ // The same sizing all three times, so they share one span -- the middle block just carries
+ // two characters in its two columns.
+ t.Write(Sized("w=2", "甲") + Sized("w=2", "乙丙") + Sized("w=2", "丁"));
+ Assert.Equal(new[] { (0, 6) }, Spans(t));
+
+ t.Write($"{Esc}[1;4Hx");
+
+ Assert.Equal("甲 x丁", Text(t));
+ Assert.Equal(new[] { (0, 2), (4, 6) }, Spans(t));
+ }
+
+ [Fact]
+ public void Scaled_blocks_from_one_sequence_are_separate_blocks()
+ {
+ var t = Fresh();
+ t.Write(Sized("s=2", "abc"));
+ Assert.Equal(new[] { (0, 6) }, Spans(t));
+
+ t.Write($"{Esc}[1;3Hx");
+
+ Assert.Equal("a", Row(t)[0].Content);
+ Assert.Equal("x", Row(t)[2].Content);
+ Assert.Equal(" ", Row(t)[3].Content);
+ Assert.Equal("c", Row(t)[4].Content);
+ Assert.Equal(new[] { (0, 2), (4, 6) }, Spans(t));
+ }
+
+ [Fact]
+ public void Writing_the_second_half_of_a_scaled_block_erases_all_of_it_and_only_it()
+ {
+ var t = Fresh();
+ t.Write(Sized("s=2", "abc"));
+
+ // Column 3 is the second half of b. The wide-cell repair has blanked b's first cell to one
+ // column before the run is told, so the block's start has to come from a's end.
+ t.Write($"{Esc}[1;4HX");
+
+ Assert.Equal("a", Row(t)[0].Content);
+ Assert.Equal(" ", Row(t)[2].Content);
+ Assert.Equal("X", Row(t)[3].Content);
+ Assert.Equal("c", Row(t)[4].Content);
+ Assert.Equal(new[] { (0, 2), (4, 6) }, Spans(t));
+
+ // Nothing hangs over column 2 of the row below any more, so text lands there.
+ t.Write($"{Esc}[2;3HY");
+ Assert.Equal("Y", Row(t, 1)[2].Content);
+ }
+
+ [Fact]
+ public void Scaled_blocks_of_different_widths_are_told_apart_by_their_cells()
+ {
+ var t = Fresh();
+ t.Write(Sized("s=2", "a甲b")); // blocks of 2, 4 and 2 columns
+ Assert.Equal(new[] { (0, 8) }, Spans(t));
+
+ // Column 4 is the third column of the four-column block.
+ t.Write($"{Esc}[1;5Hx");
+
+ Assert.Equal("a", Row(t)[0].Content);
+ Assert.Equal(" ", Row(t)[2].Content);
+ Assert.Equal(1, Row(t)[2].Width);
+ Assert.Equal("x", Row(t)[4].Content);
+ Assert.Equal(" ", Row(t)[5].Content);
+ Assert.Equal("b", Row(t)[6].Content);
+ Assert.Equal(new[] { (0, 2), (6, 8) }, Spans(t));
+ }
+
+ [Fact]
+ public void Erasing_the_row_under_a_tall_block_takes_only_the_block_above_it()
+ {
+ var t = Fresh();
+ t.Write(Sized("s=2", "abc"));
+
+ // Row 2, column 3 is under the second block's lower half.
+ t.Write($"{Esc}[2;3H{Esc}[X");
+
+ Assert.Equal("a", Row(t)[0].Content);
+ Assert.Equal(" ", Row(t)[2].Content);
+ Assert.Equal("c", Row(t)[4].Content);
+ Assert.Equal(new[] { (0, 2), (4, 6) }, Spans(t));
+ Assert.True(t.Buffer.HasMultiRowSizedRuns);
+ }
+
+ [Fact]
+ public void A_block_written_after_a_split_joins_the_span_again()
+ {
+ var t = Fresh();
+ t.Write(Blocks("w=2", "甲乙丙"));
+ t.Write($"{Esc}[1;5H ");
+
+ t.Write($"{Esc}[1;5H" + Sized("w=2", "丁"));
+
+ Assert.Equal("甲乙丁", Text(t));
+ Assert.Equal(new[] { (0, 6) }, Spans(t));
+ }
+
+ /// The bytes OpenCode wrote, colours and cursor visibility left out of the first chunk.
+ [Fact]
+ public void The_captured_OpenCode_backspace_deletes_one_character()
+ {
+ var t = Fresh(cols: 80, rows: 24);
+ var column = 17;
+ foreach (var c in "甲乙中文测试")
+ {
+ t.Write($"{Esc}[17;{column}H" + Sized("w=2", c.ToString()));
+ column += 2;
+ }
+ Assert.Equal(" 甲乙中文测试", Text(t, 16));
+
+ t.Write($"{Esc}[?2026h{Esc}[?25l{Esc}[17;27H"
+ + $"{Esc}[38;2;255;255;255m{Esc}[48;2;30;30;30m "
+ + $"{Esc}[0m{Esc}[0m{Esc}[17;27H{Esc}[?25h{Esc}[?2026l");
+
+ Assert.Equal(" 甲乙中文测", Text(t, 16));
+ }
+
+ [Theory]
+ [InlineData("\b\b\u001b[K")]
+ [InlineData("\u001b[2D\u001b[K")]
+ [InlineData("\u001b[2D\u001b[2X")]
+ [InlineData("\u001b[2D\u001b[2P")]
+ [InlineData("\u001b[1;5H ")]
+ public void Ordinary_wide_text_deletes_one_character(string delete)
+ {
+ var t = Fresh();
+ t.Write("甲乙丙");
+
+ t.Write(delete);
+
+ Assert.Equal("甲乙", Text(t));
+ }
+}
diff --git a/src/XTerm.NET/Buffer/BufferLine.cs b/src/XTerm.NET/Buffer/BufferLine.cs
index bc4a051..9c90fc5 100644
--- a/src/XTerm.NET/Buffer/BufferLine.cs
+++ b/src/XTerm.NET/Buffer/BufferLine.cs
@@ -822,7 +822,8 @@ public bool TryGetSizedRunAt(int column, out LineSizedRun run)
/// only the printer knows what sizing was in force for the text it just wrote.
/// Adjacent columns with identical sizing are joined into one span. Where one scaled block
/// ends and the next begins is recoverable from the cells — the first cell of a block carries its
- /// full width and the rest are zero-width — so the span does not have to say it twice.
+ /// full width and the rest are zero-width — so the span does not have to say it twice. Erasing
+ /// reads those edges back, so overwriting one block of a span erases that block alone.
///
internal void NoteSizedRun(int column, int count, Common.TextSizing sizing)
{
@@ -904,11 +905,23 @@ private void EraseSizedRunsOver(int column, int count, bool blankAll = false, in
if (run.Rows <= reachingRows)
continue;
+ // The span may hold several blocks that only share a sizing, and only the ones the range
+ // touches are erased. Their edges come from the cells, which the caller may already have
+ // overwritten inside the range -- so each edge is found from the outside of it.
+ var eraseStart = BlockStartAt(run, column);
+ var eraseEnd = BlockEndAt(run, end);
+
_sizedRuns.RemoveAt(i);
+ // Back in place, so the list stays left to right.
+ if (eraseEnd < run.EndColumn)
+ _sizedRuns.Insert(i, new LineSizedRun(eraseEnd, run.EndColumn - eraseEnd, run.Sizing));
+ if (eraseStart > run.Column)
+ _sizedRuns.Insert(i, new LineSizedRun(run.Column, eraseStart - run.Column, run.Sizing));
+
// What is left of the block becomes spaces. Its own attributes are kept, so a run erased
// out of a coloured background does not punch a hole in it.
- for (int c = Math.Max(0, run.Column); c < run.EndColumn && c < _length; c++)
+ for (int c = Math.Max(0, eraseStart); c < eraseEnd && c < _length; c++)
{
if (!blankAll && c >= column && c < end)
continue;
@@ -925,6 +938,72 @@ private void EraseSizedRunsOver(int column, int count, bool blankAll = false, in
_sizedRuns = null;
}
+ ///
+ /// The first column of the block in that covers ,
+ /// or itself when it begins one.
+ ///
+ ///
+ /// With an explicit w every block in the span is s * w columns, since they
+ /// share a sizing, so the edge is arithmetic. With w=0 each grapheme is its own block of
+ /// s times its width, and the edge is read from the cells LEFT of the range: the block's
+ /// first cell carries its full width and the rest are zero-width continuations.
+ /// The cells are read past one that is not a multiple of s wide, because the print
+ /// path's wide-cell repair has already turned the other half of a two-column cell into a
+ /// one-column space by the time this runs. At s=1 that space cannot be told from a block,
+ /// and stays in the span as one -- a blank either way.
+ ///
+ private int BlockStartAt(LineSizedRun run, int column)
+ {
+ if (column <= run.Column)
+ return run.Column;
+
+ var sizing = run.Sizing;
+ if (sizing.Width > 0)
+ {
+ var cols = sizing.Scale * sizing.Width;
+ return column - (column - run.Column) % cols;
+ }
+
+ for (int c = Math.Min(column, _length) - 1; c >= run.Column; c--)
+ {
+ var width = _cells[c].Width;
+ // A block ending short of the column means the cells between hold no first cell of
+ // their own: they are the block whose first cell the repair took, and it starts where
+ // this one ends.
+ if (width > 0 && width % sizing.Scale == 0)
+ return c + width > column ? c : c + width;
+ }
+
+ return run.Column;
+ }
+
+ ///
+ /// One past the last column of the block in that covers
+ /// - 1. The mirror of , reading the cells
+ /// from on: what is there before the next block's first cell belongs to a
+ /// block that started inside the range.
+ ///
+ private int BlockEndAt(LineSizedRun run, int end)
+ {
+ if (end >= run.EndColumn)
+ return run.EndColumn;
+
+ var sizing = run.Sizing;
+ if (sizing.Width > 0)
+ {
+ var cols = sizing.Scale * sizing.Width;
+ var into = (end - run.Column) % cols;
+ return into == 0 ? end : end + cols - into;
+ }
+
+ var c = end;
+ while (c < run.EndColumn && c < _length
+ && (_cells[c].Width == 0 || _cells[c].Width % sizing.Scale != 0))
+ c++;
+
+ return c < _length ? c : run.EndColumn;
+ }
+
///
/// The distinct images this line shows.
///