Skip to content

Commit

Permalink
feat(imgui): ttf
Browse files Browse the repository at this point in the history
  • Loading branch information
zly2006 committed May 15, 2024
1 parent 9ab9fff commit ad6721b
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,14 @@ allprojects {
exclude(dependency("org.lwjgl:lwjgl-glfw"))
exclude(dependency("org.lwjgl:lwjgl-opengl"))

// glfw is embedded in minecraft
exclude("windows/x64/org/lwjgl/**")
exclude("windows/x86/org/lwjgl/**")
exclude("linux/x64/org/lwjgl/**")
exclude("linux/x86/org/lwjgl/**")
exclude("macos/x64/org/lwjgl/**")
exclude("macos/arm64/org/lwjgl/**")
exclude("org/lwjgl/**")
}
}

Expand Down
57 changes: 57 additions & 0 deletions src/main/java/com/github/zly2006/reden/Imgui.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.github.zly2006.reden

import com.github.zly2006.reden.gui.FontAwesomeIcons
import com.github.zly2006.reden.utils.ResourceLoader
import imgui.ImFontConfig
import imgui.ImFontGlyphRangesBuilder
import imgui.ImGui
import imgui.ImGuiIO
import imgui.assertion.ImAssertCallback
import imgui.flag.ImGuiConfigFlags
import imgui.flag.ImGuiStyleVar
Expand Down Expand Up @@ -29,6 +34,7 @@ fun initImgui(windowHandle: Long) {
val io = ImGui.getIO()
io.addConfigFlags(ImGuiConfigFlags.NavEnableKeyboard)
io.addConfigFlags(ImGuiConfigFlags.DockingEnable)
initFonts(io)

// 初始化 ImGui 的 GLFW 和 OpenGL 实现
imGuiGlfw.init(windowHandle, true)
Expand All @@ -44,6 +50,57 @@ fun initImgui(windowHandle: Long) {
})
}

private fun initFonts(io: ImGuiIO) {
io.fonts.addFontDefault() // Add default font for latin glyphs

// You can use the ImFontGlyphRangesBuilder helper to create glyph ranges based on text input.
// For example: for a game where your script is known, if you can feed your entire script to it (using addText) and only build the characters the game needs.
// Here we are using it just to combine all required glyphs in one place
val rangesBuilder = ImFontGlyphRangesBuilder() // Glyphs ranges provide
rangesBuilder.addRanges(io.fonts.glyphRangesDefault)
rangesBuilder.addRanges(io.fonts.glyphRangesCyrillic)
rangesBuilder.addRanges(io.fonts.glyphRangesJapanese)
rangesBuilder.addRanges(io.fonts.glyphRangesChineseFull)
rangesBuilder.addRanges(io.fonts.glyphRangesChineseSimplifiedCommon)
rangesBuilder.addRanges(io.fonts.glyphRangesKorean)
rangesBuilder.addRanges(FontAwesomeIcons._IconRange)
rangesBuilder.addRanges(shortArrayOf(0, 0xffff.toShort()))

// Font config for additional fonts
// This is a natively allocated struct so don't forget to call destroy after atlas is built
val fontConfig = ImFontConfig()
fontConfig.mergeMode = true // Enable merge mode to merge cyrillic, japanese and icons with default font

val glyphRanges = rangesBuilder.buildRanges()
io.fonts.addFontFromMemoryTTF(
ResourceLoader.loadBytes("Tahoma.ttf"),
14f,
fontConfig,
glyphRanges
) // cyrillic glyphs
io.fonts.addFontFromMemoryTTF(
ResourceLoader.loadBytes("NotoSansCJKjp-Medium.otf"),
16f,
fontConfig,
glyphRanges
) // japanese glyphs
io.fonts.addFontFromMemoryTTF(
ResourceLoader.loadBytes("fa-regular-400.ttf"),
14f,
fontConfig,
glyphRanges
) // font awesome
io.fonts.addFontFromMemoryTTF(
ResourceLoader.loadBytes("fa-solid-900.ttf"),
14f,
fontConfig,
glyphRanges
) // font awesome
io.fonts.build()

fontConfig.destroy()
}

val renderers = mutableMapOf<String, () -> Unit>()
val hudRenderers = mutableMapOf<String, () -> Unit>()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.zly2006.reden.rvc.gui.reference

import com.github.zly2006.reden.Reden
import com.github.zly2006.reden.rvc.template.RvcTemplate
import kotlinx.serialization.json.Json
import java.nio.file.Path
import kotlin.io.path.readText

class RvcUseTemplateScreen(path: Path) {
val template = Json.decodeFromString<RvcTemplate>(path.resolve("rvc_template.json").readText())

init {
}

companion object {
fun fromClasspath(path: String) = RvcUseTemplateScreen(
Path.of(
Reden::class.java.classLoader.getResource(path)?.toURI()
?: throw IllegalArgumentException("Resource not found: $path")
)
)
}
}
Binary file added src/main/resources/NotoSansCJKjp-Medium.otf
Binary file not shown.
Binary file added src/main/resources/Tahoma.ttf
Binary file not shown.
Binary file added src/main/resources/fa-regular-400.ttf
Binary file not shown.
Binary file added src/main/resources/fa-solid-900.ttf
Binary file not shown.

0 comments on commit ad6721b

Please sign in to comment.