CImGui and special characters (unicode)

I try to display some unicode characters in CImGui, but I am failing.
This is the simple code mainly from the demo.jl:

using GLFW
using ModernGL
using CSyntax
using CSyntax.CStatic

using CImGui
using CImGui.GLFWBackend
using CImGui.OpenGLBackend

@static if Sys.isapple()
    # OpenGL 3.2 + GLSL 150
    tmp_glsl_version = 150
    GLFW.WindowHint(GLFW.CONTEXT_VERSION_MAJOR, 3)
    GLFW.WindowHint(GLFW.CONTEXT_VERSION_MINOR, 2)
    GLFW.WindowHint(GLFW.OPENGL_PROFILE, GLFW.OPENGL_CORE_PROFILE) # 3.2+ only
    GLFW.WindowHint(GLFW.OPENGL_FORWARD_COMPAT, GL_TRUE) # required on Mac
else
    # OpenGL 3.0 + GLSL 130
    tmp_glsl_version = 130
    GLFW.WindowHint(GLFW.CONTEXT_VERSION_MAJOR, 3)
    GLFW.WindowHint(GLFW.CONTEXT_VERSION_MINOR, 0)
    # GLFW.WindowHint(GLFW.OPENGL_PROFILE, GLFW.OPENGL_CORE_PROFILE) # 3.2+ only
    # GLFW.WindowHint(GLFW.OPENGL_FORWARD_COMPAT, GL_TRUE) # 3.0+ only
    GLFW.WindowHint(GLFW.FOCUSED,GL_TRUE)
end
const glsl_version=tmp_glsl_version

# setup GLFW error callback
error_callback(err::GLFW.GLFWError) = @error "GLFW ERROR: code $(err.code) msg: $(err.description)"
GLFW.SetErrorCallback(error_callback)

# create window
window = GLFW.CreateWindow(1100, 720, "PlayGround")

@assert window != C_NULL
GLFW.MakeContextCurrent(window)
GLFW.SwapInterval(1)  # enable vsync

# setup Dear ImGui context
ctx = CImGui.CreateContext()

# setup Dear ImGui style
CImGui.StyleColorsClassic()

#
# start to build th font:
#

builder=CImGui.ImFontGlyphRangesBuilder_ImFontGlyphRangesBuilder()
CImGui.ImFontGlyphRangesBuilder_AddText(builder,"????","") 
CImGui.ImFontGlyphRangesBuilder_AddChar(builder,'?')
CImGui.ImFontGlyphRangesBuilder_AddChar(builder,'?')
CImGui.ImFontGlyphRangesBuilder_AddChar(builder,'?')
CImGui.ImFontGlyphRangesBuilder_AddChar(builder,'?')
ranges=CImGui.ImVector_ImWchar_create()
CImGui.ImFontGlyphRangesBuilder_BuildRanges(builder, ranges)

#fonts_dir = joinpath(pathof(CImGui), "..","..","fonts")
fonts_dir = raw"c:\Windows\Fonts";
fonts = CImGui.GetIO().Fonts

CImGui.AddFontFromFileTTF(fonts, joinpath(fonts_dir, "arial.ttf"), 16, C_NULL, ranges )
#CImGui.AddFontFromFileTTF(fonts, joinpath(fonts_dir, "arial.ttf"), 16, C_NULL, CImGui.GetGlyphRangesChineseSimplifiedCommon(fonts) )

CImGui.Build(fonts)
default_font = CImGui.AddFontDefault(fonts)
#@assert default_font != C_NULL

# setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForOpenGL(window, true)
ImGui_ImplOpenGL3_Init(glsl_version)

