feat: add cjk text support

This commit is contained in:
Louis Dalibard 2024-10-21 01:30:26 +02:00
parent 54e3cb5188
commit 1a312e5e4a
4 changed files with 58 additions and 3 deletions

49
fonts/fonts.go Normal file
View File

@ -0,0 +1,49 @@
package fonts
import (
"unicode"
"xyosc/utils"
"github.com/flopp/go-findfont"
rl "github.com/gen2brain/raylib-go/raylib"
)
func getAllChars(table *unicode.RangeTable) []rune {
res := make([]rune, 0)
for _, r := range table.R16 {
for c := r.Lo; c <= r.Hi; c += r.Stride {
res = append(res, rune(c))
}
}
for _, r := range table.R32 {
for c := r.Lo; c <= r.Hi; c += r.Stride {
res = append(res, rune(c))
}
}
return res
}
var FontIosevka32 rl.Font
var FontIosevka16 rl.Font
func Init() {
// NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required)
fontPath, err := findfont.Find("SourceHanSansJP-Heavy.otf")
utils.CheckError(err)
runes := make([]rune, 0)
runes = append(runes, getAllChars(unicode.Katakana)...)
runes = append(runes, getAllChars(unicode.Hiragana)...)
runes = append(runes, getAllChars(unicode.Latin)...)
runes = append(runes, getAllChars(unicode.Digit)...)
runes = append(runes, getAllChars(unicode.Punct)...)
runes = append(runes, getAllChars(unicode.Han)...)
runes = append(runes, getAllChars(unicode.Symbol)...)
FontIosevka32 = rl.LoadFontEx(fontPath, 32, runes)
rl.GenTextureMipmaps(&FontIosevka32.Texture)
rl.SetTextureFilter(FontIosevka32.Texture, rl.FilterPoint)
FontIosevka16 = rl.LoadFontEx(fontPath, 16, runes)
rl.GenTextureMipmaps(&FontIosevka16.Texture)
rl.SetTextureFilter(FontIosevka16.Texture, rl.FilterPoint)
}

1
go.mod
View File

@ -3,6 +3,7 @@ module xyosc
go 1.23.2 go 1.23.2
require ( require (
github.com/flopp/go-findfont v0.1.0
github.com/gen2brain/malgo v0.11.22 github.com/gen2brain/malgo v0.11.22
github.com/gen2brain/raylib-go/raylib v0.0.0-20241019150900-b7833eeae8d0 github.com/gen2brain/raylib-go/raylib v0.0.0-20241019150900-b7833eeae8d0
github.com/godbus/dbus v4.1.0+incompatible github.com/godbus/dbus v4.1.0+incompatible

2
go.sum
View File

@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ebitengine/purego v0.7.1 h1:6/55d26lG3o9VCZX8lping+bZcmShseiqlh2bnUDiPA= github.com/ebitengine/purego v0.7.1 h1:6/55d26lG3o9VCZX8lping+bZcmShseiqlh2bnUDiPA=
github.com/ebitengine/purego v0.7.1/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ= github.com/ebitengine/purego v0.7.1/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
github.com/flopp/go-findfont v0.1.0 h1:lPn0BymDUtJo+ZkV01VS3661HL6F4qFlkhcJN55u6mU=
github.com/flopp/go-findfont v0.1.0/go.mod h1:wKKxRDjD024Rh7VMwoU90i6ikQRCr+JTHB5n4Ejkqvw=
github.com/gen2brain/malgo v0.11.22 h1:fRtTbzVI9CDWnfEJGo/GxKxN7pXtCb0NsAeUVUjZk9U= github.com/gen2brain/malgo v0.11.22 h1:fRtTbzVI9CDWnfEJGo/GxKxN7pXtCb0NsAeUVUjZk9U=
github.com/gen2brain/malgo v0.11.22/go.mod h1:f9TtuN7DVrXMiV/yIceMeWpvanyVzJQMlBecJFVMxww= github.com/gen2brain/malgo v0.11.22/go.mod h1:f9TtuN7DVrXMiV/yIceMeWpvanyVzJQMlBecJFVMxww=
github.com/gen2brain/raylib-go/raylib v0.0.0-20241019150900-b7833eeae8d0 h1:ssHgKQvc9AIHFSlpkfHr89lJU8nzGBdpKoQJBTPkvnQ= github.com/gen2brain/raylib-go/raylib v0.0.0-20241019150900-b7833eeae8d0 h1:ssHgKQvc9AIHFSlpkfHr89lJU8nzGBdpKoQJBTPkvnQ=

View File

@ -4,6 +4,7 @@ import (
"encoding/binary" "encoding/binary"
"xyosc/audio" "xyosc/audio"
"xyosc/config" "xyosc/config"
"xyosc/fonts"
"xyosc/media" "xyosc/media"
rl "github.com/gen2brain/raylib-go/raylib" rl "github.com/gen2brain/raylib-go/raylib"
@ -28,6 +29,8 @@ func main() {
var AY float32 var AY float32
var BX float32 var BX float32
var BY float32 var BY float32
fonts.Init()
for !rl.WindowShouldClose() { for !rl.WindowShouldClose() {
rl.BeginDrawing() rl.BeginDrawing()
rl.ClearBackground(rl.Blank) rl.ClearBackground(rl.Blank)
@ -48,9 +51,9 @@ func main() {
if config.Config.FPSCounter { if config.Config.FPSCounter {
rl.DrawFPS(16, config.Config.WindowHeight) rl.DrawFPS(16, config.Config.WindowHeight)
} }
rl.DrawText(media.PlayingMediaInfo.Artist+" - "+media.PlayingMediaInfo.Title, 16, 16, 32, config.AccentColor) rl.DrawTextEx(fonts.FontIosevka32, media.PlayingMediaInfo.Artist+" - "+media.PlayingMediaInfo.Title, rl.NewVector2(16, 16), 32, 2, config.AccentColor)
rl.DrawText(media.PlayingMediaInfo.Album, 16, 48, 16, config.ThirdColor) rl.DrawTextEx(fonts.FontIosevka16, media.PlayingMediaInfo.Album, rl.NewVector2(16, 48), 16, 1, config.ThirdColor)
rl.DrawText(media.FmtDuration(media.PlayingMediaInfo.Position)+" / "+media.FmtDuration(media.PlayingMediaInfo.Duration), 16, 64, 32, config.AccentColor) rl.DrawTextEx(fonts.FontIosevka32, media.FmtDuration(media.PlayingMediaInfo.Position)+" / "+media.FmtDuration(media.PlayingMediaInfo.Duration), rl.NewVector2(16, 64), 32, 2, config.AccentColor)
rl.EndDrawing() rl.EndDrawing()
} }