3 using Microsoft.Xna.Framework.Graphics;
7 using XnaV2 = Microsoft.Xna.Framework.Vector2;
8 using XnaColor = Microsoft.Xna.Framework.Color;
12 internal enum ContentSource
27 public static readonly
Font Default =
new Font(
"MediumFont", ContentSource.ResourceContent );
42 public static readonly
Font DefaultBold =
new Font(
"MediumFontBold", ContentSource.ResourceContent );
54 private SpriteFont xnaFont;
56 private ContentSource source;
57 private Vector[] charsizes;
59 internal SpriteFont XnaFont
61 get { DoLoad();
return xnaFont; }
69 get {
return XnaFont.MeasureString(
"X" ).X; }
77 get {
return XnaFont.MeasureString(
"X" ).Y; }
80 internal Font(
string name, ContentSource source )
83 this.charsizes = null;
88 internal Font( SpriteFont xnaFont )
90 this.xnaFont = xnaFont;
91 this.charsizes =
new Vector[xnaFont.Characters.Count];
93 this.source = ContentSource.ResourceContent;
98 if ( xnaFont == null )
100 if ( source == ContentSource.ResourceContent )
101 xnaFont =
Game.ResourceContent.Load<SpriteFont>( name );
103 xnaFont =
Game.
Instance.Content.Load<SpriteFont>( name );
105 charsizes =
new Vector[xnaFont.Characters.Count];
116 int index = XnaFont.Characters.IndexOf( c );
121 XnaV2 xnaSize = XnaFont.MeasureString( c.ToString() );
122 charsizes[index] =
new Vector( xnaSize.X, xnaSize.Y );
125 return charsizes[index];
137 StringBuilder builder =
new StringBuilder( str );
138 double realWidth = XnaFont.MeasureString( str ).X;
140 while ( realWidth > maxLineWidth )
142 builder.Remove( builder.Length - 1, 1 );
143 realWidth = XnaFont.MeasureString( builder ).X;
146 return builder.ToString();
149 private static void appendLine(StringBuilder dest, StringBuilder line)
151 if ( dest.Length > 0 ) dest.Append(
"\n" );
152 line.RemoveLeading( c => Char.IsWhiteSpace( c ) );
162 public string WrapText(
string text,
double softLineWidth,
double hardLineWidth )
164 if ( softLineWidth <= 0 || hardLineWidth <= 0 )
166 throw new ArgumentException(
"Width must be positive." );
169 StringBuilder src =
new StringBuilder( text );
170 StringBuilder word =
new StringBuilder();
171 StringBuilder line =
new StringBuilder();
172 StringBuilder dest =
new StringBuilder();
173 double lineWidth = 0;
175 while ( src.Length > 0 || word.Length > 0 )
177 if (word.Length == 0)
178 src.TakeFirstWord( word );
180 var wordWidth = XnaFont.MeasureString( word ).X;
182 if ( lineWidth + wordWidth > hardLineWidth )
184 int wi = FindWrapIndex( word, hardLineWidth - lineWidth,
false );
185 word.PutTo( line, 0, wi + 1 );
186 appendLine( dest, line );
189 else if ( lineWidth + wordWidth > softLineWidth )
191 appendLine( dest, line );
198 lineWidth += wordWidth;
202 if ( line.Length > 0 )
203 appendLine( dest, line );
205 return dest.ToString();
216 private int FindWrapIndex( StringBuilder text,
double maxWidth,
bool fromRight )
218 double currentWidth = -XnaFont.Spacing;
219 int i = fromRight ? text.Length - 1 : 0;
220 int step = fromRight ? -1 : 1;
223 while ( ( fromRight && i >= 0 ) || ( !fromRight && i < text.Length ) )
226 if ( currentWidth >= maxWidth )
return i;
230 return fromRight ? -1 : text.Length;
string WrapText(string text, double softLineWidth, double hardLineWidth)
Rivittää tekstin.
double CharacterHeight
Merkin korkeus.
static readonly Font DefaultBold
Paksunnettu oletusfontti.
static readonly Font DefaultLargeBold
Paksunnettu suuri oletusfontti.
double CharacterWidth
Merkin leveys.
static readonly Font DefaultLarge
Suuri oletusfontti.
string TruncateText(string str, double maxLineWidth)
Katkaisee merkkijonon loppupäästä niin että se sopii annettuun pikselileveyteen fontilla kirjoitettun...
static readonly Vector Zero
Nollavektori.
Vector GetCharacterSize(char c)
Palauttaa annetun merkin koon tässä fontissa.
Peliluokka reaaliaikaisille peleille.
static readonly Font DefaultSmallBold
Paksunnettu pieni oletusfontti.
static readonly Font Default
OletusFontti.
static readonly Font DefaultSmall
Pieni oletusfontti.