From 1a312e5e4a405c3604345432c7c11d7b647be7c4 Mon Sep 17 00:00:00 2001 From: Louis Dalibard Date: Mon, 21 Oct 2024 01:30:26 +0200 Subject: [PATCH] feat: add cjk text support --- fonts/fonts.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ main.go | 9 ++++++--- 4 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 fonts/fonts.go diff --git a/fonts/fonts.go b/fonts/fonts.go new file mode 100644 index 0000000..43c91f8 --- /dev/null +++ b/fonts/fonts.go @@ -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) +} diff --git a/go.mod b/go.mod index a741895..0ea5f3e 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module xyosc go 1.23.2 require ( + github.com/flopp/go-findfont v0.1.0 github.com/gen2brain/malgo v0.11.22 github.com/gen2brain/raylib-go/raylib v0.0.0-20241019150900-b7833eeae8d0 github.com/godbus/dbus v4.1.0+incompatible diff --git a/go.sum b/go.sum index ea9d744..ed0ab12 100644 --- a/go.sum +++ b/go.sum @@ -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/ebitengine/purego v0.7.1 h1:6/55d26lG3o9VCZX8lping+bZcmShseiqlh2bnUDiPA= 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/go.mod h1:f9TtuN7DVrXMiV/yIceMeWpvanyVzJQMlBecJFVMxww= github.com/gen2brain/raylib-go/raylib v0.0.0-20241019150900-b7833eeae8d0 h1:ssHgKQvc9AIHFSlpkfHr89lJU8nzGBdpKoQJBTPkvnQ= diff --git a/main.go b/main.go index fce7a62..e371775 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "encoding/binary" "xyosc/audio" "xyosc/config" + "xyosc/fonts" "xyosc/media" rl "github.com/gen2brain/raylib-go/raylib" @@ -28,6 +29,8 @@ func main() { var AY float32 var BX float32 var BY float32 + + fonts.Init() for !rl.WindowShouldClose() { rl.BeginDrawing() rl.ClearBackground(rl.Blank) @@ -48,9 +51,9 @@ func main() { if config.Config.FPSCounter { rl.DrawFPS(16, config.Config.WindowHeight) } - rl.DrawText(media.PlayingMediaInfo.Artist+" - "+media.PlayingMediaInfo.Title, 16, 16, 32, config.AccentColor) - rl.DrawText(media.PlayingMediaInfo.Album, 16, 48, 16, config.ThirdColor) - rl.DrawText(media.FmtDuration(media.PlayingMediaInfo.Position)+" / "+media.FmtDuration(media.PlayingMediaInfo.Duration), 16, 64, 32, config.AccentColor) + rl.DrawTextEx(fonts.FontIosevka32, media.PlayingMediaInfo.Artist+" - "+media.PlayingMediaInfo.Title, rl.NewVector2(16, 16), 32, 2, config.AccentColor) + rl.DrawTextEx(fonts.FontIosevka16, media.PlayingMediaInfo.Album, rl.NewVector2(16, 48), 16, 1, config.ThirdColor) + rl.DrawTextEx(fonts.FontIosevka32, media.FmtDuration(media.PlayingMediaInfo.Position)+" / "+media.FmtDuration(media.PlayingMediaInfo.Duration), rl.NewVector2(16, 64), 32, 2, config.AccentColor) rl.EndDrawing() }