Візуалізація тексту за допомогою шрифту Type1 | C++

Візуалізація тексту

Щоб відобразити текст, підсистема відтворення вимагає впровадження інтерфейсу Aspose.Font.Rendering.IGlyphOutlinePainter для малювання гліфа. Цього можна досягти за допомогою наступних кроків.

  1. Реалізуйте методи IGlyphOutlinePainter, створивши клас GlyphOutlinePainter, якому потрібен об’єкт типу System.Drawing.Drawing2D.GraphicsPainter для цілей графічного малювання. Реалізація показана нижче.
 1For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
 2RenderingText::GlyphOutlinePainter::GlyphOutlinePainter(System::SharedPtr<System::Drawing::Drawing2D::GraphicsPath> path)
 3{
 4    _path = path;
 5}
 6
 7void RenderingText::GlyphOutlinePainter::MoveTo(System::SharedPtr<Aspose::Font::RenderingPath::MoveTo> moveTo)
 8{
 9    _path->CloseFigure();
10    _currentPoint.set_X((float)moveTo->get_X());
11    _currentPoint.set_Y((float)moveTo->get_Y());
12}
13
14void RenderingText::GlyphOutlinePainter::LineTo(System::SharedPtr<Aspose::Font::RenderingPath::LineTo> lineTo)
15{
16    float x = (float)lineTo->get_X();
17    float y = (float)lineTo->get_Y();
18    _path->AddLine(_currentPoint.get_X(), _currentPoint.get_Y(), x, y);
19    _currentPoint.set_X(x);
20    _currentPoint.set_Y(y);
21}
22
23void RenderingText::GlyphOutlinePainter::CurveTo(System::SharedPtr<Aspose::Font::RenderingPath::CurveTo> curveTo)
24{
25    float x3 = (float)curveTo->get_X3();
26    float y3 = (float)curveTo->get_Y3();
27    
28    _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);
29    
30    _currentPoint.set_X(x3);
31    _currentPoint.set_Y(y3);
32}
33
34void RenderingText::GlyphOutlinePainter::ClosePath()
35{
36    _path->CloseFigure();
37}
38
39System::Object::shared_members_type Aspose::Font::Examples::WorkingWithType1Fonts::RenderingText::GlyphOutlinePainter::GetSharedMembers()
40{
41    auto result = System::Object::GetSharedMembers();
42    
43    result.Add("Aspose::Font::Examples::WorkingWithType1Fonts::RenderingText::GlyphOutlinePainter::_path", this->_path);
44    result.Add("Aspose::Font::Examples::WorkingWithType1Fonts::RenderingText::GlyphOutlinePainter::_currentPoint", this->_currentPoint);
45    
46    return result;
47}
  1. Створіть метод DrawText(), який малює вказаний текст в об’єкт System.Drawing.Bitmap і зберігає отримане растрове зображення на диску. Це включатиме наступні кроки:

Допоміжні кроки для цієї стратегії

