@@ -85,121 +85,175 @@ public ImFontPtr AddFontDefault(ImFontConfigPtr font_cfg)
8585 public ImFontPtr AddFontFromFileTTF ( string filename , float size_pixels )
8686 {
8787 byte * native_filename ;
88+ int filename_byteCount = 0 ;
8889 if ( filename != null )
8990 {
90- int filename_byteCount = Encoding . UTF8 . GetByteCount ( filename ) ;
91- byte * native_filename_stackBytes = stackalloc byte [ filename_byteCount + 1 ] ;
92- native_filename = native_filename_stackBytes ;
93- fixed ( char * filename_ptr = filename )
91+ filename_byteCount = Encoding . UTF8 . GetByteCount ( filename ) ;
92+ if ( filename_byteCount > Util . StackAllocationSizeLimit )
9493 {
95- int native_filename_offset = Encoding . UTF8 . GetBytes ( filename_ptr , filename . Length , native_filename , filename_byteCount ) ;
96- native_filename [ native_filename_offset ] = 0 ;
94+ native_filename = Util . Allocate ( filename_byteCount + 1 ) ;
9795 }
96+ else
97+ {
98+ byte * native_filename_stackBytes = stackalloc byte [ filename_byteCount + 1 ] ;
99+ native_filename = native_filename_stackBytes ;
100+ }
101+ int native_filename_offset = Util . GetUtf8 ( filename , native_filename , filename_byteCount ) ;
102+ native_filename [ native_filename_offset ] = 0 ;
98103 }
99104 else { native_filename = null ; }
100105 ImFontConfig * font_cfg = null ;
101106 ushort * glyph_ranges = null ;
102107 ImFont * ret = ImGuiNative . ImFontAtlas_AddFontFromFileTTF ( NativePtr , native_filename , size_pixels , font_cfg , glyph_ranges ) ;
108+ if ( filename_byteCount > Util . StackAllocationSizeLimit )
109+ {
110+ Util . Free ( native_filename ) ;
111+ }
103112 return new ImFontPtr ( ret ) ;
104113 }
105114 public ImFontPtr AddFontFromFileTTF ( string filename , float size_pixels , ImFontConfigPtr font_cfg )
106115 {
107116 byte * native_filename ;
117+ int filename_byteCount = 0 ;
108118 if ( filename != null )
109119 {
110- int filename_byteCount = Encoding . UTF8 . GetByteCount ( filename ) ;
111- byte * native_filename_stackBytes = stackalloc byte [ filename_byteCount + 1 ] ;
112- native_filename = native_filename_stackBytes ;
113- fixed ( char * filename_ptr = filename )
120+ filename_byteCount = Encoding . UTF8 . GetByteCount ( filename ) ;
121+ if ( filename_byteCount > Util . StackAllocationSizeLimit )
122+ {
123+ native_filename = Util . Allocate ( filename_byteCount + 1 ) ;
124+ }
125+ else
114126 {
115- int native_filename_offset = Encoding . UTF8 . GetBytes ( filename_ptr , filename . Length , native_filename , filename_byteCount ) ;
116- native_filename [ native_filename_offset ] = 0 ;
127+ byte * native_filename_stackBytes = stackalloc byte [ filename_byteCount + 1 ] ;
128+ native_filename = native_filename_stackBytes ;
117129 }
130+ int native_filename_offset = Util . GetUtf8 ( filename , native_filename , filename_byteCount ) ;
131+ native_filename [ native_filename_offset ] = 0 ;
118132 }
119133 else { native_filename = null ; }
120134 ImFontConfig * native_font_cfg = font_cfg . NativePtr ;
121135 ushort * glyph_ranges = null ;
122136 ImFont * ret = ImGuiNative . ImFontAtlas_AddFontFromFileTTF ( NativePtr , native_filename , size_pixels , native_font_cfg , glyph_ranges ) ;
137+ if ( filename_byteCount > Util . StackAllocationSizeLimit )
138+ {
139+ Util . Free ( native_filename ) ;
140+ }
123141 return new ImFontPtr ( ret ) ;
124142 }
125143 public ImFontPtr AddFontFromFileTTF ( string filename , float size_pixels , ImFontConfigPtr font_cfg , IntPtr glyph_ranges )
126144 {
127145 byte * native_filename ;
146+ int filename_byteCount = 0 ;
128147 if ( filename != null )
129148 {
130- int filename_byteCount = Encoding . UTF8 . GetByteCount ( filename ) ;
131- byte * native_filename_stackBytes = stackalloc byte [ filename_byteCount + 1 ] ;
132- native_filename = native_filename_stackBytes ;
133- fixed ( char * filename_ptr = filename )
149+ filename_byteCount = Encoding . UTF8 . GetByteCount ( filename ) ;
150+ if ( filename_byteCount > Util . StackAllocationSizeLimit )
151+ {
152+ native_filename = Util . Allocate ( filename_byteCount + 1 ) ;
153+ }
154+ else
134155 {
135- int native_filename_offset = Encoding . UTF8 . GetBytes ( filename_ptr , filename . Length , native_filename , filename_byteCount ) ;
136- native_filename [ native_filename_offset ] = 0 ;
156+ byte * native_filename_stackBytes = stackalloc byte [ filename_byteCount + 1 ] ;
157+ native_filename = native_filename_stackBytes ;
137158 }
159+ int native_filename_offset = Util . GetUtf8 ( filename , native_filename , filename_byteCount ) ;
160+ native_filename [ native_filename_offset ] = 0 ;
138161 }
139162 else { native_filename = null ; }
140163 ImFontConfig * native_font_cfg = font_cfg . NativePtr ;
141164 ushort * native_glyph_ranges = ( ushort * ) glyph_ranges . ToPointer ( ) ;
142165 ImFont * ret = ImGuiNative . ImFontAtlas_AddFontFromFileTTF ( NativePtr , native_filename , size_pixels , native_font_cfg , native_glyph_ranges ) ;
166+ if ( filename_byteCount > Util . StackAllocationSizeLimit )
167+ {
168+ Util . Free ( native_filename ) ;
169+ }
143170 return new ImFontPtr ( ret ) ;
144171 }
145172 public ImFontPtr AddFontFromMemoryCompressedBase85TTF ( string compressed_font_data_base85 , float size_pixels )
146173 {
147174 byte * native_compressed_font_data_base85 ;
175+ int compressed_font_data_base85_byteCount = 0 ;
148176 if ( compressed_font_data_base85 != null )
149177 {
150- int compressed_font_data_base85_byteCount = Encoding . UTF8 . GetByteCount ( compressed_font_data_base85 ) ;
151- byte * native_compressed_font_data_base85_stackBytes = stackalloc byte [ compressed_font_data_base85_byteCount + 1 ] ;
152- native_compressed_font_data_base85 = native_compressed_font_data_base85_stackBytes ;
153- fixed ( char * compressed_font_data_base85_ptr = compressed_font_data_base85 )
178+ compressed_font_data_base85_byteCount = Encoding . UTF8 . GetByteCount ( compressed_font_data_base85 ) ;
179+ if ( compressed_font_data_base85_byteCount > Util . StackAllocationSizeLimit )
154180 {
155- int native_compressed_font_data_base85_offset = Encoding . UTF8 . GetBytes ( compressed_font_data_base85_ptr , compressed_font_data_base85 . Length , native_compressed_font_data_base85 , compressed_font_data_base85_byteCount ) ;
156- native_compressed_font_data_base85 [ native_compressed_font_data_base85_offset ] = 0 ;
181+ native_compressed_font_data_base85 = Util . Allocate ( compressed_font_data_base85_byteCount + 1 ) ;
157182 }
183+ else
184+ {
185+ byte * native_compressed_font_data_base85_stackBytes = stackalloc byte [ compressed_font_data_base85_byteCount + 1 ] ;
186+ native_compressed_font_data_base85 = native_compressed_font_data_base85_stackBytes ;
187+ }
188+ int native_compressed_font_data_base85_offset = Util . GetUtf8 ( compressed_font_data_base85 , native_compressed_font_data_base85 , compressed_font_data_base85_byteCount ) ;
189+ native_compressed_font_data_base85 [ native_compressed_font_data_base85_offset ] = 0 ;
158190 }
159191 else { native_compressed_font_data_base85 = null ; }
160192 ImFontConfig * font_cfg = null ;
161193 ushort * glyph_ranges = null ;
162194 ImFont * ret = ImGuiNative . ImFontAtlas_AddFontFromMemoryCompressedBase85TTF ( NativePtr , native_compressed_font_data_base85 , size_pixels , font_cfg , glyph_ranges ) ;
195+ if ( compressed_font_data_base85_byteCount > Util . StackAllocationSizeLimit )
196+ {
197+ Util . Free ( native_compressed_font_data_base85 ) ;
198+ }
163199 return new ImFontPtr ( ret ) ;
164200 }
165201 public ImFontPtr AddFontFromMemoryCompressedBase85TTF ( string compressed_font_data_base85 , float size_pixels , ImFontConfigPtr font_cfg )
166202 {
167203 byte * native_compressed_font_data_base85 ;
204+ int compressed_font_data_base85_byteCount = 0 ;
168205 if ( compressed_font_data_base85 != null )
169206 {
170- int compressed_font_data_base85_byteCount = Encoding . UTF8 . GetByteCount ( compressed_font_data_base85 ) ;
171- byte * native_compressed_font_data_base85_stackBytes = stackalloc byte [ compressed_font_data_base85_byteCount + 1 ] ;
172- native_compressed_font_data_base85 = native_compressed_font_data_base85_stackBytes ;
173- fixed ( char * compressed_font_data_base85_ptr = compressed_font_data_base85 )
207+ compressed_font_data_base85_byteCount = Encoding . UTF8 . GetByteCount ( compressed_font_data_base85 ) ;
208+ if ( compressed_font_data_base85_byteCount > Util . StackAllocationSizeLimit )
209+ {
210+ native_compressed_font_data_base85 = Util . Allocate ( compressed_font_data_base85_byteCount + 1 ) ;
211+ }
212+ else
174213 {
175- int native_compressed_font_data_base85_offset = Encoding . UTF8 . GetBytes ( compressed_font_data_base85_ptr , compressed_font_data_base85 . Length , native_compressed_font_data_base85 , compressed_font_data_base85_byteCount ) ;
176- native_compressed_font_data_base85 [ native_compressed_font_data_base85_offset ] = 0 ;
214+ byte * native_compressed_font_data_base85_stackBytes = stackalloc byte [ compressed_font_data_base85_byteCount + 1 ] ;
215+ native_compressed_font_data_base85 = native_compressed_font_data_base85_stackBytes ;
177216 }
217+ int native_compressed_font_data_base85_offset = Util . GetUtf8 ( compressed_font_data_base85 , native_compressed_font_data_base85 , compressed_font_data_base85_byteCount ) ;
218+ native_compressed_font_data_base85 [ native_compressed_font_data_base85_offset ] = 0 ;
178219 }
179220 else { native_compressed_font_data_base85 = null ; }
180221 ImFontConfig * native_font_cfg = font_cfg . NativePtr ;
181222 ushort * glyph_ranges = null ;
182223 ImFont * ret = ImGuiNative . ImFontAtlas_AddFontFromMemoryCompressedBase85TTF ( NativePtr , native_compressed_font_data_base85 , size_pixels , native_font_cfg , glyph_ranges ) ;
224+ if ( compressed_font_data_base85_byteCount > Util . StackAllocationSizeLimit )
225+ {
226+ Util . Free ( native_compressed_font_data_base85 ) ;
227+ }
183228 return new ImFontPtr ( ret ) ;
184229 }
185230 public ImFontPtr AddFontFromMemoryCompressedBase85TTF ( string compressed_font_data_base85 , float size_pixels , ImFontConfigPtr font_cfg , IntPtr glyph_ranges )
186231 {
187232 byte * native_compressed_font_data_base85 ;
233+ int compressed_font_data_base85_byteCount = 0 ;
188234 if ( compressed_font_data_base85 != null )
189235 {
190- int compressed_font_data_base85_byteCount = Encoding . UTF8 . GetByteCount ( compressed_font_data_base85 ) ;
191- byte * native_compressed_font_data_base85_stackBytes = stackalloc byte [ compressed_font_data_base85_byteCount + 1 ] ;
192- native_compressed_font_data_base85 = native_compressed_font_data_base85_stackBytes ;
193- fixed ( char * compressed_font_data_base85_ptr = compressed_font_data_base85 )
236+ compressed_font_data_base85_byteCount = Encoding . UTF8 . GetByteCount ( compressed_font_data_base85 ) ;
237+ if ( compressed_font_data_base85_byteCount > Util . StackAllocationSizeLimit )
238+ {
239+ native_compressed_font_data_base85 = Util . Allocate ( compressed_font_data_base85_byteCount + 1 ) ;
240+ }
241+ else
194242 {
195- int native_compressed_font_data_base85_offset = Encoding . UTF8 . GetBytes ( compressed_font_data_base85_ptr , compressed_font_data_base85 . Length , native_compressed_font_data_base85 , compressed_font_data_base85_byteCount ) ;
196- native_compressed_font_data_base85 [ native_compressed_font_data_base85_offset ] = 0 ;
243+ byte * native_compressed_font_data_base85_stackBytes = stackalloc byte [ compressed_font_data_base85_byteCount + 1 ] ;
244+ native_compressed_font_data_base85 = native_compressed_font_data_base85_stackBytes ;
197245 }
246+ int native_compressed_font_data_base85_offset = Util . GetUtf8 ( compressed_font_data_base85 , native_compressed_font_data_base85 , compressed_font_data_base85_byteCount ) ;
247+ native_compressed_font_data_base85 [ native_compressed_font_data_base85_offset ] = 0 ;
198248 }
199249 else { native_compressed_font_data_base85 = null ; }
200250 ImFontConfig * native_font_cfg = font_cfg . NativePtr ;
201251 ushort * native_glyph_ranges = ( ushort * ) glyph_ranges . ToPointer ( ) ;
202252 ImFont * ret = ImGuiNative . ImFontAtlas_AddFontFromMemoryCompressedBase85TTF ( NativePtr , native_compressed_font_data_base85 , size_pixels , native_font_cfg , native_glyph_ranges ) ;
253+ if ( compressed_font_data_base85_byteCount > Util . StackAllocationSizeLimit )
254+ {
255+ Util . Free ( native_compressed_font_data_base85 ) ;
256+ }
203257 return new ImFontPtr ( ret ) ;
204258 }
205259 public ImFontPtr AddFontFromMemoryCompressedTTF ( IntPtr compressed_font_data , int compressed_font_size , float size_pixels )
0 commit comments