Home > front end >  How can I declare GlyphRun object?
How can I declare GlyphRun object?

Time:08-01

I need to draw just a couple of digits. Is it enough to define only GlyphRun and FontRenderingEmSize? If not, please, suggest me, how to draw, for example, string "123" (how to define GlyphRun object for that). I wrote this code:

var gr = new GlyphRun();
gr.Characters = new List<char>() { '1', '2' }; //Mistake
gr.FontRenderingEmSize = 20;
var Glyph = new GlyphRunDrawing(Brushes.Black, gr);

CodePudding user response:

I found this old helper class of mine. Maybe you can make use of it.

public static class GlyphRunText
{
    public static GlyphRun Create(
        string text, Typeface typeface, double emSize, Point baselineOrigin)
    {
        GlyphTypeface glyphTypeface;

        if (!typeface.TryGetGlyphTypeface(out glyphTypeface))
        {
            throw new ArgumentException(string.Format(
                "{0}: no GlyphTypeface found", typeface.FontFamily));
        }

        var glyphIndices = new ushort[text.Length];
        var advanceWidths = new double[text.Length];

        for (int i = 0; i < text.Length; i  )
        {
            var glyphIndex = glyphTypeface.CharacterToGlyphMap[text[i]];
            glyphIndices[i] = glyphIndex;
            advanceWidths[i] = glyphTypeface.AdvanceWidths[glyphIndex] * emSize;
        }

        return new GlyphRun(
            glyphTypeface, 0, false, emSize,
            glyphIndices, baselineOrigin, advanceWidths,
            null, null, null, null, null, null);
    }
}
  • Related