The following forum(s) are migrating to a new home on Microsoft Q&A (Preview): Developing Universal Windows apps!

Ask new questions on Microsoft Q&A (Preview).
Interact with existing posts until December 13, 2019, after which content will be closed to all new and existing posts.

Learn More

 none
[UWP][C#]How to Determine if Glyph Exists? RRS feed

  • Question

  • When I set the glyph for a FontIcon for example in XAML:

    <FontIcon FontFamily="Segoe MDL2 Assets" Glyph="&#xE76C;">

    Or in code:

    FontIcon newFontIcon = new FontIcon(); 
    newFontIcon.FontFamily = new FontFamily("Segoe MDL2 Assets");
    newFontIcon.Glyph = "\xE76C";
    

    Sometimes the Glyph does not exist, for example "\x0000" so the icon gets rendered as a small square box.  Is there a way to determine whether a glyph exists for a given font family?  I'd like to provide a list of glyphs for the user to select, but I don't want to include any glyphs that don't exist.

    I can determine whether the glyph exists after it is displayed (checking the actual size for example), but the only way I can see to do that is to allow the user to select the glyph...then in the selection changed event check it's size...then remove it from the list...not a very good user experience.  Is there an enum, method, property, class, etc. with which I can load a font so that I can check whether the glyph exists for that font?


    Steven Edward Wood Computer Engineer

    Sunday, July 14, 2019 12:04 AM

Answers

  • Hi Steven,

    This is an easy way if you want to check if the currently entered character is supported by the font.

    public bool CheckCharacterInFontSet(char text,string fontName)
    {
        // Get the system fonts
        var systemFonts = CanvasFontSet.GetSystemFontSet();
        
        // Get the source font with font name
        var segoe = systemFonts.GetMatchingFonts(fontName, FontWeights.Normal, FontStretch.Normal, FontStyle.Normal);
        
        uint c = Convert.ToUInt32(text);
        
        if (segoe != null && segoe.Fonts.Count > 0)
        {
            // Get the font character range
            var range = segoe.Fonts[0].UnicodeRanges;
            if(range.Any(p=>c>=p.First && c<=p.Last))
            {
                return true;
            }
        }
        return false;
    }

    This requires you to install the Win2D.UWP nuget package.

    Best regards.

    Monday, July 15, 2019 5:44 AM

All replies

  • Hi Steven,

    This is an easy way if you want to check if the currently entered character is supported by the font.

    public bool CheckCharacterInFontSet(char text,string fontName)
    {
        // Get the system fonts
        var systemFonts = CanvasFontSet.GetSystemFontSet();
        
        // Get the source font with font name
        var segoe = systemFonts.GetMatchingFonts(fontName, FontWeights.Normal, FontStretch.Normal, FontStyle.Normal);
        
        uint c = Convert.ToUInt32(text);
        
        if (segoe != null && segoe.Fonts.Count > 0)
        {
            // Get the font character range
            var range = segoe.Fonts[0].UnicodeRanges;
            if(range.Any(p=>c>=p.First && c<=p.Last))
            {
                return true;
            }
        }
        return false;
    }

    This requires you to install the Win2D.UWP nuget package.

    Best regards.

    Monday, July 15, 2019 5:44 AM
  • Thank you very much...this is exactly what I needed!  To build a list of glyphs I simply need to iterate through the UnicodeRanges.  This will save me a lot of time since I would have needed to iterate and display the entire 4 Billion unsigned integer glyph space in order to get the size of each glyph...and some glyphs are non-standard sizes and some are transparent, for example the "Segoe MDL2 Assets" font glyph #9 is transparent and has a width 36 points and glyph #32 is also transparent and has a width of 6 points.  However, glyph #9 is not in a Unicode Range, but #13 is and it is also transparent but its width is 0...which won't cause any problems because glyphs in the range 0 to 32 will be the ASCII Communication markers so I don't want to include those in the list of choices that I'm presenting to the user.



    Steven Edward Wood Computer Engineer

    Wednesday, July 17, 2019 6:06 PM
  • Maybe methods like Char.IsWhitespace() and Char.IsControl() and others are useful.
    Wednesday, July 17, 2019 8:01 PM