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;
}
'/
<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
Function GetLines(ByVal
words As WordPosCollection)
As WordPosCollection()
Dim
lines As New
ArrayList
Dim
firstLine As Boolean
= True
Dim
paragraph As Boolean
= True
Dim
WordIndex As Integer
= 0
Dim
line As WordPosCollection
Do
line
= New WordPosCollection
line.NewParagraph
= True
Dim
lineTotal As Single
= LeftMargin
'Does
this line need to be indented?
If
firstLine And Indent <> 0
Then
Dim
iwp As New
WordPos
iwp.PagePos
= LeftMargin
iwp.WhiteSpace
= WhiteSpace.Space
iwp.WordWidth
= Indent
iwp.Font
= words(0).Font
line.Add(iwp)
End
If
firstLine = False
While
WordIndex < words.Count
'calculate
the word Pos
Dim
wp As WordPos = words(WordIndex).Clone()
wp.PagePos = lineTotal
'holds
the additional size to be added to lineTotal
'This
takes care of odd values added due to tab-stops etc.
Dim
extra As Single
= 0
Dim
newline As Boolean
= False
'calculate
the lineTotal shift
Select
Case wp.WhiteSpace
Case
WhiteSpace.Space, WhiteSpace.None
extra += wp.WordWidth
Case
WhiteSpace.Tab
extra += Me.GetNextTab(lineTotal) -
lineTotal
wp.WordWidth = extra
Case
WhiteSpace.NewLine
extra = 0
line.Add(wp)
newline = True
paragraph = True
End
Select
If
lineTotal + extra > LeftMargin + ColumnWidth Then
newline = True
End
If
'if
the line is long enough, justify whats in this line and move on
If
newline Then
DoJustify(line)
lines.Add(line)
line = New WordPosCollection
line.NewParagraph = paragraph
paragraph = False
lineTotal = LeftMargin
wp.PagePos = lineTotal
If
extra <> 0 Then
line.Add(wp)
lineTotal += extra
End
If
'just
add the current entity to the line
Else
line.Add(wp)
lineTotal += extra
End
If
WordIndex += 1
End
While
Loop
While WordIndex < words.Count - 1
'until
there are no more entities left in the word list
If
line.Count <> 0 Then
'Takes
care of the last line in a justified paragraph.
'which
must always be left justified.
If
Me.Justify = JustificationStyles.Justified
Then
DoJustify(line,
JustificationStyles.Left)
Else
DoJustify(line,
Me.Justify)
End
If
lines.Add(line)
End
If
Dim
linearray(lines.Count - 1) As WordPosCollection
lines.CopyTo(linearray)
Return
linearray
End
Function 'GetLines