Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: allow multiple ttf fonts at the same time #49

Merged
merged 2 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ TARGET = build/examples_epd.hex \
build/examples_unicode_font_pyportal.hex \
build/examples_unicode_font_wioterminal.hex \
build/examples_unicode_font2_pyportal.hex \
build/examples_unicode_font2_wioterminal.hex
build/examples_unicode_font2_wioterminal.hex \
build/examples_unicode_font3_const2bit_pyportal.hex
.PHONY: smoke-test $(TARGET)
smoke-test: clean $(TARGET)

Expand Down Expand Up @@ -70,3 +71,7 @@ build/examples_unicode_font2_pyportal.hex:
build/examples_unicode_font2_wioterminal.hex:
$(TINYGO) build -size short -o $@ -target=wioterminal ./examples/unicode_font2
@$(MD5SUM) $@

build/examples_unicode_font3_const2bit_pyportal.hex:
$(TINYGO) build -size short -o $@ -target=pyportal ./examples/unicode_font3_const2bit
@$(MD5SUM) $@
62 changes: 34 additions & 28 deletions cmd/tinyfontgen-ttf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"
"strings"

"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
"golang.org/x/image/font/sfnt"
"golang.org/x/image/math/fixed"
Expand Down Expand Up @@ -85,46 +86,51 @@ func main() {
}
}

err := run(*sz, *dpi, fonts[0])
err := run(*sz, *dpi, fonts)
if err != nil {
log.Fatal(err)
}
}

func run(size, dpi int, fontfile string) error {
bb, err := ioutil.ReadFile(fontfile)
if err != nil {
return err
}

ft, err := opentype.Parse(bb)
if err != nil {
return err
}

face, err := opentype.NewFace(ft, &opentype.FaceOptions{Size: float64(size), DPI: float64(dpi)})
if err != nil {
return err
}

sfntBuf := &sfnt.Buffer{}
func run(size, dpi int, fonts []string) error {
type xx struct {
Rune rune
Index uint16
Face font.Face
}
indexes := []xx{}

const runeMax = 0x3FFFF
for r := rune(0); r < runeMax; r++ {
idx, err := ft.GlyphIndex(sfntBuf, r)
for _, fontfile := range fonts {
bb, err := ioutil.ReadFile(fontfile)
if err != nil {
return err
}
if idx != 0 && (*all || runesMap[r]) {
indexes = append(indexes, xx{
Rune: r,
Index: uint16(idx),
})

ft, err := opentype.Parse(bb)
if err != nil {
return err
}

face, err := opentype.NewFace(ft, &opentype.FaceOptions{Size: float64(size), DPI: float64(dpi)})
if err != nil {
return err
}

sfntBuf := &sfnt.Buffer{}

const runeMax = 0x3FFFF
for r := rune(0); r < runeMax; r++ {
idx, err := ft.GlyphIndex(sfntBuf, r)
if err != nil {
return err
}
if idx != 0 && (*all || runesMap[r]) {
indexes = append(indexes, xx{
Rune: r,
Index: uint16(idx),
Face: face,
})
}
}
}

Expand All @@ -133,7 +139,7 @@ func run(size, dpi int, fontfile string) error {
Glyphs: make([]Glyph, len(indexes)),
}
for i, xxx := range indexes {
dr, img, _, adv, ok := face.Glyph(fixed.Point26_6{}, xxx.Rune)
dr, img, _, adv, ok := xxx.Face.Glyph(fixed.Point26_6{}, xxx.Rune)
if !ok {
continue
}
Expand Down Expand Up @@ -215,7 +221,7 @@ func (f Font) SaveTo(w io.Writer) {
fontname := *fontname
yadv := *yadvance

fmt.Fprintf(w, "// ttfgen %s\n", strings.Join(os.Args[1:], " "))
fmt.Fprintf(w, "// tinyfnotgen-ttf %s\n", strings.Join(os.Args[1:], " "))
fmt.Fprintf(w, "\n")

fmt.Fprintf(w, "package %s\n", pkg)
Expand Down
20 changes: 20 additions & 0 deletions examples/unicode_font3_const2bit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# unicode\_font3\_const

Testing with pyportal.

```
$ tinygo build -o app.uf2 -target pyportal -size short ./examples/unicode_font3_const2bit
```

![](unicode_font3_const2bit.jpg)

The font was created as follows.

```
tinyfnotgen-ttf --size 12 --verbose --output ./font.go --string-file ./main.go --package main --fontname Notosans12pt ./NotoSansJP-Regular.otf ./NotoSansKR-Regular.ttf
```

You can download the original TTF/OTF fonts used with tinyfontgen-ttf from the following link. Please refer to the downloaded file for the license and other details.

* https://fonts.google.com/noto/specimen/Noto+Sans+JP
* https://fonts.google.com/noto/specimen/Noto+Sans+KR
Loading
Loading