@@ -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 ;
0 commit comments