From 1aa522ffa195d6e38d2e153206f2b3cf8a68cfcd Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Thu, 30 Jun 2022 13:24:51 -0700 Subject: [PATCH] avoid TextBatch.Draw smashing the stack with big strings --- src/Graphics/Font/TextBatch.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Graphics/Font/TextBatch.cs b/src/Graphics/Font/TextBatch.cs index 1627bcf8..9b6d0731 100644 --- a/src/Graphics/Font/TextBatch.cs +++ b/src/Graphics/Font/TextBatch.cs @@ -35,10 +35,16 @@ namespace MoonWorks.Graphics.Font HorizontalAlignment horizontalAlignment = HorizontalAlignment.Left, VerticalAlignment verticalAlignment = VerticalAlignment.Baseline ) { - fixed (char* chars = text) + var byteCount = System.Text.Encoding.UTF8.GetByteCount(text); + + if (StringBytes.Length < byteCount) + { + System.Array.Resize(ref StringBytes, byteCount); + } + + fixed (char* chars = text) + fixed (byte* bytes = StringBytes) { - var byteCount = System.Text.Encoding.UTF8.GetByteCount(text); - var bytes = stackalloc byte[byteCount]; System.Text.Encoding.UTF8.GetBytes(chars, text.Length, bytes, byteCount); var result = Wellspring.Wellspring_Draw( @@ -57,8 +63,6 @@ namespace MoonWorks.Graphics.Font { throw new System.ArgumentException("Could not decode string!"); } - - PrimitiveCount += (uint) (text.Length * 2); } }