From c0bb03677e3215575f124bd7b79f492b0a5cb192 Mon Sep 17 00:00:00 2001 From: rdon-key Date: Sun, 31 Aug 2025 21:11:30 +0900 Subject: [PATCH 1/3] feat: add filename comment to index table output --- cmd/tinyfontgen-ttf/main.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/cmd/tinyfontgen-ttf/main.go b/cmd/tinyfontgen-ttf/main.go index 5948323..838e1aa 100644 --- a/cmd/tinyfontgen-ttf/main.go +++ b/cmd/tinyfontgen-ttf/main.go @@ -94,9 +94,10 @@ func main() { func run(size, dpi int, fonts []string) error { type xx struct { - Rune rune - Index uint16 - Face font.Face + Rune rune + Index uint16 + Face font.Face + Filename string } indexes := []xx{} @@ -129,6 +130,7 @@ func run(size, dpi int, fonts []string) error { Rune: r, Index: uint16(idx), Face: face, + Filename: fontfile, }) } } @@ -166,6 +168,7 @@ func run(size, dpi int, fonts []string) error { } font.Glyphs[i].Rune = xxx.Rune + font.Glyphs[i].Filename = filepath.Base(xxx.Filename) font.Glyphs[i].Width = uint8(img.Bounds().Max.X - img.Bounds().Min.X) font.Glyphs[i].Height = uint8(img.Bounds().Max.Y - img.Bounds().Min.Y) font.Glyphs[i].XAdvance = uint8(adv.Ceil()) @@ -207,6 +210,7 @@ type Glyph struct { XOffset int8 YOffset int8 Bitmaps []byte + Filename string } // Font is a struct that implements Fonter interface. @@ -246,7 +250,7 @@ func (f Font) SaveTo(w io.Writer) { fmt.Fprintf(w, ` "\x%02X\x%02X\x%02X" + `, byte(x.Rune>>16), byte(x.Rune>>8), byte(x.Rune)) fmt.Fprintf(w, `"\x%02X\x%02X\x%02X" + `, byte(offset>>16), byte(offset>>8), byte(offset)) if x.Rune > 0 { - fmt.Fprintf(w, `// %c`, x.Rune) + fmt.Fprintf(w, `// %c %s`, x.Rune, x.Filename) } else { fmt.Fprintf(w, `//`) } From fd3f945d700fd8425aa76d133f1f3cdc3cb5ac14 Mon Sep 17 00:00:00 2001 From: rdon-key Date: Sun, 31 Aug 2025 22:03:10 +0900 Subject: [PATCH 2/3] feat: Add font duplicate check. --- cmd/tinyfontgen-ttf/main.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/cmd/tinyfontgen-ttf/main.go b/cmd/tinyfontgen-ttf/main.go index 838e1aa..0550a37 100644 --- a/cmd/tinyfontgen-ttf/main.go +++ b/cmd/tinyfontgen-ttf/main.go @@ -100,6 +100,8 @@ func run(size, dpi int, fonts []string) error { Filename string } indexes := []xx{} + seen := map[rune]string{} + for _, fontfile := range fonts { bb, err := ioutil.ReadFile(fontfile) @@ -126,10 +128,17 @@ func run(size, dpi int, fonts []string) error { return err } if idx != 0 && (*all || runesMap[r]) { + if first, ok := seen[r]; ok { + fmt.Fprintf(os.Stderr, + "warning: rune U+%04X (%c) already registered from %s, skipping from %s\n", + r, r, filepath.Base(first), filepath.Base(fontfile)) + continue + } + seen[r] = fontfile indexes = append(indexes, xx{ - Rune: r, - Index: uint16(idx), - Face: face, + Rune: r, + Index: uint16(idx), + Face: face, Filename: fontfile, }) } From 572c7cee4db4a163cf61b6a36963801d3e01a54a Mon Sep 17 00:00:00 2001 From: rdon-key Date: Sun, 31 Aug 2025 22:19:43 +0900 Subject: [PATCH 3/3] feat: sort runes before generating index table --- cmd/tinyfontgen-ttf/main.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/cmd/tinyfontgen-ttf/main.go b/cmd/tinyfontgen-ttf/main.go index 0550a37..cf28c34 100644 --- a/cmd/tinyfontgen-ttf/main.go +++ b/cmd/tinyfontgen-ttf/main.go @@ -8,6 +8,7 @@ import ( "log" "os" "path/filepath" + "sort" "strings" "golang.org/x/image/font" @@ -100,8 +101,7 @@ func run(size, dpi int, fonts []string) error { Filename string } indexes := []xx{} - seen := map[rune]string{} - + seen := map[rune]string{} for _, fontfile := range fonts { bb, err := ioutil.ReadFile(fontfile) @@ -130,8 +130,8 @@ func run(size, dpi int, fonts []string) error { if idx != 0 && (*all || runesMap[r]) { if first, ok := seen[r]; ok { fmt.Fprintf(os.Stderr, - "warning: rune U+%04X (%c) already registered from %s, skipping from %s\n", - r, r, filepath.Base(first), filepath.Base(fontfile)) + "warning: rune U+%04X (%c) already registered from %s, skipping from %s\n", + r, r, filepath.Base(first), filepath.Base(fontfile)) continue } seen[r] = fontfile @@ -144,7 +144,9 @@ func run(size, dpi int, fonts []string) error { } } } - + sort.Slice(indexes, func(i, j int) bool { + return indexes[i].Rune < indexes[j].Rune + }) fontBuffer := [256]uint8{} font := Font{ Glyphs: make([]Glyph, len(indexes)),