Read the underline position and thickness from NotoSans-Regular.ttf, included in the example zip.
dotnet add package RecursiveAI.OneFontRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneFont; var bytes = File.ReadAllBytes( "NotoSans-Regular.ttf"); var m = FontFace.Load(bytes).Metrics; float scale = 20f / m.UnitsPerEm; float top = 30f - m.UnderlinePosition * scale; float height = m.UnderlineThickness * scale; Console.WriteLine((top, height));
Unzip the example, then dotnet run.
Prints (top, thickness) in pixels at a 20 px font size and a baseline at y = 30 px on a y-down screen.
(32, 1)The full program
The download also prints the position and thickness in the font’s own design units.
using System; using System.IO; using System.Globalization; using OneFont; CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; var bytes = File.ReadAllBytes( "NotoSans-Regular.ttf"); var metrics = FontFace.Load(bytes).Metrics; int position = metrics.UnderlinePosition; int thickness = metrics.UnderlineThickness; float scale = 20f / metrics.UnitsPerEm; float top = 30f - position * scale; Console.WriteLine( $"Units: {position}, {thickness}"); Console.WriteLine($"Top: {top:0.##} px"); float pixels = thickness * scale; Console.WriteLine($"Thickness: {pixels:0.##} px");
Units: -100, 50 Top: 32 px Thickness: 1 px
If the font has no readable post underline fields, both values are zero; choose a fallback if you need a visible line. The bundled Noto Sans is distributed under the SIL Open Font License included in the zip.
Variations
Compare underline offsets at two sizes
Append this to print the distance from the baseline to the top of the underline and the thickness, both in pixels. There’s no absolute y coordinate in this output.
foreach (float size in new[] { 12f, 24f }) { float factor = size / metrics.UnitsPerEm; float depth = -position * factor; float height = thickness * factor; Console.WriteLine($"{size}px: {depth:0.##}, {height:0.##}"); }
12px: 1.2, 0.6 24px: 2.4, 1.2
Related recipes
The short answer
Read m.UnderlinePosition and m.UnderlineThickness from the loaded face’s metrics. Both are in font design units, so multiply them by the font size in pixels divided by m.UnitsPerEm. On a y-down screen, subtract the scaled position from the baseline’s y coordinate. Scale the thickness without subtracting it from the baseline; it’s a distance, not a coordinate.
Where does the font store underline position and thickness?
OneFont reads both values from the OpenType post table. The specification describes underlinePosition as the suggested y-coordinate of the top of the underline. That’s not the same as the center of a stroke. If another API takes a stroke center, it’s half the scaled thickness farther down than the top edge on a y-down screen.
OneFont’s FontMetrics API describes the position as “relative to the baseline (negative is below), in design units.” The baseline comes from your layout, not from the underline field. Measuring an underscore glyph instead would give its outline, not the font’s suggested decoration metrics.
How do underline spacing and thickness change with font size?
The example font has 1,000 design units per em, so at 20 px each design unit is 0.02 px. Its −100-unit position is 2 px below the baseline, which puts the top at y = 32 px when the baseline is y = 30 px. The 50-unit thickness becomes 1 px. At 12 px the same thickness is 0.6 px; these are geometric values, not a decision about how to round or snap a line to pixels.
Use the size of the face you’re measuring, not the height of its line box. Line height can include extra spacing, while the scale is based on the em size. This example reads metrics and computes coordinates only; it doesn’t decide the length of the underline or draw it.