Render text using glyphs
.NET API Solution. to display text with the desired fonts
Aspose.Font API Solution has a rich functionality to work with fonts. Conversion, glyph manipulations, detection of Latin symbols, and many more. Some of the features are linked to manipulations with glyphs like rendering texts.
A glyph is an individually designed character of a typeface or a graphical representation of a symbol/character. To learn more about this font unit, read the Introduction to Glyph article.
This page describes the option on how to display text «Hello world» using glyphs but the whole functionality is carefully described in the Using Glyph objects article. There you will find much more C# code examples, and learn the functionality of Aspose.Font for working with glyphs and the Glyph objects in particular.
To type out texts with glyphs we need:
Aspose.Font for .NET API which is a feature-rich, powerful and easy-to-use document manipulation and conversion API for C# platform.
Open the NuGet package manager, and search for Aspose.Font and install. You may also use the following command from the Package Manager Console.
Package Manager Console Command
PM> Install-Package Aspose.Font
Steps to render text «Hello world» using C#
- Create the class GlyphOutlinePainter which requires an object of type System.Drawing.Drawing2D.GraphicsPath for graphic drawing.
- Implement the Rendering.IGlyphOutlinePainter interface to draw glyphs with the help of this interface.
- The rendering subsystem will call methods of GlyphOutlinePainter to draw the glyph, so the glyph’s internal path will be rendered into the GraphicsPath object as a result of glyph rendering. To get a glyph image in bitmap representation, the resultant GraphicsPath must be drawn into System.Drawing.Bitmap object.
- To get a more detailed code example with clear explanations learn Text Rendering using TrueType Font article.
C# Code to render the phrase
using Aspose.Font.Rendering;
using Aspose.Font.RenderingPath;
class GlyphOutlinePainter: IGlyphOutlinePainter
{
private System.Drawing.Drawing2D.GraphicsPath _path;
private System.Drawing.PointF _currentPoint;
public GlyphOutlinePainter(System.Drawing.Drawing2D.GraphicsPath path)
{
_path = path;
}
public void MoveTo(MoveTo moveTo)
{
_path.CloseFigure();
_currentPoint.X = (float)moveTo.X;
_currentPoint.Y = (float)moveTo.Y;
}
public void LineTo(LineTo lineTo)
{
float x = (float)lineTo.X;
float y = (float)lineTo.Y;
_path.AddLine(_currentPoint.X, _currentPoint.Y, x, y);
_currentPoint.X = x;
_currentPoint.Y = y;
}
public void CurveTo(CurveTo curveTo)
{
float x3 = (float)curveTo.X3;
float y3 = (float)curveTo.Y3;
_path.AddBezier(
_currentPoint.X,
_currentPoint.Y,
(float)curveTo.X1,
(float)curveTo.Y1,
(float)curveTo.X2,
(float)curveTo.Y2,
x3,
y3);
_currentPoint.X = x3;
_currentPoint.Y = y3;
}
public void ClosePath()
{
_path.CloseFigure();
}
}
FAQ
1. What is a text rendering?
According to Microsoft , text rendering is the process of converting a string to a format that is readable to the user. In simpler words, it is displaying on the screen texts using font units - glyphs.
2. How is the font rendered?
To render fonts using this API solution you will need the entities of Aspose.Font.Rendering and Aspose.Font.Renderers namespaces. First, implement the IGlyphOutlinePainter methods. Then create the DrawText() Method. Create the utility method to calculate font width and call the rendering text functionality.
3. What is kerning?
Kerning is adjusting spaces between glyphs so that text would look smoother. It also helps shorten the length of the text string.