try
    clear_color = Cfloat[0.45, 0.55, 0.60, 1.00]

    while  !GLFW.WindowShouldClose(window)
        GLFW.PollEvents()

        # start the Dear ImGui frame
        ImGui_ImplOpenGL3_NewFrame()
        ImGui_ImplGlfw_NewFrame()
        CImGui.NewFrame()
        CImGui.SetNextWindowSize(CImGui.ImVec2(350.0,200.0),CImGui.ImGuiCond_Once);

        CImGui.Begin("Special strings")
        CImGui.Text("????")
        CImGui.Text("p ? ? ? ?")
        CImGui.End()

        # rendering
        CImGui.Render()
        GLFW.MakeContextCurrent(window)
        display_w, display_h = GLFW.GetFramebufferSize(window)
        glViewport(0, 0, display_w, display_h)
        glClearColor(clear_color...)
        glClear(GL_COLOR_BUFFER_BIT)
        ImGui_ImplOpenGL3_RenderDrawData(CImGui.GetDrawData())

        GLFW.MakeContextCurrent(window)
        GLFW.SwapBuffers(window)
    end

catch e
    @error "Error in renderloop!" exception=e
    Base.show_backtrace(stderr, catch_backtrace())
    display_close(display)
finally
    ImGui_ImplOpenGL3_Shutdown()
    ImGui_ImplGlfw_Shutdown()
    CImGui.DestroyContext(ctx)
    GLFW.DestroyWindow(window)
end

It fails with the following error:

Please submit a bug report with steps to reproduce this fault, and any error messages that follow (in their entirety). Thanks.
Exception: EXCEPTION_ACCESS_VIOLATION at 0x70c4fe49 -- _ZN5ImGui14SetCurrentFontEP6ImFont at C:\Users\Oli\.julia\artifacts\d47e9affaed6dd0e59dff721ebb86dd8347b057f\bin\libimgui-cpp.dll (unknown line)
in expression starting at REPL[41]:1
_ZN5ImGui14SetCurrentFontEP6ImFont at C:\Users\Oli\.julia\artifacts\d47e9affaed6dd0e59dff721ebb86dd8347b057f\bin\libimgui-cpp.dll (unknown line)
_ZN5ImGui8NewFrameEv at C:\Users\Oli\.julia\artifacts\d47e9affaed6dd0e59dff721ebb86dd8347b057f\bin\libimgui-cpp.dll (unknown line)
igNewFrame at C:\Users\Oli\.julia\packages\CImGui\tJ4ZL\gen\libcimgui_api.jl:54 [inlined]
NewFrame at C:\Users\Oli\.julia\packages\CImGui\tJ4ZL\src\wrapper.jl:54 [inlined]
top-level scope at .\REPL[41]:100:

Does anybody know what’s the proper way to display unicode characters in a CImGui window?

The following code works but display only ? for each unicode character:

using GLFW
using ModernGL
using CSyntax
using CSyntax.CStatic

using CImGui
using CImGui.GLFWBackend
using CImGui.OpenGLBackend

@static if Sys.isapple()
    # OpenGL 3.2 + GLSL 150
    tmp_glsl_version = 150
    GLFW.WindowHint(GLFW.CONTEXT_VERSION_MAJOR, 3)
    GLFW.WindowHint(GLFW.CONTEXT_VERSION_MINOR, 2)
    GLFW.WindowHint(GLFW.OPENGL_PROFILE, GLFW.OPENGL_CORE_PROFILE) # 3.2+ only
    GLFW.WindowHint(GLFW.OPENGL_FORWARD_COMPAT, GL_TRUE) # required on Mac
else
    # OpenGL 3.0 + GLSL 130
    tmp_glsl_version = 130
    GLFW.WindowHint(GLFW.CONTEXT_VERSION_MAJOR, 3)
    GLFW.WindowHint(GLFW.CONTEXT_VERSION_MINOR, 0)
    # GLFW.WindowHint(GLFW.OPENGL_PROFILE, GLFW.OPENGL_CORE_PROFILE) # 3.2+ only
    # GLFW.WindowHint(GLFW.OPENGL_FORWARD_COMPAT, GL_TRUE) # 3.0+ only
    GLFW.WindowHint(GLFW.FOCUSED,GL_TRUE)
end
const glsl_version=tmp_glsl_version

# setup GLFW error callback
error_callback(err::GLFW.GLFWError) = @error "GLFW ERROR: code $(err.code) msg: $(err.description)"
GLFW.SetErrorCallback(error_callback)

# create window
window = GLFW.CreateWindow(1100, 720, "PlayGround")

