Skip to content

Commit 666b239

Browse files
committed
Improve the marshalling of ImWchar* parameters (glyph ranges, mainly)
1 parent b4734ca commit 666b239

6 files changed

Lines changed: 136 additions & 141 deletions

File tree

src/CodeGenerator/Program.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -694,11 +694,12 @@ private static void EmitOverload(
694694
preCallLines.Add($"byte* {nativeArgName} = &{nativeArgName}_val;");
695695
postCallLines.Add($"{correctedIdentifier} = {nativeArgName}_val != 0;");
696696
}
697-
else if (tr.Type == "void*")
697+
else if (tr.Type == "void*" || tr.Type == "ImWchar*")
698698
{
699+
string nativePtrTypeName = tr.Type == "void*" ? "void*" : "ushort*";
699700
string nativeArgName = "native_" + tr.Name;
700701
marshalledParameters[i] = new MarshalledParameter("IntPtr", false, nativeArgName, false);
701-
preCallLines.Add($"void* {nativeArgName} = {correctedIdentifier}.ToPointer();");
702+
preCallLines.Add($"{nativePtrTypeName} {nativeArgName} = ({nativePtrTypeName}){correctedIdentifier}.ToPointer();");
702703
}
703704
else if (GetWrappedType(tr.Type, out string wrappedParamType)
704705
&& !s_wellKnownTypes.ContainsKey(tr.Type)
@@ -795,6 +796,10 @@ private static void EmitOverload(
795796
{
796797
writer.WriteLine("return Util.StringFromPtr(ret);");
797798
}
799+
else if (overload.ReturnType == "ImWchar*")
800+
{
801+
writer.WriteLine("return (IntPtr)ret;");
802+
}
798803
else if (overload.ReturnType == "void*")
799804
{
800805
writer.WriteLine("return (IntPtr)ret;");
@@ -829,7 +834,7 @@ private static string GetSafeType(string nativeRet)
829834
{
830835
return "string";
831836
}
832-
else if (nativeRet == "void*")
837+
else if (nativeRet == "ImWchar*" || nativeRet == "void*")
833838
{
834839
return "IntPtr";
835840
}

src/ImGui.NET/Generated/GlyphRangesBuilder.gen.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,10 @@ public void AddText(string text)
3434
byte* native_text_end = null;
3535
ImGuiNative.GlyphRangesBuilder_AddText(NativePtr, native_text, native_text_end);
3636
}
37-
public void AddRanges(ref ushort ranges)
37+
public void AddRanges(IntPtr ranges)
3838
{
39-
fixed (ushort* native_ranges = &ranges)
40-
{
41-
ImGuiNative.GlyphRangesBuilder_AddRanges(NativePtr, native_ranges);
42-
}
39+
ushort* native_ranges = (ushort*)ranges.ToPointer();
40+
ImGuiNative.GlyphRangesBuilder_AddRanges(NativePtr, native_ranges);
4341
}
4442
public void BuildRanges(out ImVector out_ranges)
4543
{

src/ImGui.NET/Generated/ImDrawList.gen.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public void AddRect(Vector2 a, Vector2 b, uint col, float rounding, int rounding
179179
}
180180
public void AddCallback(IntPtr callback, IntPtr callback_data)
181181
{
182-
void* native_callback_data = callback_data.ToPointer();
182+
void* native_callback_data = (void*)callback_data.ToPointer();
183183
ImGuiNative.ImDrawList_AddCallback(NativePtr, callback, native_callback_data);
184184
}
185185
public void PathRect(Vector2 rect_min, Vector2 rect_max)

src/ImGui.NET/Generated/ImFontAtlas.gen.cs

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public ImFontPtr AddFontFromMemoryCompressedBase85TTF(string compressed_font_dat
7474
ImFont* ret = ImGuiNative.ImFontAtlas_AddFontFromMemoryCompressedBase85TTF(NativePtr, native_compressed_font_data_base85, size_pixels, native_font_cfg, glyph_ranges);
7575
return new ImFontPtr(ret);
7676
}
77-
public ImFontPtr AddFontFromMemoryCompressedBase85TTF(string compressed_font_data_base85, float size_pixels, ImFontConfigPtr font_cfg, ref ushort glyph_ranges)
77+
public ImFontPtr AddFontFromMemoryCompressedBase85TTF(string compressed_font_data_base85, float size_pixels, ImFontConfigPtr font_cfg, IntPtr glyph_ranges)
7878
{
7979
int compressed_font_data_base85_byteCount = Encoding.UTF8.GetByteCount(compressed_font_data_base85);
8080
byte* native_compressed_font_data_base85 = stackalloc byte[compressed_font_data_base85_byteCount + 1];
@@ -84,11 +84,9 @@ public ImFontPtr AddFontFromMemoryCompressedBase85TTF(string compressed_font_dat
8484
native_compressed_font_data_base85[native_compressed_font_data_base85_offset] = 0;
8585
}
8686
ImFontConfig* native_font_cfg = font_cfg.NativePtr;
87-
fixed (ushort* native_glyph_ranges = &glyph_ranges)
88-
{
89-
ImFont* ret = ImGuiNative.ImFontAtlas_AddFontFromMemoryCompressedBase85TTF(NativePtr, native_compressed_font_data_base85, size_pixels, native_font_cfg, native_glyph_ranges);
90-
return new ImFontPtr(ret);
91-
}
87+
ushort* native_glyph_ranges = (ushort*)glyph_ranges.ToPointer();
88+
ImFont* ret = ImGuiNative.ImFontAtlas_AddFontFromMemoryCompressedBase85TTF(NativePtr, native_compressed_font_data_base85, size_pixels, native_font_cfg, native_glyph_ranges);
89+
return new ImFontPtr(ret);
9290
}
9391
public bool Build()
9492
{
@@ -129,30 +127,30 @@ public bool IsBuilt()
129127
byte ret = ImGuiNative.ImFontAtlas_IsBuilt(NativePtr);
130128
return ret != 0;
131129
}
132-
public ushort* GetGlyphRangesThai()
130+
public IntPtr GetGlyphRangesThai()
133131
{
134132
ushort* ret = ImGuiNative.ImFontAtlas_GetGlyphRangesThai(NativePtr);
135-
return ret;
133+
return (IntPtr)ret;
136134
}
137-
public ushort* GetGlyphRangesCyrillic()
135+
public IntPtr GetGlyphRangesCyrillic()
138136
{
139137
ushort* ret = ImGuiNative.ImFontAtlas_GetGlyphRangesCyrillic(NativePtr);
140-
return ret;
138+
return (IntPtr)ret;
141139
}
142-
public ushort* GetGlyphRangesChineseSimplifiedCommon()
140+
public IntPtr GetGlyphRangesChineseSimplifiedCommon()
143141
{
144142
ushort* ret = ImGuiNative.ImFontAtlas_GetGlyphRangesChineseSimplifiedCommon(NativePtr);
145-
return ret;
143+
return (IntPtr)ret;
146144
}
147-
public ushort* GetGlyphRangesChineseFull()
145+
public IntPtr GetGlyphRangesChineseFull()
148146
{
149147
ushort* ret = ImGuiNative.ImFontAtlas_GetGlyphRangesChineseFull(NativePtr);
150-
return ret;
148+
return (IntPtr)ret;
151149
}
152-
public ushort* GetGlyphRangesDefault()
150+
public IntPtr GetGlyphRangesDefault()
153151
{
154152
ushort* ret = ImGuiNative.ImFontAtlas_GetGlyphRangesDefault(NativePtr);
155-
return ret;
153+
return (IntPtr)ret;
156154
}
157155
public void SetTexID(IntPtr id)
158156
{
@@ -172,55 +170,51 @@ public void Clear()
172170
}
173171
public ImFontPtr AddFontFromMemoryCompressedTTF(IntPtr compressed_font_data, int compressed_font_size, float size_pixels)
174172
{
175-
void* native_compressed_font_data = compressed_font_data.ToPointer();
173+
void* native_compressed_font_data = (void*)compressed_font_data.ToPointer();
176174
ImFontConfig* font_cfg = null;
177175
ushort* glyph_ranges = null;
178176
ImFont* ret = ImGuiNative.ImFontAtlas_AddFontFromMemoryCompressedTTF(NativePtr, native_compressed_font_data, compressed_font_size, size_pixels, font_cfg, glyph_ranges);
179177
return new ImFontPtr(ret);
180178
}
181179
public ImFontPtr AddFontFromMemoryCompressedTTF(IntPtr compressed_font_data, int compressed_font_size, float size_pixels, ImFontConfigPtr font_cfg)
182180
{
183-
void* native_compressed_font_data = compressed_font_data.ToPointer();
181+
void* native_compressed_font_data = (void*)compressed_font_data.ToPointer();
184182
ImFontConfig* native_font_cfg = font_cfg.NativePtr;
185183
ushort* glyph_ranges = null;
186184
ImFont* ret = ImGuiNative.ImFontAtlas_AddFontFromMemoryCompressedTTF(NativePtr, native_compressed_font_data, compressed_font_size, size_pixels, native_font_cfg, glyph_ranges);
187185
return new ImFontPtr(ret);
188186
}
189-
public ImFontPtr AddFontFromMemoryCompressedTTF(IntPtr compressed_font_data, int compressed_font_size, float size_pixels, ImFontConfigPtr font_cfg, ref ushort glyph_ranges)
187+
public ImFontPtr AddFontFromMemoryCompressedTTF(IntPtr compressed_font_data, int compressed_font_size, float size_pixels, ImFontConfigPtr font_cfg, IntPtr glyph_ranges)
190188
{
191-
void* native_compressed_font_data = compressed_font_data.ToPointer();
189+
void* native_compressed_font_data = (void*)compressed_font_data.ToPointer();
192190
ImFontConfig* native_font_cfg = font_cfg.NativePtr;
193-
fixed (ushort* native_glyph_ranges = &glyph_ranges)
194-
{
195-
ImFont* ret = ImGuiNative.ImFontAtlas_AddFontFromMemoryCompressedTTF(NativePtr, native_compressed_font_data, compressed_font_size, size_pixels, native_font_cfg, native_glyph_ranges);
196-
return new ImFontPtr(ret);
197-
}
191+
ushort* native_glyph_ranges = (ushort*)glyph_ranges.ToPointer();
192+
ImFont* ret = ImGuiNative.ImFontAtlas_AddFontFromMemoryCompressedTTF(NativePtr, native_compressed_font_data, compressed_font_size, size_pixels, native_font_cfg, native_glyph_ranges);
193+
return new ImFontPtr(ret);
198194
}
199195
public ImFontPtr AddFontFromMemoryTTF(IntPtr font_data, int font_size, float size_pixels)
200196
{
201-
void* native_font_data = font_data.ToPointer();
197+
void* native_font_data = (void*)font_data.ToPointer();
202198
ImFontConfig* font_cfg = null;
203199
ushort* glyph_ranges = null;
204200
ImFont* ret = ImGuiNative.ImFontAtlas_AddFontFromMemoryTTF(NativePtr, native_font_data, font_size, size_pixels, font_cfg, glyph_ranges);
205201
return new ImFontPtr(ret);
206202
}
207203
public ImFontPtr AddFontFromMemoryTTF(IntPtr font_data, int font_size, float size_pixels, ImFontConfigPtr font_cfg)
208204
{
209-
void* native_font_data = font_data.ToPointer();
205+
void* native_font_data = (void*)font_data.ToPointer();
210206
ImFontConfig* native_font_cfg = font_cfg.NativePtr;
211207
ushort* glyph_ranges = null;
212208
ImFont* ret = ImGuiNative.ImFontAtlas_AddFontFromMemoryTTF(NativePtr, native_font_data, font_size, size_pixels, native_font_cfg, glyph_ranges);
213209
return new ImFontPtr(ret);
214210
}
215-
public ImFontPtr AddFontFromMemoryTTF(IntPtr font_data, int font_size, float size_pixels, ImFontConfigPtr font_cfg, ref ushort glyph_ranges)
211+
public ImFontPtr AddFontFromMemoryTTF(IntPtr font_data, int font_size, float size_pixels, ImFontConfigPtr font_cfg, IntPtr glyph_ranges)
216212
{
217-
void* native_font_data = font_data.ToPointer();
213+
void* native_font_data = (void*)font_data.ToPointer();
218214
ImFontConfig* native_font_cfg = font_cfg.NativePtr;
219-
fixed (ushort* native_glyph_ranges = &glyph_ranges)
220-
{
221-
ImFont* ret = ImGuiNative.ImFontAtlas_AddFontFromMemoryTTF(NativePtr, native_font_data, font_size, size_pixels, native_font_cfg, native_glyph_ranges);
222-
return new ImFontPtr(ret);
223-
}
215+
ushort* native_glyph_ranges = (ushort*)glyph_ranges.ToPointer();
216+
ImFont* ret = ImGuiNative.ImFontAtlas_AddFontFromMemoryTTF(NativePtr, native_font_data, font_size, size_pixels, native_font_cfg, native_glyph_ranges);
217+
return new ImFontPtr(ret);
224218
}
225219
public ImFontPtr AddFontFromFileTTF(string filename, float size_pixels)
226220
{
@@ -250,7 +244,7 @@ public ImFontPtr AddFontFromFileTTF(string filename, float size_pixels, ImFontCo
250244
ImFont* ret = ImGuiNative.ImFontAtlas_AddFontFromFileTTF(NativePtr, native_filename, size_pixels, native_font_cfg, glyph_ranges);
251245
return new ImFontPtr(ret);
252246
}
253-
public ImFontPtr AddFontFromFileTTF(string filename, float size_pixels, ImFontConfigPtr font_cfg, ref ushort glyph_ranges)
247+
public ImFontPtr AddFontFromFileTTF(string filename, float size_pixels, ImFontConfigPtr font_cfg, IntPtr glyph_ranges)
254248
{
255249
int filename_byteCount = Encoding.UTF8.GetByteCount(filename);
256250
byte* native_filename = stackalloc byte[filename_byteCount + 1];
@@ -260,11 +254,9 @@ public ImFontPtr AddFontFromFileTTF(string filename, float size_pixels, ImFontCo
260254
native_filename[native_filename_offset] = 0;
261255
}
262256
ImFontConfig* native_font_cfg = font_cfg.NativePtr;
263-
fixed (ushort* native_glyph_ranges = &glyph_ranges)
264-
{
265-
ImFont* ret = ImGuiNative.ImFontAtlas_AddFontFromFileTTF(NativePtr, native_filename, size_pixels, native_font_cfg, native_glyph_ranges);
266-
return new ImFontPtr(ret);
267-
}
257+
ushort* native_glyph_ranges = (ushort*)glyph_ranges.ToPointer();
258+
ImFont* ret = ImGuiNative.ImFontAtlas_AddFontFromFileTTF(NativePtr, native_filename, size_pixels, native_font_cfg, native_glyph_ranges);
259+
return new ImFontPtr(ret);
268260
}
269261
public ImFontPtr AddFontDefault()
270262
{
@@ -278,10 +270,10 @@ public ImFontPtr AddFontDefault(ImFontConfigPtr font_cfg)
278270
ImFont* ret = ImGuiNative.ImFontAtlas_AddFontDefault(NativePtr, native_font_cfg);
279271
return new ImFontPtr(ret);
280272
}
281-
public ushort* GetGlyphRangesJapanese()
273+
public IntPtr GetGlyphRangesJapanese()
282274
{
283275
ushort* ret = ImGuiNative.ImFontAtlas_GetGlyphRangesJapanese(NativePtr);
284-
return ret;
276+
return (IntPtr)ret;
285277
}
286278
public void GetTexDataAsAlpha8(out byte* out_pixels, out int out_width, out int out_height)
287279
{
@@ -334,10 +326,10 @@ public bool GetMouseCursorTexData(ImGuiMouseCursor cursor, out Vector2 out_offse
334326
}
335327
}
336328
}
337-
public ushort* GetGlyphRangesKorean()
329+
public IntPtr GetGlyphRangesKorean()
338330
{
339331
ushort* ret = ImGuiNative.ImFontAtlas_GetGlyphRangesKorean(NativePtr);
340-
return ret;
332+
return (IntPtr)ret;
341333
}
342334
public void GetTexDataAsRGBA32(out byte* out_pixels, out int out_width, out int out_height)
343335
{

0 commit comments

Comments
 (0)