none
Convert String to GlyphIndices

    Question

  • Hi: What is the best way to convert a string to the unicode numbers in order to create a string of them so you can place them into the

    GlyphIndices property of a glyphrun?

    Here is the method I will be using it in.

     public static XElement DrawingTextXml(string userText, int userThickness, SolidColorBrush userColor)
            {
                //string for color
                String strColor = userColor.ToString();
                //convert user text to unicode numbers
                String convertedIndices = ConvertTextToIndices(userText);
                //glyph indices
                //String indices = "43 72 79 79 82 3 58 82 85 79 71";
                //base line origin
                String baseLine = "0,12.29";
                // font rendering EM size
                String emSize = "13.333333333333334";
                //device font name
                String dfName = "{x:Null}";
                //advanced widths
                String advancedWidths = "9.62666666666667 7.41333333333333 2.96 2.96 7.41333333333333 3.70666666666667 12.5866666666667 7.41333333333333 4.44 2.96 7.41333333333333";
                //BidiLevel
                String bidiLevel = "0";
                //font URI
                String fontURI = @"C:\WINDOWS\Fonts\TIMES.TTF"; 
    
                XElement myXml = new XElement(rootNS + "GlyphRunDrawing",
                                                            new XAttribute("ForegroundBrush", strColor),
                                                            new XElement(rootNS + "GlyphRunDrawing.GlyphRun",
                                                                new XElement(rootNS + "GlyphRun",
                                                                    new XAttribute("CaretStops", "{x:Null}"),
                                                                    new XAttribute("ClusterMap", "{x:Null}"),
                                                                    new XAttribute("IsSideways", "False"),
                                                                    new XAttribute("GlyphOffsets", "{x:Null}"),
                                                                    new XAttribute("GlyphIndices", convertedIndices),
                                                                    new XAttribute("BaselineOrigin", baseLine),
                                                                    new XAttribute("FontRenderingEmSize", emSize),
                                                                    new XAttribute("DeviceFontName", dfName),
                                                                    new XAttribute("AdvanceWidths", advancedWidths),
                                                                    new XAttribute("BidiLevel", bidiLevel),
                                                                    new XElement(rootNS + "GlyphRun.GlyphTypeface",
                                                                        new XElement(rootNS + "GlyphTypeface",
                                                                            new XAttribute("FontUri", fontURI))))));
    
    
                return myXml;
    
    
            }

    This is the method I am trying to create.

     private static String ConvertTextToIndices(String userText)
            {
                String outputString;
                Encoding unicode = Encoding.Unicode;
                Double charNumber;
                StringBuilder myIndiceString = new StringBuilder(userText.Length+1);
                myIndiceString.Length = userText.Length;
                //char myChar;
    
                //SByte[] arrayUniChar = new SByte[userText.Length+1];
                try
                {
                    // Convert the string into a byte array. 
                    //byte[] unicodeBytes = unicode.GetBytes(userText);
                    char[] charNumArray;
                    // Convert the new byte[] into a char[] and then into a string.
                    //create the char array
                   //char[] unicodeChars = new char[unicode.GetCharCount(unicodeBytes, 0, unicodeBytes.Length)];
                    //put the string into the char array
                   // unicode.GetChars(unicodeBytes, 0, unicodeBytes.Length, unicodeChars, 0);
                    //convert each char numeric value to a string and add it to the output string
                    for (int charIndex = 0; charIndex < userText.Length; charIndex++)
                    {
                        charNumber = char.GetNumericValue(userText, charIndex);
                        charNumArray[charIndex] = Convert.ToChar(charNumber);
    
                    }
                    
                    
                    
                   // string unicodeString = new string(unicodeChars);
                   // return unicodeString;
                                  
                }


    Mike Gallinger C.Tech. Cutting Edge Computing Software Developer

    Saturday, January 25, 2014 3:21 PM

Answers

  • Hi, if you want get the data of GlyphIndices, you can use GlyphTypeface.CharacterToGlyphMap.TryGetValue, please see below:

    String convertedIndices = ConvertTextToIndices(@"C:\WINDOWS\Fonts\TIMES.TTF", userText);

    private static string ConvertTextToIndices(string fontUri, string userText) { GlyphTypeface glyphTypeface = new GlyphTypeface(new Uri(fontUri)); StringBuilder buffer = new StringBuilder(); ushort glyphIndex; for (int i = 0; i < userText.Length - 1; i++) { glyphTypeface.CharacterToGlyphMap.TryGetValue(userText[i], out glyphIndex); buffer.Append(glyphIndex); buffer.Append(" "); } glyphTypeface.CharacterToGlyphMap.TryGetValue(userText[userText.Length - 1], out glyphIndex); buffer.Append(glyphIndex); return buffer.ToString(); }


    Sunday, January 26, 2014 9:40 AM

All replies

  • Glyph indices aren't the same thing as character codes, there's no way to obtain a list of glyph indices from the string alone. You need the font face too because its the one which contains the mapping between characters and glyph indices.

    In theory something like the following code should work:

    static string ConvertTextToIndices(string text, string fontUri) {
        var face = new GlyphTypeface(new Uri(fontUri));
        var cmap = face.CharacterToGlyphMap;
        var indices = new ushort[text.Length];
    
        for (var i = 0; i < text.Length; i++)
            cmap.TryGetValue(text[i], out indices[i]);
    
        return String.Join(" ", indices);
    }
    

    In practice things are more complicated and I'm not sure if such code will always work. It probably works with ASCII characters and not much else. You should leave this job to the framework, for example you could use a Glyphs element to produce a GlyphRun and take the indices from it:

    static string ConvertTextToIndices(string text, string fontUri) {
        var glyphs = new Glyphs {
            UnicodeString = "foobar",
            FontUri = new Uri(@"C:\WINDOWS\Fonts\TIMES.TTF")
        };
    
        return String.Join(" ", glyphs.ToGlyphRun().GlyphIndices);
    }
    

    Sunday, January 26, 2014 9:28 AM
    Moderator
  • Hi, if you want get the data of GlyphIndices, you can use GlyphTypeface.CharacterToGlyphMap.TryGetValue, please see below:

    String convertedIndices = ConvertTextToIndices(@"C:\WINDOWS\Fonts\TIMES.TTF", userText);

    private static string ConvertTextToIndices(string fontUri, string userText) { GlyphTypeface glyphTypeface = new GlyphTypeface(new Uri(fontUri)); StringBuilder buffer = new StringBuilder(); ushort glyphIndex; for (int i = 0; i < userText.Length - 1; i++) { glyphTypeface.CharacterToGlyphMap.TryGetValue(userText[i], out glyphIndex); buffer.Append(glyphIndex); buffer.Append(" "); } glyphTypeface.CharacterToGlyphMap.TryGetValue(userText[userText.Length - 1], out glyphIndex); buffer.Append(glyphIndex); return buffer.ToString(); }


    Sunday, January 26, 2014 9:40 AM
  • Thanks "lapheal" your code works perfect. My next job will be to figure out how advanced widths work. but for now I am greatful! :-)

    Mike Gallinger C.Tech. Cutting Edge Computing Software Developer

    Sunday, January 26, 2014 5:38 PM