imgui-java icon indicating copy to clipboard operation
imgui-java copied to clipboard

What is the appropriate way of adding emojis to a font?

Open vini2003 opened this issue 1 year ago • 0 comments

Version

1.86.11

What happened?

Hi! I'm running into a small issue & I think I'm just missing something obvious - not sure this is the best place to ask, but... I hope it works.

Anyhow - I'm trying to load Twemoji as an ImGuiFontReference, but can't seem to pass the correct glyph ranges for the Unicode emojis (since the setGlyphRanges method takes a short).

Reproduction

This is the code I am using:

public static ImGuiFontReference register(Identifier id, int size) {
    var supplier = Suppliers.memoize(() -> {
        try {
            var identifier = new Identifier(id.getNamespace(), "fonts/" + id.getPath() + ".ttf");
            var resource = MinecraftClient.getInstance().getResourceManager().getResource(identifier);
            var bytes = IOUtils.toByteArray(resource.orElseThrow().getInputStream());

            var config = new ImFontConfig();
            config.setName(id.toString());
            config.setGlyphRanges(GLYPH_RANGES); // Normal Glyph ranges; not the Emojis.
            config.setRasterizerMultiply(1.2f);
            config.setOversampleH(2);
            config.setOversampleV(2);

            var fonts = ImGui.getIO().getFonts();

            ImFont mainFont = fonts.addFontFromMemoryTTF(bytes, size, config);

            var twemojiIdentifier = new Identifier("knob", "fonts/twemoji.ttf");
            var twemojiResource = MinecraftClient.getInstance().getResourceManager().getResource(twemojiIdentifier);
            var twemojiBytes = IOUtils.toByteArray(twemojiResource.orElseThrow().getInputStream());

            var twemojiConfig = new ImFontConfig();

            twemojiConfig.setFontBuilderFlags(ImGuiFreeTypeBuilderFlags.LoadColor);
            twemojiConfig.setMergeMode(true); 

            // Now, how do I tell it to load the Emoji range here?

            fonts.addFontFromMemoryTTF(twemojiBytes, size, twemojiConfig);

            return mainFont;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });
    ImGuiFontReference font = new ImGuiFontReference(supplier);
    FONTS.add(font);
    return font;
}

Relevant log output

No response

vini2003 avatar Aug 20 '24 14:08 vini2003