Jypeli 10
The simple game programming library
Font.cs
Siirry tämän tiedoston dokumentaatioon.
1using System;
2using Microsoft.Xna.Framework.Graphics;
3using System.Text;
4using XnaV2 = Microsoft.Xna.Framework.Vector2;
6using System.IO;
7using Microsoft.Xna.Framework.Content;
8using System.ComponentModel;
9using System.Collections.Generic;
10
11namespace Jypeli
12{
13 internal enum ContentSource
14 {
17 }
18
19
23 public class Font
24 {
25 private static string defaultFont = "Roboto-Regular.ttf";
26 private static string defaultFontBold = "Roboto-Bold.ttf";
27
31 public static readonly Font Default = new Font(defaultFont, ContentSource.ResourceContent, 25);
32
36 public static readonly Font DefaultBold = new Font(defaultFontBold, ContentSource.ResourceContent, 25);
37
38 private DynamicSpriteFont xnaFont;
40 private string name;
41 private int size;
43
44 private int blurAmount = 0;
45 private int strokeAmount = 0;
46
47 private List<string> mergedFonts = new List<string>();
48
49 internal DynamicSpriteFont XnaFont
50 {
51 get { DoLoad(); return xnaFont; }
52 }
53
55 {
56 get { DoLoad(); return fontSystem; }
57 }
58
67 public int Size
68 {
69 get { return size; }
70 set
71 {
72 DoLoad();
73 if (value <= 0) throw new Exception("Fontsize must be greater than zero.");
74 size = value;
75 xnaFont = fontSystem.GetFont(size);
76 fontSystem = null;
77 DoLoad();
78 }
79 }
80
90 public int BlurAmount
91 {
92 get { return blurAmount; }
93 set
94 {
95 DoLoad();
96 blurAmount = value;
97 strokeAmount = 0;
98 fontSystem = null;
99 DoLoad();
100 }
101 }
102
112 public int StrokeAmount
113 {
114 get { return strokeAmount; }
115 set
116 {
117 DoLoad();
118 strokeAmount = value;
119 blurAmount = 0;
120 fontSystem = null;
121 DoLoad();
122 }
123 }
124
128 public double CharacterWidth
129 {
130 get { return XnaFont.MeasureString( "X" ).X; } // TODO: pitäisi todellisuudessa etsiä fontin suurin merkki ja katsoa sen mitat.
131 }
132
136 public double CharacterHeight
137 {
138 get { return XnaFont.MeasureString( "X" ).Y; }
139 }
140
146 public static Font FromContent(string name)
147 {
148 Font font = new Font("Content/" + name, ContentSource.GameContent);
149 return font;
150 }
151
156 public Font(int fontSize=25) : this(defaultFont, ContentSource.ResourceContent, fontSize)
157 {
158 }
159
165 public Font(int fontSize, bool bold) : this(bold ? defaultFontBold : defaultFont, ContentSource.ResourceContent, fontSize)
166 {
167 }
168
173 public Font(string name) : this(name, ContentSource.GameContent) { }
174
180 public Font(string name, int fontSize) : this(name, ContentSource.GameContent, fontSize) { }
181
182 internal Font(string name, ContentSource source)
183 {
184 this.xnaFont = null;
185 this.name = name;
186 this.source = source;
187 this.size = 25;
188 }
189
190 internal Font( string name, ContentSource source, int fontSize)
191 {
192 this.xnaFont = null;
193 this.name = name;
194 this.source = source;
195 this.size = fontSize;
196 }
197
198 internal Font( DynamicSpriteFont xnaFont )
199 {
200 this.xnaFont = xnaFont;
201 this.name = null;
202 this.source = ContentSource.ResourceContent;
203 }
204
205 private void DoLoad()
206 {
207 if (fontSystem == null)
208 {
209 fontSystem = new FontSystem(StbTrueTypeSharpFontLoader.Instance, new Texture2DManager(Game.GraphicsDevice), 1024, 1024, blurAmount, StrokeAmount);
210
211 Stream s;
212 if (this.source == ContentSource.ResourceContent) s = Game.ResourceContent.StreamInternalFont(name);
213 else s = File.Open(name, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); // Tässä pitää jostain syystä olla ReadWrite, vaikka tiedosto ainoastaan luetaan.
214
215 fontSystem.AddFont(s);
216 xnaFont = fontSystem.GetFont(size);
217
218 mergedFonts.ForEach(name => MergeFont(name));
219
220 s.Dispose();
221 }
222 }
223
229 public void AddFont(string filename)
230 {
231 DoLoad();
232 MergeFont(filename);
233 mergedFonts.Add(filename);
234 }
235
236
237 private void MergeFont(string filename)
238 {
239 Stream s = File.Open("Content/" + filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
240 fontSystem.AddFont(s);
241 }
242
248 public Vector GetCharacterSize( char c )
249 {
250 return XnaFont.MeasureString(c.ToString());
251 }
252
260 public string TruncateText( string str, double maxLineWidth )
261 {
262 StringBuilder builder = new StringBuilder( str );
263 double realWidth = XnaFont.MeasureString( str ).X;
264
265 while ( realWidth > maxLineWidth )
266 {
267 builder.Remove( builder.Length - 1, 1 );
268 realWidth = XnaFont.MeasureString( builder ).X;
269 }
270
271 return builder.ToString();
272 }
273
279 public Vector MeasureSize(string str)
280 {
281 var xnaVector2 = XnaFont.MeasureString(str);
282 return new Vector(xnaVector2.X, xnaVector2.Y);
283 }
284
285 private static void appendLine(StringBuilder dest, StringBuilder line)
286 {
287 if ( dest.Length > 0 ) dest.Append( "\n" );
288 line.RemoveLeading( c => Char.IsWhiteSpace( c ) );
289 line.PutTo( dest );
290 }
291
298 public string WrapText( string text, double softLineWidth, double hardLineWidth )
299 {
300 if ( softLineWidth <= 0 || hardLineWidth <= 0 )
301 {
302 throw new ArgumentException( "Width must be positive." );
303 }
304
305 StringBuilder src = new StringBuilder( text );
306 StringBuilder word = new StringBuilder();
307 StringBuilder line = new StringBuilder();
308 StringBuilder dest = new StringBuilder();
309 double lineWidth = 0;
310
311 while ( src.Length > 0 || word.Length > 0 )
312 {
313 if (word.Length == 0)
314 src.TakeFirstWord( word );
315
316 var wordWidth = XnaFont.MeasureString( word ).X;
317
318 if ( lineWidth + wordWidth > softLineWidth )
319 {
320 appendLine( dest, line );
321 word.PutTo( line );
322 lineWidth = 0;
323 }
324 else if (lineWidth + wordWidth > hardLineWidth)
325 {
326 int wi = FindWrapIndex(word, hardLineWidth - lineWidth, false);
327 word.PutTo(line, 0, wi + 1);
328 appendLine(dest, line);
329 lineWidth = 0;
330 }
331 else
332 {
333 word.PutTo( line );
334 lineWidth += wordWidth;
335 }
336 }
337
338 if ( line.Length > 0 )
339 appendLine( dest, line );
340
341 return dest.ToString();
342 }
343
352 private int FindWrapIndex( StringBuilder text, double maxWidth, bool fromRight )
353 {
354
355 double currentWidth = -xnaFont.FontSystem.CharacterSpacing;
356 int i = fromRight ? text.Length - 1 : 0;
357 int step = fromRight ? -1 : 1;
358
359 //for ( int i = 0; i < text.Length; i++ )
360 while ( ( fromRight && i >= 0 ) || ( !fromRight && i < text.Length ) )
361 {
362 currentWidth += xnaFont.FontSystem.CharacterSpacing + GetCharacterSize( text[i] ).X;
363 if ( currentWidth >= maxWidth ) return i;
364 i += step;
365 }
366
367 return fromRight ? -1 : text.Length;
368 }
369 }
370}
Microsoft.Xna.Framework.Vector2 XnaV2
Definition: Font.cs:4
Fontti.
Definition: Font.cs:24
Font(string name, int fontSize)
Lataa uuden fontin contentista.
Definition: Font.cs:180
Font(string name, ContentSource source, int fontSize)
Definition: Font.cs:190
DynamicSpriteFont XnaFont
Definition: Font.cs:50
void MergeFont(string filename)
Definition: Font.cs:237
int FindWrapIndex(StringBuilder text, double maxWidth, bool fromRight)
Etsii katkaisuindeksin merkkijonolle merkki kerrallaan. Välilyönneillä ei ole erikoisasemaaa.
Definition: Font.cs:352
int StrokeAmount
Asettaa tekstin reunuksen paksuuden.
Definition: Font.cs:113
Font(string name)
Lataa uuden fontin contentista.
Definition: Font.cs:173
double CharacterHeight
Merkin korkeus.
Definition: Font.cs:137
List< string > mergedFonts
Definition: Font.cs:47
Font(int fontSize, bool bold)
Luo uuden oletusfontin halutulla koolla.
Definition: Font.cs:165
static string defaultFontBold
Definition: Font.cs:26
Vector MeasureSize(string str)
Laskee tekstin koon fontilla.
Definition: Font.cs:279
Vector GetCharacterSize(char c)
Palauttaa annetun merkin koon tässä fontissa.
Definition: Font.cs:248
DynamicSpriteFont xnaFont
Definition: Font.cs:38
static readonly Font Default
Oletusfontti.
Definition: Font.cs:31
string WrapText(string text, double softLineWidth, double hardLineWidth)
Rivittää tekstin.
Definition: Font.cs:298
void DoLoad()
Definition: Font.cs:205
Font(string name, ContentSource source)
Definition: Font.cs:182
int strokeAmount
Definition: Font.cs:45
Font(int fontSize=25)
Luo uuden oletusfontin halutulla koolla.
Definition: Font.cs:156
int Size
Fontin koko.
Definition: Font.cs:68
string name
Definition: Font.cs:40
static readonly Font DefaultBold
Lihavoitu oletusfontti.
Definition: Font.cs:36
int blurAmount
Definition: Font.cs:44
string TruncateText(string str, double maxLineWidth)
Katkaisee merkkijonon loppupäästä niin että se sopii annettuun pikselileveyteen fontilla kirjoitettun...
Definition: Font.cs:260
int BlurAmount
Asettaa tekstin sumennuksen määrän.
Definition: Font.cs:91
FontSystem FontSystem
Definition: Font.cs:55
static Font FromContent(string name)
Lataa uuden fontin contentista.
Definition: Font.cs:146
ContentSource source
Definition: Font.cs:42
static void appendLine(StringBuilder dest, StringBuilder line)
Definition: Font.cs:285
void AddFont(string filename)
Lisää toisen fontin merkistön tähän fonttiin. Jos fontit sisältävät päällekkäistä merkistöä,...
Definition: Font.cs:229
Font(DynamicSpriteFont xnaFont)
Definition: Font.cs:198
int size
Definition: Font.cs:41
static string defaultFont
Definition: Font.cs:25
FontSystem fontSystem
Definition: Font.cs:39
double CharacterWidth
Merkin leveys.
Definition: Font.cs:129
static new GraphicsDevice GraphicsDevice
XNA:n grafiikkakortti.
Definition: Graphics.cs:49
static JypeliContentManager ResourceContent
Kirjaston mukana tuleva sisältö. Voidaan käyttää esimerkiksi sisäisten tekstuurien lataamiseen.
Definition: Content.cs:67
ContentSource
Definition: Font.cs:14
2D-vektori.
Definition: Vector.cs:67
double X
Vektorin X-komponentti.
Definition: Vector.cs:334