feat(chat): CT-A4 — speaker names render in retail's tag colour

Campaign CT slice A4, and the first slice of Group A that shows on screen: a
player's name in a chat line now draws green while the rest of the line keeps
its message colour.

The colour is AUTHORED, not computed. Retail keeps two parallel index-selected
colour arrays on the text element and refreshes both from the same caller index
on every append (UIElement_Text::AppendStringInfoWithFont @0x00469DE0):
property 0x1B for ordinary glyphs, 0x1D for glyphs under an open tag. Property
0x1D is read exactly the way 0x1B already was, carried on ElementInfo, and
seeded onto UiText beside DefaultColor. Measured on the chat transcript
(0x2100006F / 0x10000011) as RGB(0,178,0).

It deliberately does NOT go into RetailChatColorTable. That table is the
runtime-built per-LogTextType mapping; the tag colour is per-element authored
data, and filing it there would put it somewhere it would look right in tests
and be wrong in principle.

RunsForFragment is the load-bearing piece and is pure. Wrapping can drop the
space it broke on, so a fragment is NOT simply the next N characters of the
line — BuildLines locates each fragment in the source text to keep the span
offsets honest, and the mapper clips spans to the fragment window. A tag
straddling a wrap break is therefore split across both fragments and stays
green on both, instead of changing colour mid-word.

Two guards worth naming. A fragment containing no tag returns NULL rather than
a single-run list, so the overwhelming majority of lines keep the existing flat
draw path untouched. And an element authoring no 0x1D falls back to the line
colour, so a name never renders in a colour nobody chose.

The run/fragment contract is property-tested across every substring of a tell
line, because CT-A1's RunsMatchLine refuses mismatched runs by silently falling
back to flat text — a mapping bug here would degrade quietly rather than fail.

Solution builds clean; full hermetic gate green.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Erik 2026-08-21 07:31:20 +02:00
parent 44fb74f8a6
commit 53395e4de4
6 changed files with 318 additions and 2 deletions

View file