Реалізація методу DrawText показана нижче.

 1For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
 2void RenderingText::DrawText(System::String text, System::SharedPtr<IFont> font, double fontSize, System::SharedPtr<System::Drawing::Brush> backgroundBrush, System::SharedPtr<System::Drawing::Brush> textBrush, System::String outFile)
 3{
 4    //Get glyph identifiers for every symbol in text line
 5    System::ArrayPtr<System::SharedPtr<GlyphId>> gids = System::MakeArray<System::SharedPtr<Aspose::Font::Glyphs::GlyphId>>(text.get_Length());
 6    for (int32_t i = 0; i < text.get_Length(); i++)
 7    {
 8        gids[i] = font->get_Encoding()->DecodeToGid(text[i]);
 9    }
10    // set common drawing settings
11    double dpi = 300;
12    
13    double resolutionCorrection = dpi / 72;
14    // 72 is font's internal dpi
15    // prepare output bitmap
16    System::SharedPtr<System::Drawing::Bitmap> outBitmap = System::MakeObject<System::Drawing::Bitmap>(960, 720);
17    outBitmap->SetResolution((float)dpi, (float)dpi);
18    System::SharedPtr<System::Drawing::Graphics> outGraphics = System::Drawing::Graphics::FromImage(outBitmap);
19    outGraphics->FillRectangle(backgroundBrush, 0, 0, outBitmap->get_Width(), outBitmap->get_Height());
20    outGraphics->set_SmoothingMode(System::Drawing::Drawing2D::SmoothingMode::HighQuality);
21    //declare coordinate variables and previous gid
22    System::SharedPtr<GlyphId> previousGid;
23    double glyphXCoordinate = 0;
24    double glyphYCoordinate = fontSize * resolutionCorrection;
25    //loop which paints every glyph in gids
26    
27    {
28        for (System::SharedPtr<GlyphId> gid : gids)
29        {
30            // if the font contains the gid
31            if (gid != nullptr)
32            {
33                System::SharedPtr<Glyph> glyph = font->get_GlyphAccessor()->GetGlyphById(gid);
34                if (glyph == nullptr)
35                {
36                    continue;
37                }
38                
39                // path that accepts drawing instructions
40                System::SharedPtr<System::Drawing::Drawing2D::GraphicsPath> path = System::MakeObject<System::Drawing::Drawing2D::GraphicsPath>();
41                
42                // Create IGlyphOutlinePainter implementation
43                System::SharedPtr<RenderingText::GlyphOutlinePainter> outlinePainter = System::MakeObject<RenderingText::GlyphOutlinePainter>(path);
44                
45                // Create the renderer
46                System::SharedPtr<Aspose::Font::Renderers::IGlyphRenderer> renderer = System::MakeObject<Aspose::Font::Renderers::GlyphOutlineRenderer>(outlinePainter);
47                
48                // get common glyph properties
49                double kerning = 0;
50                
51                // get kerning value
52                if (previousGid != nullptr)
53                {
54                    kerning = (font->get_Metrics()->GetKerningValue(previousGid, gid) / glyph->get_SourceResolution()) * fontSize * resolutionCorrection;
55                    kerning += FontWidthToImageWith(font->get_Metrics()->GetGlyphWidth(previousGid), glyph->get_SourceResolution(), fontSize);
56                }
57                
58                // glyph positioning - increase glyph X coordinate according to kerning distance
59                glyphXCoordinate += kerning;
60                
61                // Glyph placement matrix
62                System::SharedPtr<TransformationMatrix> glyphMatrix = System::MakeObject<TransformationMatrix>(System::MakeArray<double>({fontSize * resolutionCorrection, 0, 0, -fontSize * resolutionCorrection, glyphXCoordinate, glyphYCoordinate}));
63                
64                // render current glyph
65                renderer->RenderGlyph(font, gid, glyphMatrix);
66                // fill the path
67                path->set_FillMode(System::Drawing::Drawing2D::FillMode::Winding);
68                outGraphics->FillPath(textBrush, path);
69            }
70            //set current gid as previous to get correct kerning for next glyph
71            previousGid = gid;
72        }
73        
74    }
75    //Save results
76    outBitmap->Save(outFile);
77}
  1. Створіть допоміжний метод для розрахунку ширини шрифту до ширини зображення, як показано у прикладі коду нижче.
1For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
2double RenderingText::FontWidthToImageWith(double width, int32_t fontSourceResulution, double fontSize, double dpi /* = 300*/)
3{
4    double resolutionCorrection = dpi / 72;
5    // 72 is font's internal dpi
6    return (width / fontSourceResulution) * fontSize * resolutionCorrection;
7}

Виклик функції відтворення тексту

Щоб використовувати наведені вище реалізації, наведений нижче приклад коду можна виконати з методу Main консольної програми.

 1For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
 2System::String fileName = dataDir + u"courier.pfb";
 3//Font file name with full path
 4    
 5System::SharedPtr<FontDefinition> fd = System::MakeObject<FontDefinition>(Aspose::Font::FontType::Type1, System::MakeObject<FontFileDefinition>(u"pfb", System::MakeObject<FileSystemStreamSource>(fileName)));
 6System::SharedPtr<Type1Font> font = System::DynamicCast_noexcept<Aspose::Font::Type1::Type1Font>(Aspose::Font::Font::Open(fd));
 7    
 8    
 9DrawText(u"Hello world", font, 14, System::Drawing::Brushes::get_White(), System::Drawing::Brushes::get_Black(), dataDir + u"hello1_type1_out.jpg");
10DrawText(u"Hello world", font, 14, System::Drawing::Brushes::get_Yellow(), System::Drawing::Brushes::get_Red(), dataDir + u"hello2_type1_out.jpg");
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.