Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Advanced Width Property of a Glyph

$
0
0

Hi: I am creating the xml for a user defined string. Everything works but I need to calculate the advanced widths for each character in the string. I know the font type, the indices, ect. but I need to know how I can calculate the advanced widths for each character. Attached is the method and helper method. They work if I run it using 2 characters and give fictitious advanced widths of 7 or something.

//this creates text in xml to add to the resource dictionary
        //called by DrawingObjectText CreateNewElement 
        public static XElement DrawingTextXml(string userText, int userThickness, SolidColorBrush userColor)
        {
            //string for color
            String strColor = userColor.ToString();
            //font URI
            String fontURI = @"C:\WINDOWS\Fonts\TIMES.TTF"; 
            //convert user text to unicode numbers
            String convertedIndices = ConvertTextToIndices(fontURI, 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";
            //works for 1 character only
            String advancedWidths = "5 5"; // userText.Length.ToString();//"9.62666666666667 9.5";
            //BidiLevel
            String bidiLevel = "0";
            /*
             * <GlyphRunDrawing ForegroundBrush="#FFFFFFFF"><GlyphRunDrawing.GlyphRun><GlyphRun 
                                      CaretStops="{x:Null}" 
                                      ClusterMap="{x:Null}" 
                                      IsSideways="False" 
                                      GlyphOffsets="{x:Null}" 
                                      GlyphIndices="43 72 79 79 82 3 58 82 85 79 71" 
                                      BaselineOrigin="0,12.29"  
                                      FontRenderingEmSize="13.333333333333334" 
                                      DeviceFontName="{x:Null}" 
                                      AdvanceWidths="9.62666666666667 7.41333333333333 2.96 2.96 7.41333333333333 3.70666666666667 12.5866666666667 7.41333333333333 4.44 2.96 7.41333333333333" 
                                      BidiLevel="0"><GlyphRun.GlyphTypeface><GlyphTypeface FontUri="C:\WINDOWS\Fonts\TIMES.TTF" /></GlyphRun.GlyphTypeface></GlyphRun></GlyphRunDrawing.GlyphRun></GlyphRunDrawing>
             */


            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;


        }
        #region Helper Methods
        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();
        }


Mike Gallinger C.Tech. Cutting Edge Computing Software Developer


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>