.
In Depth Banner
Skip Navigation Links

Select your preferred language

The GetLines method.

    /// <summary>                                 

    /// Calculates an array of lines that are correctly justified for a paragraph of a certain column width.

    /// </summary>

    /// <param name="words">The raw list of words in the paragraph</param>

    /// <returns>An array of lines</returns>

    public WordPosCollection[] GetLines(WordPosCollection words)

    {

      ArrayList lines=new ArrayList();

      bool firstLine=true;

      bool paragraph=true;

      int WordIndex=0;

      WordPosCollection line;

      do

      {

        line = new WordPosCollection();

        line.NewParagraph=true;

        float lineTotal=LeftMargin;

        if(firstLine && Indent!=0)

        {

          WordPos iwp=new WordPos();

          iwp.PagePos=LeftMargin;

          iwp.WhiteSpace=WhiteSpace.Space;

          iwp.WordWidth=Indent;

          iwp.Font=words[0].Font;

          line.Add(iwp);

        }

 

        firstLine=false;

 

 

        for(;WordIndex<words.Count;WordIndex++)

        {

          //calculate the word Pos

          WordPos wp=words[WordIndex].Clone();

          wp.PagePos=lineTotal;

 

          //holds the additional size to be added to lineTotal

          float extra=0;

 

          bool newline = false;

 

          //calculate the lineTotal shift

          switch(wp.WhiteSpace)

          {

            case WhiteSpace.Space:

            case WhiteSpace.None:

              extra+=wp.WordWidth;

              break;

            case WhiteSpace.Tab:

              extra+=this.GetNextTab(lineTotal)-lineTotal;

              wp.WordWidth=extra;

              break;

            case WhiteSpace.NewLine:

              extra=0;

              line.Add(wp);

              newline=true;

              paragraph=true;

              break;

          }

 

          if(lineTotal+extra > LeftMargin+ColumnWidth)

            newline=true;

 

          if(newline)

          {

            DoJustify(ref line);

            lines.Add(line);

            line=new WordPosCollection();

            line.NewParagraph=paragraph;

            paragraph=false;

            lineTotal=LeftMargin;

            wp.PagePos=lineTotal;

            if(extra!=0)

            {

              line.Add(wp);

              lineTotal+=extra;

            }

          }

          else

          {

            line.Add(wp);

            lineTotal+=extra;

          }

        }      

      }

      while(WordIndex<words.Count-1);

 

      if(line.Count!=0)

      {

        if(this.Justify==JustificationStyles.Justified)

          DoJustify(ref line,JustificationStyles.Left);

        else

          DoJustify(ref line,this.Justify);

        lines.Add(line);

      }

 

      WordPosCollection[] linearray=new WordPosCollection[lines.Count];

      lines.CopyTo(linearray);

 

      return linearray;

    }

Return to the article.

Copyright © Bob Powell 2000-.  All rights reserved.