/*
* Util.TextUtility.cs
* Authors: Adam Nabinger
* Copyright (c) 2007-2008 Cornell University
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Xna.Framework.Graphics;
namespace Util
{
///
/// A static class for holding basic text utility methods.
///
public static class TextUtility
{
///
/// If the line 'text' would appear longer than 'maxLen' when drawn in 'font',
/// Split the text into lines, and return an array of lines.
/// Breaks are done on spaces, if possible.
///
/// The string to be broken into pieces.
/// The max length of any line.
/// The sprite font containing the size of the font.
/// The array of string pieces
[DebuggerHidden]
public static string[] BreakString(string text, float maxLen, SpriteFont font)
{
if ( string.IsNullOrEmpty(text) || font.MeasureString(text).X <= maxLen)
{
return new string[] { text };
}
float testLength = font.MeasureString("A").X;
if ( maxLen < testLength )
{
throw new ArgumentOutOfRangeException("maxLen","A string can not be split to fit in less than " + testLength + " pixels.");
}
List results = new List();
string nextstring = string.Empty;
// NOTE: Not really optimized:
do
{
nextstring = nextstring.Insert(0, text.Substring(text.Length - 1));
text = text.Remove(text.Length - 1);
} while (font.MeasureString(text).X > maxLen);
if (text.EndsWith(" ", StringComparison.OrdinalIgnoreCase))
{
text = text.Remove(text.Length - 1);
}
else if (nextstring.StartsWith(" ", StringComparison.OrdinalIgnoreCase))
{
nextstring = nextstring.Remove(0, 1);
}
else if (text.Contains(" "))
{
while (!text.EndsWith(" ", StringComparison.OrdinalIgnoreCase))
{
nextstring = nextstring.Insert(0, text.Substring(text.Length - 1));
text = text.Remove(text.Length - 1);
}
text = text.Remove(text.Length - 1);
}
results.Add(text);
results.AddRange(BreakString(nextstring, maxLen, font));
return results.ToArray();
}
///
/// If the line 'text' would appear longer than 'maxLen' when drawn in 'font',
/// Split the text into lines, and return an array of lines.
/// Breaks are done on spaces, if possible, but leaves the spaces in the resultant strings.
///
/// The string to be broken into pieces.
/// The max length of any line.
/// The sprite font containing the size of the font.
/// The array of string pieces
[DebuggerHidden]
public static string[] BreakStringWithSpaces(string text, float maxLen, SpriteFont font)
{
if (string.IsNullOrEmpty(text) || font.MeasureString(text).X <= maxLen)
{
return new string[] { text };
}
float testLength = font.MeasureString("A").X;
if (maxLen < testLength)
{
throw new ArgumentOutOfRangeException("maxLen", "A string can not be split to fit in less than " + testLength + " pixels.");
}
List results = new List();
string nextstring = string.Empty;
// NOTE: Not really optimized:
do
{
nextstring = nextstring.Insert(0, text.Substring(text.Length - 1));
text = text.Remove(text.Length - 1);
} while (font.MeasureString(text).X > maxLen);
if (!(text.EndsWith(" ", StringComparison.OrdinalIgnoreCase) || nextstring.StartsWith(" ", StringComparison.OrdinalIgnoreCase)) && text.Contains(" "))
{
while (!text.EndsWith(" ", StringComparison.OrdinalIgnoreCase))
{
nextstring = nextstring.Insert(0, text.Substring(text.Length - 1));
text = text.Remove(text.Length - 1);
}
}
results.Add(text);
results.AddRange(BreakStringWithSpaces(nextstring, maxLen, font));
return results.ToArray();
}
}
}