@@ -2,6 +2,8 @@ package com.github.fdh911.render
22
33import com.github.fdh911.render.opengl.GLState2
44import imgui.ImFont
5+ import imgui.ImFontConfig
6+ import imgui.ImFontGlyphRangesBuilder
57import imgui.ImGui
68import imgui.ImVec4
79import imgui.flag.ImGuiCond
@@ -12,7 +14,6 @@ import net.minecraft.client.MinecraftClient
1214import net.minecraft.client.gui.DrawContext
1315import net.minecraft.client.gui.screen.Screen
1416import net.minecraft.text.Text
15- import java.io.File
1617
1718object UserInterface {
1819 init {
@@ -34,19 +35,28 @@ object UserInterface {
3435 private val largeFont: ImFont
3536
3637 init {
37- val classpathSmall = UserInterface ::class .java.getResourceAsStream(" /fonts/0xProtoNerdFont-Regular.ttf" )
38- ? : throw RuntimeException (" Could not open small font file" )
39- val tempfileSmall = File .createTempFile(" imgui" , " smallfont" )
40- tempfileSmall.writeBytes(classpathSmall.readAllBytes())
41- smallFont = io.fonts.addFontFromFileTTF(tempfileSmall.absolutePath, 20.0f )
42- tempfileSmall.delete()
43-
44- val classpathLarge = UserInterface ::class .java.getResourceAsStream(" /fonts/0xProtoNerdFont-Bold.ttf" )
45- ? : throw RuntimeException (" Could not open large font file" )
46- val tempfileLarge = File .createTempFile(" imgui" , " largefont" )
47- tempfileLarge.writeBytes(classpathLarge.readAllBytes())
48- largeFont = io.fonts.addFontFromFileTTF(tempfileLarge.absolutePath, 28.0f )
49- tempfileLarge.delete()
38+ fun loadMergedFont (baseFont : String , iconsFont : String , size : Float , range : ShortArray ): ImFont {
39+ val cfg = ImFontConfig ()
40+ val baseIS = object {}::class .java.getResourceAsStream(" /fonts/$baseFont .ttf" )
41+ ? : throw RuntimeException (" Could not open base font file $baseFont " )
42+ val iconsIS = object {}::class .java.getResourceAsStream(" /fonts/$iconsFont .ttf" )
43+ ? : throw RuntimeException (" Could not open icons font file $iconsFont " )
44+ val font = io.fonts.addFontFromMemoryTTF(baseIS.readAllBytes(), size, cfg, range)
45+ cfg.mergeMode = true
46+ io.fonts.addFontFromMemoryTTF(iconsIS.readAllBytes(), size, cfg, range)
47+ baseIS.close()
48+ iconsIS.close()
49+ return font
50+ }
51+
52+ val customRange = with (ImFontGlyphRangesBuilder ()) {
53+ addRanges(io.fonts.glyphRangesDefault)
54+ addText(Unicodes .toString())
55+ buildRanges()
56+ }
57+
58+ smallFont = loadMergedFont(" 0xProtoNerdFont-Regular" , " fa-regular-400" , 20.0f , customRange)
59+ largeFont = loadMergedFont(" 0xProtoNerdFont-Bold" , " fa-solid-900" , 28.0f , customRange)
5060
5161 io.fonts.build()
5262
0 commit comments