Skip to content

Commit 7f2821f

Browse files
committed
Reapply "Reapply "Improved TextToy image formatting""
This reverts commit 1ac3698.
1 parent c5f46b4 commit 7f2821f

2 files changed

Lines changed: 26 additions & 27 deletions

File tree

LabExtended/API/Images/Conversion/ToyStringImageConvertor.cs

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
using NorthwoodLib.Pools;
1+
using LabExtended.API.Toys;
2+
3+
using NorthwoodLib.Pools;
4+
25
using LabExtended.Utilities;
6+
37
using UnityEngine;
8+
49
using Color = System.Drawing.Color;
510

611
namespace LabExtended.API.Images.Conversion;
@@ -36,7 +41,6 @@ public class ToyStringImageData
3641
/// </summary>
3742
public static char Character { get; set; } = '█';
3843

39-
// TODO: Optimize the string conversion - aka lets not have a different collection item for each row.
4044
/// <summary>
4145
/// Assigns text toy data to each image frame.
4246
/// </summary>
@@ -47,26 +51,26 @@ public static void ConvertImage(ImageFile file)
4751
if (file is null)
4852
throw new ArgumentNullException(nameof(file));
4953

54+
file.toyDisplaySize = new(file.Height, file.Width);
55+
5056
var builder = StringBuilderPool.Shared.Rent();
5157

5258
Color? last = null;
5359

54-
file.toyDisplaySize = new(file.Height, file.Width);
55-
5660
for (var index = 0; index < file.Frames.Count; index++)
5761
{
5862
var frame = file.Frames[index];
5963

64+
builder.Clear();
65+
66+
builder.Append("<size=");
67+
builder.Append(file.toyStringImageData.CharacterSize);
68+
builder.Append("%><line-height=");
69+
builder.Append(file.toyStringImageData.CharacterHeight);
70+
builder.Append("%>");
71+
6072
for (var x = 0; x < frame.Pixels.Count; x++)
6173
{
62-
builder.Clear();
63-
64-
builder.Append("<size=");
65-
builder.Append(file.toyStringImageData.CharacterSize);
66-
builder.Append("%><line-height=");
67-
builder.Append(file.toyStringImageData.CharacterHeight);
68-
builder.Append("%>");
69-
7074
var pixels = frame.Pixels[x];
7175

7276
for (var y = 0; y < pixels.Count; y++)
@@ -78,29 +82,19 @@ public static void ConvertImage(ImageFile file)
7882
builder.Append("<color=");
7983
builder.Append(pixel.Color.ToShortHex());
8084
builder.Append(">");
81-
85+
8286
last = pixel.Color;
8387
}
8488

8589
builder.Append(Character);
8690
}
8791

88-
builder.Append("</color>");
89-
90-
frame.toyFrameData.Add(builder.ToString());
91-
}
92-
93-
builder.Clear();
94-
95-
for (var i = 0; i < file.Height; i++)
96-
{
97-
builder.Append("{");
98-
builder.Append(i);
99-
builder.Append("}");
100-
builder.Append("\\n");
92+
builder.AppendLine();
10193
}
10294

103-
frame.toyFrameFormat = builder.ToString();
95+
builder.Append("</color>");
96+
97+
TextToy.SplitStringNonAlloc(builder.ToString(), frame.toyFrameData, out frame.toyFrameFormat);
10498
}
10599

106100
StringBuilderPool.Shared.Return(builder);

LabExtended/API/MirrorMethods.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ namespace LabExtended.API;
2222
/// </summary>
2323
public static class MirrorMethods
2424
{
25+
/// <summary>
26+
/// Gets the maximum length of a string.
27+
/// </summary>
28+
public const int MaxStringLength = 65534;
29+
2530
/// <summary>
2631
/// Gets full names of all RPCs.
2732
/// <remarks>Keys are formatted as the declaring type of the property and then the name of the property

0 commit comments

Comments
 (0)