2021-02-27 16:58:36 +01:00
|
|
|
using FontStashSharp;
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
using MLEM.Font;
|
|
|
|
|
|
|
|
namespace MLEM.Extended.Font {
|
|
|
|
/// <inheritdoc/>
|
|
|
|
public class GenericStashFont : GenericFont {
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The <see cref="SpriteFontBase"/> that is being wrapped by this generic font
|
|
|
|
/// </summary>
|
|
|
|
public readonly SpriteFontBase Font;
|
2024-01-30 20:47:05 +01:00
|
|
|
|
2021-02-27 16:58:36 +01:00
|
|
|
/// <inheritdoc />
|
|
|
|
public override GenericFont Bold { get; }
|
|
|
|
/// <inheritdoc />
|
|
|
|
public override GenericFont Italic { get; }
|
|
|
|
/// <inheritdoc />
|
2021-08-19 21:43:17 +02:00
|
|
|
public override float LineHeight => this.Font.LineHeight;
|
2021-02-27 16:58:36 +01:00
|
|
|
|
2024-01-30 20:47:05 +01:00
|
|
|
/// <summary>
|
|
|
|
/// The character spacing that will be passed to the underlying <see cref="Font"/>.
|
|
|
|
/// </summary>
|
|
|
|
public float CharacterSpacing { get; set; }
|
|
|
|
/// <summary>
|
|
|
|
/// The line spacing that will be passed to the underlying <see cref="Font"/>.
|
|
|
|
/// </summary>
|
|
|
|
public float LineSpacing { get; set; }
|
|
|
|
|
2021-02-27 16:58:36 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new generic font using <see cref="SpriteFontBase"/>.
|
|
|
|
/// 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>
|
2021-08-19 21:43:17 +02:00
|
|
|
public GenericStashFont(SpriteFontBase font, SpriteFontBase bold = null, SpriteFontBase italic = null) {
|
2021-02-27 16:58:36 +01:00
|
|
|
this.Font = font;
|
2021-08-19 21:43:17 +02:00
|
|
|
this.Bold = bold != null ? new GenericStashFont(bold) : this;
|
|
|
|
this.Italic = italic != null ? new GenericStashFont(italic) : this;
|
2021-02-27 16:58:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2022-10-15 13:48:45 +02:00
|
|
|
protected override float MeasureCharacter(int codePoint) {
|
2024-01-30 20:47:05 +01:00
|
|
|
return this.Font.MeasureString(CodePointSource.ToString(codePoint), null, this.CharacterSpacing, this.LineSpacing).X;
|
2021-02-27 16:58:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2024-04-10 20:27:00 +02:00
|
|
|
public override void DrawCharacter(SpriteBatch batch, int codePoint, string character, Vector2 position, Color color, float rotation, Vector2 scale, SpriteEffects effects, float layerDepth) {
|
2024-01-30 20:47:05 +01:00
|
|
|
this.Font.DrawText(batch, character, position, color, rotation, Vector2.Zero, scale, layerDepth, this.CharacterSpacing, this.LineSpacing);
|
2021-02-27 16:58:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-06-17 18:23:47 +02:00
|
|
|
}
|