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;
}
'/ <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
Overloads Function
GetWords(ByVal [text]
As String, ByVal
font As Font, ByVal
g As Graphics) As
WordPosCollection
If
[text] Is Nothing
Or font Is
Nothing Then
Return
New WordPosCollection
End
If
Dim
PossArray As New
WordPosCollection
Dim
wp As WordPos
'remove
carriage returns..
Dim
subsnocr As String()
= [text].Split(New [Char]() {ControlChars.Cr})
Dim
sb As New
StringBuilder
Dim
ss As String
For
Each ss In
subsnocr
sb.Append(ss)
Next
ss
Dim
s As String =
sb.ToString()
'we
test for this whitespace...
Dim
testarray() As Char
= {" "c, ControlChars.Tab, ControlChars.Lf}
Do
Dim
done As Boolean
= False
Do
If
s.IndexOfAny(testarray) = 0 Then
Select
Case s.Chars(0)
Case " "c
wp = New WordPos(WhiteSpace.Space)
wp.Word = " "
Case ControlChars.Tab
wp = New WordPos(WhiteSpace.Tab)
wp.Word = ControlChars.Tab
Case ControlChars.Lf
wp = New
WordPos(WhiteSpace.NewLine)
Case Else
wp = New WordPos
End
Select
wp.Font = font
wp.WordWidth = Me.GetWordWidth(wp.Word,
font, g)
PossArray.Add(wp)
s
= s.Substring(1, s.Length - 1)
Else
done = True
End
If
Loop
While Not done
If
s.Length = 0 Then
GoTo
ContinueDo1
End
If
Dim
wsindex As Integer
= s.IndexOfAny(testarray)
wp =
New WordPos
If
wsindex > -1 Then
wp.Word
= s.Substring(0, wsindex)
wp.WordWidth
= Me.GetWordWidth(wp.Word, font, g)
s =
s.Substring(wsindex, s.Length - wsindex)
Else
wp.Word
= s
wp.WordWidth
= GetWordWidth(s, font, g)
s =
""
End
If
wp.Font
= font
PossArray.Add(wp)
ContinueDo1:
Loop
While s.Length > 0
Return
PossArray
End
Function 'GetWords
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
Function GetWordWidth(ByVal
s As String,
ByVal font As
Font, ByVal g As
Graphics) As Single
Dim
sf As StringFormat =
StringFormat.GenericTypographic
sf.FormatFlags
= sf.FormatFlags Or
StringFormatFlags.MeasureTrailingSpaces
Dim
sz As SizeF = g.MeasureString(s, font, 4096, sf)
Return
sz.Width
End
Function 'GetWordWidth