.
In Depth Banner
Skip Navigation Links

Select your preferred language

The GetWords method.

    /// <summary>

    /// Returns a word collection for a specified paragraph.

    /// </summary>

    /// <param name="text">The text to format</param>

    /// <param name="font">The font used to display the word</param>

    /// <param name="g">The graphics device upon which formatted text will be displayed</param>

    /// <returns>the collection of words after analysis</returns>

    public WordPosCollection GetWords(string text, Font font, Graphics g)

    {

      if(text==null || font==null)

        return new WordPosCollection();

      WordPosCollection PossArray = new WordPosCollection();

      WordPos wp;

      //remove carriage returns preserves linefeeds..

      string[] subsnocr = text.Split(new Char[]{'\r'});

      StringBuilder sb = new StringBuilder();

      foreach(string ss in subsnocr)

        sb.Append(ss);

      string s=sb.ToString();

 

      //we test for this whitespace...

      char[] testarray = new char[]{' ','\t','\n'};

 

      do

      {

        bool done=false;

        do

        {

          if(s.IndexOfAny(testarray)==0)

          {

            switch(s[0])

            {

              case ' ':

                wp=new WordPos(WhiteSpace.Space);

                wp.Word=" ";

                break;

              case '\t':

                wp=new WordPos(WhiteSpace.Tab);

                wp.Word="\t";

                break;

              case '\n':

                wp=new WordPos(WhiteSpace.NewLine);

                break;

              default:

                wp=new WordPos();

                break;

            }

 

            wp.Font=font;

            wp.WordWidth=this.GetWordWidth(wp.Word,font,g);

 

            PossArray.Add(wp);

 

            s=s.Substring(1,s.Length-1);

          }

          else

            done=true;

        }

        while(!done);

 

        if(s.Length==0)

          continue;

 

        int wsindex = s.IndexOfAny(testarray);

        

        wp = new WordPos();

        

        if(wsindex>-1)

        {

          wp.Word=s.Substring(0,wsindex);

          wp.WordWidth=this.GetWordWidth(wp.Word,font,g);

          s=s.Substring(wsindex,s.Length-wsindex);

        }

        else

        {

          wp.Word=s;

          wp.WordWidth=GetWordWidth(s,font,g);

          s="";

        }

 

        wp.Font=font;

 

        PossArray.Add(wp);

      }

      while(s.Length>0);

    

      return PossArray;

 

    }

 

The following method measures the word width.

 

    /// <summary>

    /// Returns a word-width for a specific word, font and graphics object

    /// </summary>

    /// <param name="s">The word to measure</param>

    /// <param name="font">The font used to display the word</param>

    /// <param name="g">The graphics device upon which formatted text will be displayed</param>

    /// <returns></returns>

    public float GetWordWidth(string s, Font font, Graphics g)

    {

      StringFormat sf=(StringFormat)StringFormat.GenericTypographic.Clone();
      sf.FormatFlags|=StringFormatFlags.MeasureTrailingSpaces;

      SizeF sz = g.MeasureString(s,font,4096,sf);

      return sz.Width;

    }

 

Return to the article

Copyright © Bob Powell 2000-.  All rights reserved.