@assert window != C_NULL
GLFW.MakeContextCurrent(window)
GLFW.SwapInterval(1)  # enable vsync

# setup Dear ImGui context
ctx = CImGui.CreateContext()

# setup Dear ImGui style
CImGui.StyleColorsClassic()

#
# start to build th font:
#

#builder=CImGui.ImFontGlyphRangesBuilder_ImFontGlyphRangesBuilder()
#CImGui.ImFontGlyphRangesBuilder_AddText(builder,"????","") 
#CImGui.ImFontGlyphRangesBuilder_AddChar(builder,'?')
#CImGui.ImFontGlyphRangesBuilder_AddChar(builder,'?')
#CImGui.ImFontGlyphRangesBuilder_AddChar(builder,'?')
#CImGui.ImFontGlyphRangesBuilder_AddChar(builder,'?')
#ranges=CImGui.ImVector_ImWchar_create()
#CImGui.ImFontGlyphRangesBuilder_BuildRanges(builder, ranges)

#fonts_dir = joinpath(pathof(CImGui), "..","..","fonts")
fonts_dir = raw"c:\Windows\Fonts";
fonts = CImGui.GetIO().Fonts

#CImGui.AddFontFromFileTTF(fonts, joinpath(fonts_dir, "arial.ttf"), 16, C_NULL, ranges )
CImGui.AddFontFromFileTTF(fonts, joinpath(fonts_dir, "arial.ttf"), 16, C_NULL, CImGui.GetGlyphRangesChineseSimplifiedCommon(fonts) )

CImGui.Build(fonts)
default_font = CImGui.AddFontDefault(fonts)
#@assert default_font != C_NULL

# setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForOpenGL(window, true)
ImGui_ImplOpenGL3_Init(glsl_version)

try
    clear_color = Cfloat[0.45, 0.55, 0.60, 1.00]

    while  !GLFW.WindowShouldClose(window)
        GLFW.PollEvents()

        # start the Dear ImGui frame
        ImGui_ImplOpenGL3_NewFrame()
        ImGui_ImplGlfw_NewFrame()
        CImGui.NewFrame()
        CImGui.SetNextWindowSize(CImGui.ImVec2(350.0,200.0),CImGui.ImGuiCond_Once);

        CImGui.Begin("Special strings")
        CImGui.Text("????")
        CImGui.Text("p ? ? ? ?")
        CImGui.End()

        # rendering
        CImGui.Render()
        GLFW.MakeContextCurrent(window)
        display_w, display_h = GLFW.GetFramebufferSize(window)
        glViewport(0, 0, display_w, display_h)
        glClearColor(clear_color...)
        glClear(GL_COLOR_BUFFER_BIT)
        ImGui_ImplOpenGL3_RenderDrawData(CImGui.GetDrawData())

        GLFW.MakeContextCurrent(window)
        GLFW.SwapBuffers(window)
    end

catch e
    @error "Error in renderloop!" exception=e
    Base.show_backtrace(stderr, catch_backtrace())
    display_close(display)
finally
    ImGui_ImplOpenGL3_Shutdown()
    ImGui_ImplGlfw_Shutdown()
    CImGui.DestroyContext(ctx)
    GLFW.DestroyWindow(window)
end

@Gnimuc , can you help?

It looks like you just let it display ???? here.

BTW, custom glyph ranges are not supported for now. Unicode is not compatible? · Issue #30 · Gnimuc/CImGui.jl · GitHub

Oh, I didn’t see that. It’s a copy & paste problem, in the code it is:
CImGui.Text("π ϕ θ ♐ ☽")
for example.

Ok, in this case how to solve this? Which glyph range can one use?

Quoted from the issue:

Custom glyph range APIs are not fully wrapped, so currently, there is no easy way to draw all unicode texts. I will revisit this if I have time.

Anything I can do? In which direction should I look? Any tips on how to setup a development environment for this?

https://github.com/ocornut/imgui/blob/master/docs/FONTS.md#using-custom-glyph-ranges

The official fonts doc is a good starting point. If there are any C++ APIs that are used in the doc but not wrapped in CImGui.jl, basically you need to add corresponding C89 wrappers and regenerate the bindings.

