C# .NET Font glyph processing and outline extraction API
Inspect, extract, render, and manipulate individual font glyphs directly in C# .NET across TTF, OTF, CFF, and Type1 formats. Powered by Aspose.Font for .NET, this API gives developers direct low-level control over font glyph identifiers (GlyphId), vector path contours, bounding boxes, advanced metrics, and custom rendering pipelines without external dependencies like GDI+ or OS font installations.
- Format-agnostic object model: niformly process glyphs across TrueType (
.ttf), OpenType (.otf), Compact Font Format (.cff), and PostScript Type1 fonts. - 100% native managed code: Cross-platform support for .NET Core, .NET 6/7/8/9+, Windows, Linux, and macOS without relying on
System.Drawing.Commonor platform font engines. - Precise vector control: Direct access to bezier curves, line segments, and coordinate points for custom rendering engines or CAD/vector graphics workflows.
- Robust encoding tables: Built-in mapping helpers handle Unicode, Windows ANSI, Mac, and custom CMap tables automatically.
Key glyph operations at a glance
To map characters or glyph indexes to font glyph objects - use IFont.Encoding.DecodeToGid() and IFont.GetGlyphById() methods.
To handle integer IDs (GlyphUInt32Id) and string names (GlyphStringId) - use GlyphId class.
To extract exact visual vector path definitions for any glyph - use Glyph.Path, SegmentPath, and PathDefinition classes.
To measure character advance width, side bearings, and kerning - use IGlyphOutlinePainter interface
To draw glyph vector paths directly onto bitmap surfaces or SVG nodes - use IGlyphOutlinePainter, GlyphOutlineRenderer.
Extract a Glyph object by a character
Map a character (e.g., ‘A’) to its corresponding font GlyphId and extract the Glyph object:
using System;
using Aspose.Font;
using Aspose.Font.Glyphs;
// 1. Open the font file
FontDefinition fontDef = new FontDefinition(FontType.TTF, new FontFileDefinition("ttf", new FileSystemStreamSource("Arial.ttf")));
IFont font = Font.Open(fontDef);
// 2. Decode character 'A' to GlyphId
GlyphId glyphId = font.Encoding.DecodeToGid('A');
// 3. Retrieve the Glyph object from the font
Glyph glyph = font.GetGlyphById(glyphId);
if (glyph != null)
{
Console.WriteLine($"Glyph ID: {glyph.Id.ToUInt32()}");
Console.WriteLine($"Width Vector: {glyph.WidthVector.X}");
}Inspect vector outlines (path segments)
Iterate over the vector path segments (lines, curves, movements) that define the shape of a glyph:
using System;
using Aspose.Font;
using Aspose.Font.Glyphs;
// Retrieve the glyph object
GlyphId gid = font.Encoding.DecodeToGid('G');
Glyph glyph = font.GetGlyphById(gid);
// Read the vector path definition
SegmentPath path = glyph.Path;
Console.WriteLine($"Total segments in path: {path.Segments.Count}");
foreach (var segment in path.Segments)
{
// Evaluate segment type (MoveTo, LineTo, CurveTo, ClosePath)
Console.WriteLine($"Segment Type: {segment.GetType().Name}");
}Retrieve kerning and advance width between glyphs
Calculate precise positioning metrics for text layout engines:
using System;
using Aspose.Font;
using Aspose.Font.Glyphs;
// Get Glyph IDs for 'A' and 'V'
GlyphId gidA = font.Encoding.DecodeToGid('A');
GlyphId gidV = font.Encoding.DecodeToGid('V');
// Read advance width for 'A'
Glyph glyphA = font.GetGlyphById(gidA);
double advanceWidthA = glyphA.WidthVector.X;
// Extract kerning adjustment between 'A' and 'V'
double kerningValue = font.Metrics.GetKerningValue(gidA, gidV);
Console.WriteLine($"Advance Width ('A'): {advanceWidthA}");
Console.WriteLine($"Kerning ('A' -> 'V'): {kerningValue}");Installation and Setup
Install the NuGet package via the .NET CLI or Package Manager Console:
Package Manager Console Command
Bash
dotnet add package Aspose.Font
using Aspose.Font;
using Aspose.Font.Glyphs;
using Aspose.Font.Ttf;
FAQ
1. How do I convert a character into a GlyphId in C#?
Use IFont.Encoding.DecodeToGid(char) or IFont.Encoding.DecodeToGid(uint). This queries the font’s encoding/CMap tables and returns a concrete GlyphId instance.
2. Can I extract glyph outlines as raw geometric path data?
Yes. The Glyph.Path property returns a SegmentPath containing individual segments (LineTo, CurveTo, MoveTo). You can iterate over these segments to construct custom GDI+ GraphicsPath instances, WPF PathGeometry structures, or SVG <path> elements.
3. Does Aspose.Font support kerning calculations between glyphs?
Yes. Use IFont.Metrics.GetKerningValue(previousGid, currentGid) to retrieve glyph pair kerning values in font design units.