1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-29 03:23:37 +02:00
MLEM/MLEM/Font/GenericFont.cs

85 lines
3.9 KiB
C#
Raw Normal View History

2019-08-06 14:20:11 +02:00
using System.Collections.Generic;
using System.Text;
2020-03-28 22:25:06 +01:00
using Microsoft.Xna.Framework;
2019-08-06 14:20:11 +02:00
using Microsoft.Xna.Framework.Graphics;
2020-03-28 22:25:06 +01:00
namespace MLEM.Font {
public abstract class GenericFont {
2019-08-06 14:20:11 +02:00
2020-03-28 22:25:06 +01:00
public abstract float LineHeight { get; }
2020-03-28 22:25:06 +01:00
public abstract Vector2 MeasureString(string text);
public abstract Vector2 MeasureString(StringBuilder text);
public abstract void DrawString(SpriteBatch batch, string text, Vector2 position, Color color);
public abstract void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth);
public abstract void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth);
public abstract void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color);
public abstract void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth);
public abstract void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth);
2019-09-05 18:15:51 +02:00
2020-03-28 22:25:06 +01:00
public string TruncateString(string text, float width, float scale, bool fromBack = false) {
2019-09-05 18:15:51 +02:00
var total = new StringBuilder();
for (var i = 0; i < text.Length; i++) {
if (fromBack) {
total.Insert(0, text[text.Length - 1 - i]);
} else {
total.Append(text[i]);
}
2020-03-28 22:25:06 +01:00
if (this.MeasureString(total).X * scale >= width)
2019-09-05 18:15:51 +02:00
return total.ToString(fromBack ? 1 : 0, total.Length - 1);
}
return total.ToString();
}
2020-03-28 22:25:06 +01:00
public string SplitString(string text, float width, float scale) {
var total = new StringBuilder();
2019-08-24 00:07:54 +02:00
foreach (var line in text.Split('\n')) {
var curr = new StringBuilder();
2019-08-24 00:07:54 +02:00
foreach (var word in line.Split(' ')) {
2020-03-28 22:25:06 +01:00
if (this.MeasureString(word).X * scale >= width) {
if (curr.Length > 0) {
2020-02-08 18:25:49 +01:00
total.Append(curr).Append('\n');
curr.Clear();
}
var wordBuilder = new StringBuilder();
for (var i = 0; i < word.Length; i++) {
wordBuilder.Append(word[i]);
2020-03-28 22:25:06 +01:00
if (this.MeasureString(wordBuilder.ToString()).X * scale >= width) {
total.Append(wordBuilder.ToString(0, wordBuilder.Length - 1)).Append('\n');
wordBuilder.Remove(0, wordBuilder.Length - 1);
}
}
curr.Append(wordBuilder).Append(' ');
} else {
curr.Append(word).Append(' ');
2020-03-28 22:25:06 +01:00
if (this.MeasureString(curr.ToString()).X * scale >= width) {
var len = curr.Length - word.Length - 1;
if (len > 0) {
total.Append(curr.ToString(0, len)).Append('\n');
curr.Remove(0, len);
}
}
2019-08-24 00:07:54 +02:00
}
2019-08-06 14:20:11 +02:00
}
total.Append(curr).Append('\n');
2019-08-06 14:20:11 +02:00
}
return total.ToString(0, total.Length - 2);
2019-08-06 14:20:11 +02:00
}
2020-03-28 22:25:06 +01:00
public string GetWidthString(float width, char content = ' ') {
var strg = content.ToString();
2020-03-28 22:25:06 +01:00
while (this.MeasureString(strg).X < width)
strg += content;
return strg;
}
2019-08-06 14:20:11 +02:00
}
}