https://github.com/JuliaPackaging/Yggdrasil/blob/master/C/CImGui/bundled/wrapper/helper.c

I don’t know too. There are only a few glyph ranges that are provided by IMGUI, so one simple way is to just test them one-by-one.

Ok, the links and references aren’t that much helpfull. I will give it a try anyways, but I am not so optimistic about my skills :frowning:

@oheil I fixed your first example and it should be working now. Feel free to open a PR if you’d like to add some high-level helper functions for this.

fonts_dir = joinpath(homedir(), "Downloads")
fonts = CImGui.GetIO().Fonts
# default_font = CImGui.AddFontDefault(fonts)
# CImGui.AddFontFromFileTTF(fonts, joinpath(fonts_dir, "Cousine-Regular.ttf"), 15)
# CImGui.AddFontFromFileTTF(fonts, joinpath(fonts_dir, "DroidSans.ttf"), 16)
# CImGui.AddFontFromFileTTF(fonts, joinpath(fonts_dir, "Karla-Regular.ttf"), 10)
# CImGui.AddFontFromFileTTF(fonts, joinpath(fonts_dir, "ProggyTiny.ttf"), 10)
# CImGui.AddFontFromFileTTF(fonts, joinpath(fonts_dir, "Roboto-Medium.ttf"), 16)
# @assert default_font != C_NULL

using CImGui.LibCImGui
ranges = ImVector_ImWchar_create()
ImVector_ImWchar_Init(ranges)
builder = ImFontGlyphRangesBuilder_ImFontGlyphRangesBuilder()
ImFontGlyphRangesBuilder_AddChar(builder, 'ϕ')
ImFontGlyphRangesBuilder_AddChar(builder, '♐')
ImFontGlyphRangesBuilder_AddRanges(builder, ImFontAtlas_GetGlyphRangesDefault(fonts))
ImFontGlyphRangesBuilder_BuildRanges(builder, ranges)
r = unsafe_wrap(Vector{ImVector_ImWchar}, ranges, 1)
CImGui.AddFontFromFileTTF(fonts, joinpath(fonts_dir, "arial-unicode-ms.ttf"), 16, C_NULL, r[1].Data)

BTW, don’t forget to call the corresponding destroy methods in the finally block to clean up resources.

finally
    ImGui_ImplOpenGL3_Shutdown()
    ImGui_ImplGlfw_Shutdown()
    ImVector_ImWchar_destroy(ranges)
    ImFontGlyphRangesBuilder_destroy(builder)
    CImGui.DestroyContext(ctx)
    GLFW.DestroyWindow(window)
end
2 Likes

Awesome, it works now.

Hmm, I don’t know. I’m afraid that this is quite tedious and my results are probably insufficient. Thanks a lot for your effort. Maybe I can help others when these question come up again.

1 Like

Here is a complete example.

I have added another recipe to add a complete glyph range using AddRanges (ImFontGlyphRangesBuilder_AddRanges) without the restriction, that you have to use the few available GetGlyphRanges... functions.

This is the snippet for this to add the complete glyph range of greek characters (0x0300-0x03ff):

struct GlyphRange
	first::ImWchar
	last::ImWchar
	zero::ImWchar
	function GlyphRange(first::ImWchar,last::ImWchar)
		new(first,last,0x0)
	end
end
greekRange=GlyphRange(0x0300, 0x03ff)
greekRange_ptr=Core.Ptr{ImWchar}(Base.pointer_from_objref(Ref(greekRange)))
ImFontGlyphRangesBuilder_AddRanges(builder, greekRange_ptr)

The following example adds glyphs with AddText, AddChar, AddRanges with arbitrary glyph range and AddRanges with GetGlyphRangesDefault. It uses the font JuliaMono-Regular.ttf which is on my system located in c:\Windows\Fonts, because I have installed it a while ago. The example is based on the demo.jl of package CImGui.jl which has the benefit, that the Style Editor is available, which can be used to inspect the font:

