---
title: "Convert a TTF glyph to an SVG path in C#"
url: https://recursiveai.org/onefont/cookbook/render-glyph-outline-as-svg-path/
description: "Read a TTF glyph outline in C# and emit SVG M, L, Q, C and Z commands with a OneFont outline sink. Flip the y-axis without rasterizing the glyph."
version: "1.4.0"
---

# Convert a TTF glyph to an SVG path 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/render-glyph-outline-as-svg-path/render-glyph-outline-as-svg-path.zip)

Use `GetOutline` to export the shape of `A`, not pixels. The zip includes `sans.ttf`.

```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;
using System.Numerics;

var face = FontFace.Load(
    File.ReadAllBytes("sans.ttf"));
var outline = face.GetOutline(
    face.GetGlyphIndex('A'));
var path = new SvgPath();
outline.Write(path,
    Matrix3x2.CreateScale(1, -1));
Console.WriteLine(path);

sealed class SvgPath : IOutlineSink
{
    readonly System.Text.StringBuilder
        data = new();
    void Add(FormattableString command) =>
        data.Append(FormattableString
            .Invariant(command));
    public void MoveTo(float x, float y) =>
        Add($"M{x} {y}");
    public void LineTo(float x, float y) =>
        Add($"L{x} {y}");
    public void QuadTo(float cx, float cy,
        float x, float y) =>
        Add($"Q{cx} {cy} {x} {y}");
    public void CubicTo(float ax, float ay,
        float bx, float by, float x, float y) =>
        Add($"C{ax} {ay} {bx} {by} {x} {y}");
    public void Close() => data.Append('Z');
    public override string ToString() =>
        data.ToString();
}
```

Unzip [the example](https://recursiveai.org/onefont/cookbook/render-glyph-outline-as-svg-path/render-glyph-outline-as-svg-path.zip), then `dotnet run`.

Prints the value to put in an SVG `<path>` element’s `d` attribute:

```text
M545 0L459 -221L176 -221L91 0L0 0L279 -717L360 -717L638 0ZM352 -517Q349 -525 342 -546Q335 -567 328.5 -589.5Q322 -612 318 -624Q311 -593 302 -563.5Q293 -534 287 -517L206 -301L432 -301Z
```

`GetGlyphIndex` returns 0 for a missing character; check `face.HasGlyph` if you want to reject it rather than exporting the font’s missing-glyph outline.

## Variations

### Save a standalone SVG

Replace `Console.WriteLine(path);` with the following. The box comes from the actual outline, and the top edge is `-maxY` because the path has been flipped:

```csharp
var (minX, minY, maxX, maxY) = outline.TightBox();
float width = maxX - minX;
float height = maxY - minY;
string box = FormattableString.Invariant(
    $"{minX} {-maxY} {width} {height}");
string svg = $"""
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="{box}">
      <path d="{path}"/>
    </svg>
    """;
File.WriteAllText("glyph.svg", svg);
```

Open `glyph.svg` in a browser. This variation expects a visible outline; a space may have no segments and give a zero-sized box.

## Related recipes

- [Convert shaped text to an SVG path](https://recursiveai.org/onefont/cookbook/text-to-svg-path/)
- [Measure a glyph bounding box](https://recursiveai.org/onefont/cookbook/measure-glyph-ink-bounds/)
- [Read a TTF or OTF font file](https://recursiveai.org/onefont/cookbook/read-font-file/)

---

## The short answer

Map the character to a glyph ID with `GetGlyphIndex`, read its outline with `GetOutline`, and call `Write` with an `IOutlineSink` that writes SVG commands. `Write` replays the geometry. It doesn’t make an image or create the surrounding SVG document.

## How do I turn a TrueType outline into SVG path commands?

Map the sink calls directly to [SVG path commands](https://www.w3.org/TR/SVG2/paths.html#PathData): `MoveTo` to `M`, `LineTo` to `L`, `QuadTo` to `Q`, `CubicTo` to `C`, and `Close` to `Z`. TrueType curves are quadratic, so use `Q` without converting them to cubic curves or flattening them into lines. The sink also handles the cubic segments returned for OpenType CFF outlines. Keep separate contours and their closing commands; a glyph may have an inner contour that forms a hole. Format numbers with an invariant culture so a decimal comma doesn’t change the meaning of the path data.

Mapping a character straight to a glyph is sufficient for this single `A`. It isn’t a substitute for shaping a word. To export text that uses ligatures or localized glyph substitutions, shape first and use the resulting glyph IDs, advances, and offsets with the same font.

## Why is the glyph upside down in SVG?

OneFont’s outlines use design units with y increasing upward. The initial [SVG coordinate system](https://www.w3.org/TR/SVG2/coords.html#InitialCoordinateSystem) has y increasing downward. The example negates y before it writes any coordinates to the SVG path. After the flip, a point above the font baseline has a negative y coordinate; the `viewBox` must include it or the glyph can be clipped.

The standalone variation fits around the ink using `TightBox`, not the glyph’s advance. That’s useful for a single glyph. For a line of text, position each glyph relative to a common baseline instead of fitting a new box around every glyph. Spaces can have advances even though they supply nothing to the path.
