locked
Calculate the width of a space? RRS feed

  • Question

  • So I have a string, like "The cow jumped over the moon." I see how through the GlyphTypeface that I can measure the width of each individual character. What works great! What I can't figure out how to do is get the width of the space between the words. Let's say I have the following method:

           
    Code Snippet

    public double CalculateWidthWithSpaceAtTheEnd()
            {
                double width = 0;
                ushort glyphIndex = 0;
                Geometry geo = null;
                foreach (char ch in Text)
                {
                    glyphIndex = Typeface.CharacterToGlyphMap[ch];
                    geo = Typeface.GetGlyphOutline(glyphIndex, EmSize, 10);
                    width += geo.Bounds.Width;
                }

                char spaceChar = ' ';
                glyphIndex = Typeface.CharacterToGlyphMap[spaceChar];
                geo = Typeface.GetGlyphOutline(glyphIndex, EmSize, 10);

                width += geo.Bounds.Width;

                return width;

            }


    I would think what that would do is add up all the widths of the individual characters in a string, then get the width for the space for that font, size, and hinting, and add that to the total width, giving me the width of a word with a space at the end.

    However, the width in the bottom portion comes out as -Infinity...which obviously isn't really getting me what I want. Any clue?

    Or, does anyone know of a way to calculate the actual width of a string (like "The cow jumped over the moon.") without having to go through the task of measuring each individual character?

    Any help would be appreciated.

    Sunday, August 19, 2007 1:35 AM

All replies

  • No ideas from anyone?
    Monday, August 27, 2007 9:22 PM
  • Have a look at

    GetFontData, GetOutlineTextMetrix, GetCharABCWidths, GetGlyphOutline, ...

    you should be able to calculate the width of a text, including whitespaces.
    Tuesday, August 28, 2007 7:20 AM