2019-08-25 19:07:45 +02:00
|
|
|
using System;
|
2019-08-06 14:20:11 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Text;
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
2020-03-21 00:12:09 +01:00
|
|
|
using MLEM.Font;
|
2019-08-06 14:20:11 +02:00
|
|
|
|
|
|
|
namespace MLEM.Extensions {
|
|
|
|
public static class SpriteFontExtensions {
|
|
|
|
|
2019-08-25 19:07:45 +02:00
|
|
|
public static string SplitString(this SpriteFont font, string text, float width, float scale) {
|
|
|
|
return SplitString(s => font.MeasureString(s).X, text, width, scale);
|
|
|
|
}
|
|
|
|
|
2019-09-05 18:15:51 +02:00
|
|
|
public static string TruncateString(this SpriteFont font, string text, float width, float scale, bool fromBack = false) {
|
|
|
|
return TruncateString(s => font.MeasureString(s).X, text, width, scale, fromBack);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string TruncateString(Func<StringBuilder, float> widthFunc, string text, float width, float scale, bool fromBack = false) {
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (widthFunc(total) * scale >= width)
|
|
|
|
return total.ToString(fromBack ? 1 : 0, total.Length - 1);
|
|
|
|
}
|
|
|
|
return total.ToString();
|
|
|
|
}
|
|
|
|
|
2020-02-06 02:27:21 +01:00
|
|
|
public static string SplitString(Func<string, float> widthFunc, string text, float width, float scale) {
|
2019-08-25 19:07:45 +02:00
|
|
|
var total = new StringBuilder();
|
2019-08-24 00:07:54 +02:00
|
|
|
foreach (var line in text.Split('\n')) {
|
2019-08-25 19:07:45 +02:00
|
|
|
var curr = new StringBuilder();
|
2019-08-24 00:07:54 +02:00
|
|
|
foreach (var word in line.Split(' ')) {
|
2020-02-06 02:27:21 +01:00
|
|
|
if (widthFunc(word) * scale >= width) {
|
|
|
|
if (curr.Length > 0) {
|
2020-02-08 18:25:49 +01:00
|
|
|
total.Append(curr).Append('\n');
|
2020-02-06 02:27:21 +01:00
|
|
|
curr.Clear();
|
|
|
|
}
|
|
|
|
var wordBuilder = new StringBuilder();
|
|
|
|
for (var i = 0; i < word.Length; i++) {
|
|
|
|
wordBuilder.Append(word[i]);
|
|
|
|
if (widthFunc(wordBuilder.ToString()) * 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(' ');
|
|
|
|
if (widthFunc(curr.ToString()) * scale >= width) {
|
|
|
|
var len = curr.Length - word.Length - 1;
|
|
|
|
if (len > 0) {
|
2020-02-08 18:24:14 +01:00
|
|
|
total.Append(curr.ToString(0, len)).Append('\n');
|
2020-02-06 02:27:21 +01:00
|
|
|
curr.Remove(0, len);
|
|
|
|
}
|
2019-09-06 11:25:31 +02:00
|
|
|
}
|
2019-08-24 00:07:54 +02:00
|
|
|
}
|
2019-08-06 14:20:11 +02:00
|
|
|
}
|
2020-02-08 18:24:14 +01:00
|
|
|
total.Append(curr).Append('\n');
|
2019-08-06 14:20:11 +02:00
|
|
|
}
|
2020-03-19 03:50:01 +01:00
|
|
|
return total.ToString(0, total.Length - 2);
|
2019-08-06 14:20:11 +02:00
|
|
|
}
|
|
|
|
|
2020-03-21 00:12:09 +01:00
|
|
|
public static string GetWidthString(IGenericFont font, float width, char content = ' ') {
|
|
|
|
var strg = content.ToString();
|
|
|
|
while (font.MeasureString(strg).X < width)
|
|
|
|
strg += content;
|
|
|
|
return strg;
|
|
|
|
}
|
|
|
|
|
2019-08-06 14:20:11 +02:00
|
|
|
}
|
|
|
|
}
|