---
title: "Read a glyph’s left side bearing in C#"
url: https://recursiveai.org/onefont/cookbook/read-glyph-left-side-bearing/
description: "Read the signed left side bearing for a character’s glyph in a font file with OneFont. See how it differs from advance width and convert it to pixels."
version: "1.4.0"
---

# Read a glyph’s left side bearing in C#

**onefont** is a free, open-source font and text-shaping library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneFont) · [GitHub](https://github.com/recursive-ai-oss/onefont-csharp) · [Docs](https://recursiveai.org/onefont/docs/) · [Download example (.zip)](https://recursiveai.org/onefont/cookbook/read-glyph-left-side-bearing/read-glyph-left-side-bearing.zip)

Use `GetLeftSideBearing` to find where the glyph `j` starts relative to its drawing origin. Use `sans.ttf` from the zip.

```shell
dotnet add package RecursiveAI.OneFont
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onefont/cookbook/read-glyph-left-side-bearing/read-glyph-left-side-bearing.zip), then `dotnet run`.

Prints the bearing in font units. A negative value means the glyph extends left of its origin.

```text
-55
```

## The full program

```csharp
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}");
```

```text
-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.

```csharp
double pixels = bearing * (20.0 / face.UnitsPerEm);
Console.WriteLine(FormattableString.Invariant($"{pixels:G} px"));
```

```text
-1.1 px
```

## Related recipes

- [Measure glyph ink bounds](https://recursiveai.org/onefont/cookbook/measure-glyph-ink-bounds/)
- [Check if a font supports a character](https://recursiveai.org/onefont/cookbook/font-supports-character/)
- [Read a glyph pair’s kerning adjustment](https://recursiveai.org/onefont/cookbook/read-font-kerning-pair-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](https://learn.microsoft.com/en-us/typography/opentype/spec/hmtx), 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](https://freetype.org/freetype2/docs/glyphs/glyphs-3.html) 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`](https://github.com/recursive-ai-oss/onefont-csharp/blob/7db48bc21a7eec46272aa73f4e76acf07645db03/src/OneFont/FontFace.cs#L81-L99) 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.
