Use GetLeftSideBearing to find where the glyph j starts relative to its drawing origin. Use sans.ttf from the zip.
dotnet add package RecursiveAI.OneFontRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneFont; var face = FontFace.Load(File.ReadAllBytes( "sans.ttf")); int glyph = face.GetGlyphIndex('j'); if (glyph == 0) throw new Exception("No j glyph."); int bearing = face.GetLeftSideBearing(glyph); Console.WriteLine(bearing);
Unzip the example, then dotnet run.
Prints the bearing in font units. A negative value means the glyph extends left of its origin.
-55The full program
using OneFont; var face = FontFace.Load(File.ReadAllBytes( "sans.ttf")); int glyph = face.GetGlyphIndex('j'); if (glyph == 0) throw new Exception("No j glyph."); int bearing = face.GetLeftSideBearing(glyph); Console.WriteLine(bearing); Console.WriteLine($"Advance: {face.GetAdvance(glyph)}"); Console.WriteLine($"Units per em: {face.UnitsPerEm}");
-55 Advance: 258 Units per em: 1000
If the font has no glyph for j, GetGlyphIndex returns 0; the program stops instead of reading the missing glyph’s metrics. The zip includes a Noto Sans subset with its SIL Open Font License (OFL.txt).
Variations
Convert the bearing to pixels
Append this to the snippet at the top for a font size of 20 pixels.
double pixels = bearing * (20.0 / face.UnitsPerEm); Console.WriteLine(FormattableString.Invariant($"{pixels:G} px"));
-1.1 pxRelated recipes
- Measure glyph ink bounds
- Check if a font supports a character
- Read a glyph pair’s kerning adjustment
The short answer
Map a Unicode scalar to a glyph ID with face.GetGlyphIndex, then pass the ID to face.GetLeftSideBearing. The result is the signed value from the OpenType hmtx horizontal metrics table, not the glyph’s width. For a pixel offset, multiply by the font size in pixels divided by face.UnitsPerEm.
What is a left side bearing in a font?
The bearing measures where the glyph starts relative to the point you draw it from. Put the origin at X = 0 in the example and the left edge of j reaches X = −55 in font units. A positive bearing leaves space between the origin and the glyph’s left edge; a negative bearing lets the glyph extend back past the origin. This can matter if you position the first glyph of a label against a clipping edge.
The horizontal glyph metrics describe three different measurements. Use the bearing to learn where the left edge sits relative to the origin. Use the outline’s bounds if you also need the right, top, or bottom edge. Use GetAdvance to learn where the next glyph’s origin would go before any pair or run positioning. For the example j, the left edge is at −55, but the next origin advances by +258. Neither number is the ink width.
At a size of 20 pixels, each unit in this face (1,000 units per em) is 0.02 pixels. The −55 bearing thus becomes −1.1 pixels. That’s a scaled design metric, not a measurement of the first colored pixel in a rendered image.
How do you find a glyph in a font?
A character’s Unicode value identifies the character, not its slot in a font. GetGlyphIndex reads that face’s Unicode character map. For j, the C# character literal supplies the scalar directly. For a supplementary Unicode character, supply its full scalar value, not one half of a UTF-16 surrogate pair.
Map the character and read its metric from the same FontFace: the same numeric glyph ID can mean something else in a different font. A result of 0 from GetGlyphIndex means the face lacks that character, not that the character has a bearing of zero. If you already have a glyph ID from a shaped run, use the ID and the face that produced it; mapping the original character again can miss the substituted glyph that is actually being drawn.