OneFont, a free, open-source .NET library from Recursive AI (MIT or Apache-2.0), loads fonts and shapes text: it picks glyphs and positions for the text. NuGet · GitHub · Docs
Formula:
px = units * fontSizePx / unitsPerEm
dotnet new console dotnet add package RecursiveAI.OneFont --version 1.4.0
The em sets the scale, not the ink size
The em square is the font’s reference grid. A face with 1,000 units per em places 1,000 design units across one em. At a font size of 16 px, those 1,000 units span 16 px; that does not make every glyph 16 px wide or tall. An advance is how far the pen moves before placing the next glyph.
Use floating-point arithmetic for fontSizePx / face.UnitsPerEm. Apply the resulting scale to the shaped advances and offsets, and round only as required by the final layout or rasterizer. If your font size is in CSS pixels, the result is in CSS pixels; apply the output’s device-pixel scale separately. OneFont returns design units, so the same run can be measured at multiple sizes without reshaping for size alone.
One shaped run at 16 px and 32 px
Use the .NET 10 SDK and the default console template above (implicit usings enabled). Download the pinned font fixture beside the project, then replace Program.cs and run it from that directory. The sample includes a trailing space so the advance and ink distinction is visible in the numbers.
curl -LO https://raw.githubusercontent.com/recursive-ai-oss/onefont-csharp/7db48bc21a7eec46272aa73f4e76acf07645db03/tests/testdata/fonts/NotoSans-Shaping-Subset.ttfusing System.Globalization; using OneFont; CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; var face = FontFace.Load(File.ReadAllBytes("NotoSans-Shaping-Subset.ttf")); var run = TextShaping.ShapeRun("AV ", face); var m = face.Metrics; Console.WriteLine($"Units per em: {face.UnitsPerEm}"); foreach (var cluster in run) Console.WriteLine($"Cluster {cluster.Start}: advance {cluster.Advance} units"); var advance = run.Sum(cluster => cluster.Advance); foreach (double size in new[] { 16.0, 32.0 }) { double scale = size / face.UnitsPerEm; Console.WriteLine($"At {size:F0} px: advance {advance * scale:F3} px"); Console.WriteLine($" Ascent {m.Ascender * scale:F3}, descent {m.Descender * scale:F3}, line height {m.LineHeight * scale:F3} px"); }
Units per em: 1000 Cluster 0: advance 599 units Cluster 1: advance 600 units Cluster 2: advance 260 units At 16 px: advance 23.344 px Ascent 17.104, descent -4.688, line height 21.792 px At 32 px: advance 46.688 px Ascent 34.208, descent -9.376, line height 43.584 px
The shaped run advances 1,459 design units, including 260 for the space. At 16 px that space alone advances 4.16 px but paints no ink. At 32 px the run advances twice as far with the same glyph placements in font units. Use the shaped advances, not isolated glyph widths added together: shaping can substitute glyphs or change positions through kerning and other positioning rules.
Pick the metric that answers your question
- Where does the next run start? Use the shaped advance. It measures pen movement, including blank space, not a tight rectangle around the glyphs.
- Where is the baseline? Use the face’s ascender and descender as inputs to your line layout. The ascender is positive above the baseline, the descender negative below it. OneFont’s natural line height is ascender minus descender plus line gap, not the height of the visible letters or a required CSS line height. In a drawing coordinate system where y increases downward, subtract the scaled ascender to get the nominal top from the baseline.
- What pixels can the text cover? For geometric ink bounds, place each glyph outline at its pen position plus its shaped offset, scale it, and bound the resulting curves. Ink may overhang an advance or leave part of it empty. For actual painted pixel bounds, measure the rasterizer’s output; OneFont does not draw or hint text.