Files
sbox-public/engine/Sandbox.Test.Unit/UI/Text/Justification.cs
Garry Newman d27c236689 UI additions and optimizations (#4968)
ADDED
css min(), max() and clamp() for lengths
css-wide keywords inherit, initial, unset, revert (incl. shorthands)
added currentColor keyword to css
colors can parse oklch(), lab() and hwb()
added css text-align: justify
css white-space: pre-wrap and break-spaces
added css word-break: break-word
parses css image-rendering: crisp-edges (point sampling)
added css inset shorthand
now parses 1–4 values in the css border-width shorthand
added css transition-duration, -delay, -property and -timing-function longhands
css :has() now supports descendant selectors
added css opacity percentages
added css overflow: auto (maps to scroll)
added css gap: normal
added css filter: none
css transform: scale() now takes comma-separated args
css animations now accept ms units
added more css justify-content, align-* and text-align keywords (start, end, …)
parses css dvh/svh/lvh/dvw viewport units (treated as vh/vw)
parses css object-fit: scale-down (treated as contain)
added css margin-block / margin-inline (+ the logical margin sides)
added css padding-block / padding-inline (+ the logical padding sides)
added css inset-block / inset-inline (+ the logical inset sides)
added the css flex-flow shorthand
added the css font shorthand
added css font-size keywords (xx-small … xxx-large)
css letter-spacing and word-spacing now accept normal
added css font-smooth: none
css aspect-ratio now accepts the auto form
css font-family now maps generic families (serif, sans-serif, monospace)
added css flex-flow
added css font

FIXED
fixed css !important dropping the whole declaration
css line-height without units is now a multiplier
fixed css calc() division and full-expression parsing
fixed the css flex shorthand for single-number and three-value forms
fixed the css transition shorthand when the property is omitted
css font-family now uses the first family in a comma stack
colors now parse fully in the css background shorthand
an unknown css @-rule no longer drops the whole stylesheet
css variables no longer match on partial tokens
injected css variables are kept across stylesheet hotloads
fixed css selector specificity overflowing when comparing rules
css background: none now resets the background (image and colour)
css filter: none now overrides a filter set by a base class
css transform: none now overrides a transform set by a base class

IMPROVED
css rule matching is ~3–5× faster on large stylesheets (rules indexed by class)
~2× fewer allocations in css transitions (snapshot styles once, not per property)
finished css animations no longer re-layout every frame
less GC in css sibling/child selector queries (no list copies or wrappers)
less GC diffing active css rules (no LINQ or hashsets)
faster, lower-GC Yoga layout wrapper
css stylesheet hotload now watches newly imported files
2026-06-02 19:19:23 +01:00

87 lines
2.6 KiB
C#

using System;
using Topten.RichTextKit;
namespace UITest.Text;
/// <summary>
/// Rendering-level coverage for text-align: justify, exercised directly through RichTextKit's
/// layout so we verify words are actually spread to fill the line (not just that the style parses).
/// </summary>
[TestClass]
public class Justification
{
const float MaxWidth = 140;
/// <summary>
/// Lays out a short multi-word string that wraps to several lines under the given alignment.
/// </summary>
static TextBlock Build( TextAlignment alignment )
{
var tb = new TextBlock();
tb.MaxWidth = MaxWidth;
tb.Alignment = alignment;
var style = new Style { FontFamily = "Arial", FontSize = 16 };
tb.AddText( "aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo pp qq rr ss tt uu vv", style );
tb.Layout();
return tb;
}
/// <summary>
/// The right edge of a line's visible (non-trailing-whitespace) content.
/// </summary>
static float LineRight( TextLine line )
{
float right = 0;
foreach ( var run in line.Runs )
{
if ( run.RunKind != FontRunKind.Normal )
continue;
right = MathF.Max( right, run.XCoord + run.Width );
}
return right;
}
/// <summary>
/// A justified line (other than the last) should be stretched to fill the available width.
/// </summary>
[TestMethod]
public void JustifyFillsFirstLine()
{
var tb = Build( TextAlignment.Justify );
Assert.IsTrue( tb.Lines.Count >= 2, $"expected the text to wrap, got {tb.Lines.Count} line(s)" );
Assert.IsTrue( LineRight( tb.Lines[0] ) >= MaxWidth - 1f, $"justified line only reached {LineRight( tb.Lines[0] )} of {MaxWidth}" );
}
/// <summary>
/// The same line is wider when justified than when left-aligned (proving the spacing was added,
/// not that the text just happened to fit).
/// </summary>
[TestMethod]
public void JustifyWidensVersusLeft()
{
var justified = Build( TextAlignment.Justify );
var left = Build( TextAlignment.Left );
Assert.IsTrue( LineRight( justified.Lines[0] ) > LineRight( left.Lines[0] ) + 1f,
$"justified {LineRight( justified.Lines[0] )} vs left {LineRight( left.Lines[0] )}" );
}
/// <summary>
/// The last line of the block is not justified - it stays the same width as when left-aligned.
/// </summary>
[TestMethod]
public void JustifyLeavesLastLineRagged()
{
var justified = Build( TextAlignment.Justify );
var left = Build( TextAlignment.Left );
int last = justified.Lines.Count - 1;
Assert.AreEqual( LineRight( left.Lines[last] ), LineRight( justified.Lines[last] ), 0.5f );
Assert.IsTrue( LineRight( justified.Lines[last] ) < MaxWidth - 1f, "last line should not fill the width" );
}
}