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; double scale = 20.0 / metrics.UnitsPerEm; Console.WriteLine(FormattableString.Invariant( $"{metrics.XHeight * scale:0.##}")); Console.WriteLine(FormattableString.Invariant( $"{metrics.CapHeight * scale:0.##}"));
Unzip the example, then dotnet run.
Prints x-height and cap height in pixels at a 20 px font size, in that order.
10.72 14.28
0 means the font doesn’t declare that height.
The full program
using OneFont; var data = File.ReadAllBytes( "NotoSans-Regular.ttf"); var metrics = FontFace.Load(data).Metrics; const double size = 20; double scale = size / metrics.UnitsPerEm; Report("x-height", metrics.XHeight, scale); Report("Cap height", metrics.CapHeight, scale); static void Report(string name, int units, double scale) { if (units == 0) { Console.WriteLine($"{name}: not declared"); return; } Console.WriteLine(FormattableString.Invariant( $"{name}: {units} units, {units * scale:0.##} px")); }
x-height: 536 units, 10.72 px Cap height: 714 units, 14.28 px
A zero field means the font didn’t declare a height; the full program reports that separately for each metric. Noto Sans is included under the SIL Open Font License.
Variations
Append this to the snippet at the top to find the font size that gives lowercase x a declared height of 12 px.
// Size this font for a 12 px x-height. if (metrics.XHeight > 0) { double size = 12.0 * metrics.UnitsPerEm / metrics.XHeight; Console.WriteLine(FormattableString.Invariant( $"Font size: {size:0.##} px")); }
Font size: 22.39 pxRelated recipes
The short answer
Read XHeight and CapHeight from FontFace.Metrics and multiply each by fontSizeInPixels / UnitsPerEm. These are the values declared by the font; OneFont does not measure glyph outlines to fill the fields. If either value is zero, treat that value as unavailable rather than as a height of zero pixels.
What is the difference between x-height and cap height in fonts?
The OpenType OS/2 specification defines x-height as the approximate height of non-ascending lowercase letters and cap height as the approximate height of uppercase letters, both above the baseline. The lowercase x and uppercase H are useful references. Neither field is a line height or the maximum extent of all the letters in the font. A lowercase g, for example, can go below the baseline without changing x-height.
At 20 px, the bundled face declares 10.72 px for x-height and 14.28 px for cap height. The 20 px applies to the em, not either letter height. When choosing a fallback font, matching the lowercase heights can make a mixed-case paragraph look more consistent. For an all-caps label, compare cap heights instead. The variation solves the same scaling equation backward to choose a font size for a target x-height.
How do you find the x-height when a font does not declare it?
OneFont reads sxHeight and sCapHeight from version 2 or later of the OS/2 table when the fields are present. A zero in one field tells you nothing about the other, so check them independently. Keep the original size if matching a height is optional; don’t divide by zero to calculate a new size.
If you need a measured fallback, use the outline of x for x-height or H for cap height. The specification recommends these reference glyphs when an optical value isn’t available. Measure from the baseline to the top of the outline, then apply the same em scale. That gives you the height of a specific glyph in that face, not recovered metadata. If the face doesn’t contain the reference glyph, leave the height unknown or choose a different face for the comparison.