using CImGui
using CImGui.LibCImGui
using CImGui.CSyntax
using CImGui.CSyntax.CStatic
using CImGui.GLFWBackend
using CImGui.OpenGLBackend
using CImGui.GLFWBackend.GLFW
using CImGui.OpenGLBackend.ModernGL
using Printf

@static if Sys.isapple()
    # OpenGL 3.2 + GLSL 150
    const glsl_version = 150
    GLFW.WindowHint(GLFW.CONTEXT_VERSION_MAJOR, 3)
    GLFW.WindowHint(GLFW.CONTEXT_VERSION_MINOR, 2)
    GLFW.WindowHint(GLFW.OPENGL_PROFILE, GLFW.OPENGL_CORE_PROFILE) # 3.2+ only
    GLFW.WindowHint(GLFW.OPENGL_FORWARD_COMPAT, GL_TRUE) # required on Mac
else
    # OpenGL 3.0 + GLSL 130
    const glsl_version = 130
    GLFW.WindowHint(GLFW.CONTEXT_VERSION_MAJOR, 3)
    GLFW.WindowHint(GLFW.CONTEXT_VERSION_MINOR, 0)
    # GLFW.WindowHint(GLFW.OPENGL_PROFILE, GLFW.OPENGL_CORE_PROFILE) # 3.2+ only
    # GLFW.WindowHint(GLFW.OPENGL_FORWARD_COMPAT, GL_TRUE) # 3.0+ only
end

# setup GLFW error callback
error_callback(err::GLFW.GLFWError) = @error "GLFW ERROR: code $(err.code) msg: $(err.description)"
GLFW.SetErrorCallback(error_callback)

# create window
window = GLFW.CreateWindow(1280, 720, "Demo")
@assert window != C_NULL
GLFW.MakeContextCurrent(window)
GLFW.SwapInterval(1)  # enable vsync

# setup Dear ImGui context
ctx = CImGui.CreateContext()

# setup Dear ImGui style
CImGui.StyleColorsDark()
# CImGui.StyleColorsClassic()
# CImGui.StyleColorsLight()

# load Fonts
# - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use `CImGui.PushFont/PopFont` to select them.
# - `CImGui.AddFontFromFileTTF` will return the `Ptr{ImFont}` so you can store it if you need to select the font among multiple.
# - If the file cannot be loaded, the function will return C_NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
# - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling `CImGui.Build()`/`GetTexDataAsXXXX()``, which `ImGui_ImplXXXX_NewFrame` below will call.
# - Read 'fonts/README.txt' for more instructions and details.
#fonts_dir = joinpath(@__DIR__, "..", "fonts")
#fonts = CImGui.GetIO().Fonts
# default_font = CImGui.AddFontDefault(fonts)
# CImGui.AddFontFromFileTTF(fonts, joinpath(fonts_dir, "Cousine-Regular.ttf"), 15)
# CImGui.AddFontFromFileTTF(fonts, joinpath(fonts_dir, "DroidSans.ttf"), 16)
# CImGui.AddFontFromFileTTF(fonts, joinpath(fonts_dir, "Karla-Regular.ttf"), 10)
# CImGui.AddFontFromFileTTF(fonts, joinpath(fonts_dir, "ProggyTiny.ttf"), 10)
#CImGui.AddFontFromFileTTF(fonts, joinpath(fonts_dir, "Roboto-Medium.ttf"), 16)
# @assert default_font != C_NULL

#fonts_dir = joinpath(pathof(CImGui), "..","..","fonts")
fonts_dir = raw"c:\Windows\Fonts";
fonts = CImGui.GetIO().Fonts

builder=ImFontGlyphRangesBuilder_ImFontGlyphRangesBuilder()
ImFontGlyphRangesBuilder_AddText(builder, "♐", C_NULL)
ImFontGlyphRangesBuilder_AddChar(builder,'☽')
ImFontGlyphRangesBuilder_AddRanges(builder, ImFontAtlas_GetGlyphRangesDefault(fonts))

struct GlyphRange
	first::ImWchar
	last::ImWchar
	zero::ImWchar
	function GlyphRange(first::ImWchar,last::ImWchar)
		new(first,last,0x0)
	end
