Skip to content

Commit 0e6ed64

Browse files
zaafarmellinoe
authored andcommitted
CalcTextSize now supports all arguments
1 parent 351a3de commit 0e6ed64

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/ImGui.NET/ImGui.Manual.cs

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

358+
public static Vector2 CalcTextSize(
359+
string text,
360+
int? start = null,
361+
int? length = null,
362+
bool hideTextAfterDoubleHash = false,
363+
float wrapWidth = -1.0f)
364+
{
365+
Vector2 ret;
366+
byte* nativeText = null;
367+
byte* nativeTextStart = null;
368+
byte* nativeTextEnd = null;
369+
int textByteCount = 0;
370+
int textByteSize = 0;
371+
if (text != null)
372+
{
373+
textByteCount = Encoding.UTF8.GetByteCount(text);
374+
textByteSize = Encoding.UTF8.GetByteCount("X");
375+
if (textByteCount > Util.StackAllocationSizeLimit)
376+
{
377+
nativeText = Util.Allocate(textByteCount + 1);
378+
}
379+
else
380+
{
381+
byte* nativeTextStackBytes = stackalloc byte[textByteCount + 1];
382+
nativeText = nativeTextStackBytes;
383+
}
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;
388+
}
389+
390+
ImGuiNative.igCalcTextSize(&ret, nativeTextStart, nativeTextEnd, *((byte*)(&hideTextAfterDoubleHash)), wrapWidth);
391+
if (textByteCount > Util.StackAllocationSizeLimit)
392+
{
393+
Util.Free(nativeText);
394+
}
395+
396+
return ret;
397+
}
398+
358399
public static bool InputText(
359400
string label,
360401
IntPtr buf,

0 commit comments

Comments
 (0)