Отрисовка текста с использованием шрифта Type1 | .NET
Contents
[
Hide
Show
]Отрисовка текста
Для визуализации текста подсистеме рендеринга требуется реализация интерфейса Aspose.Font.Rendering.IGlyphOutlinePainter для рисования глифа. Этого можно достичь, выполнив следующие шаги.
- Реализуйте методы IGlyphOutlinePainter, создав класс GlyphOutlinePainter, которому требуется объект типа System.Drawing.Drawing2D.GraphicsPath для целей графического рисования. Реализация показана ниже.
 1// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
 2class GlyphOutlinePainter : IGlyphOutlinePainter
 3{ 
 4    private System.Drawing.Drawing2D.GraphicsPath _path;
 5    private System.Drawing.PointF _currentPoint;
 6
 7    public GlyphOutlinePainter(System.Drawing.Drawing2D.GraphicsPath path)
 8    {
 9        _path = path;
10    }
11
12    public void MoveTo(MoveTo moveTo)
13    {
14        _path.CloseFigure();
15        _currentPoint.X = (float)moveTo.X;
16        _currentPoint.Y = (float)moveTo.Y;
17    }
18
19    public void LineTo(LineTo lineTo)
20    {
21        float x = (float)lineTo.X;
22        float y = (float)lineTo.Y;
23        _path.AddLine(_currentPoint.X, _currentPoint.Y, x, y);
24        _currentPoint.X = x;
25        _currentPoint.Y = y;
26    }
27
28    public void CurveTo(CurveTo curveTo)
29    {
30        float x3 = (float)curveTo.X3;
31        float y3 = (float)curveTo.Y3;
32
33        _path.AddBezier(
34                  _currentPoint.X,
35                  _currentPoint.Y,
36                  (float)curveTo.X1,
37                  (float)curveTo.Y1,
38                  (float)curveTo.X2,
39                  (float)curveTo.Y2,
40                  x3,
41                  y3);
42
43        _currentPoint.X = x3;
44        _currentPoint.Y = y3;
45    }
46
47    public void ClosePath()
48    {
49        _path.CloseFigure();
50    }
51}- Создайте метод DrawText(), который рисует указанный текст в объект System.Drawing.Bitmap и сохраняет полученное растровое изображение на диске. Это будет включать в себя следующие шаги:
- Перебрать все символы в текстовой строке.
- Получить идентификатор глифа для каждого обработанного символа — gid.
- Создайте объект типа GlyphOutlinePainter, который необходим подсистеме рендеринга для рисования текущего глифа.
- Создайте объект типа Aspose.Font.Renderers.GlyphOutlineRenderer и передайте только что созданный объект типа GlyphOutlinePainter в конструктор для GlyphOutlineRenderer. Этот объект GlyphOutlineRenderer предназначен для визуализации указанного глифа.
- Отображение текущего обработанного глифа с помощью метода GlyphOutlineRenderer.RenderGlyph(). Объект Aspose.Fonts.Matrix используется для указания координат глифа. Глиф для рендеринга задается параметром gid.
Вспомогательные шаги для этой стратегии
- Координата глифа по оси Y является постоянной для этого фрагмента кода.
- Координата глифа по оси X рассчитывается для каждого обработанного глифа.
- Координаты X и Y передаются в объект Aspose.Fonts.Matrix, который используется GlyphOutlineRenderer для рисования глифа.
- Расстояние между только что обработанными и предыдущими глифами рассчитывается на каждом шаге итерации. Это расстояние влияет на координату каждой буквы «X».
- Объект типа GlyphOutlinePainter рисует глиф с помощью GlyphOutlinePainter не непосредственно в Bitmap, а в объект GraphicsPath, который был передан в конструктор для GlyphOutlinePainter, поэтому мы используем объект типа System.Drawing.Graphics для отрисовки GraphicsPath в Bitmap.
- Метод FontWidthToImageWith(), который вычисляет ширину глифа для системы координат растрового изображения.
Реализация метода DrawText показана ниже.
 1// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
 2static void DrawText(string text, IFont font, double fontSize,
 3            Brush backgroundBrush, Brush textBrush, string outFile)
 4{
 5    //Get glyph identifiers for every symbol in text line
 6    GlyphId[] gids = new GlyphId[text.Length];
 7    for (int i = 0; i < text.Length; i++)
 8        gids[i] = font.Encoding.DecodeToGid(text[i]);
 9    // set common drawing settings
10    double dpi = 300;
11
12    double resolutionCorrection = dpi / 72; // 72 is font's internal dpi
13    // prepare output bitmap
14    Bitmap outBitmap = new Bitmap(960, 720);
15    outBitmap.SetResolution((float)dpi, (float)dpi);
16    Graphics outGraphics = Graphics.FromImage(outBitmap);
17    outGraphics.FillRectangle(backgroundBrush, 0, 0, outBitmap.Width, outBitmap.Height);
18    outGraphics.SmoothingMode = SmoothingMode.HighQuality;
19    //declare coordinate variables and previous gid
20    GlyphId previousGid = null;
21    double glyphXCoordinate = 0;
22    double glyphYCoordinate = fontSize * resolutionCorrection;
23    //loop which paints every glyph in gids
24    foreach (GlyphId gid in gids)
25    {
26        // if the font contains the gid
27        if (gid != null)
28        {
29            Glyph glyph = font.GlyphAccessor.GetGlyphById(gid);
30            if (glyph == null)
31                continue;
32
33            // path that accepts drawing instructions
34            GraphicsPath path = new GraphicsPath();
35
36            // Create IGlyphOutlinePainter implementation
37            GlyphOutlinePainter outlinePainter = new GlyphOutlinePainter(path);
38
39            // Create the renderer
40            Aspose.Font.Renderers.IGlyphRenderer renderer = new
41                Aspose.Font.Renderers.GlyphOutlineRenderer(outlinePainter);
42
43            // get common glyph properties
44            double kerning = 0;
45
46            // get kerning value
47            if (previousGid != null)
48            {
49                kerning = (font.Metrics.GetKerningValue(previousGid, gid) /
50                           glyph.SourceResolution) * fontSize * resolutionCorrection;
51                kerning += FontWidthToImageWith(font.Metrics.GetGlyphWidth(previousGid),
52                        glyph.SourceResolution, fontSize);
53            }
54
55            // glyph positioning - increase glyph X coordinate according to kerning distance
56            glyphXCoordinate += kerning;
57
58            // Glyph placement matrix
59            TransformationMatrix glyphMatrix =
60                new TransformationMatrix(
61                    new double[]
62                            {
63                                    fontSize*resolutionCorrection,
64                                    0,
65                                    0,
66                                // negative because of bitmap coordinate system begins from the top
67                                    - fontSize*resolutionCorrection,
68                                    glyphXCoordinate,
69                                    glyphYCoordinate
70                            });
71
72            // render current glyph
73            renderer.RenderGlyph(font, gid, glyphMatrix);
74            // fill the path
75            path.FillMode = FillMode.Winding;
76            outGraphics.FillPath(textBrush, path);
77        }
78        //set current gid as previous to get correct kerning for next glyph
79        previousGid = gid;
80    }
81    //Save results
82    outBitmap.Save(outFile);
83}- Создайте служебный метод для расчета ширины шрифта по ширине изображения, как показано в примере кода ниже.
1// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
2static double FontWidthToImageWith(double width, int fontSourceResulution, double fontSize, double dpi = 300)
3{
4    double resolutionCorrection = dpi / 72; // 72 is font's internal dpi
5    return (width / fontSourceResulution) * fontSize * resolutionCorrection;
6}Вызов функции «Отрисовка текста»
Чтобы использовать приведенные выше реализации, можно выполнить следующий пример кода из метода Main консольного приложения.
1// For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-.NET
2string fileName = dataDir + "courier.pfb"; //Font file name with full path
3
4FontDefinition fd = new FontDefinition(FontType.Type1, new FontFileDefinition("pfb", new FileSystemStreamSource(fileName)));
5Type1Font font = Aspose.Font.Font.Open(fd) as Type1Font;
6            
7
8DrawText("Hello world", font, 14, Brushes.White, Brushes.Black, dataDir + "hello1_type1_out.jpg");
9DrawText("Hello world", font, 14, Brushes.Yellow, Brushes.Red, dataDir + "hello2_type1_out.jpg");