Render text using glyphs

C++ API Solution. to display text with the desired font.

 

Aspose.Font API Solution offers rich capabilities for working with fonts, including conversion, manipulation of glyphs, detection of Latin symbols, and more. Some of the features are related to glyph manipulation, such as text rendering.

A glyph is a unique design of a character in a typeface or a graphical representation of a symbol or character. To gain a deeper understanding of this font unit, read the Introduction to Glyph article.

This page explains how to display the text using glyphs, but a comprehensive explanation of the functionality is provided in the article Using Glyph objects . There, you will find more C++ code examples and learn about Aspose.Font capabilities for working with glyphs and Glyph objects specifically. You can also learn complete examples and data files at Aspose Github Project .

To type out texts with glyphs we need:

  • Aspose.Font for C++ API which is a feature-rich, powerful and easy-to-use document manipulation and conversion API.

  • 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

Render text with glyphs using C++

The next steps are usually required to render text with the glyphs:

  1. Load the font file into memory. Here you need to create a font object, such as a Type1Font object, and pass it in the font file path.
  2. Decode the text into its corresponding glyph IDs. For this, pass each character of the text through the font’s encoding object and decode it to its corresponding glyph ID.
  3. Get the outline of each glyph. The outline is a path that defines the shape of the character. This can be achieved using the GetGlyphOutline() Method of the Font object.
  4. Draw the outlines of each glyph using a graphics object, such as a GraphicsPath object. Just create an object that implements the IGlyphOutlinePainter interface, which is responsible for drawing the outlines of the glyphs.
  5. Display the text by using a graphics object to draw the outlines of each glyph. This can be done using the DrawPath() Method of the graphics object.

C++ Code to render text

    using Aspose::Font::Glyphs;
    using Aspose::Font::Rendering;
    using Aspose::Font::RenderingPath;
    class GlyphOutlinePainter: IGlyphOutlinePainter
    RenderingText::GlyphOutlinePainter::GlyphOutlinePainter(System::SharedPtr<System::Drawing::Drawing2D::GraphicsPath> path)
    {
        _path = path;
    }

    void RenderingText::GlyphOutlinePainter::MoveTo(System::SharedPtr<Aspose::Font::RenderingPath::MoveTo> moveTo)
    {
        _path->CloseFigure();
        _currentPoint.set_X((float)moveTo->get_X());
        _currentPoint.set_Y((float)moveTo->get_Y());
    }

    void RenderingText::GlyphOutlinePainter::LineTo(System::SharedPtr<Aspose::Font::RenderingPath::LineTo> lineTo)
    {
        float x = (float)lineTo->get_X();
        float y = (float)lineTo->get_Y();
        _path->AddLine(_currentPoint.get_X(), _currentPoint.get_Y(), x, y);
        _currentPoint.set_X(x);
        _currentPoint.set_Y(y);
    }

    void RenderingText::GlyphOutlinePainter::CurveTo(System::SharedPtr<Aspose::Font::RenderingPath::CurveTo> curveTo)
    {
        float x3 = (float)curveTo->get_X3();
        float y3 = (float)curveTo->get_Y3();
    
        _path->AddBezier(_currentPoint.get_X(), _currentPoint.get_Y(), (float)curveTo->get_X1(), (float)curveTo->get_Y1(), (float)curveTo->get_X2(), (float)curveTo->get_Y2(), x3, y3);
    
        _currentPoint.set_X(x3);
        _currentPoint.set_Y(y3);
    }

    void RenderingText::GlyphOutlinePainter::ClosePath()
    {
        _path->CloseFigure();
    }

    System::Object::shared_members_type Aspose::Font::Examples::WorkingWithTrueTypeAndOpenTypeFonts::RenderingText::GlyphOutlinePainter::GetSharedMembers()
    {
        auto result = System::Object::GetSharedMembers();
    
        result.Add("Aspose::Font::Examples::WorkingWithTrueTypeAndOpenTypeFonts::RenderingText::GlyphOutlinePainter::_path", this->_path);
        result.Add("Aspose::Font::Examples::WorkingWithTrueTypeAndOpenTypeFonts::RenderingText::GlyphOutlinePainter::_currentPoint", this->_currentPoint);
    
        return result;
    }



FAQ

1. How is the font rendered via Aspose.Font?

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.

2. What factors should you pay attention to when rendering text with glyphs?

When rendering text with glyphs, there are several important factors to consider to ensure accurate and visually appealing results like font selection, hinting, kerning and tracking, glyph metrics, color and contrast, etc.

3. What is font hinting?

Hinting can help maintain the clarity and legibility of text, particularly at smaller sizes or on low-resolution screens.