embed font file

This commit is contained in:
Louis Dalibard 2024-12-21 18:02:17 +01:00
parent d60dfeb0e4
commit e099777404
11 changed files with 6 additions and 271 deletions

Binary file not shown.

View File

@ -1,20 +1,21 @@
package fonts
import (
"os"
"xyosc/utils"
"github.com/flopp/go-findfont"
"embed"
"github.com/hajimehoshi/ebiten/v2/text/v2"
)
//go:embed assets/SourceHanSansJP-Heavy.otf
var embedFS embed.FS
var Font *text.GoTextFaceSource
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)
f, err := os.Open(fontPath)
f, err := embedFS.Open("assets/SourceHanSansJP-Heavy.otf")
utils.CheckError(err)
Font, err = text.NewGoTextFaceSource(f)
utils.CheckError(err)

1
go.mod
View File

@ -4,7 +4,6 @@ go 1.23.2
require (
github.com/chewxy/math32 v1.11.1
github.com/flopp/go-findfont v0.1.0
github.com/fsnotify/fsnotify v1.8.0
github.com/gen2brain/malgo v0.11.22
github.com/godbus/dbus v4.1.0+incompatible

2
go.sum
View File

@ -8,8 +8,6 @@ github.com/ebitengine/hideconsole v1.0.0 h1:5J4U0kXF+pv/DhiXt5/lTz0eO5ogJ1iXb8Yj
github.com/ebitengine/hideconsole v1.0.0/go.mod h1:hTTBTvVYWKBuxPr7peweneWdkUwEuHuB3C1R/ielR1A=
github.com/ebitengine/purego v0.8.0 h1:JbqvnEzRvPpxhCJzJJ2y0RbiZ8nyjccVUrSM3q+GvvE=
github.com/ebitengine/purego v0.8.0/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
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/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
github.com/gen2brain/malgo v0.11.22 h1:fRtTbzVI9CDWnfEJGo/GxKxN7pXtCb0NsAeUVUjZk9U=

View File

@ -1,21 +0,0 @@
The MIT License (MIT)
Copyright (c) 2016 Florian Pigorsch
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -1,57 +0,0 @@
[![PkgGoDev](https://pkg.go.dev/badge/github.com/flopp/go-findfont)](https://pkg.go.dev/github.com/flopp/go-findfont)
[![Go Report Card](https://goreportcard.com/badge/github.com/flopp/go-findfont)](https://goreportcard.com/report/github.com/flopp/go-findfont)
[![License MIT](https://img.shields.io/badge/license-MIT-lightgrey.svg?style=flat)](https://github.com/flopp/go-findfont/)
# go-findfont
A platform-agnostic go (golang) library to easily locate truetype font files in your system's user and system font directories.
## What?
`go-findfont` is a golang library that allows you to locate font file on your system. The library is currently aware of the default font directories on Linux/Unix, Windows, and MacOS.
## How?
### Installation
Installing `go-findfont` is as easy as
```bash
go get -u github.com/flopp/go-findfont
```
### Library Usage
```go
import (
"fmt"
"io/ioutil"
"github.com/flopp/go-findfont"
"github.com/golang/freetype/truetype"
)
func main() {
fontPath, err := findfont.Find("arial.ttf")
if err != nil {
panic(err)
}
fmt.Printf("Found 'arial.ttf' in '%s'\n", fontPath)
// load the font with the freetype library
fontData, err := ioutil.ReadFile(fontPath)
if err != nil {
panic(err)
}
font, err := truetype.Parse(fontData)
if err != nil {
panic(err)
}
// use the font...
}
```
## License
Copyright 2016 Florian Pigorsch. All rights reserved.
Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.

View File

@ -1,114 +0,0 @@
// Copyright 2016 Florian Pigorsch. All rights reserved.
//
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package findfont
import (
"fmt"
"os"
"os/user"
"path/filepath"
"strings"
)
// Find tries to locate the specified font file in the current directory as
// well as in platform specific user and system font directories; if there is
// no exact match, Find tries substring matching.
func Find(fileName string) (filePath string, err error) {
// check if fileName already points to a readable file
if _, err := os.Stat(fileName); err == nil {
return fileName, nil
}
// search in user and system directories
return find(filepath.Base(fileName))
}
// List returns a list of all font files found on the system.
func List() (filePaths []string) {
pathList := []string{}
walkF := func(path string, info os.FileInfo, err error) error {
if err == nil {
if info.IsDir() == false && isFontFile(path) {
pathList = append(pathList, path)
}
}
return nil
}
for _, dir := range getFontDirectories() {
filepath.Walk(dir, walkF)
}
return pathList
}
func isFontFile(fileName string) bool {
lower := strings.ToLower(fileName)
return strings.HasSuffix(lower, ".ttf") || strings.HasSuffix(lower, ".ttc") || strings.HasSuffix(lower, ".otf")
}
func stripExtension(fileName string) string {
return strings.TrimSuffix(fileName, filepath.Ext(fileName))
}
func expandUser(path string) (expandedPath string) {
if strings.HasPrefix(path, "~") {
if u, err := user.Current(); err == nil {
return strings.Replace(path, "~", u.HomeDir, -1)
}
}
return path
}
func find(needle string) (filePath string, err error) {
lowerNeedle := strings.ToLower(needle)
lowerNeedleBase := stripExtension(lowerNeedle)
match := ""
partial := ""
partialScore := -1
walkF := func(path string, info os.FileInfo, err error) error {
// we have already found a match -> nothing to do
if match != "" {
return nil
}
if err != nil {
return nil
}
lowerPath := strings.ToLower(info.Name())
if info.IsDir() == false && isFontFile(lowerPath) {
lowerBase := stripExtension(lowerPath)
if lowerPath == lowerNeedle {
// exact match
match = path
} else if strings.Contains(lowerBase, lowerNeedleBase) {
// partial match
score := len(lowerBase) - len(lowerNeedle)
if partialScore < 0 || score < partialScore {
partialScore = score
partial = path
}
}
}
return nil
}
for _, dir := range getFontDirectories() {
filepath.Walk(dir, walkF)
if match != "" {
return match, nil
}
}
if partial != "" {
return partial, nil
}
return "", fmt.Errorf("cannot find font '%s' in user or system directories", needle)
}

View File

@ -1,14 +0,0 @@
// Copyright 2016 Florian Pigorsch. All rights reserved.
//
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package findfont
func getFontDirectories() (paths []string) {
return []string{
expandUser("~/Library/Fonts/"),
"/Library/Fonts/",
"/System/Library/Fonts/",
}
}

View File

@ -1,36 +0,0 @@
// +build dragonfly freebsd linux nacl netbsd openbsd solaris
// Copyright 2016 Florian Pigorsch. All rights reserved.
//
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package findfont
import (
"os"
"path/filepath"
)
func getFontDirectories() (paths []string) {
directories := getUserFontDirs()
directories = append(directories, getSystemFontDirs()...)
return directories
}
func getUserFontDirs() (paths []string) {
if dataPath := os.Getenv("XDG_DATA_HOME"); dataPath != "" {
return []string{expandUser("~/.fonts/"), filepath.Join(expandUser(dataPath), "fonts")}
}
return []string{expandUser("~/.fonts/"), expandUser("~/.local/share/fonts/")}
}
func getSystemFontDirs() (paths []string) {
if dataPaths := os.Getenv("XDG_DATA_DIRS"); dataPaths != "" {
for _, dataPath := range filepath.SplitList(dataPaths) {
paths = append(paths, filepath.Join(expandUser(dataPath), "fonts"))
}
return paths
}
return []string{"/usr/local/share/fonts/", "/usr/share/fonts/"}
}

View File

@ -1,18 +0,0 @@
// Copyright 2016 Florian Pigorsch. All rights reserved.
//
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package findfont
import (
"os"
"path/filepath"
)
func getFontDirectories() (paths []string) {
return []string{
filepath.Join(os.Getenv("windir"), "Fonts"),
filepath.Join(os.Getenv("localappdata"), "Microsoft", "Windows", "Fonts"),
}
}

3
vendor/modules.txt vendored
View File

@ -23,9 +23,6 @@ github.com/ebitengine/purego/internal/cgo
github.com/ebitengine/purego/internal/fakecgo
github.com/ebitengine/purego/internal/strings
github.com/ebitengine/purego/objc
# github.com/flopp/go-findfont v0.1.0
## explicit; go 1.13
github.com/flopp/go-findfont
# github.com/fsnotify/fsnotify v1.8.0
## explicit; go 1.17
github.com/fsnotify/fsnotify