using System;
using System.Collections.Generic;
using System.IO;
using AcDream.App.UI;
using DatReaderWriter;
using DatReaderWriter.DBObjs;
using DatReaderWriter.Options;
using DatReaderWriter.Types;
using SysEnv = System.Environment;
namespace AcDream.App.Tests.UI;
///
/// Pins Font.NumHorizontalBorderPixels/NumVerticalBorderPixels
/// (Font::Serialize @0x00443650) against the real installed DAT, and the
/// plumbing that carries them from
/// onto /
/// — the field this project's font reader
/// dropped entirely before Campaign CH round 4
/// (docs/research/2026-08-10-retail-ui-text-style.md §1.4). A repo-wide
/// grep for BorderPixel returned zero hits before this fix; these tests
/// are the regression guard against that gap reappearing.
///
///
/// The live-dat tests follow 's
/// pattern: skip (not fail) when the real dats aren't present, so the suite stays
/// green in environments without the installed game.
///
///
public sealed class UiDatFontBorderPixelTests
{
// Measured values — docs/research/2026-08-10-retail-ui-text-style.md §1.3.
[Theory]
[InlineData(0x40000000u, 4u, 4u)] // 16px bold serif — chat transcript face
[InlineData(0x40000001u, 4u, 4u)] // 18px bold serif — SpewBox face (round 4)
[InlineData(0x40000002u, 3u, 3u)] // 14px bold serif
[InlineData(0x40000025u, 3u, 3u)] // 11px — the pre-round-4 SpewBox placeholder face
public void RealDatFont_HasExpectedBorderPixels(uint fontId, uint expectedHorizontal, uint expectedVertical)
{
string? datDir = ResolveDatDir();
if (datDir is null)
return;
using var dats = new DatCollection(datDir, DatAccessType.Read);
Assert.True(dats.TryGet(fontId, out Font? font), $"Font 0x{fontId:X8} not found");
Assert.NotNull(font);
Assert.Equal(expectedHorizontal, font!.NumHorizontalBorderPixels);
Assert.Equal(expectedVertical, font.NumVerticalBorderPixels);
}
[Fact]
public void RealDatFont_EveryFontWithABackgroundAtlas_HasANonZeroBorder()
{
// docs/research/2026-08-10-retail-ui-text-style.md §1.2: "every font that
// has a background atlas has border >= 3, and every font without one has
// border == 0" — the data-driven dispatch DrawStringDat's outline pass
// relies on (background plane vs 8-neighbour fallback) is exactly this
// correlation. Sweep the documented populated range (0x40000000-0x40000032).
string? datDir = ResolveDatDir();
if (datDir is null)
return;
using var dats = new DatCollection(datDir, DatAccessType.Read);
int checkedCount = 0;
for (uint id = 0x40000000u; id <= 0x40000032u; id++)
{
if (!dats.TryGet(id, out Font? font) || font is null)
continue;
checkedCount++;
bool hasBackground = font.BackgroundSurfaceDataId != 0;
bool hasBorder = font.NumHorizontalBorderPixels > 0 || font.NumVerticalBorderPixels > 0;
Assert.True(
hasBackground == hasBorder,
$"Font 0x{id:X8}: hasBackground={hasBackground} but hasBorder={hasBorder}");
}
Assert.True(checkedCount > 10, "expected the documented font sweep to find multiple populated fonts");
}
///
/// Pure plumbing check — no dat, no GL: the ctor
/// stores / verbatim onto
/// /, and existing
/// callers that omit them (pre-round-4 test fixtures) still default to zero.
///
[Theory]
[InlineData(4, 4)]
[InlineData(3, 3)]
[InlineData(0, 0)]
public void Ctor_StoresBorderPixelsVerbatim(int borderX, int borderY)
{
var font = new UiDatFont(
fgTex: 1, fgW: 64, fgH: 64,
bgTex: 2, bgW: 64, bgH: 64,
lineHeight: 16f, baselineOffset: 12f,
glyphs: new Dictionary(),
borderX: borderX, borderY: borderY);
Assert.Equal(borderX, font.BorderX);
Assert.Equal(borderY, font.BorderY);
}
[Fact]
public void Ctor_OmittedBorderPixels_DefaultToZero()
{
// Pins backward compatibility for the existing UiDatFontTests/SpewBoxControllerTests
// call sites that construct UiDatFont without borderX/borderY.
var font = new UiDatFont(
fgTex: 0, fgW: 0, fgH: 0,
bgTex: 0, bgW: 0, bgH: 0,
lineHeight: 16f, baselineOffset: 12f,
glyphs: new Dictionary());
Assert.Equal(0, font.BorderX);
Assert.Equal(0, font.BorderY);
}
private static string? ResolveDatDir()
{
string? fromEnv = SysEnv.GetEnvironmentVariable("ACDREAM_DAT_DIR");
if (!string.IsNullOrWhiteSpace(fromEnv) && Directory.Exists(fromEnv))
return fromEnv;
string defaultDir = Path.Combine(
SysEnv.GetFolderPath(SysEnv.SpecialFolder.UserProfile),
"Documents",
"Asheron's Call");
return Directory.Exists(defaultDir) ? defaultDir : null;
}
}