11using System ;
2- using System . Collections . Generic ;
32using System . Linq ;
43using Helion . Geometry ;
54using Helion . Geometry . Boxes ;
65using Helion . Geometry . Vectors ;
76using Helion . Graphics ;
87using Helion . Graphics . Fonts ;
9- using Helion . Graphics . Geometry ;
108using Helion . Render . Common . Enums ;
119using Helion . Util ;
1210using Helion . Util . Container ;
@@ -16,12 +14,10 @@ namespace Helion.Render.OpenGL.Texture.Fonts;
1614/// <summary>
1715/// A collection of render information that can be used to draw a string.
1816/// </summary>
19- public class RenderableString
17+ public partial class RenderableString
2018{
2119 public static readonly Color DefaultColor = Color . White ;
2220
23- private static readonly DynamicArray < ColorRange > ColorRanges = new ( ) ;
24-
2521 /// <summary>
2622 /// The font used when rendering this.
2723 /// </summary>
@@ -42,7 +38,6 @@ public class RenderableString
4238 /// <summary>
4339 /// Creates a rendered string that is ready to be passed to a renderer.
4440 /// </summary>
45- /// <param name="dataCache">The DataCache to use.</param>
4641 /// <param name="font">The font to use.</param>
4742 /// <param name="str">The colored string to process.</param>
4843 /// <param name="fontSize">The height of the characters, in pixels. If
@@ -52,40 +47,40 @@ public class RenderableString
5247 /// <param name="align">Alignment (only needed if there are multiple
5348 /// lines, otherwise it does not matter).</param>
5449 /// <param name="maxWidth">How wide before wrapping around.</param>
55- public RenderableString ( DataCache dataCache , ReadOnlySpan < char > str , Font font , int fontSize , TextAlign align = TextAlign . Left ,
50+ public RenderableString ( ReadOnlySpan < char > str , Font font , int fontSize , TextAlign align = TextAlign . Left ,
5651 int maxWidth = int . MaxValue , Color ? drawColor = null , bool shouldFree = true )
5752 {
5853 ShouldFree = shouldFree ;
5954 Font = font ;
60- Sentences = PopulateSentences ( dataCache , str , font , fontSize , maxWidth , drawColor ) ;
55+ Sentences = PopulateSentences ( str , font , fontSize , maxWidth , drawColor ) ;
6156 DrawArea = CalculateDrawArea ( Sentences ) ;
6257 AlignTo ( align ) ;
6358 RecalculateGlyphLocations ( ) ;
6459 }
6560
66- public void Set ( DataCache dataCache , ReadOnlySpan < char > str , Font font , int fontSize , TextAlign align = TextAlign . Left ,
61+ public void Set ( ReadOnlySpan < char > str , Font font , int fontSize , TextAlign align = TextAlign . Left ,
6762 int maxWidth = int . MaxValue , Color ? drawColor = null )
6863 {
6964 // This is kind of a hack. If reusing this string the underlying data needs to freed.
7065 if ( ! ShouldFree )
71- dataCache . FreeRenderableStringData ( this ) ;
66+ FreeData ( ) ;
7267
7368 Font = font ;
74- Sentences = PopulateSentences ( dataCache , str , font , fontSize , maxWidth , drawColor ) ;
69+ Sentences = PopulateSentences ( str , font , fontSize , maxWidth , drawColor ) ;
7570 DrawArea = CalculateDrawArea ( Sentences ) ;
7671 AlignTo ( align ) ;
7772 RecalculateGlyphLocations ( ) ;
7873 }
7974
80- public static DynamicArray < RenderableSentence > PopulateSentences ( DataCache dataCache , ReadOnlySpan < char > str , Font font , int fontSize ,
75+ public static DynamicArray < RenderableSentence > PopulateSentences ( ReadOnlySpan < char > str , Font font , int fontSize ,
8176 int maxWidth , Color ? drawColor )
8277 {
8378 int currentWidth = 0 ;
8479 int currentHeight = 0 ;
8580 int drawAreaWidth = 0 ;
8681 int drawAreaHeight = 0 ;
8782
88- var sentences = dataCache . GetRenderableSentences ( ) ;
83+ var sentences = GetSentences ( ) ;
8984 if ( str . Length == 0 )
9085 return sentences ;
9186
@@ -132,7 +127,7 @@ public static DynamicArray<RenderableSentence> PopulateSentences(DataCache dataC
132127
133128 if ( currentSentence == null )
134129 {
135- currentSentence = dataCache . GetRenderableGlyphs ( ) ;
130+ currentSentence = GetGlyphs ( ) ;
136131 currentSentence . EnsureCapacity ( str . Length ) ;
137132 }
138133
@@ -150,6 +145,8 @@ public static DynamicArray<RenderableSentence> PopulateSentences(DataCache dataC
150145 }
151146 }
152147
148+ FreeColorRange ( colorRanges ) ;
149+
153150 CreateAndAddSentenceIfPossible ( sentences , ref currentSentence , ref drawAreaWidth , ref drawAreaHeight , ref currentWidth , ref currentHeight ) ;
154151 return sentences ;
155152 }
@@ -172,38 +169,38 @@ private static void CreateAndAddSentenceIfPossible(DynamicArray<RenderableSenten
172169
173170 private static DynamicArray < ColorRange > GetColorRanges ( ReadOnlySpan < char > str , Color ? drawColor )
174171 {
175- ColorRanges . Clear ( ) ;
172+ var colorRanges = GetColorRange ( ) ;
176173 if ( drawColor != null )
177174 {
178- ColorRanges . Add ( new ColorRange ( 0 , str . Length , drawColor . Value ) ) ;
179- return ColorRanges ;
175+ colorRanges . Add ( new ColorRange ( 0 , str . Length , drawColor . Value ) ) ;
176+ return colorRanges ;
180177 }
181178
182- ColorRanges . Add ( new ColorRange ( 0 , DefaultColor ) ) ;
179+ colorRanges . Add ( new ColorRange ( 0 , DefaultColor ) ) ;
183180
184181 bool success = FindNextColorIndex ( str , 0 , out int startIndex , out int endIndex ) ;
185182 while ( success )
186183 {
187- ColorRange currentColorInfo = ColorRanges [ ColorRanges . Length - 1 ] ;
184+ ColorRange currentColorInfo = colorRanges [ colorRanges . Length - 1 ] ;
188185 currentColorInfo . EndIndex = startIndex ;
189- ColorRanges [ ColorRanges . Length - 1 ] = currentColorInfo ;
186+ colorRanges [ colorRanges . Length - 1 ] = currentColorInfo ;
190187
191188 Color color = ColorDefinitionToColor ( str . Slice ( startIndex , endIndex - startIndex ) ) ;
192- ColorRanges . Add ( new ColorRange ( endIndex , color ) ) ;
189+ colorRanges . Add ( new ColorRange ( endIndex , color ) ) ;
193190 startIndex = endIndex + 1 ;
194191 success = FindNextColorIndex ( str , startIndex , out startIndex , out endIndex ) ;
195192 }
196193
197194 // Since we never set the very last element's ending point due to
198195 // the loop invariant, we do that now.
199- var last = ColorRanges [ ColorRanges . Length - 1 ] ;
196+ var last = colorRanges [ colorRanges . Length - 1 ] ;
200197 last . EndIndex = str . Length ;
201- ColorRanges [ ColorRanges . Length - 1 ] = last ;
198+ colorRanges [ colorRanges . Length - 1 ] = last ;
202199
203200 if ( last . StartIndex == last . EndIndex )
204- ColorRanges . Length -- ;
201+ colorRanges . Length -- ;
205202
206- return ColorRanges ;
203+ return colorRanges ;
207204 }
208205
209206 private static bool FindNextColorIndex ( ReadOnlySpan < char > str , int index , out int startIndex , out int endIndex )
0 commit comments