1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-12-27 02:29:23 +01:00
MLEM/MLEM.Extended/Font/GenericBitmapFont.cs

73 lines
3.4 KiB
C#
Raw Normal View History

2019-08-09 19:28:48 +02:00
using System.Collections.Generic;
2019-08-09 14:26:20 +02:00
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
2019-08-09 19:28:48 +02:00
using MLEM.Extended.Extensions;
2019-08-09 14:26:20 +02:00
using MLEM.Font;
using MonoGame.Extended.BitmapFonts;
namespace MLEM.Extended.Font {
2020-03-28 22:25:06 +01:00
public class GenericBitmapFont : GenericFont {
2019-08-09 14:26:20 +02:00
public readonly BitmapFont Font;
public override GenericFont Bold { get; }
public override GenericFont Italic { get; }
2020-03-28 22:25:06 +01:00
public override float LineHeight => this.Font.LineHeight;
2019-08-09 14:26:20 +02:00
public GenericBitmapFont(BitmapFont font, BitmapFont bold = null, BitmapFont italic = null) {
2019-08-09 14:26:20 +02:00
this.Font = font;
this.Bold = bold != null ? new GenericBitmapFont(bold) : this;
this.Italic = italic != null ? new GenericBitmapFont(italic) : this;
2019-08-09 14:26:20 +02:00
}
2020-03-28 22:25:06 +01:00
public override Vector2 MeasureString(string text) {
if (text.Length == 1 && this.SingleCharacterWidthFix(text, out var size))
return size;
2019-08-09 14:26:20 +02:00
return this.Font.MeasureString(text);
}
2020-03-28 22:25:06 +01:00
public override Vector2 MeasureString(StringBuilder text) {
if (text.Length == 1 && this.SingleCharacterWidthFix(text.ToString(), out var size))
return size;
2019-08-09 14:26:20 +02:00
return this.Font.MeasureString(text);
}
2020-03-28 22:25:06 +01:00
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color) {
2019-08-09 14:26:20 +02:00
batch.DrawString(this.Font, text, position, color);
}
2020-03-28 22:25:06 +01:00
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
2019-08-09 14:26:20 +02:00
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
}
2020-03-28 22:25:06 +01:00
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
2019-08-09 14:26:20 +02:00
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
}
2020-03-28 22:25:06 +01:00
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color) {
2019-08-09 14:26:20 +02:00
batch.DrawString(this.Font, text, position, color);
}
2020-03-28 22:25:06 +01:00
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
2019-08-09 14:26:20 +02:00
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
}
2020-03-28 22:25:06 +01:00
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
2019-08-09 14:26:20 +02:00
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
}
// this fixes an issue with BitmapFonts where, if only given a single character,
// only the width of the character itself (disregarding spacing) is returned
private bool SingleCharacterWidthFix(string text, out Vector2 size) {
var codePoint = char.ConvertToUtf32(text, 0);
var region = this.Font.GetCharacterRegion(codePoint);
if (region != null) {
size = new Vector2(region.XAdvance, region.Height);
return true;
}
size = default;
return false;
}
2019-08-09 14:26:20 +02:00
}
}