using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Extensions;
namespace MLEM.Font {
///
public class GenericSpriteFont : GenericFont {
///
/// The that is being wrapped by this generic font
///
public readonly SpriteFont Font;
///
public override GenericFont Bold { get; }
///
public override GenericFont Italic { get; }
///
public override float LineHeight => this.Font.LineSpacing;
///
/// Creates a new generic font using .
/// Optionally, a bold and italic version of the font can be supplied.
///
/// The font to wrap
/// A bold version of the font
/// An italic version of the font
public GenericSpriteFont(SpriteFont font, SpriteFont bold = null, SpriteFont italic = null) {
this.Font = SetDefaults(font);
this.Bold = bold != null ? new GenericSpriteFont(SetDefaults(bold)) : this;
this.Italic = italic != null ? new GenericSpriteFont(SetDefaults(italic)) : this;
}
///
protected override float MeasureChar(char c) {
return this.Font.MeasureString(c.ToCachedString()).X;
}
///
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
}
///
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
}
private static SpriteFont SetDefaults(SpriteFont font) {
// we copy the font here to set the default character to a space
return new SpriteFont(
font.Texture,
font.Glyphs.Select(g => g.BoundsInTexture).ToList(),
font.Glyphs.Select(g => g.Cropping).ToList(),
font.Characters.ToList(),
font.LineSpacing,
font.Spacing,
font.Glyphs.Select(g => new Vector3(g.LeftSideBearing, g.Width, g.RightSideBearing)).ToList(),
' ');
}
}
}