end
greekRange=GlyphRange(0x0300, 0x03ff)
greekRange_ptr=Core.Ptr{ImWchar}(Base.pointer_from_objref(Ref(greekRange)))
ImFontGlyphRangesBuilder_AddRanges(builder, greekRange_ptr)

ranges=ImVector_ImWchar_create()
ImVector_ImWchar_Init(ranges)
ImFontGlyphRangesBuilder_BuildRanges(builder, ranges)
r = unsafe_wrap(Vector{ImVector_ImWchar}, ranges, 1)
CImGui.AddFontFromFileTTF(fonts, joinpath(fonts_dir, "JuliaMono-Regular.ttf"), 16, C_NULL, r[1].Data )

CImGui.Build(fonts)
default_font = CImGui.AddFontDefault(fonts)
#@assert default_font != C_NULL

# setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForOpenGL(window, true)
ImGui_ImplOpenGL3_Init(glsl_version)

try
    show_demo_window = true
    show_another_window = false
    clear_color = Cfloat[0.45, 0.55, 0.60, 1.00]
    while !GLFW.WindowShouldClose(window)
        GLFW.PollEvents()
        # start the Dear ImGui frame
        ImGui_ImplOpenGL3_NewFrame()
        ImGui_ImplGlfw_NewFrame()
        CImGui.NewFrame()

        # show the big demo window
        show_demo_window && @c CImGui.ShowDemoWindow(&show_demo_window)

        # show a simple window that we create ourselves.
        # we use a Begin/End pair to created a named window.
        @cstatic f=Cfloat(0.0) counter=Cint(0) begin
            CImGui.Begin("Hello, world!")  # create a window called "Hello, world!" and append into it.
            CImGui.Text("This is some useful text.")  # display some text
            @c CImGui.Checkbox("Demo Window", &show_demo_window)  # edit bools storing our window open/close state
            @c CImGui.Checkbox("Another Window", &show_another_window)

            @c CImGui.SliderFloat("float", &f, 0, 1)  # edit 1 float using a slider from 0 to 1
            CImGui.ColorEdit3("clear color", clear_color)  # edit 3 floats representing a color
            CImGui.Button("Button") && (counter += 1)

            CImGui.SameLine()
            CImGui.Text("counter = $counter")
            CImGui.Text(@sprintf("Application average %.3f ms/frame (%.1f FPS)", 1000 / CImGui.GetIO().Framerate, CImGui.GetIO().Framerate))

            CImGui.End()
        end

        # show another simple window.
        if show_another_window
            @c CImGui.Begin("Another Window", &show_another_window)  # pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
            CImGui.Text("Hello from another window!")
            CImGui.Button("Close Me") && (show_another_window = false;)
            CImGui.End()
        end

		CImGui.Begin("Special characters")
		CImGui.Text("π ϕ θ ♐ ☽")
		CImGui.End()

        # rendering
        CImGui.Render()
        GLFW.MakeContextCurrent(window)
        display_w, display_h = GLFW.GetFramebufferSize(window)
        glViewport(0, 0, display_w, display_h)
        glClearColor(clear_color...)
        glClear(GL_COLOR_BUFFER_BIT)
        ImGui_ImplOpenGL3_RenderDrawData(CImGui.GetDrawData())

        GLFW.MakeContextCurrent(window)
        GLFW.SwapBuffers(window)
    end
catch e
    @error "Error in renderloop!" exception=e
    Base.show_backtrace(stderr, catch_backtrace())
finally
    ImGui_ImplOpenGL3_Shutdown()
    ImGui_ImplGlfw_Shutdown()
    ImVector_ImWchar_destroy(ranges)
    ImFontGlyphRangesBuilder_destroy(builder)
	CImGui.DestroyContext(ctx)
    GLFW.DestroyWindow(window)
end

Again thanks to @Gnimuc for his efforts which brought me on the path to glyphs.

For everybody who needs Unicode+CImGui, this is a must read https://github.com/ocornut/imgui/blob/master/docs/FONTS.md. There is important information e.g. that your font atlas can grow too large which is crucial to know if you add large glyph ranges.

Everything about Unicode: Unicode Technical Site

3 Likes