Question

I'm doing some custom controls and I have some text being drawn via Glyph Runs

My problem is that °C isn't being rendered properly by my glyphs (regardless of font family) and looks like this:

Problem symbol

So my code (simplified) for the glyph run is:

var typeface = new Typeface(this.FontFamily, this.FontStyle, this.FontWeight, this.FontStretch);

...

GlyphRun glyphRun = CreateGlyphRun(typeface, "°C", 10, new Point(0,0));
dc.DrawGlyphRun(brush, glyphRun);

Where

internal static GlyphRun CreateGlyphRun(Typeface typeface, string text, double size, Point origin)
{
    if (text.Length == 0)
        return null;

    GlyphTypeface glyphTypeface;

    typeface.TryGetGlyphTypeface(out glyphTypeface)

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

    for (int n = 0; n < text.Length; n++)
    {
        var glyphIndex = (ushort)(text[n] - 29);
        glyphIndexes[n] = glyphIndex;
        advanceWidths[n] = glyphTypeface.AdvanceWidths[glyphIndex] * size;
    }

    var glyphRun = new GlyphRun(glyphTypeface, 0, false, size, glyphIndexes, origin, advanceWidths, null, null,
                                null,
                                null, null, null);
    return glyphRun;
}

I assume its some kind of localisation issue. Any help would be appreciated.

Was it helpful?

Solution

Try to replace

var glyphIndex = (ushort)(text[n] - 29);

with

var glyphIndex = glyphTypeface.CharacterToGlyphMap[text[n]];

OTHER TIPS

How about using the Glyphs class instead? The degree sign will show when using that:

<Glyphs FontUri="C:\WINDOWS\Fonts\SegoeUI.TTF" FontRenderingEmSize="80" 
    StyleSimulations="BoldSimulation" UnicodeString="It's 30°C" 
    Fill="Black" HorizontalAlignment="Center" VerticalAlignment="Center" />

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top