Skip to content

Commit

Permalink
Embed symbol font as well
Browse files Browse the repository at this point in the history
Save the generated file in the repository and only have the script generate it instead of bundling. It is very small anyway.
  • Loading branch information
Jacalz committed Jan 24, 2025
1 parent 57c3055 commit 346ab2d
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 70 deletions.
7 changes: 7 additions & 0 deletions theme/bundled-fonts.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,10 @@ var monospace = &fyne.StaticResource{
StaticName: "DejaVuSansMono-Powerline.ttf",
StaticContent: dejaVuSansMono,
}

//go:embed font/InterSymbols-Regular.ttf
var interSymbolsRegular []byte
var symbol = &fyne.StaticResource{
StaticName: "InterSymbols-Regular.ttf",
StaticContent: interSymbolsRegular,
}
11 changes: 0 additions & 11 deletions theme/bundled-symbol-font.go

This file was deleted.

2 changes: 0 additions & 2 deletions theme/font/.gitignore

This file was deleted.

Binary file added theme/font/InterSymbols-Regular.ttf
Binary file not shown.
57 changes: 0 additions & 57 deletions theme/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
package main

import (
"bytes"
"fmt"
"go/format"
"io"
"os"
"os/exec"
"path/filepath"
Expand All @@ -15,11 +12,6 @@ import (
"fyne.io/fyne/v2"
)

const fontFace = "NotoSans"

const fileHeader = "// auto-generated\n" + // to exclude this file in goreportcard (it has to be first)
"// Code generated by '$ fyne bundle'. DO NOT EDIT." // idiomatic mark, see https://golang.org/s/generatedcode

func main() {
symbolFont := "InterSymbols-Regular.ttf"
err := createFontByStripping(symbolFont, "Inter-Regular.ttf", []rune{
Expand Down Expand Up @@ -51,41 +43,6 @@ func main() {
fyne.LogError("symbol font creation failed", err)
os.Exit(1)
}
fmt.Println("Symbol font created successfully.")

fmt.Println("Bundle fonts…")
f := &bytes.Buffer{}
f.WriteString(fileHeader + "\n\npackage theme\n\nimport \"fyne.io/fyne/v2\"\n\n")
bundleFont(symbolFont, "symbol", f)

err = writeFile("bundled-symbol-font.go", f.Bytes())
if err != nil {
fyne.LogError("unable to write file", err)
os.Exit(1)
}
}

func bundleFile(name string, filepath string, f io.Writer) {
res, err := fyne.LoadResourceFromPath(filepath)
if err != nil {
fyne.LogError("Unable to load file "+filepath, err)
return
}
staticRes, ok := res.(*fyne.StaticResource)
if !ok {
fyne.LogError("Unable to format resource", fmt.Errorf("unexpected resource type %T", res))
return
}
_, err = fmt.Fprintf(f, "var %s = &fyne.StaticResource{\n\tStaticName: %q,\n\tStaticContent: []byte(%q),\n}\n\n",
formatVariable(name), staticRes.StaticName, staticRes.StaticContent)
if err != nil {
fyne.LogError("Unable to write to bundled file", err)
}
}

func bundleFont(fontFile, varName string, f io.Writer) {
path := fontPath(fontFile)
bundleFile(varName, path, f)
}

func createFontByStripping(newFontFile, fontFile string, runes []rune) error {
Expand All @@ -112,17 +69,3 @@ func fontPath(filename string) string {
dirname, _ := os.Getwd()
return filepath.Join(dirname, "font", filename)
}

func formatVariable(name string) string {
str := strings.Replace(name, "-", "", -1)
return strings.Replace(str, "_", "", -1)
}

func writeFile(filename string, contents []byte) error {
formatted, err := format.Source(contents)
if err != nil {
return err
}
dirname, _ := os.Getwd()
return os.WriteFile(filepath.Join(dirname, filename), formatted, 0644)
}

0 comments on commit 346ab2d

Please sign in to comment.