bryanedds / Nu

Repository hosting the open-source Nu Game Engine and related projects.
MIT License
1.14k stars 154 forks source link

Non-Latin font support in ImGui #543

Open reinux opened 1 year ago

reinux commented 1 year ago

The ProggyClean font embedded in ImGui has only Latin (presumably ASCII) glyphs, and ImGui doesn't have a mechanism for automatically referencing system fallback fonts. To render non-Latin fonts thus takes some manual work.

Aesthetically, the free PixelMplus 12px font seems to fit well in addition to Proggy for Latin, but I have yet to figure out a way to get it to render pixel perfectly. The 10px version renders correctly when the size is set to 11, so there's likely some value that works. It'll take some experimentation.

We would also need to a general way to load an appropriate font for any language, not just Japanese. Given that there are several hurdles to overcome before it's ready at all, I'll just leave this code here (under fonts.AddFontDefault () in ImGui.fs) as a starting point.

        // add Japanese font
        let config: ImFontConfigPtr = ImGuiNative.ImFontConfig_ImFontConfig()
        config.PixelSnapH <- true
        config.MergeMode <- true
        try
            fonts.AddFontFromFileTTF(@"PixelMplus12-Regular.ttf", 13.5f, config, fonts.GetGlyphRangesJapanese()) |> ignore<ImFontPtr>
        finally
            config.Destroy()

Here's also a Japanese blog post on how someone used a GDI32 call to find the file location of a built-in Windows Japanese font. This may be something to consider, as although PixelMPlus is just over 1mb, most CJK fonts are 10mb+, so we may want to use system fonts by default if we move away from monochrome monospace fonts.

As per our discussion, for Japanese users, it seems like it would make sense to leave most of the UI in English, except text-heavy dialog boxes and logs and such. But just for fun, here's a screenshot of how it might look if we were to translate some UI elements (with the nasty font smoothing issue not yet resolved):

image

reinux commented 1 month ago

A bunch more bitmap fonts here: https://coliss.com/articles/freebies/pixel-japanese-free-fonts.html

Aside from PixelMPlus, I'm also liking Best 10 and Maruminya, which are 10x10 and 12x12 respectively.