@ -0,0 +1,161 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using AcDream.App.UI;
using AcDream.App.UI.Layout;
using AcDream.Core.Chat;
using AcDream.UI.Abstractions.Panels.Chat;
namespace AcDream.App.Tests.UI.Layout;
/// <summary>
/// Campaign CT slice A4: a tagged speaker name draws in the element's authored
/// TAG colour while the rest of its line keeps the message colour.
/// </summary>
public sealed class ChatTranscriptRunsTests
{
private static readonly Vector4 LineColor = new(0.8f, 0.8f, 0.8f, 1f);
/// <summary>Measured from the installed dats: chat 0x2100006F / 0x10000011.</summary>
private static readonly Vector4 TagGreen = new(0f, 178f / 255f, 0f, 1f);
/// <summary>10px per character, so wrap points are predictable.</summary>
private static float Measure(string text) => text.Length * 10f;
private static IReadOnlyList<ChatTextSpan> TellSpans(string name, string rest)
=> new[]
{
new ChatTextSpan(name, new ChatTextTag("Tell", "IIDString", $"1342177290:{name}")),
new ChatTextSpan(rest, null),
};
[Fact]
public void TheNameTakesTheTagColourAndTheRestTakesTheLineColour()
{
IReadOnlyList<UiText.TextRun>? runs = ChatTranscriptRenderer.RunsForFragment(
TellSpans("Dww", " tells you"),
fragmentStart: 0,
fragmentLength: "Dww tells you".Length,
LineColor,
TagGreen);
Assert.NotNull(runs);
Assert.Equal(2, runs!.Count);
Assert.Equal(("Dww", TagGreen), (runs[0].Text, runs[0].Color));
Assert.Equal((" tells you", LineColor), (runs[1].Text, runs[1].Color));
}
[Fact]
public void AFragmentWithNoTagInItGetsNoRunsAtAll()
{
// A single-colour fragment must take the ordinary flat draw path
// rather than a one-run list that means the same thing.
IReadOnlyList<ChatTextSpan> spans = TellSpans("Dww", " tells you, hello there");
Assert.Null(ChatTranscriptRenderer.RunsForFragment(
spans,
fragmentStart: 10, // well past the name
fragmentLength: 5,
LineColor,
TagGreen));
}
[Fact]
public void ATagStraddlingAWrapBreakIsSplitAcrossBothFragments()
{
// The name is long enough to be cut in half by the wrap; both halves
// must still be green, or a name would change colour mid-word.
IReadOnlyList<ChatTextSpan> spans = TellSpans("Bartholomew", " tells you");
IReadOnlyList<UiText.TextRun>? first = ChatTranscriptRenderer.RunsForFragment(
spans, fragmentStart: 0, fragmentLength: 5, LineColor, TagGreen);
IReadOnlyList<UiText.TextRun>? second = ChatTranscriptRenderer.RunsForFragment(
spans, fragmentStart: 5, fragmentLength: 6, LineColor, TagGreen);
Assert.Equal("Barth", Assert.Single(first!).Text);
Assert.Equal(TagGreen, first![0].Color);
Assert.Equal("olomew", Assert.Single(second!).Text);
Assert.Equal(TagGreen, second![0].Color);
}
[Fact]
public void RunsAlwaysReproduceTheFragmentTheyCover()
{
// UiText.RunsMatchLine refuses to draw runs that disagree with the
// line, so a mapping bug here would silently fall back to flat text
// rather than fail loudly. Assert the contract directly.
IReadOnlyList<ChatTextSpan> spans = TellSpans("Dww", " tells you, \"hi\"");
string line = string.Concat(spans.Select(s => s.Text));
for (int start = 0; start < line.Length; start++)
{
for (int len = 1; len <= line.Length - start; len++)
{
IReadOnlyList<UiText.TextRun>? runs =
ChatTranscriptRenderer.RunsForFragment(
spans, start, len, LineColor, TagGreen);
if (runs is null)
continue;
string fragment = line.Substring(start, len);
Assert.True(
UiText.RunsMatchLine(runs, fragment),
$"runs disagree with fragment [{start}..{start + len})");
}
}
}
[Fact]
public void BuildLines_EmitsOneRunEntryPerWrappedFragment()
{
// The run list is index-aligned with the lines the transcript draws;
// if the two ever fall out of step, names colour on the wrong rows.
var detailed = new List<FormattedLine>
{
new("Dww tells you, hello there friend", ChatKind.Tell, null, 0x03u,
TellSpans("Dww", " tells you, hello there friend")),
new("Welcome.", ChatKind.System, null, 0x05u),
};
var runs = new List<IReadOnlyList<UiText.TextRun>?>();
List<UiText.Line> lines = ChatTranscriptRenderer.BuildLines(
detailed,
maxW: 150f, // forces the first entry to wrap
Measure,
accept: null,
defaultColor: LineColor,
tagColor: TagGreen,
runsPerLine: runs);
Assert.True(lines.Count > 2, "the first line should have wrapped");
Assert.Equal(lines.Count, runs.Count);
// The very first fragment holds the name, so it is the one with runs.
Assert.NotNull(runs[0]);
Assert.Equal(TagGreen, runs[0]![0].Color);
// The untagged system line never gets runs.
Assert.Null(runs[^1]);
}
[Fact]
public void BuildLines_WithoutATagColourFallsBackToTheLineColour()
{
// An element that authors no 0x1D must not render the name in a
// colour nobody chose; it renders like the rest of the line.
var detailed = new List<FormattedLine>
{
new("Dww tells you", ChatKind.Tell, null, 0x03u,
TellSpans("Dww", " tells you")),
};
var runs = new List<IReadOnlyList<UiText.TextRun>?>();
ChatTranscriptRenderer.BuildLines(
detailed, maxW: 1000f, Measure, accept: null,
defaultColor: LineColor, tagColor: null, runsPerLine: runs);
IReadOnlyList<UiText.TextRun> line = Assert.Single(runs)!;
Assert.All(line, run => Assert.Equal(line[0].Color, run.Color));
}
}