Place a strikethrough relative to the text baseline. The zip includes NotoSans-Regular.ttf.
dotnet add package RecursiveAI.OneFontRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneFont; var data = File.ReadAllBytes( "NotoSans-Regular.ttf"); var metrics = FontFace.Load(data).Metrics; var offset = metrics.StrikeoutPosition; var thickness = metrics.StrikeoutSize; foreach (double size in new[] { 16, 32 }) { double scale = size / metrics.UnitsPerEm; double top = 40 - offset * scale; double height = thickness * scale; Console.WriteLine( FormattableString.Invariant( $"({top:0.###}, {height:0.###})")); }
Unzip the example, then dotnet run.
Prints (top y, thickness) in pixels for 16 px and 32 px text, both on a baseline at y = 40.
(34.848, 0.8) (29.696, 1.6)
The full program
using OneFont; var data = File.ReadAllBytes( "NotoSans-Regular.ttf"); var metrics = FontFace.Load(data).Metrics; var offset = metrics.StrikeoutPosition; var thickness = metrics.StrikeoutSize; foreach (double size in new[] { 16, 32 }) { double scale = size / metrics.UnitsPerEm; double top = 40 - offset * scale; double height = thickness * scale; Console.WriteLine( FormattableString.Invariant( $"({top:0.###}, {height:0.###})")); } Console.WriteLine($"Position: {offset} design units"); Console.WriteLine($"Thickness: {thickness} design units");
(34.848, 0.8) (29.696, 1.6) Position: 322 design units Thickness: 50 design units
If StrikeoutSize is zero or negative, don’t draw a stroke of that thickness; skip it or choose a fallback. Noto Sans is included under the SIL Open Font License.
Variations
If the drawing API takes the center of a stroke instead of the top of a rectangle, append this to calculate the center y for the 32 px case.
// Centerline for a stroke-based drawing API. double factor = 32.0 / metrics.UnitsPerEm; double center = 40 - metrics.StrikeoutPosition * factor + metrics.StrikeoutSize * factor / 2; Console.WriteLine($"Stroke center: {center:0.##}");
Stroke center: 30.5Related recipes
The short answer
Read StrikeoutPosition and StrikeoutSize from FontFace.Metrics. Multiply both by fontSizeInPixels / UnitsPerEm. On a screen where y increases downward, subtract the scaled position from the baseline y to get the top edge of the rule. Its height is the scaled StrikeoutSize. You don’t need a renderer to calculate either value.
How do you set the vertical position of a strikethrough?
Start with the baseline of the text you’re decorating, not the top of its line box. The OpenType definition of yStrikeoutPosition specifies the top edge. A positive offset moves that edge above the baseline, so the screen y coordinate gets smaller. In a y-up coordinate system you’d add the offset instead.
For a filled rectangle, use the top edge as its y coordinate. Its bottom edge is top + thickness in y-down coordinates. If the drawing API centers its stroke around the given path, move the path down by half the thickness as shown in the variation. Otherwise the stroke will sit half its thickness too high. The text run determines the horizontal start and end; the two strikeout metrics only describe the vertical placement.
How thick should a strikethrough line be?
Use the scaled yStrikeoutSize from the font as the default. OneFont reads it alongside yStrikeoutPosition in the OS/2 table. Doubling the font size doubles the thickness and the distance from the baseline, but it doesn’t double the baseline y itself. Keep both values as fractions of a pixel until drawing; if you need a minimum visible stroke on a small display, choose that in the rendering code rather than changing the font metric.
For a run containing more than one face, decide whether to follow each face’s metrics or to draw one continuous line with a single face’s settings. The first option preserves each font’s placement, but the line can step where the face changes. The second keeps the line level. In both cases, use the laid-out baseline, not a fixed offset from the top of the text box.