1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 12:58:33 +01:00

fixed formatting codes at the beginning of strings being ignored

This commit is contained in:
Ellpeck 2020-05-30 19:17:18 +02:00
parent c41875d574
commit 78049c9cd8
2 changed files with 9 additions and 5 deletions

View file

@ -57,6 +57,10 @@ namespace MLEM.Formatting {
public TokenizedString Tokenize(GenericFont font, string s) {
var tokens = new List<Token>();
var codes = new List<Code>();
// add the formatting code right at the start of the string
var firstCode = this.GetNextCode(s, 0, 0);
if (firstCode != null)
codes.Add(firstCode);
var rawIndex = 0;
while (rawIndex < s.Length) {
var index = StripFormatting(font, s.Substring(0, rawIndex), tokens.SelectMany(t => t.AppliedCodes)).Length;
@ -82,10 +86,10 @@ namespace MLEM.Formatting {
return new TokenizedString(font, s, StripFormatting(font, s, tokens.SelectMany(t => t.AppliedCodes)), tokens.ToArray());
}
private Code GetNextCode(string s, int index) {
private Code GetNextCode(string s, int index, int maxIndex = int.MaxValue) {
var (c, m, r) = this.Codes
.Select(kv => (c: kv.Value, m: kv.Key.Match(s, index), r: kv.Key))
.Where(kv => kv.m.Success)
.Where(kv => kv.m.Success && kv.m.Index <= maxIndex)
.OrderBy(kv => kv.m.Index)
.FirstOrDefault();
return c?.Invoke(this, m, r);

View file

@ -124,21 +124,21 @@ namespace Sandbox {
var formatter = new TextFormatter();
formatter.AddImage("Test", new TextureRegion(tex, 0, 8, 24, 24));
var strg = "This <c CornflowerBlue>is a formatted string</c> with <c #ffff0000>two bits of formatting</c>! It also includesavery<c Pink>long</c>wordthatis<c Blue>formatted</c>aswell. Additionally, it <a wobbly>wobbles</a> and has a <s>shadow</s> or a <s #ffff0000 4>weird shadow</s>. We like icons too! <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test><i Test><i Test><i Test><i Test><i Test><i Test><i Test><i Test><i Test><i Test><i Test><i Test>";
var strg = "<s>This</s> <c CornflowerBlue>is a formatted string</c> with <c #ffff0000>two bits of formatting</c>! It also includesavery<c Pink>long</c>wordthatis<c Blue>formatted</c>aswell. Additionally, it <a wobbly>wobbles</a> and has a <s>shadow</s> or a <s #ffff0000 4>weird shadow</s>. We like icons too! <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test><i Test><i Test><i Test><i Test><i Test><i Test><i Test><i Test><i Test><i Test><i Test><i Test>";
//var strg = "Lorem Ipsum <i Test> is simply dummy text of the <i Test> printing and typesetting <i Test> industry. Lorem Ipsum has been the industry's standard dummy text <i Test> ever since the <i Test> 1500s, when <i Test><i Test><i Test><i Test><i Test><i Test><i Test> an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
//var strg = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
//var strg = "This is <u>a test of the underlined formatting code</u>!";
this.tokenized = formatter.Tokenize(font, strg);
this.tokenized.Split(font, 400, 5);
/*this.OnDraw += (g, time) => {
this.OnDraw += (g, time) => {
this.SpriteBatch.Begin(samplerState: SamplerState.PointClamp);
this.SpriteBatch.FillRectangle(new RectangleF(400, 20, 400, 1000), Color.Green);
font.DrawString(this.SpriteBatch, this.tokenized.DisplayString, new Vector2(400, 20), Color.White * 0.25F, 0, Vector2.Zero, 5, SpriteEffects.None, 0);
this.tokenized.Draw(time, this.SpriteBatch, new Vector2(400, 20), font, Color.White, 5, 0);
this.SpriteBatch.DrawGrid(new Vector2(30, 30), new Vector2(40, 60), new Point(10, 5), Color.Yellow, 3);
this.SpriteBatch.End();
};*/
};
this.OnUpdate += (g, time) => {
if (this.InputHandler.IsPressed(Keys.W)) {
this.tokenized = formatter.Tokenize(font, strg);