A display list records what to draw. Read its rectangles and clips to write a diagnostic SVG.
dotnet add package RecursiveAI.OneRenderRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
The full program below fills the list from HTML with page.Paint(list); this snippet builds it by hand and draws only the bounds for diagnostics.
using System.Numerics; using OneRender.Painting; var list = new DisplayList(); // Each new(n) supplies a unique item ID. list.PushClipRect(new(1), new(0, 0, 40, 30)); list.PushTransform(new(2), Matrix3x2.CreateTranslation(12, 6)); list.FillRect(new(3), new(0, 0, 40, 20), PaintColor.Black); list.PopTransform(); list.PopClip(); list.Validate(); var svg = """ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 40"> """; for (int i = 0; i < list.Count; i++) { var item = list[i]; if (item.Kind != ItemKind.FillRect) continue; var (x, y, w, h) = item.RootBounds; svg += FormattableString.Invariant($""" <rect x="{x}" y="{y}" width="{w}" height="{h}"/> """); } File.WriteAllText("bounds.svg", svg + "</svg>"); Console.WriteLine("Saved bounds.svg");
Writes a black rectangle clipped to x = 12..40 and y = 6..26 in bounds.svg.
Saved bounds.svgUnzip the example, then dotnet run.
The full program
The snippet at the top draws the clipped bounds of a rectangle from a list built by hand. The full program paints the same geometry from inline HTML and CSS and writes trace.svg. It preserves the scopes with SVG groups instead of flattening them to bounds.
using System.Xml; using OneRender; using OneRender.Painting; using OneRender.Text; var page = HtmlPage.Create(""" <style> body { margin: 0 } .frame { width: 40px; height: 30px; overflow: hidden } .tile { width: 40px; height: 20px; transform: translate(12px, 6px); background: royalblue } </style> <div class="frame"><div class="tile"></div></div> """, new(new TextFontProvider(new FontCatalog())) { ViewportWidth = 64, ViewportHeight = 40 }); var list = new DisplayList(); page.Paint(list); list.Validate(); using var buffer = new StringWriter(); using (var svg = XmlWriter.Create(buffer, new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true })) { svg.WriteStartElement("svg", "http://www.w3.org/2000/svg"); svg.WriteAttributeString("viewBox", "0 0 64 40"); for (int i = 0; i < list.Count; i++) { var item = list[i]; switch (item.Kind) { case ItemKind.PushClipRect: string id = $"clip{i}"; svg.WriteStartElement("clipPath"); svg.WriteAttributeString("id", id); WriteRect(svg, item.Rect); svg.WriteEndElement(); svg.WriteStartElement("g"); svg.WriteAttributeString("clip-path", $"url(#{id})"); break; case ItemKind.PushTransform: var m = item.Matrix; svg.WriteStartElement("g"); svg.WriteAttributeString("transform", FormattableString.Invariant( $"matrix({m.M11} {m.M12} {m.M21} {m.M22} {m.M31} {m.M32})")); break; case ItemKind.PopClip: case ItemKind.PopTransform: svg.WriteEndElement(); break; case ItemKind.FillRect when item.Paint.Kind == PaintKind.Solid: WriteRect(svg, item.Rect, item.Paint.Color); break; default: throw new NotSupportedException( $"Item {i}: unsupported {item.Kind} or paint"); } } svg.WriteEndElement(); } File.WriteAllText("trace.svg", buffer.ToString()); Console.WriteLine($"trace.svg: {list.Count} paint items"); static void WriteRect(XmlWriter svg, RectF rect, PaintColor? fill = null) { if (rect.IsEmpty) return; svg.WriteStartElement("rect"); svg.WriteAttributeString("x", XmlConvert.ToString(rect.X)); svg.WriteAttributeString("y", XmlConvert.ToString(rect.Y)); svg.WriteAttributeString("width", XmlConvert.ToString(rect.Width)); svg.WriteAttributeString("height", XmlConvert.ToString(rect.Height)); if (fill is { } color) { string hex = $"#{PaintColor.ToByte(color.R):X2}" + $"{PaintColor.ToByte(color.G):X2}{PaintColor.ToByte(color.B):X2}"; svg.WriteAttributeString("fill", hex); svg.WriteAttributeString("fill-opacity", XmlConvert.ToString(color.A)); } svg.WriteEndElement(); }
trace.svg: 11 paint itemsOpen trace.svg to see a blue rectangle shifted right and down, with its right end cut off at x = 40. The count includes clip and transform commands, not just fills. If the list contains an unsupported item or a non-solid fill, this sink throws NotSupportedException before writing a new SVG.
Variations
To move the tile left instead of right, replace the full program’s transform declaration with:
transform: translate(-12px, 6px);The left clip edge now cuts off the tile. The SVG contains matrix(1 0 0 1 -12 6) for that transform.
Related recipes
The short answer
Call HtmlPage.Paint with a DisplayList, then read that list in order. For an SVG diagnostic, open a group for each rectangular clip or 2D transform, write each solid fill as a rectangle, and close the last group on each pop. Call Validate() first so an unclosed scope cannot turn into malformed output.
How do I write a custom backend for a C# HTML renderer?
Start with a page that contains shapes but no text. The full example uses an empty font catalog because it has no glyphs to draw. HtmlPage parses and lays out the markup; the backend only consumes the display list. It does not need to parse overflow or transform itself.
Process the items in paint order, from back to front. The nesting of clips and transforms matters even when a push does not draw anything on its own. In the program, a clip starts a group that references a <clipPath>; a transform starts another group with an SVG matrix. Closing an inner group leaves the outer group in effect, and closing the outer group leaves subsequent items unclipped by it. A backend without nested SVG groups needs its own stack of drawing state.
Use item.Rect when emitting geometry inside those groups. It is in the item’s local coordinates. RootBounds, used in the snippet at the top, is useful for reporting or ruling out offscreen draws. Under rotation or a nonrectangular clip it can cover pixels the shape does not touch, so don’t substitute it for the shape or apply the transform to it again.
What is a display list in rendering?
It is a record of drawing commands, not an image or a copy of the DOM. OneRender stores coordinates in CSS pixels with the origin at the top left, and lets each backend choose the output scale. That’s why the same kind of list can drive PDF output or an image: it describes what to draw without specifying pixels or PDF operators.
The diagnostic SVG deliberately stops on rounded clips, text, gradients, images, opacity groups and other unsupported commands. Skipping a draw item can lose content; skipping a state change can make every following draw wrong. Start by rejecting both. When adding a new item, use the format’s fields for that operation rather than guessing from bounds: a text run has glyph positions, for example, and an image item carries a host-provided handle, not its pixels.