Skip to content

Commit 93d375a

Browse files
zaafarmellinoe
authored andcommitted
handling comments
- Adding overloaded functions for CalcTextSize - Making CalcTextSizeImpl private - Removing the use of textByteSize
1 parent 33aefc0 commit 93d375a

File tree

2 files changed

+66
-13
lines changed

2 files changed

+66
-13
lines changed

src/ImGui.NET/ImGui.Manual.cs

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -355,42 +355,74 @@ public static bool InputTextWithHint(
355355
return result != 0;
356356
}
357357

358-
public static Vector2 CalcTextSize(
358+
public static Vector2 CalcTextSize(string text)
359+
=> CalcTextSizeImpl(text);
360+
361+
public static Vector2 CalcTextSize(string text, int start)
362+
=> CalcTextSizeImpl(text, start);
363+
364+
public static Vector2 CalcTextSize(string text, float wrapWidth)
365+
=> CalcTextSizeImpl(text, wrapWidth: wrapWidth);
366+
367+
public static Vector2 CalcTextSize(string text, bool hideTextAfterDoubleHash)
368+
=> CalcTextSizeImpl(text, hideTextAfterDoubleHash: hideTextAfterDoubleHash);
369+
370+
public static Vector2 CalcTextSize(string text, int start, int length)
371+
=> CalcTextSizeImpl(text, start, length);
372+
373+
public static Vector2 CalcTextSize(string text, int start, bool hideTextAfterDoubleHash)
374+
=> CalcTextSizeImpl(text, start, hideTextAfterDoubleHash: hideTextAfterDoubleHash);
375+
376+
public static Vector2 CalcTextSize(string text, int start, float wrapWidth)
377+
=> CalcTextSizeImpl(text, start, wrapWidth: wrapWidth);
378+
379+
public static Vector2 CalcTextSize(string text, bool hideTextAfterDoubleHash, float wrapWidth)
380+
=> CalcTextSizeImpl(text, hideTextAfterDoubleHash: hideTextAfterDoubleHash, wrapWidth: wrapWidth);
381+
382+
public static Vector2 CalcTextSize(string text, int start, int length, bool hideTextAfterDoubleHash)
383+
=> CalcTextSizeImpl(text, start, length, hideTextAfterDoubleHash);
384+
385+
public static Vector2 CalcTextSize(string text, int start, int length, float wrapWidth)
386+
=> CalcTextSizeImpl(text, start, length, wrapWidth: wrapWidth);
387+
388+
public static Vector2 CalcTextSize(string text, int start, int length, bool hideTextAfterDoubleHash, float wrapWidth)
389+
=> CalcTextSizeImpl(text, start, length, hideTextAfterDoubleHash, wrapWidth);
390+
391+
private static Vector2 CalcTextSizeImpl(
359392
string text,
360-
int? start = null,
393+
int start = 0,
361394
int? length = null,
362395
bool hideTextAfterDoubleHash = false,
363396
float wrapWidth = -1.0f)
364397
{
365398
Vector2 ret;
366-
byte* nativeText = null;
367399
byte* nativeTextStart = null;
368400
byte* nativeTextEnd = null;
369401
int textByteCount = 0;
370-
int textByteSize = 0;
371402
if (text != null)
372403
{
373-
textByteCount = Encoding.UTF8.GetByteCount(text);
374-
textByteSize = Encoding.UTF8.GetByteCount("X");
404+
405+
int textToCopyLen = length.HasValue ? length.Value : text.Length;
406+
textByteCount = Util.CalcUtf8(text, start, textToCopyLen);
375407
if (textByteCount > Util.StackAllocationSizeLimit)
376408
{
377-
nativeText = Util.Allocate(textByteCount + 1);
409+
nativeTextStart = Util.Allocate(textByteCount + 1);
378410
}
379411
else
380412
{
381413
byte* nativeTextStackBytes = stackalloc byte[textByteCount + 1];
382-
nativeText = nativeTextStackBytes;
414+
nativeTextStart = nativeTextStackBytes;
383415
}
384-
int nativeTextOffset = Util.GetUtf8(text, nativeText, textByteCount);
385-
nativeText[nativeTextOffset] = 0;
386-
nativeTextStart = nativeText + (start.HasValue ? (start.Value * textByteSize) : 0);
387-
nativeTextEnd = length.HasValue ? nativeTextStart + (length.Value * textByteSize) : null;
416+
417+
int nativeTextOffset = Util.GetUtf8(text, nativeTextStart, textByteCount, start, textToCopyLen);
418+
nativeTextStart[nativeTextOffset] = 0;
419+
nativeTextEnd = nativeTextStart + nativeTextOffset;
388420
}
389421

390422
ImGuiNative.igCalcTextSize(&ret, nativeTextStart, nativeTextEnd, *((byte*)(&hideTextAfterDoubleHash)), wrapWidth);
391423
if (textByteCount > Util.StackAllocationSizeLimit)
392424
{
393-
Util.Free(nativeText);
425+
Util.Free(nativeTextStart);
394426
}
395427

396428
return ret;

src/ImGui.NET/Util.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,33 @@ internal static bool AreStringsEqual(byte* a, int aLength, byte* b)
3333

3434
internal static byte* Allocate(int byteCount) => (byte*)Marshal.AllocHGlobal(byteCount);
3535
internal static void Free(byte* ptr) => Marshal.FreeHGlobal((IntPtr)ptr);
36+
internal static int CalcUtf8(string s, int start, int length)
37+
{
38+
if (start > s.Length - 1 || length > s.Length || start + length > s.Length)
39+
{
40+
throw new ArgumentOutOfRangeException();
41+
}
42+
43+
fixed (char* utf16Ptr = s)
44+
{
45+
return Encoding.UTF8.GetByteCount(utf16Ptr + start, length);
46+
}
47+
}
48+
3649
internal static int GetUtf8(string s, byte* utf8Bytes, int utf8ByteCount)
3750
{
3851
fixed (char* utf16Ptr = s)
3952
{
4053
return Encoding.UTF8.GetBytes(utf16Ptr, s.Length, utf8Bytes, utf8ByteCount);
4154
}
4255
}
56+
57+
internal static int GetUtf8(string s, byte* utf8Bytes, int utf8ByteCount, int start = 0, int length = 0)
58+
{
59+
fixed (char* utf16Ptr = s)
60+
{
61+
return Encoding.UTF8.GetBytes(utf16Ptr + start, length, utf8Bytes, utf8ByteCount);
62+
}
63+
}
4364
}
4465
}

0 commit comments

Comments
 (0)