GetClientRects measures each piece of a wrapped <span> so a highlight can follow the text.
dotnet add package RecursiveAI.OneRender.ImageRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneRender; using OneRender.Image; using OneRender.Interaction; using OneRender.Layout; const string html = """ <div style="width:100px"> <span id=label>First Second Third</span> </div> """; var options = new HtmlPageOptions( FontProviders.CreateDefault()); var page = InteractivePage.Create(html, options); var label = page.Page.Document .GetElementById("label"); // Eight slots here; retry if the buffer fills. Span<Rect> rects = stackalloc Rect[8]; int count = page.Layout .GetClientRects(label, rects); foreach (var r in rects[..count]) { string pos = FormattableString.Invariant( $"({r.X:F1}, {r.Y:F1})"); string size = FormattableString.Invariant( $"{r.Width:F1} × {r.Height:F1}"); Console.WriteLine($"{pos} {size}"); }
Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.
Prints two rectangles as (x, y) width × height in CSS pixels:
(8.0, 8.0) 80.4 × 17.0 (8.0, 26.0) 35.5 × 17.0
The example uses the fonts from RecursiveAI.OneRender.Image to lay out the text. If the element has no layout box, as with display: none, the query writes zero rectangles.
Variations
Get one enclosing rectangle. Use this in place of the rectangle buffer and loop when you want one outline around the whole span:
var bounds = page.Layout.GetBoundingClientRect(label); Console.WriteLine(FormattableString.Invariant($"{bounds.Width:F1} × {bounds.Height:F1}"));
Related recipes
The short answer
Lay out the HTML with InteractivePage, find the inline element, and call page.Layout.GetClientRects(element, buffer). Each rectangle describes the border box of one laid-out piece. The first rectangle in this example covers “First Second” and the second covers “Third.” Read only the elements of the buffer up to the returned count.
What’s the difference between GetClientRects and GetBoundingClientRect?
GetClientRects gives you the separate pieces in tree order. GetBoundingClientRect gives you the smallest rectangle that encloses the nonempty pieces. In the example, that larger rectangle extends past the end of “Third,” because it has to be wide enough for the first line. Use that rectangle to position a tooltip next to the whole span; use the separate rectangles when the blank area to the right of the second line shouldn’t be highlighted.
OneRender’s methods follow the distinction in CSSOM View’s geometry APIs. The rectangles describe CSS border boxes, not pixel outlines of the glyphs. Use the same layout width and fonts as the surface you’re annotating: both affect where the span breaks.
What are client rectangles relative to?
The origin is the top-left of the viewport, not the top-left of the element or document. The coordinates account for scrolling, and the widths and positions can be fractional. If you draw an overlay in device pixels, use the same CSS-to-device scale you used for the page; don’t round each rectangle to whole CSS pixels first.
The buffer is supplied by the caller. If it fills, OneRender drops any remaining rectangles and returns the number it wrote, not the total. A result equal to the buffer length might be an exact fit or might be truncated. If you need every piece for arbitrary input, retry with a larger buffer when it fills.