Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aspose-com-gists/d7ed52e575cbd6a5aa75b9e7e50ddc07 to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-gists/d7ed52e575cbd6a5aa75b9e7e50ddc07 to your computer and use it in GitHub Desktop.
Glyph retrieval, outlines, point coordinates, kerning and composite glyphs.

Glyph Objects Examples

Glyph advances, path point coordinates and pair kerning.

Status

Three documentation examples are implemented in GlyphObjectsTests (four test cases). Runtime verification is pending the user-run tests.

Running the examples

From the repository root:

dotnet test src/Aspose.Font.Net/Aspose.Font.Tests.csproj --filter "TestCategory=GlyphExamples"

All examples use tests-data/Aspose.Font/TTF/LatoWeb-Regular.ttf. BaseTests supplies the license and DataDir. Font loading and glyph selection are test setup outside the snippet markers. When reusing the snippets, supply a loaded Font; the point example also requires a nonempty Glyph. The examples produce no output files.

  • Sum glyph advances for Hello world at size 10, without pair kerning. The fixture's hmtx/head tables give an expected width of 50.99.
  • Collect integer endpoint and Bezier control-point coordinates for A and S. Casting truncates fractional coordinates; the list does not group contours or process ClosePath.
  • Query kerning for A/C. The supplied font's horizontal kern table has no pair adjustment, so the expected value is zero.

Assertions remain outside all snippet markers. The source page has no separate executable composite-glyph example.

Related Documentation

Related Resources

Requirements

  • .NET 5.0+, .NET Core, or .NET Framework
  • Aspose.Font for .NET library
Aspose.Font for .NET – Glyph Objects Examples
// Calculate text width from glyph advances
// Learn more: https://docs.aspose.com/font/net/developer-guide/using-glyph-objects/
const string text = "Hello world";
const int fontSize = 10;
double width = 0;
// Sum glyph advances at the requested size, without pair kerning.
foreach (char symbol in text)
{
GlyphId gid = font.Encoding.UnicodeToGid(symbol);
Glyph glyph = font.GetGlyphById(gid);
width += ((double)glyph.WidthVectorX / font.Metrics.UnitsPerEM) * fontSize;
}
// Collect integer coordinates from a glyph path
// Learn more: https://docs.aspose.com/font/net/developer-guide/using-glyph-objects/
List<Point> points = new List<Point>();
IPathSegment? prevSegment = null;
// Collect endpoints and Bezier control points without grouping contours.
foreach (IPathSegment segment in glyph.Path.Segments)
{
if (segment is LineTo || segment is CurveTo)
{
if (prevSegment is MoveTo moveTo)
{
AddPoint((int)moveTo.X, (int)moveTo.Y, points);
}
if (segment is LineTo line)
{
AddPoint((int)line.X, (int)line.Y, points);
}
else if (segment is CurveTo curve)
{
AddPoint((int)curve.X1, (int)curve.Y1, points);
AddPoint((int)curve.X2, (int)curve.Y2, points);
AddPoint((int)curve.X3, (int)curve.Y3, points);
}
}
prevSegment = segment;
}
void AddPoint(int x, int y, List<Point> target)
{
target.Add(new Point(x, y));
}
// Get kerning for the glyphs associated with A and C
// Learn more: https://docs.aspose.com/font/net/developer-guide/using-glyph-objects/
double kerning = font.Metrics.GetKerningValue(
font.Encoding.UnicodeToGid('A'), font.Encoding.UnicodeToGid('C'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment