1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-13 20:58:45 +02:00
MLEM/MLEM/Font/GenericSpriteFont.cs

66 lines
2.6 KiB
C#
Raw Normal View History

2019-08-09 14:26:20 +02:00
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
2022-08-20 11:39:28 +02:00
#if !FNA
using System.Linq;
#endif
2019-08-09 14:26:20 +02:00
namespace MLEM.Font {
2020-05-20 23:59:40 +02:00
/// <inheritdoc/>
2020-03-28 22:25:06 +01:00
public class GenericSpriteFont : GenericFont {
2019-08-09 14:26:20 +02:00
/// <summary>
/// The <see cref="SpriteFont"/> that is being wrapped by this generic font
/// </summary>
2019-08-09 14:26:20 +02:00
public readonly SpriteFont Font;
2020-05-20 23:59:40 +02:00
/// <inheritdoc/>
public override GenericFont Bold { get; }
2020-05-20 23:59:40 +02:00
/// <inheritdoc/>
public override GenericFont Italic { get; }
2020-05-20 23:59:40 +02:00
/// <inheritdoc/>
2020-03-28 22:25:06 +01:00
public override float LineHeight => this.Font.LineSpacing;
2019-08-09 14:26:20 +02:00
2020-05-20 23:59:40 +02:00
/// <summary>
/// Creates a new generic font using <see cref="SpriteFont"/>.
/// Optionally, a bold and italic version of the font can be supplied.
/// </summary>
/// <param name="font">The font to wrap</param>
/// <param name="bold">A bold version of the font</param>
/// <param name="italic">An italic version of the font</param>
public GenericSpriteFont(SpriteFont font, SpriteFont bold = null, SpriteFont italic = null) {
this.Font = GenericSpriteFont.SetDefaults(font);
this.Bold = bold != null ? new GenericSpriteFont(GenericSpriteFont.SetDefaults(bold)) : this;
this.Italic = italic != null ? new GenericSpriteFont(GenericSpriteFont.SetDefaults(italic)) : this;
2019-08-09 14:26:20 +02:00
}
/// <inheritdoc />
protected override float MeasureCharacter(int codePoint) {
return this.Font.MeasureString(CodePointSource.ToString(codePoint)).X;
2019-08-09 14:26:20 +02:00
}
2021-12-22 12:46:17 +01:00
/// <inheritdoc />
protected override void DrawCharacter(SpriteBatch batch, int codePoint, string character, Vector2 position, Color color, float rotation, Vector2 scale, SpriteEffects effects, float layerDepth) {
batch.DrawString(this.Font, character, position, color, rotation, Vector2.Zero, scale, effects, layerDepth);
2019-08-09 14:26:20 +02:00
}
private static SpriteFont SetDefaults(SpriteFont font) {
2022-12-13 13:11:36 +01:00
#if FNA
2022-06-24 14:01:26 +02:00
// none of the copying is available with FNA
return font;
2022-12-13 13:11:36 +01:00
#else
// 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(),
' ');
2022-12-13 13:11:36 +01:00
#endif
}
2019-08-09 14:26:20 +02:00
}
2022-06-17 18:23:47 +02:00
}